Skip to content

Commit

Permalink
[FEATURE] Project specific color scheme, set through default styles t…
Browse files Browse the repository at this point in the history
…ab in project properties
  • Loading branch information
nyalldawson committed Aug 16, 2014
1 parent cd54d6a commit 9ef4bd6
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 26 deletions.
28 changes: 28 additions & 0 deletions python/core/qgscolorscheme.sip
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,31 @@ class QgsCustomColorScheme : QgsColorScheme

QgsColorScheme* clone() const /Factory/;
}; // class QgsCustomColorScheme

/** \ingroup core
* \class QgsProjectColorScheme
* \brief A color scheme which contains project specific colors set through project properties dialog.
* \note Added in version 2.5
*/
class QgsProjectColorScheme : QgsColorScheme
{
%TypeHeaderCode
#include <qgscolorscheme.h>
%End
public:

QgsProjectColorScheme();

virtual ~QgsProjectColorScheme();

virtual QString schemeName() const;

virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() );

virtual bool isEditable() const;

virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );

QgsColorScheme* clone() const /Factory/;
}; // class QgsProjectColorScheme
28 changes: 28 additions & 0 deletions src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "qgsrelationmanagerdialog.h"
#include "qgsrelationmanager.h"
#include "qgisapp.h"
#include "qgscolorschemeregistry.h"

//qt includes
#include <QColorDialog>
Expand Down Expand Up @@ -477,6 +478,15 @@ QgsProjectProperties::QgsProjectProperties( QgsMapCanvas* mapCanvas, QWidget *pa
mStyle = QgsStyleV2::defaultStyle();
populateStyles();

// Color palette
QList<QgsProjectColorScheme *> projectSchemes;
QgsColorSchemeRegistry::instance()->schemes( projectSchemes );
if ( projectSchemes.length() > 0 )
{
mTreeProjectColors->setScheme( projectSchemes.at( 0 ) );
}


// Project macros
QString pythonMacros = QgsProject::instance()->readEntry( "Macros", "/pythonCode", QString::null );
grpPythonMacros->setChecked( !pythonMacros.isEmpty() );
Expand Down Expand Up @@ -885,6 +895,7 @@ void QgsProjectProperties::apply()
QgsProject::instance()->writeEntry( "DefaultStyles", "/ColorRamp", cboStyleColorRamp->currentText() );
QgsProject::instance()->writeEntry( "DefaultStyles", "/AlphaInt", ( int )( 255 - ( mTransparencySlider->value() * 2.55 ) ) );
QgsProject::instance()->writeEntry( "DefaultStyles", "/RandomColors", cbxStyleRandomColors->isChecked() );
mTreeProjectColors->saveColorsToScheme();

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

updateEllipsoidUI( myIndex );
}

void QgsProjectProperties::on_mButtonAddColor_clicked()
{
QColor newColor = QColorDialog::getColor( QColor(), this->parentWidget(), tr( "Select color" ), QColorDialog::ShowAlphaChannel );
if ( !newColor.isValid() )
{
return;
}
activateWindow();

mTreeProjectColors->addColor( newColor );
}

void QgsProjectProperties::on_mButtonRemoveColor_clicked()
{
mTreeProjectColors->removeSelection();
}
4 changes: 4 additions & 0 deletions src/app/qgsprojectproperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class APP_EXPORT QgsProjectProperties : public QgsOptionsDialogBase, private Ui:
//! sets the right ellipsoid for measuring (from settings)
void projectionSelectorInitialized();

void on_mButtonAddColor_clicked();

void on_mButtonRemoveColor_clicked();

signals:
//! Signal used to inform listeners that the mouse display precision may have changed
void displayPrecisionChanged();
Expand Down
71 changes: 70 additions & 1 deletion src/core/qgscolorscheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include "qgscolorscheme.h"

#include <QSettings>

#include "qgsproject.h"
#include "qgssymbollayerv2utils.h"

