Skip to content

Commit cd54d6a

Browse files
committedAug 16, 2014
New GUI control for an editable list of colors from a color scheme.
Supports drag and drop reordering, drag and drop colors from external apps (on supported OS).
1 parent 6312ff5 commit cd54d6a

File tree

8 files changed

+888
-126
lines changed

8 files changed

+888
-126
lines changed
 

‎python/gui/gui.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
%Include qgscolorbuttonv2.sip
3737
%Include qgscolordialog.sip
3838
%Include qgscolorswatchgrid.sip
39+
%Include qgscolorschemelist.sip
3940
%Include qgscomposerview.sip
4041
%Include qgscredentialdialog.sip
4142
%Include qgsdetaileditemdata.sip

‎python/gui/qgscolorschemelist.sip

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/** \ingroup gui
2+
* \class QgsColorSwatchDelegate
3+
* A delegate for showing a color swatch in a list
4+
* @see QgsColorSchemeList
5+
* @note introduced in QGIS 2.5
6+
*/
7+
class QgsColorSwatchDelegate : QAbstractItemDelegate
8+
{
9+
%TypeHeaderCode
10+
#include <qgscolorschemelist.h>
11+
%End
12+
13+
public:
14+
QgsColorSwatchDelegate( QWidget *parent /TransferThis/ = 0 );
15+
void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
16+
QSize sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
17+
bool editorEvent( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index );
18+
};
19+
20+
/** \ingroup gui
21+
* \class QgsColorSchemeModel
22+
* A model for colors in a color scheme
23+
* @see QgsColorSchemeList
24+
* @note introduced in QGIS 2.5
25+
*/
26+
class QgsColorSchemeModel: QAbstractItemModel
27+
{
28+
%TypeHeaderCode
29+
#include <qgscolorschemelist.h>
30+
%End
31+
32+
public:
33+
34+
/**Constructor
35+
* @param scheme color scheme for list
36+
* @param context context string for color scheme
37+
* @param baseColor base color for color scheme
38+
* @param parent parent object
39+
*/
40+
explicit QgsColorSchemeModel( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor(), QObject* parent /TransferThis/ = 0 );
41+
42+
~QgsColorSchemeModel();
43+
44+
//reimplemented QAbstractItemModel methods
45+
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
46+
QModelIndex parent( const QModelIndex &index ) const;
47+
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
48+
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
49+
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
50+
Qt::ItemFlags flags( const QModelIndex & index ) const;
51+
bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
52+
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
53+
Qt::DropActions supportedDropActions() const;
54+
QStringList mimeTypes() const;
55+
bool removeRows( int row, int count, const QModelIndex & parent = QModelIndex() );
56+
bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() );
57+
QMimeData *mimeData( const QModelIndexList &indexes ) const;
58+
bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
59+
60+
/**Returns a list of colors shown in the widget
61+
* @returns colors shown in the widget
62+
*/
63+
QgsNamedColorList colors() const;
64+
65+
/**Sets the color scheme to show in the widget
66+
* @param scheme color scheme
67+
* @param context context for color scheme
68+
* @param baseColor base color for color scheme
69+
*/
70+
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );
71+
72+
/**Get the current color scheme context for the model
73+
* @returns context string which is passed to scheme for color generation
74+
* @see baseColor
75+
*/
76+
QString context() const;
77+
78+
/**Get the base color for the color scheme used by the model
79+
* @returns base color which is passed to scheme for color generation
80+
* @see context
81+
*/
82+
QColor baseColor() const;
83+
84+
/**Add a color to the list
85+
* @param color color to add
86+
* @param label label for color
87+
*/
88+
void addColor( const QColor color, const QString label = QString() );
89+
90+
};
91+
92+
/** \ingroup gui
93+
* \class QgsColorSchemeList
94+
* An editable list of color swatches, taken from an associated QgsColorScheme.
95+
* @see QgsColorSchemeList
96+
* @note introduced in QGIS 2.5
97+
*/
98+
class QgsColorSchemeList: QTreeView
99+
{
100+
%TypeHeaderCode
101+
#include <qgscolorschemelist.h>
102+
%End
103+
104+
public:
105+
106+
/**Construct a new color swatch grid.
107+
* @param parent parent widget
108+
* @param scheme QgsColorScheme for colors to show in the list
109+
* @param context context string provided to color scheme
110+
* @param baseColor base color for color scheme
111+
*/
112+
QgsColorSchemeList( QWidget *parent /TransferThis/ = 0, QgsColorScheme* scheme = 0, const QString context = QString(), const QColor baseColor = QColor() );
113+
114+
virtual ~QgsColorSchemeList();
115+
116+
/**Sets the color scheme to show in the list
117+
* @param scheme QgsColorScheme for colors to show in the list
118+
* @param context context string provided to color scheme
119+
* @param baseColor base color for color scheme
120+
*/
121+
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );
122+
123+
/**Saves the current colors shown in the list back to a color scheme, if supported
124+
* by the color scheme.
125+
* @note this method is only effective if the color scheme is editable
126+
*/
127+
bool saveColorsToScheme();
128+
129+
/**Removes any selected colors from the list
130+
*/
131+
void removeSelection();
132+
133+
/**Adds a color to the list
134+
* @param color color to add
135+
* @param label optional label for color
136+
*/
137+
void addColor( const QColor color, const QString label = QString() );
138+
139+
};
140+

