Skip to content

Commit 9ef4bd6

Browse files
committed
[FEATURE] Project specific color scheme, set through default styles tab in project properties
1 parent cd54d6a commit 9ef4bd6

File tree

7 files changed

+206
-26
lines changed

7 files changed

+206
-26
lines changed

python/core/qgscolorscheme.sip

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,31 @@ class QgsCustomColorScheme : QgsColorScheme
115115

116116
QgsColorScheme* clone() const /Factory/;
117117
}; // class QgsCustomColorScheme
118+
119+
/** \ingroup core
120+
* \class QgsProjectColorScheme
121+
* \brief A color scheme which contains project specific colors set through project properties dialog.
122+
* \note Added in version 2.5
123+
*/
124+
class QgsProjectColorScheme : QgsColorScheme
125+
{
126+
%TypeHeaderCode
127+
#include <qgscolorscheme.h>
128+
%End
129+
public:
130+
131+
QgsProjectColorScheme();
132+
133+
virtual ~QgsProjectColorScheme();
134+
135+
virtual QString schemeName() const;
136+
137+
virtual QgsNamedColorList fetchColors( const QString context = QString(),
138+
const QColor baseColor = QColor() );
139+
140+
virtual bool isEditable() const;
141+
142+
virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );
143+
144+
QgsColorScheme* clone() const /Factory/;
145+
}; // class QgsProjectColorScheme

src/app/qgsprojectproperties.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "qgsrelationmanagerdialog.h"
4747
#include "qgsrelationmanager.h"
4848
#include "qgisapp.h"
49+
#include "qgscolorschemeregistry.h"
4950

5051
//qt includes
5152
#include <QColorDialog>
@@ -477,6 +478,15 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
477478
mStyle = QgsStyleV2::defaultStyle();
478479
populateStyles();
479480

481+
// Color palette
482+
QList<QgsProjectColorScheme *> projectSchemes;
483+
QgsColorSchemeRegistry::instance()->schemes( projectSchemes );
484+
if ( projectSchemes.length() > 0 )
485+
{
486+
mTreeProjectColors->setScheme( projectSchemes.at( 0 ) );
487+
}
488+
489+
480490
// Project macros
481491
QString pythonMacros = QgsProject::instance()->readEntry( "Macros", "/pythonCode", QString::null );
482492
grpPythonMacros->setChecked( !pythonMacros.isEmpty() );
@@ -885,6 +895,7 @@ void QgsProjectProperties::apply()
885895
QgsProject::instance()->writeEntry( "DefaultStyles", "/ColorRamp", cboStyleColorRamp->currentText() );
886896
QgsProject::instance()->writeEntry( "DefaultStyles", "/AlphaInt", ( int )( 255 - ( mTransparencySlider->value() * 2.55 ) ) );
887897
QgsProject::instance()->writeEntry( "DefaultStyles", "/RandomColors", cbxStyleRandomColors->isChecked() );
898+
mTreeProjectColors->saveColorsToScheme();
888899

889900
// store project macros
890901
QString pythonMacros = ptePythonMacros->text();
@@ -1657,3 +1668,20 @@ void QgsProjectProperties::projectionSelectorInitialized()
16571668

16581669
updateEllipsoidUI( myIndex );
16591670
}
1671+
1672+
void QgsProjectProperties::on_mButtonAddColor_clicked()
1673+
{
1674+
QColor newColor = QColorDialog::getColor( QColor(), this->parentWidget(), tr( "Select color" ), QColorDialog::ShowAlphaChannel );
1675+
if ( !newColor.isValid() )
1676+
{
1677+
return;
1678+
}
1679+
activateWindow();
1680+
1681+
mTreeProjectColors->addColor( newColor );
1682+
}
1683+
1684+
void QgsProjectProperties::on_mButtonRemoveColor_clicked()
1685+
{
1686+
mTreeProjectColors->removeSelection();
1687+
}

src/app/qgsprojectproperties.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
166166
//! sets the right ellipsoid for measuring (from settings)
167167
void projectionSelectorInitialized();
168168

169+
void on_mButtonAddColor_clicked();
170+
171+
void on_mButtonRemoveColor_clicked();
172+
169173
signals:
170174
//! Signal used to inform listeners that the mouse display precision may have changed
171175
void displayPrecisionChanged();