QgsColorScheme::QgsColorScheme()
{
Expand Down Expand Up @@ -165,3 +166,71 @@ QgsColorScheme *QgsCustomColorScheme::clone() const
{
return new QgsCustomColorScheme();
}


QgsProjectColorScheme::QgsProjectColorScheme()
{

}

QgsProjectColorScheme::~QgsProjectColorScheme()
{

}

QgsNamedColorList QgsProjectColorScheme::fetchColors( const QString context, const QColor baseColor )
{
Q_UNUSED( context );
Q_UNUSED( baseColor );

QgsNamedColorList colorList;

QStringList colorStrings = QgsProject::instance()->readListEntry( "Palette", "/Colors" );
QStringList colorLabels = QgsProject::instance()->readListEntry( "Palette", "/Labels" );

//generate list from custom colors
int colorIndex = 0;
for ( QStringList::iterator it = colorStrings.begin();
it != colorStrings.end(); ++it )
{
QColor color = QgsSymbolLayerV2Utils::decodeColor( *it );
QString label;
if ( colorLabels.length() > colorIndex )
{
label = colorLabels.at( colorIndex );
}

colorList.append( qMakePair( color, label ) );
colorIndex++;
}

return colorList;
}

bool QgsProjectColorScheme::setColors( const QgsNamedColorList colors, const QString context, const QColor baseColor )
{
Q_UNUSED( context );
Q_UNUSED( baseColor );

// save colors to project
QSettings settings;
QStringList customColors;
QStringList customColorLabels;

QgsNamedColorList::const_iterator colorIt = colors.constBegin();
for ( ; colorIt != colors.constEnd(); ++colorIt )
{
QString color = QgsSymbolLayerV2Utils::encodeColor(( *colorIt ).first );
QString label = ( *colorIt ).second;
customColors.append( color );
customColorLabels.append( label );
}
QgsProject::instance()->writeEntry( "Palette", "/Colors", customColors );
QgsProject::instance()->writeEntry( "Palette", "/Labels", customColorLabels );
return true;
}

QgsColorScheme *QgsProjectColorScheme::clone() const
{
return new QgsProjectColorScheme();
}
25 changes: 25 additions & 0 deletions src/core/qgscolorscheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,29 @@ class CORE_EXPORT QgsCustomColorScheme : public QgsColorScheme
QgsColorScheme* clone() const;
};

/** \ingroup core
* \class QgsProjectColorScheme
* \brief A color scheme which contains project specific colors set through project properties dialog.
* \note Added in version 2.5
*/
class CORE_EXPORT QgsProjectColorScheme : public QgsColorScheme
{
public:

QgsProjectColorScheme();

virtual ~QgsProjectColorScheme();

virtual QString schemeName() const { return QT_TR_NOOP( "Project colors" ); }

virtual QgsNamedColorList fetchColors( const QString context = QString(),
const QColor baseColor = QColor() );

virtual bool isEditable() const { return true; }

virtual bool setColors( const QgsNamedColorList colors, const QString context = QString(), const QColor baseColor = QColor() );

QgsColorScheme* clone() const;
};

#endif
1 change: 1 addition & 0 deletions src/core/qgscolorschemeregistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void QgsColorSchemeRegistry::addDefaultSchemes()
//default color schemes
addColorScheme( new QgsRecentColorScheme() );
addColorScheme( new QgsCustomColorScheme() );
addColorScheme( new QgsProjectColorScheme() );
}

void QgsColorSchemeRegistry::addColorScheme( QgsColorScheme *scheme )
Expand Down
75 changes: 50 additions & 25 deletions src/ui/qgsprojectpropertiesbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
</sizepolicy>
</property>
<property name="currentIndex">
<number>1</number>
<number>3</number>
</property>
<widget class="QWidget" name="mProjOpts_01">
<layout class="QVBoxLayout" name="verticalLayout_6">
Expand All @@ -211,8 +211,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>683</width>
<height>779</height>
<width>650</width>
<height>657</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
Expand Down Expand Up @@ -848,8 +848,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>132</width>
<height>100</height>
<width>683</width>
<height>779</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_10">
Expand Down Expand Up @@ -920,17 +920,11 @@
<rect>
<x>0</x>
<y>0</y>
<width>376</width>
<height>324</height>
<width>683</width>
<height>779</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QgsCollapsibleGroupBox" name="groupBox">
<property name="title">
Expand Down Expand Up @@ -1252,17 +1246,43 @@
</layout>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
<widget class="QgsCollapsibleGroupBox" name="groupBox_7">
<property name="title">
<string>Project colors</string>
</property>
</spacer>
<layout class="QGridLayout" name="gridLayout_12">
<item row="2" column="1">
<spacer name="verticalSpacer_12">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="mButtonAddColor">
<property name="text">
<string>Add color</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="mButtonRemoveColor">
<property name="text">
<string>Remove color</string>
</property>
</widget>
</item>
<item row="0" column="0" rowspan="4">
<widget class="QgsColorSchemeList" name="mTreeProjectColors"/>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
Expand Down Expand Up @@ -2010,8 +2030,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>683</width>
<height>779</height>
<width>166</width>
<height>46</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_17">
Expand Down Expand Up @@ -2115,6 +2135,11 @@
<header>qgscodeeditorpython.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsColorSchemeList</class>
<extends>QTreeView</extends>
<header>qgscolorschemelist.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>buttonBox</tabstop>
Expand Down

0 comments on commit 9ef4bd6

Please sign in to comment.