‎src/app/qgsoptions.cpp

Lines changed: 9 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "qgsrasterpyramidsoptionswidget.h"
3737
#include "qgsdialog.h"
3838
#include "qgscomposer.h"
39+
#include "qgscolorschemeregistry.h"
3940

4041
#include <QInputDialog>
4142
#include <QFileDialog>
@@ -687,33 +688,14 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl ) :
687688
//
688689
// Color palette
689690
//
690-
QList< QVariant > customColorVariants = settings.value( QString( "/colors/palettecolors" ) ).toList();
691-
QList< QVariant > customColorLabels = settings.value( QString( "/colors/palettelabels" ) ).toList();
692691

693-
QList<QTreeWidgetItem *> customColors;
694-
int colorIndex = 0;
695-
for ( QList< QVariant >::iterator it = customColorVariants.begin();
696-
it != customColorVariants.end(); ++it )
692+
//find custom color scheme from registry
693+
QList<QgsCustomColorScheme *> customSchemes;
694+
QgsColorSchemeRegistry::instance()->schemes( customSchemes );
695+
if ( customSchemes.length() > 0 )
697696
{
698-
QColor color = ( *it ).value<QColor>();
699-
QString label;
700-
if ( customColorLabels.length() > colorIndex )
701-
{
702-
label = customColorLabels.at( colorIndex ).toString();
703-
}
704-
705-
QTreeWidgetItem* item = new QTreeWidgetItem();
706-
item->setData( 0, PaletteLabelRole, label );
707-
item->setText( 1, label );
708-
setPaletteColor( item, color );
709-
customColors.append( item );
710-
711-
colorIndex++;
697+
mTreeCustomColors->setScheme( customSchemes.at( 0 ) );
712698
}
713-
mTreeCustomColors->clear();
714-
mTreeCustomColors->insertTopLevelItems( 0, customColors );
715-
mTreeCustomColors->resizeColumnToContents( 0 );
716-
mTreeCustomColors->setColumnWidth( 0, mTreeCustomColors->columnWidth( 0 ) + 20 );
717699

718700
//
719701
// Composer settings
@@ -1329,19 +1311,7 @@ void QgsOptions::saveOptions()
13291311
//
13301312
// Color palette
13311313
//
1332-
QList< QVariant > customColors;
1333-
QList< QVariant > customColorLabels;
1334-
int colorCount = mTreeCustomColors->topLevelItemCount();
1335-
for ( int i = 0; i < colorCount; i++ )
1336-
{
1337-
QTreeWidgetItem* item = mTreeCustomColors->topLevelItem( i );
1338-
QVariant label = item->data( 0, PaletteLabelRole );
1339-
QVariant color = item->data( 0, PaletteColorRole );
1340-
customColors.append( color );
1341-
customColorLabels.append( label );
1342-
}
1343-
settings.setValue( QString( "/colors/palettecolors" ), customColors );
1344-
settings.setValue( QString( "/colors/palettelabels" ), customColorLabels );
1314+
mTreeCustomColors->saveColorsToScheme();
13451315

13461316
//
13471317
// Composer settings
@@ -2114,73 +2084,10 @@ void QgsOptions::on_mButtonAddColor_clicked()
21142084
}
21152085
activateWindow();
21162086

