QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsexpressionstoredialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsexpressionstoredialog.cpp
3  ---------------------
4  begin : December 2019
5  copyright : (C) 2019 by Alessandro Pasotti
6  email : elpaso at itopen dot it
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include <QPushButton>
18 #include <QStyle>
19 
20 QgsExpressionStoreDialog::QgsExpressionStoreDialog( const QString &label, const QString &expression, const QString &helpText, const QStringList &existingLabels, QWidget *parent )
21  : QDialog( parent )
22  , mExistingLabels( existingLabels )
23  , mOriginalLabel( label )
24 {
25  setupUi( this );
26  mExpression->setText( expression );
27  mHelpText->setText( helpText );
28  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsExpressionStoreDialog::accept );
29  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsExpressionStoreDialog::reject );
30  mValidationError->hide();
31  mValidationError->setStyleSheet( QStringLiteral( "QLabel { color : red; }" ) );
32  QPushButton *saveBtn { buttonBox->button( QDialogButtonBox::StandardButton::Save ) };
33  saveBtn->setEnabled( false );
34  connect( mLabel, &QLineEdit::textChanged, this, [ = ]( const QString & text )
35  {
36  QString errorMessage;
37  if ( mOriginalLabel.simplified() != text.simplified() &&
38  mExistingLabels.contains( text.simplified() ) )
39  {
40  errorMessage = tr( "A stored expression with this name already exists" );
41  }
42  else if ( text.contains( '/' ) || text.contains( '\\' ) )
43  {
44  errorMessage = tr( "Labels cannot contain slashes (/ or \\)" );
45  }
46  if ( ! errorMessage.isEmpty() )
47  {
48  mValidationError->show();
49  mValidationError->setText( errorMessage );
50  saveBtn->setEnabled( false );
51  }
52  else
53  {
54  mValidationError->hide();
55  saveBtn->setEnabled( true );
56  }
57  } );
58  // No slashes in labels!
59  QString labelFixed { label };
60  labelFixed.remove( '/' ).remove( '\\' );
61  mLabel->setText( labelFixed.simplified() );
62 }
63 
65 {
66  // remove meta qrichtext instruction from html. It fails rendering
67  // when mixing with other html content
68  // see issue https://github.com/qgis/QGIS/issues/36191
69  return mHelpText->toHtml().replace( "<meta name=\"qrichtext\" content=\"1\" />", QString() );
70 }
qgsexpressionstoredialog.h
QgsExpressionStoreDialog::expression
QString expression()
Returns the expression text.
Definition: qgsexpressionstoredialog.h:61
QgsExpressionStoreDialog::helpText
QString helpText() const
Returns the help text.
Definition: qgsexpressionstoredialog.cpp:64
QgsExpressionStoreDialog::label
QString label()
Returns the label text.
Definition: qgsexpressionstoredialog.h:66
QgsExpressionStoreDialog::QgsExpressionStoreDialog
QgsExpressionStoreDialog(const QString &label, const QString &expression, const QString &helpText, const QStringList &existingLabels=QStringList(), QWidget *parent=nullptr)
Creates a QgsExpressionStoreDialog with given label, expression and helpText.
Definition: qgsexpressionstoredialog.cpp:20