QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgscolorschemelist.h
Go to the documentation of this file.
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 "qgis_gui.h"
19#include "qgis_sip.h"
20#include "qgscolorscheme.h"
21
22#include <QAbstractItemModel>
23#include <QFile>
24#include <QItemDelegate>
25#include <QTreeView>
26
27class QMimeData;
28class QgsPanelWidget;
29
36class GUI_EXPORT QgsColorSwatchDelegate : public QAbstractItemDelegate
37{
38 Q_OBJECT
39
40 public:
41 QgsColorSwatchDelegate( QWidget *parent SIP_TRANSFERTHIS = nullptr );
42 void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
43 QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
44 bool editorEvent( QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index ) override;
45
46 private slots:
47
48 void colorChanged();
49
50 private:
51 QWidget *mParent = nullptr;
52
57 QPixmap transparentBackground() const;
58};
59
60
67class GUI_EXPORT QgsColorSchemeModel : public QAbstractItemModel
68{
69 Q_OBJECT
70
71 public:
79 explicit QgsColorSchemeModel( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor(), QObject *parent SIP_TRANSFERTHIS = nullptr );
80
81 //reimplemented QAbstractItemModel methods
82 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
83 QModelIndex parent( const QModelIndex &index ) const override;
84 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
85 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
86 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
87 Qt::ItemFlags flags( const QModelIndex &index ) const override;
88 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
89 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
90 Qt::DropActions supportedDropActions() const override;
91 QStringList mimeTypes() const override;
92 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
93 bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
94 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
95 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
96
101 QgsNamedColorList colors() const { return mColors; }
102
109 void setScheme( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
110
116 QString context() const { return mContext; }
117
123 QColor baseColor() const { return mBaseColor; }
124
131 void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );
132
137 bool isDirty() const { return mIsDirty; }
138
139 private:
140 enum Columns
141 {
142 ColorSwatch = 0,
143 ColorLabel
144 };
145
146 QgsNamedColorList mColors;
147 QgsColorScheme *mScheme = nullptr;
148 QString mContext;
149 QColor mBaseColor;
150 bool mIsDirty = false;
151};
152
159class GUI_EXPORT QgsColorSchemeList : public QTreeView
160{
161 Q_OBJECT
162
163 public:
171 QgsColorSchemeList( QWidget *parent SIP_TRANSFERTHIS = nullptr, QgsColorScheme *scheme = nullptr, const QString &context = QString(), const QColor &baseColor = QColor() );
172
178 bool saveColorsToScheme();
179
185 bool importColorsFromGpl( QFile &file );
186
192 bool exportColorsToGpl( QFile &file );
193
198 bool isDirty() const;
199
205
206 public slots:
207
215 void setScheme( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
216
220 void removeSelection();
221
228 void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );
229
234 void pasteColors();
235
240 void copyColors();
241
247
253
254 signals:
255
260 void colorSelected( const QColor &color );
261
262 protected:
263 void keyPressEvent( QKeyEvent *event ) override;
264
265 void mousePressEvent( QMouseEvent *event ) override;
266
267 void mouseReleaseEvent( QMouseEvent *event ) override;
268
269 private:
270 QgsColorScheme *mScheme = nullptr;
271 QgsColorSchemeModel *mModel = nullptr;
272 QgsColorSwatchDelegate *mSwatchDelegate = nullptr;
273
274 QPoint mDragStartPosition;
275};
276
277#endif
bool exportColorsToGpl(QFile &file)
Export colors to a GPL palette file from the list.
void setScheme(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor())
Sets the color scheme to show in the list.
void pasteColors()
Pastes colors from clipboard to the list.
void removeSelection()
Removes any selected colors from the list.
bool importColorsFromGpl(QFile &file)
Import colors from a GPL palette file to the list.
void copyColors()
Copies colors from the list to the clipboard.
bool isDirty() const
Returns whether the color scheme list has been modified.
void mouseReleaseEvent(QMouseEvent *event) override
void mousePressEvent(QMouseEvent *event) override
QgsColorScheme * scheme()
Returns the scheme currently selected in the list.
void addColor(const QColor &color, const QString &label=QString(), bool allowDuplicate=false)
Adds a color to the list.
void showExportColorsDialog()
Displays a file picker dialog allowing users to export colors from the list into a file.
void colorSelected(const QColor &color)
Emitted when a color is selected from the list.
void keyPressEvent(QKeyEvent *event) override
QgsColorSchemeList(QWidget *parent=nullptr, QgsColorScheme *scheme=nullptr, const QString &context=QString(), const QColor &baseColor=QColor())
Construct a new color swatch grid.
bool saveColorsToScheme()
Saves the current colors shown in the list back to a color scheme, if supported by the color scheme.
void showImportColorsDialog()
Displays a file picker dialog allowing users to import colors into the list from a file.
A model for colors in a color scheme.
QgsNamedColorList colors() const
Returns a list of colors shown in the widget.
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
QColor baseColor() const
Gets the base color for the color scheme used by the model.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Qt::DropActions supportedDropActions() const override
QModelIndex parent(const QModelIndex &index) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex()) override
QMimeData * mimeData(const QModelIndexList &indexes) const override
bool isDirty() const
Returns whether the color scheme model has been modified.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QgsColorSchemeModel(QgsColorScheme *scheme, const QString &context=QString(), const QColor &baseColor=QColor(), QObject *parent=nullptr)
Constructor.
QStringList mimeTypes() const override
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
QString context() const
Gets the current color scheme context for the model.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Abstract base class for color schemes.
A delegate for showing a color swatch in a list.
QgsColorSwatchDelegate(QWidget *parent=nullptr)
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Base class for any widget that can be shown as an inline panel.
QList< QPair< QColor, QString > > QgsNamedColorList
List of colors paired with a friendly display name identifying the color.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53