2117-
QString newLabel = QInputDialog::getText( this, tr( "Color label" ),
2118-
tr( "Please enter a label for the color" ) );
2119-
activateWindow();
2120-
2121-
QTreeWidgetItem* item = new QTreeWidgetItem();
2122-
item->setData( 0, PaletteLabelRole, newLabel );
2123-
item->setText( 1, newLabel );
2124-
setPaletteColor( item, newColor );
2125-
mTreeCustomColors->addTopLevelItem( item );
2126-
mTreeCustomColors->resizeColumnToContents( 0 );
2127-
mTreeCustomColors->setColumnWidth( 0, mTreeCustomColors->columnWidth( 0 ) + 20 );
2087+
mTreeCustomColors->addColor( newColor );
21282088
}
21292089

21302090
void QgsOptions::on_mButtonRemoveColor_clicked()
21312091
{
2132-
QTreeWidgetItem* item = mTreeCustomColors->currentItem();
2133-
if ( !item )
2134-
return;
2135-
int index = mTreeCustomColors->indexOfTopLevelItem( item );
2136-
mTreeCustomColors->takeTopLevelItem( index );
2137-
}
2138-
2139-
void QgsOptions::on_mTreeCustomColors_itemDoubleClicked( QTreeWidgetItem* item, int column )
2140-
{
2141-
if ( column == 0 )
2142-
{
2143-
QColor newColor = QColorDialog::getColor( item->data( 0, PaletteColorRole ).value<QColor>(), this->parentWidget(), tr( "Select color" ), QColorDialog::ShowAlphaChannel );
2144-
if ( !newColor.isValid() )
2145-
{
2146-
return;
2147-
}
2148-
setPaletteColor( item, newColor );
2149-
}
2150-
else
2151-
{
2152-
bool ok;
2153-
QString label = item->data( 0, PaletteLabelRole ).toString();
2154-
QString newLabel = QInputDialog::getText( this, tr( "Color label" ),
2155-
tr( "Please enter a label for the color" ),
2156-
QLineEdit::Normal, label, &ok );
2157-
if ( !ok )
2158-
{
2159-
return;
2160-
}
2161-
2162-
item->setText( 1, newLabel );
2163-
item->setData( 0, PaletteLabelRole, newLabel );
2164-
}
2165-
}
2166-
2167-
void QgsOptions::setPaletteColor( QTreeWidgetItem* item, QColor color )
2168-
{
2169-
QSize iconSize( 16, 16 );
2170-
QPixmap pixmap( iconSize );
2171-
pixmap.fill( QColor( 0, 0, 0, 0 ) );
2172-
QRect rect( 1, 1, iconSize.width() - 2, iconSize.height() - 2 );
2173-
2174-
// draw a slightly rounded rectangle
2175-
QPainter p;
2176-
p.begin( &pixmap );
2177-
p.setPen( Qt::NoPen );
2178-
p.setRenderHint( QPainter::Antialiasing );
2179-
p.setBrush( color );
2180-
p.drawRoundedRect( rect, 2, 2 );
2181-
p.end();
2182-
2183-
item->setIcon( 0, QIcon( pixmap ) );
2184-
item->setData( 0, PaletteColorRole, color );
2185-
item->setText( 0, color.name() );
2092+
mTreeCustomColors->removeSelection();
21862093
}

‎src/app/qgsoptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
243243

244244
void on_mButtonAddColor_clicked();
245245
void on_mButtonRemoveColor_clicked();
246-
void on_mTreeCustomColors_itemDoubleClicked( QTreeWidgetItem* item, int column );
247246

248247
private:
249248
QStringList i18nList();
@@ -260,8 +259,6 @@ class APP_EXPORT QgsOptions : public QgsOptionsDialogBase, private Ui::QgsOption
260259

261260
void saveDefaultDatumTransformations();
262261

