Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Add opacity support to copyright decorator
  • Loading branch information
nirvn committed May 9, 2017
1 parent 83e8ece commit cb23ebe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
25 changes: 13 additions & 12 deletions src/app/qgsdecorationcopyright.cpp
Expand Up @@ -24,11 +24,12 @@ email : tim@linfiniti.com
#include "qgsdecorationcopyright.h"
#include "qgsdecorationcopyrightdialog.h"

#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsproject.h"
#include "qgisapp.h"
#include "qgssymbollayerutils.h"

#include <QPainter>
#include <QMenu>
Expand Down Expand Up @@ -64,23 +65,22 @@ void QgsDecorationCopyright::projectRead()
// there is no font setting in the UI, so just use the Qt/QGIS default font (what mQFont gets when created)
// mQFont.setFamily( QgsProject::instance()->readEntry( "CopyrightLabel", "/FontName", "Sans Serif" ) );
// mQFont.setPointSize( QgsProject::instance()->readNumEntry( "CopyrightLabel", "/FontSize", 9 ) );
QgsProject *prj = QgsProject::instance();
mLabelQString = prj->readEntry( mNameConfig, QStringLiteral( "/Label" ), defString );

mLabelQString = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Label" ), defString );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 );
mMarginVertical = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginV" ), 0 );
mLabelQColor.setNamedColor( prj->readEntry( mNameConfig, QStringLiteral( "/Color" ), QStringLiteral( "#000000" ) ) ); // default color is black
mColor = QgsSymbolLayerUtils::decodeColor( QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Color" ), QStringLiteral( "#000000" ) ) );
}

void QgsDecorationCopyright::saveToProject()
{
QgsDecorationItem::saveToProject();
QgsProject *prj = QgsProject::instance();
prj->writeEntry( mNameConfig, QStringLiteral( "/FontName" ), mQFont.family() );
prj->writeEntry( mNameConfig, QStringLiteral( "/FontSize" ), mQFont.pointSize() );
prj->writeEntry( mNameConfig, QStringLiteral( "/Label" ), mLabelQString );
prj->writeEntry( mNameConfig, QStringLiteral( "/Color" ), mLabelQColor.name() );
prj->writeEntry( mNameConfig, QStringLiteral( "/MarginH" ), mMarginHorizontal );
prj->writeEntry( mNameConfig, QStringLiteral( "/MarginV" ), mMarginVertical );
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( "/Color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginH" ), mMarginHorizontal );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginV" ), mMarginVertical );
}

// Slot called when the buffer menu item is activated
Expand All @@ -104,8 +104,9 @@ void QgsDecorationCopyright::render( const QgsMapSettings &mapSettings, QgsRende
QTextDocument text;
text.setDefaultFont( mQFont );
// To set the text color in a QTextDocument we use a CSS style

QString style = "<style type=\"text/css\"> p {color: " +
mLabelQColor.name() + "}</style>";
QString( "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>" );
QSizeF size = text.size();

Expand Down
4 changes: 3 additions & 1 deletion src/app/qgsdecorationcopyright.h
Expand Up @@ -54,8 +54,10 @@ class APP_EXPORT QgsDecorationCopyright : public QgsDecorationItem
QFont mQFont;
//! This is the string that will be used for the copyright label
QString mLabelQString;

//! This is the color for the copyright label
QColor mLabelQColor;
QColor mColor;

//! enable or disable use of position percentage for placement
int mMarginHorizontal;
int mMarginVertical;
Expand Down
7 changes: 4 additions & 3 deletions src/app/qgsdecorationcopyrightdialog.cpp
Expand Up @@ -50,13 +50,14 @@ QgsDecorationCopyrightDialog::QgsDecorationCopyrightDialog( QgsDecorationCopyrig
wgtUnitSelection->setUnit( mDeco.mMarginUnit );

// color
pbnColorChooser->setColor( mDeco.mLabelQColor );
pbnColorChooser->setAllowAlpha( true );
pbnColorChooser->setColor( mDeco.mColor );
pbnColorChooser->setContext( QStringLiteral( "gui" ) );
pbnColorChooser->setColorDialogTitle( tr( "Select text color" ) );

QTextCursor cursor = txtCopyrightText->textCursor();
txtCopyrightText->selectAll();
txtCopyrightText->setTextColor( mDeco.mLabelQColor );
txtCopyrightText->setTextColor( mDeco.mColor );
txtCopyrightText->setTextCursor( cursor );
}

Expand Down Expand Up @@ -89,7 +90,7 @@ void QgsDecorationCopyrightDialog::apply()
{
mDeco.mQFont = txtCopyrightText->currentFont();
mDeco.mLabelQString = txtCopyrightText->toPlainText();
mDeco.mLabelQColor = pbnColorChooser->color();
mDeco.mColor = pbnColorChooser->color();
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->currentData().toInt() ) );
mDeco.mMarginUnit = wgtUnitSelection->unit();
mDeco.mMarginHorizontal = spnHorizontal->value();
Expand Down

0 comments on commit cb23ebe

Please sign in to comment.