Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Color settings for north arrow decoration
  • Loading branch information
nirvn committed May 9, 2017
1 parent 592fcfa commit 1445505
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 37 deletions.
32 changes: 15 additions & 17 deletions images/north_arrows/default.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion src/app/qgsdecorationnortharrow.cpp
Expand Up @@ -31,6 +31,7 @@ email : tim@linfiniti.com
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsproject.h"
#include "qgssymbollayerutils.h"
#include "qgssvgcache.h"

// qt includes
Expand Down Expand Up @@ -75,6 +76,8 @@ QgsDecorationNorthArrow::~QgsDecorationNorthArrow()
void QgsDecorationNorthArrow::projectRead()
{
QgsDecorationItem::projectRead();
mColor = QgsSymbolLayerUtils::decodeColor( QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Color" ), QStringLiteral( "#000000" ) ) );
mOutlineColor = QgsSymbolLayerUtils::decodeColor( QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/OutlineColor" ), QStringLiteral( "#FFFFFF" ) ) );
mRotationInt = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/Rotation" ), 0 );
mAutomatic = QgsProject::instance()->readBoolEntry( mNameConfig, QStringLiteral( "/Automatic" ), true );
mMarginHorizontal = QgsProject::instance()->readNumEntry( mNameConfig, QStringLiteral( "/MarginH" ), 0 );
Expand All @@ -84,6 +87,8 @@ void QgsDecorationNorthArrow::projectRead()
void QgsDecorationNorthArrow::saveToProject()
{
QgsDecorationItem::saveToProject();
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/OutlineColor" ), QgsSymbolLayerUtils::encodeColor( mOutlineColor ) );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Rotation" ), mRotationInt );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Automatic" ), mAutomatic );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginH" ), mMarginHorizontal );
Expand All @@ -106,7 +111,7 @@ void QgsDecorationNorthArrow::render( const QgsMapSettings &mapSettings, QgsRend
QSize size( 64, 64 );
QSvgRenderer svg;

const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), QColor( "#000000" ), QColor( "#FFFFFF" ), 0.2, 1.0 );
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), mColor, mOutlineColor, 1.0, 1.0 );
svg.load( svgContent );

if ( svg.isValid() )
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgsdecorationnortharrow.h
Expand Up @@ -54,6 +54,11 @@ class APP_EXPORT QgsDecorationNorthArrow: public QgsDecorationItem
// static const double DEG2RAD;
static const double TOL;

//! The north arrow fill color
QColor mColor;
//! The north arrow outline color
QColor mOutlineColor;

// The amount of rotation for the north arrow
int mRotationInt;

Expand Down
35 changes: 27 additions & 8 deletions src/app/qgsdecorationnortharrowdialog.cpp
Expand Up @@ -35,8 +35,6 @@ QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorth
QPushButton *applyButton = buttonBox->button( QDialogButtonBox::Apply );
connect( applyButton, &QAbstractButton::clicked, this, &QgsDecorationNorthArrowDialog::apply );

// rotation
rotatePixmap( mDeco.mRotationInt );
// signal/slot connection defined in 'designer' causes the slider to
// be moved to reflect the change in the spinbox.
spinAngle->setValue( mDeco.mRotationInt );
Expand All @@ -57,6 +55,21 @@ QgsDecorationNorthArrowDialog::QgsDecorationNorthArrowDialog( QgsDecorationNorth

// automatic
cboxAutomatic->setChecked( mDeco.mAutomatic );

pbnChangeColor->setAllowAlpha( true );
pbnChangeColor->setColor( mDeco.mColor );
pbnChangeColor->setContext( QStringLiteral( "gui" ) );
pbnChangeColor->setColorDialogTitle( tr( "Select north arrow fill color" ) );

pbnChangeOutlineColor->setAllowAlpha( true );
pbnChangeOutlineColor->setColor( mDeco.mOutlineColor );
pbnChangeOutlineColor->setContext( QStringLiteral( "gui" ) );
pbnChangeOutlineColor->setColorDialogTitle( tr( "Select north arrow outline color" ) );

connect( pbnChangeColor, &QgsColorButton::colorChanged, this, [ = ]( QColor ) { drawNorthArrow(); } );
connect( pbnChangeOutlineColor, &QgsColorButton::colorChanged, this, [ = ]( QColor ) { drawNorthArrow(); } );

drawNorthArrow();
}

QgsDecorationNorthArrowDialog::~QgsDecorationNorthArrowDialog()
Expand Down Expand Up @@ -89,11 +102,15 @@ void QgsDecorationNorthArrowDialog::on_spinAngle_valueChanged( int spinAngle )

void QgsDecorationNorthArrowDialog::on_sliderRotation_valueChanged( int rotationValue )
{
rotatePixmap( rotationValue );
Q_UNUSED( rotationValue );

drawNorthArrow();
}

void QgsDecorationNorthArrowDialog::apply()
{
mDeco.mColor = pbnChangeColor->color();
mDeco.mOutlineColor = pbnChangeOutlineColor->color();
mDeco.mRotationInt = sliderRotation->value();
mDeco.setPlacement( static_cast< QgsDecorationItem::Placement>( cboPlacement->currentData().toInt() ) );
mDeco.mMarginUnit = wgtUnitSelection->unit();
Expand All @@ -104,12 +121,14 @@ void QgsDecorationNorthArrowDialog::apply()
mDeco.update();
}