263-
void setPaletteColor( QTreeWidgetItem *item, QColor color );
264-
265262
protected:
266263
QgisAppStyleSheet* mStyleSheetBuilder;
267264
QMap<QString, QVariant> mStyleSheetNewOpts;

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ qgscolorbutton.cpp
128128
qgscolorbuttonv2.cpp
129129
qgscolordialog.cpp
130130
qgscolorswatchgrid.cpp
131+
qgscolorschemelist.cpp
131132
qgscodeeditor.cpp
132133
qgscodeeditorpython.cpp
133134
qgscodeeditorsql.cpp
@@ -226,6 +227,7 @@ SET(QGIS_GUI_MOC_HDRS
226227
qgscolorbutton.h
227228
qgscolorbuttonv2.h
228229
qgscolorswatchgrid.h
230+
qgscolorschemelist.h
229231

230232
raster/qgsrasterminmaxwidget.h
231233
raster/qgspalettedrendererwidget.h

‎src/gui/qgscolorschemelist.cpp

Lines changed: 541 additions & 0 deletions
Large diffs are not rendered by default.

‎src/gui/qgscolorschemelist.h

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/***************************************************************************
2+
qgscolorschemelist.h
3+
--------------------
4+
Date : August 2014
5+
Copyright : (C) 2014 by Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef QGSCOLORSCHEMELIST_H
16+
#define QGSCOLORSCHEMELIST_H
17+
18+
#include "qgscolorscheme.h"
19+
#include <QTreeView>
20+
#include <QAbstractItemModel>
21+
#include <QItemDelegate>
22+
23+
class QMimeData;
24+
25+
/** \ingroup gui
26+
* \class QgsColorSwatchDelegate
27+
* A delegate for showing a color swatch in a list
28+
* @see QgsColorSchemeList
29+
* @note introduced in QGIS 2.5
30+
*/
31+
class QgsColorSwatchDelegate : public QAbstractItemDelegate
32+
{
33+
Q_OBJECT
34+
35+
public:
36+
QgsColorSwatchDelegate( QWidget *parent = 0 );
37+
void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
38+
QSize sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
39+
bool editorEvent( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index );
40+
41+
private:
42+
QWidget* mParent;
43+
44+
/**Generates a checkboard pattern for transparent color backgrounds
45+
* @returns checkboard pixmap
46+
*/
47+
const QPixmap &transparentBackground() const;
48+
};
49+
50+
51+
/** \ingroup gui
52+
* \class QgsColorSchemeModel
53+
* A model for colors in a color scheme
54+
* @see QgsColorSchemeList
55+
* @note introduced in QGIS 2.5
56+
*/
57+
class CORE_EXPORT QgsColorSchemeModel: public QAbstractItemModel
58+
{
59+
Q_OBJECT
60+
61+
public:
62+
63+
/**Constructor
64+
* @param scheme color scheme for list
65+
* @param context context string for color scheme
66+
* @param baseColor base color for color scheme
67+
* @param parent parent object
68+
*/
69+
explicit QgsColorSchemeModel( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor(), QObject* parent = 0 );
70+
71+
~QgsColorSchemeModel();
72+
73+
//reimplemented QAbstractItemModel methods
74+
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
75+
QModelIndex parent( const QModelIndex &index ) const;
76+
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
77+
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
78+
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
79+
Qt::ItemFlags flags( const QModelIndex & index ) const;
80+
bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
81+
QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
82+
Qt::DropActions supportedDropActions() const;
83+
QStringList mimeTypes() const;
84+
bool removeRows( int row, int count, const QModelIndex & parent = QModelIndex() );
85+
bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() );
86+
QMimeData *mimeData( const QModelIndexList &indexes ) const;
87+
bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
88+
89+
/**Returns a list of colors shown in the widget
90+
* @returns colors shown in the widget
91+
*/
92+
QgsNamedColorList colors() const { return mColors; }
93+
94+
/**Sets the color scheme to show in the widget
95+
* @param scheme color scheme
96+
* @param context context for color scheme
97+
* @param baseColor base color for color scheme
98+
*/
99+
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );
100+
101+
/**Get the current color scheme context for the model
102+
* @returns context string which is passed to scheme for color generation
103+
* @see baseColor
104+
*/
105+
QString context() const { return mContext; }
106+
107+
/**Get the base color for the color scheme used by the model
108+
* @returns base color which is passed to scheme for color generation
109+
* @see context
110+
*/
111+
QColor baseColor() const { return mBaseColor; }
112+
113+
/**Add a color to the list
114+
* @param color color to add
115+
* @param label label for color
116+
*/
117+
void addColor( const QColor color, const QString label = QString() );
118+
119+
private:
120+
121+
enum Columns
122+
{
123+
ColorSwatch = 0,
124+
ColorLabel
125+
};
126+
127+
QgsNamedColorList mColors;
128+
QgsColorScheme* mScheme;
129+
QString mContext;
130+
QColor mBaseColor;
131+
};
132+
133+
/** \ingroup gui
134+
* \class QgsColorSchemeList
135+
* An editable list of color swatches, taken from an associated QgsColorScheme.
136+
* @see QgsColorSchemeList
137+
* @note introduced in QGIS 2.5
138+
*/
139+
class GUI_EXPORT QgsColorSchemeList: public QTreeView
140+
{
141+
Q_OBJECT
142+
143+
public:
144+
145+
/**Construct a new color swatch grid.
146+
* @param parent parent widget
147+
* @param scheme QgsColorScheme for colors to show in the list
148+
* @param context context string provided to color scheme
149+
* @param baseColor base color for color scheme
150+
*/
151+
QgsColorSchemeList( QWidget *parent = 0, QgsColorScheme* scheme = 0, const QString context = QString(), const QColor baseColor = QColor() );
152+
153+
virtual ~QgsColorSchemeList();
154+
155+
/**Sets the color scheme to show in the list
156+
* @param scheme QgsColorScheme for colors to show in the list
157+
* @param context context string provided to color scheme
158+
* @param baseColor base color for color scheme
159+
*/
160+
void setScheme( QgsColorScheme* scheme, const QString context = QString(), const QColor baseColor = QColor() );
161+
162+
/**Saves the current colors shown in the list back to a color scheme, if supported
163+
* by the color scheme.
164+
* @note this method is only effective if the color scheme is editable
165+
*/
166+
bool saveColorsToScheme();
167+
168+
/**Removes any selected colors from the list
169+
*/
170+
void removeSelection();
171+
172+
/**Adds a color to the list
173+
* @param color color to add
174+
* @param label optional label for color
175+
*/
176+
void addColor( const QColor color, const QString label = QString() );
177+
178+
private:
179+
QgsColorScheme* mScheme;
180+
QgsColorSchemeModel* mModel;
181+
QgsColorSwatchDelegate* mSwatchDelegate;
182+
183+
};
184+
185+
#endif