src/core/qgscolorscheme.cpp

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
#include "qgscolorscheme.h"
1919

2020
#include <QSettings>
21-
21+
#include "qgsproject.h"
22+
#include "qgssymbollayerv2utils.h"
2223

2324
QgsColorScheme::QgsColorScheme()
2425
{
@@ -165,3 +166,71 @@ QgsColorScheme *QgsCustomColorScheme::clone() const
165166
{
166167
return new QgsCustomColorScheme();
167168
}
169+
170+
171+
QgsProjectColorScheme::QgsProjectColorScheme()
172+
{
173+
174+
}
175+
176+
QgsProjectColorScheme::~QgsProjectColorScheme()
177+
{
178+
179+
}
180+
181+
QgsNamedColorList QgsProjectColorScheme::fetchColors( const QString context, const QColor baseColor )
182+
{
183+
Q_UNUSED( context );
184+
Q_UNUSED( baseColor );
185+
186+
QgsNamedColorList colorList;
187+
188+
QStringList colorStrings = QgsProject::instance()->readListEntry( "Palette", "/Colors" );
189+
QStringList colorLabels = QgsProject::instance()->readListEntry( "Palette", "/Labels" );
190+
191+
//generate list from custom colors
192+
int colorIndex = 0;
193+
for ( QStringList::iterator it = colorStrings.begin();
194+
it != colorStrings.end(); ++it )
195+
{
196+
QColor color = QgsSymbolLayerV2Utils::decodeColor( *it );
197+
QString label;
198+
if ( colorLabels.length() > colorIndex )
199+
{
200+
label = colorLabels.at( colorIndex );
201+
}
202+
203+
colorList.append( qMakePair( color, label ) );
204+
colorIndex++;
205+
}
206+
207+
return colorList;
208+
}
209+
210+
bool QgsProjectColorScheme::setColors( const QgsNamedColorList colors, const QString context, const QColor baseColor )
211+
{
212+
Q_UNUSED( context );
213+
Q_UNUSED( baseColor );
214+
215+
// save colors to project
216+
QSettings settings;
217+
QStringList customColors;
218+
QStringList customColorLabels;
219+
220+
QgsNamedColorList::const_iterator colorIt = colors.constBegin();
221+
for ( ; colorIt != colors.constEnd(); ++colorIt )
222+
{
223+
QString color = QgsSymbolLayerV2Utils::encodeColor(( *colorIt ).first );
224+
QString label = ( *colorIt ).second;
225+
customColors.append( color );
226+
customColorLabels.append( label );
227+
}
228+
QgsProject::instance()->writeEntry( "Palette", "/Colors", customColors );
229+
QgsProject::instance()->writeEntry( "Palette", "/Labels", customColorLabels );
230+
return true;
231+
}
232+
233+
QgsColorScheme *QgsProjectColorScheme::clone() const
234+
{
235+
return new QgsProjectColorScheme();
236+
}

src/core/qgscolorscheme.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,29 @@ class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme
130130
QgsColorScheme* clone() const;
131131
};
132132

133+
/** \ingroup core
134+
* \class QgsProjectColorScheme
135+
* \brief A color scheme which contains project specific colors set through project properties dialog.
136+
* \note Added in version 2.5
137+
*/
138+
class CORE_EXPORT QgsProjectColorScheme : public QgsColorScheme
139+
{
140+
public:
141+
142+
QgsProjectColorScheme();
143+
144+
virtual ~QgsProjectColorScheme();
145+
146+
virtual QString schemeName() const { return QT_TR_NOOP( "Project colors" ); }
147+
148+
virtual QgsNamedColorList fetchColors( const QString context = QString(),
149+
const QColor baseColor = QColor() );
150+
151+
virtual bool isEditable() const { return true; }
152+
153+
virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );
154+
155+
QgsColorScheme* clone() const;
156+
};
157+
133158
#endif

src/core/qgscolorschemeregistry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void QgsColorSchemeRegistry::addDefaultSchemes()
6868
//default color schemes
6969
addColorScheme( new QgsRecentColorScheme() );
7070
addColorScheme( new QgsCustomColorScheme() );
71+
addColorScheme( new QgsProjectColorScheme() );
7172
}
7273

7374
void QgsColorSchemeRegistry::addColorScheme( QgsColorScheme *scheme )