void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
void QgsDecorationNorthArrowDialog::drawNorthArrow()
{
int rotation = spinAngle->value();

QSize size( 64, 64 );
QSvgRenderer svg;

const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), QColor( "#000000" ), QColor( "#FFFFFF" ), 0.2, 1.0 );
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( QStringLiteral( ":/images/north_arrows/default.svg" ), size.width(), pbnChangeColor->color(), pbnChangeOutlineColor->color(), 1.0, 1.0 );
svg.load( svgContent );

if ( svg.isValid() )
Expand All @@ -129,11 +148,11 @@ void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
//myQPainter.translate( (int)centerXDouble, (int)centerYDouble );

//rotate the canvas
myQPainter.rotate( rotationInt );
myQPainter.rotate( rotation );
//work out how to shift the image so that it appears in the center of the canvas
//(x cos a + y sin a - x, -x sin a + y cos a - y)
const double PI = 3.14159265358979323846;
double myRadiansDouble = ( PI / 180 ) * rotationInt;
double myRadiansDouble = ( PI / 180 ) * rotation;
int xShift = static_cast<int>( (
( centerXDouble * cos( myRadiansDouble ) ) +
( centerYDouble * sin( myRadiansDouble ) )
Expand Down Expand Up @@ -175,5 +194,5 @@ void QgsDecorationNorthArrowDialog::rotatePixmap( int rotationInt )
void QgsDecorationNorthArrowDialog::resizeEvent( QResizeEvent *resizeEvent )
{
Q_UNUSED( resizeEvent );
rotatePixmap( sliderRotation->value() );
drawNorthArrow();
}
2 changes: 1 addition & 1 deletion src/app/qgsdecorationnortharrowdialog.h
Expand Up @@ -26,7 +26,7 @@ class APP_EXPORT QgsDecorationNorthArrowDialog : public QDialog, private Ui::Qgs
~QgsDecorationNorthArrowDialog();

private:
void rotatePixmap( int rotationInt );
void drawNorthArrow();
void resizeEvent( QResizeEvent * ) override; //overloads qwidget

private slots:
Expand Down
96 changes: 86 additions & 10 deletions src/ui/qgsdecorationnortharrowdialog.ui
Expand Up @@ -30,7 +30,76 @@
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="2" colspan="3">
<item row="0" column="1">
<widget class="QLabel" name="textLabel1_3_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Color</string>
</property>
</widget>
</item>
<item row="0" column="2" colspan="3">
<layout class="QHBoxLayout" name="colorLayout">
<item>
<widget class="QLabel" name="fillLabel">
<property name="text">
<string>Fill</string>
</property>
</widget>
</item>
<item>
<widget class="QgsColorButton" name="pbnChangeColor">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="outlineLabel">
<property name="text">
<string>Outline</string>
</property>
</widget>
</item>
<item>
<widget class="QgsColorButton" name="pbnChangeOutlineColor">
<property name="minimumSize">
<size>
<width>150</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>120</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="2" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="labelHorizontal">
Expand Down Expand Up @@ -99,7 +168,7 @@
</item>
</layout>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QLabel" name="textLabel6">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
Expand All @@ -121,7 +190,7 @@
</property>
</widget>
</item>
<item row="0" column="4">
<item row="1" column="4">
<widget class="QSpinBox" name="spinAngle">
<property name="maximum">
<number>360</number>
Expand All @@ -134,7 +203,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QLabel" name="textLabel8">
<property name="text">
<string>Placement</string>
Expand All @@ -144,7 +213,7 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QLabel" name="lblMargin">
<property name="minimumSize">
<size>
Expand All @@ -157,14 +226,14 @@
</property>
</widget>
</item>
<item row="1" column="2" colspan="3">
<item row="2" column="2" colspan="3">
<widget class="QComboBox" name="cboPlacement">
<property name="toolTip">
<string>Placement on screen</string>
</property>
</widget>
</item>
<item row="3" column="1" colspan="2">
<item row="4" column="1" colspan="2">
<widget class="QCheckBox" name="cboxAutomatic">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
Expand Down Expand Up @@ -192,7 +261,7 @@
</property>
</widget>
</item>
<item row="4" column="1">
<item row="5" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -205,7 +274,7 @@
</property>
</spacer>
</item>
<item row="0" column="2" colspan="2">
<item row="1" column="2" colspan="2">
<widget class="QSlider" name="sliderRotation">
<property name="toolTip">
<string/>
Expand All @@ -224,7 +293,7 @@
</property>
</widget>
</item>
<item row="0" column="0" rowspan="4">
<item row="1" column="0" rowspan="4">
<widget class="QLabel" name="pixmapLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
Expand Down Expand Up @@ -263,6 +332,12 @@
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsColorButton</class>
<extends>QToolButton</extends>
<header>qgscolorbutton.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsSpinBox</class>
<extends>QSpinBox</extends>
Expand All @@ -277,6 +352,7 @@
</customwidgets>
<tabstops>
<tabstop>grpEnable</tabstop>
<tabstop>pbnChangeColor</tabstop>
<tabstop>sliderRotation</tabstop>
<tabstop>spinAngle</tabstop>
<tabstop>cboPlacement</tabstop>
Expand Down

0 comments on commit 1445505

Please sign in to comment.