‎src/ui/qgsoptionsbase.ui

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@
263263
<item>
264264
<widget class="QStackedWidget" name="mOptionsStackedWidget">
265265
<property name="currentIndex">
266-
<number>0</number>
266+
<number>4</number>
267267
</property>
268268
<widget class="QWidget" name="mOptionsPageGeneral">
269269
<layout class="QVBoxLayout" name="verticalLayout_3">
@@ -2301,33 +2301,17 @@
23012301
</property>
23022302
</widget>
23032303
</item>
2304-
<item row="0" column="0" rowspan="4">
2305-
<widget class="QTreeWidget" name="mTreeCustomColors">
2306-
<property name="rootIsDecorated">
2307-
<bool>false</bool>
2308-
</property>
2309-
<property name="columnCount">
2310-
<number>2</number>
2311-
</property>
2312-
<column>
2313-
<property name="text">
2314-
<string>Color</string>
2315-
</property>
2316-
</column>
2317-
<column>
2318-
<property name="text">
2319-
<string>Label</string>
2320-
</property>
2321-
</column>
2322-
</widget>
2323-
</item>
23242304
<item row="1" column="1">
23252305
<widget class="QPushButton" name="mButtonRemoveColor">
23262306
<property name="text">
23272307
<string>Remove color</string>
23282308
</property>
23292309
</widget>
23302310
</item>
2311+
<item row="0" column="0" rowspan="4">
2312+
<widget class="QgsColorSchemeList" name="mTreeCustomColors">
2313+
</widget>
2314+
</item>
23312315
</layout>
23322316
</widget>
23332317
</item>
@@ -4708,6 +4692,11 @@
47084692
<extends>QWidget</extends>
47094693
<header>qgsscalecombobox.h</header>
47104694
</customwidget>
4695+
<customwidget>
4696+
<class>QgsColorSchemeList</class>
4697+
<extends>QTreeView</extends>
4698+
<header>qgscolorschemelist.h</header>
4699+
</customwidget>
47114700
</customwidgets>
47124701
<resources>
47134702
<include location="../../images/images.qrc"/>

0 commit comments

Comments
 (0)
Please sign in to comment.