diff --git a/src/app/qgsdecorationcopyright.cpp b/src/app/qgsdecorationcopyright.cpp index cb4c7bb3198..6a8ce6277ba 100644 --- a/src/app/qgsdecorationcopyright.cpp +++ b/src/app/qgsdecorationcopyright.cpp @@ -26,6 +26,8 @@ email : tim@linfiniti.com #include "qgisapp.h" #include "qgsapplication.h" +#include "qgsexpression.h" +#include "qgsexpressioncontext.h" #include "qgslogger.h" #include "qgsmapcanvas.h" #include "qgsproject.h" @@ -64,7 +66,7 @@ void QgsDecorationCopyright::projectRead() // mQFont.setFamily( QgsProject::instance()->readEntry( "CopyrightLabel", "/FontName", "Sans Serif" ) ); // mQFont.setPointSize( QgsProject::instance()->readNumEntry( "CopyrightLabel", "/FontSize", 9 ) ); - mLabelQString = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Label" ), defString ); + mLabelText = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Label" ), defString ); mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 ); mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginV" ), 0 ); mColor = QgsSymbolLayerUtils::decodeColor( QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Color" ), QStringLiteral( "#000000" ) ) ); @@ -75,7 +77,7 @@ void QgsDecorationCopyright::saveToProject() QgsDecorationItem::saveToProject(); QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/FontName" ), mQFont.family() ); QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/FontSize" ), mQFont.pointSize() ); - QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Label" ), mLabelQString ); + QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Label" ), mLabelText ); QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Color" ), QgsSymbolLayerUtils::encodeColor( mColor ) ); QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginH" ), mMarginHorizontal ); QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginV" ), mMarginVertical ); @@ -95,6 +97,8 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende //Large IF statement to enable/disable copyright label if ( enabled() ) { + QString displayString = QgsExpression::replaceExpressionText( mLabelText, &context.expressionContext() ); + // need width/height of paint device int myHeight = context.painter()->device()->height(); int myWidth = context.painter()->device()->width(); @@ -105,7 +109,7 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende QString style = ""; - text.setHtml( style + "
" + mLabelQString + "
" ); + text.setHtml( style + "" + displayString + "
" ); QSizeF size = text.size(); float myXOffset( 0 ), myYOffset( 0 ); diff --git a/src/app/qgsdecorationcopyright.h b/src/app/qgsdecorationcopyright.h index 6c3bf1a544d..4ba07bdf92b 100644 --- a/src/app/qgsdecorationcopyright.h +++ b/src/app/qgsdecorationcopyright.h @@ -53,7 +53,7 @@ class APP_EXPORT QgsDecorationCopyright : public QgsDecorationItem //! This is the font that will be used for the copyright label QFont mQFont; //! This is the string that will be used for the copyright label - QString mLabelQString; + QString mLabelText; //! This is the color for the copyright label QColor mColor; diff --git a/src/app/qgsdecorationcopyrightdialog.cpp b/src/app/qgsdecorationcopyrightdialog.cpp index f6be6192981..1f365bc0411 100644 --- a/src/app/qgsdecorationcopyrightdialog.cpp +++ b/src/app/qgsdecorationcopyrightdialog.cpp @@ -13,7 +13,12 @@ #include "qgsdecorationcopyrightdialog.h" #include "qgsdecorationcopyright.h" +#include "qgisapp.h" +#include "qgsexpression.h" +#include "qgsexpressionbuilderdialog.h" +#include "qgsexpressioncontext.h" #include "qgshelp.h" +#include "qgsmapcanvas.h" #include "qgssettings.h" //qt includes @@ -30,6 +35,7 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig setupUi( this ); connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsDecorationCopyrightDialog::buttonBox_accepted ); connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsDecorationCopyrightDialog::buttonBox_rejected ); + connect( mInsertExpressionButton, &QPushButton::clicked, this, &QgsDecorationCopyrightDialog::mInsertExpressionButton_clicked ); connect( pbnColorChooser, &QgsColorButton::colorChanged, this, &QgsDecorationCopyrightDialog::pbnColorChooser_colorChanged ); connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsDecorationCopyrightDialog::showHelp ); @@ -41,7 +47,7 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig grpEnable->setChecked( mDeco.enabled() ); // text - txtCopyrightText->setPlainText( mDeco.mLabelQString ); + txtCopyrightText->setPlainText( mDeco.mLabelText ); // placement cboPlacement->addItem( tr( "Top left" ), QgsDecorationItem::TopLeft ); cboPlacement->addItem( tr( "Top right" ), QgsDecorationItem::TopRight ); @@ -82,6 +88,27 @@ void QgsDecorationCopyrightDialog::buttonBox_rejected() reject(); } +void QgsDecorationCopyrightDialog::mInsertExpressionButton_clicked() +{ + QString selText = txtCopyrightText->textCursor().selectedText(); + + // edit the selected expression if there's one + if ( selText.startsWith( QLatin1String( "[%" ) ) && selText.endsWith( QLatin1String( "%]" ) ) ) + selText = selText.mid( 2, selText.size() - 4 ); + + QgsExpressionBuilderDialog exprDlg( nullptr, selText, this, QStringLiteral( "generic" ), QgisApp::instance()->mapCanvas()->mapSettings().expressionContext() ); + + exprDlg.setWindowTitle( QObject::tr( "Insert Expression" ) ); + if ( exprDlg.exec() == QDialog::Accepted ) + { + QString expression = exprDlg.expressionText(); + if ( !expression.isEmpty() ) + { + txtCopyrightText->insertPlainText( "[%" + expression + "%]" ); + } + } +} + void QgsDecorationCopyrightDialog::pbnColorChooser_colorChanged( const QColor &c ) { QTextCursor cursor = txtCopyrightText->textCursor(); @@ -93,7 +120,7 @@ void QgsDecorationCopyrightDialog::pbnColorChooser_colorChanged( const QColor &c void QgsDecorationCopyrightDialog::apply() { mDeco.mQFont = txtCopyrightText->currentFont(); - mDeco.mLabelQString = txtCopyrightText->toPlainText(); + mDeco.mLabelText = txtCopyrightText->toPlainText(); mDeco.mColor = pbnColorChooser->color(); mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->currentData().toInt() ) ); mDeco.mMarginUnit = wgtUnitSelection->unit(); diff --git a/src/app/qgsdecorationcopyrightdialog.h b/src/app/qgsdecorationcopyrightdialog.h index 0cddffe09a1..4d1b19747be 100644 --- a/src/app/qgsdecorationcopyrightdialog.h +++ b/src/app/qgsdecorationcopyrightdialog.h @@ -31,6 +31,7 @@ class APP_EXPORT QgsDecorationCopyrightDialog : public QDialog, private Ui::QgsD private slots: void buttonBox_accepted(); void buttonBox_rejected(); + void mInsertExpressionButton_clicked(); void showHelp(); void pbnColorChooser_colorChanged( const QColor &c ); void apply(); diff --git a/src/ui/qgsdecorationcopyrightdialog.ui b/src/ui/qgsdecorationcopyrightdialog.ui index e940c0cc4e5..24945efffeb 100644 --- a/src/ui/qgsdecorationcopyrightdialog.ui +++ b/src/ui/qgsdecorationcopyrightdialog.ui @@ -56,7 +56,7 @@ -