src/ui/qgsprojectpropertiesbase.ui

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@
191191
</sizepolicy>
192192
</property>
193193
<property name="currentIndex">
194-
<number>1</number>
194+
<number>3</number>
195195
</property>
196196
<widget class="QWidget" name="mProjOpts_01">
197197
<layout class="QVBoxLayout" name="verticalLayout_6">
@@ -211,8 +211,8 @@
211211
<rect>
212212
<x>0</x>
213213
<y>0</y>
214-
<width>683</width>
215-
<height>779</height>
214+
<width>650</width>
215+
<height>657</height>
216216
</rect>
217217
</property>
218218
<layout class="QVBoxLayout" name="verticalLayout_8">
@@ -848,8 +848,8 @@
848848
<rect>
849849
<x>0</x>
850850
<y>0</y>
851-
<width>132</width>
852-
<height>100</height>
851+
<width>683</width>
852+
<height>779</height>
853853
</rect>
854854
</property>
855855
<layout class="QVBoxLayout" name="verticalLayout_10">
@@ -920,17 +920,11 @@
920920
<rect>
921921
<x>0</x>
922922
<y>0</y>
923-
<width>376</width>
924-
<height>324</height>
923+
<width>683</width>
924+
<height>779</height>
925925
</rect>
926926
</property>
927927
<layout class="QVBoxLayout" name="verticalLayout_12">
928-
<property name="topMargin">
929-
<number>0</number>
930-
</property>
931-
<property name="bottomMargin">
932-
<number>0</number>
933-
</property>
934928
<item>
935929
<widget class="QgsCollapsibleGroupBox" name="groupBox">
936930
<property name="title">
@@ -1252,17 +1246,43 @@
12521246
</layout>
12531247
</item>
12541248
<item>
1255-
<spacer name="verticalSpacer_2">
1256-
<property name="orientation">
1257-
<enum>Qt::Vertical</enum>
1258-
</property>
1259-
<property name="sizeHint" stdset="0">
1260-
<size>
1261-
<width>20</width>
1262-
<height>40</height>
1263-
</size>
1249+
<widget class="QgsCollapsibleGroupBox" name="groupBox_7">
1250+
<property name="title">
1251+
<string>Project colors</string>
12641252
</property>
1265-
</spacer>
1253+
<layout class="QGridLayout" name="gridLayout_12">
1254+
<item row="2" column="1">
1255+
<spacer name="verticalSpacer_12">
1256+
<property name="orientation">
1257+
<enum>Qt::Vertical</enum>
1258+
</property>
1259+
<property name="sizeHint" stdset="0">
1260+
<size>
1261+
<width>20</width>
1262+
<height>40</height>
1263+
</size>
1264+
</property>
1265+
</spacer>
1266+
</item>
1267+
<item row="0" column="1">
1268+
<widget class="QPushButton" name="mButtonAddColor">
1269+
<property name="text">
1270+
<string>Add color</string>
1271+
</property>
1272+
</widget>
1273+
</item>
1274+
<item row="1" column="1">
1275+
<widget class="QPushButton" name="mButtonRemoveColor">
1276+
<property name="text">
1277+
<string>Remove color</string>
1278+
</property>
1279+
</widget>
1280+
</item>
1281+
<item row="0" column="0" rowspan="4">
1282+
<widget class="QgsColorSchemeList" name="mTreeProjectColors"/>
1283+
</item>
1284+
</layout>
1285+
</widget>
12661286
</item>
12671287
</layout>
12681288
</widget>
@@ -2010,8 +2030,8 @@
20102030
<rect>
20112031
<x>0</x>
20122032
<y>0</y>
2013-
<width>683</width>
2014-
<height>779</height>
2033+
<width>166</width>
2034+
<height>46</height>
20152035
</rect>
20162036
</property>
20172037
<layout class="QVBoxLayout" name="verticalLayout_17">
@@ -2115,6 +2135,11 @@
21152135
<header>qgscodeeditorpython.h</header>
21162136
<container>1</container>
21172137
</customwidget>
2138+
<customwidget>
2139+
<class>QgsColorSchemeList</class>
2140+
<extends>QTreeView</extends>
2141+
<header>qgscolorschemelist.h</header>
2142+
</customwidget>
21182143
</customwidgets>
21192144
<tabstops>
21202145
<tabstop>buttonBox</tabstop>

0 commit comments

Comments
 (0)