Skip to content

Commit

Permalink
[FEATURE] expression support for the copyright decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Mar 21, 2018
1 parent 2be1256 commit 29e00ae
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
10 changes: 7 additions & 3 deletions src/app/qgsdecorationcopyright.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" ) ) );
Expand All @@ -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 );
Expand All @@ -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();
Expand All @@ -105,7 +109,7 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende

QString style = "<style type=\"text/css\"> p {color: " +
QStringLiteral( "rgba( %1, %2, %3, %4 )" ).arg( mColor.red() ).arg( mColor.green() ).arg( mColor.blue() ).arg( QString::number( mColor.alphaF(), 'f', 2 ) ) + "}</style>";
text.setHtml( style + "<p>" + mLabelQString + "</p>" );
text.setHtml( style + "<p>" + displayString + "</p>" );
QSizeF size = text.size();

float myXOffset( 0 ), myYOffset( 0 );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsdecorationcopyright.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 29 additions & 2 deletions src/app/qgsdecorationcopyrightdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );

Expand All @@ -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 );
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsdecorationcopyrightdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
19 changes: 13 additions & 6 deletions src/ui/qgsdecorationcopyrightdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
</property>
</widget>
</item>
<item row="4" column="1" colspan="2">
<item row="5" column="1" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="textLabel1_4">
Expand Down Expand Up @@ -122,9 +122,16 @@
</item>
</layout>
</item>
<item row="3" column="1" colspan="2">
<item row="4" column="1" colspan="2">
<widget class="QComboBox" name="cboPlacement"/>
</item>
<item row="2" column="0" colspan="3">
<widget class="QPushButton" name="mInsertExpressionButton">
<property name="text">
<string>Insert an expression...</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QTextEdit" name="txtCopyrightText">
<property name="html">
Expand All @@ -136,7 +143,7 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="textLabel16">
<property name="maximumSize">
<size>
Expand All @@ -152,7 +159,7 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="4" column="0">
<item row="5" column="0">
<widget class="QLabel" name="lblMargin">
<property name="minimumSize">
<size>
Expand All @@ -165,7 +172,7 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label">
<property name="maximumSize">
<size>
Expand All @@ -178,7 +185,7 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<item row="3" column="1" colspan="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QgsColorButton" name="pbnColorChooser">
Expand Down

0 comments on commit 29e00ae

Please sign in to comment.