QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 {
24  setupUi( this );
25  mExpression->setText( expression );
26  mHelpText->setText( helpText );
27  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsExpressionStoreDialog::accept );
28  connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsExpressionStoreDialog::reject );
29  mValidationError->hide();
30  mValidationError->setStyleSheet( QStringLiteral( "QLabel { color : red; }" ) );
31  QPushButton *saveBtn { buttonBox->button( QDialogButtonBox::StandardButton::Save ) };
32  saveBtn->setEnabled( false );
33  connect( mLabel, &QLineEdit::textChanged, this, [ = ]( const QString & text )
34  {
35  QString errorMessage;
36  if ( mExistingLabels.contains( text ) )
37  {
38  errorMessage = tr( "A stored expression with this name already exists" );
39  }
40  else if ( text.contains( '/' ) || text.contains( '\\' ) )
41  {
42  errorMessage = tr( "Labels cannot contain slashes (/ or \\)" );
43  }
44  if ( ! errorMessage.isEmpty() )
45  {
46  mValidationError->show();
47  mValidationError->setText( errorMessage );
48  saveBtn->setEnabled( false );
49  }
50  else
51  {
52  mValidationError->hide();
53  saveBtn->setEnabled( true );
54  }
55  } );
56  // No slashes in labels!
57  QString labelFixed { label };
58  labelFixed.remove( '/' ).remove( '\\' );
59  mLabel->setText( labelFixed );
60 }
61 
63 {
64  // remove meta qrichtext instruction from html. It fails rendering
65  // when mixing with other html content
66  // see issue https://github.com/qgis/QGIS/issues/36191
67  return mHelpText->toHtml().replace( "<meta name=\"qrichtext\" content=\"1\" />", QString() );
68 }
QString label()
Returns the label text.
QString expression()
Returns the expression text.
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.
QString helpText() const
Returns the help text.