QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 "qgscolorscheme.h"
19#include <QTreeView>
20#include <QAbstractItemModel>
21#include <QItemDelegate>
22#include <QFile>
23#include "qgis_gui.h"
24#include "qgis_sip.h"
25
26class QMimeData;
27class QgsPanelWidget;
28
35class GUI_EXPORT QgsColorSwatchDelegate : public QAbstractItemDelegate
36{
37 Q_OBJECT
38
39 public:
40 QgsColorSwatchDelegate( QWidget *parent SIP_TRANSFERTHIS = nullptr );
41 void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
42 QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const override;
43 bool editorEvent( QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index ) override;
44
45 private slots:
46
47 void colorChanged();
48
49 private:
50 QWidget *mParent = nullptr;
51
56 QPixmap transparentBackground() const;
57};
58
59
66class GUI_EXPORT QgsColorSchemeModel : public QAbstractItemModel
67{
68 Q_OBJECT
69
70 public:
78 explicit QgsColorSchemeModel( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor(), QObject *parent SIP_TRANSFERTHIS = nullptr );
79
80 //reimplemented QAbstractItemModel methods
81 QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
82 QModelIndex parent( const QModelIndex &index ) const override;
83 int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
84 int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
85 QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
86 Qt::ItemFlags flags( const QModelIndex &index ) const override;
87 bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
88 QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
89 Qt::DropActions supportedDropActions() const override;
90 QStringList mimeTypes() const override;
91 bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
92 bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
93 QMimeData *mimeData( const QModelIndexList &indexes ) const override;
94 bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
95
100 QgsNamedColorList colors() const { return mColors; }
101
108 void setScheme( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
109
115 QString context() const { return mContext; }
116
122 QColor baseColor() const { return mBaseColor; }
123
130 void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );
131
136 bool isDirty() const { return mIsDirty; }
137
138 private:
139 enum Columns
140 {
141 ColorSwatch = 0,
142 ColorLabel
143 };
144
145 QgsNamedColorList mColors;
146 QgsColorScheme *mScheme = nullptr;
147 QString mContext;
148 QColor mBaseColor;
149 bool mIsDirty;
150};
151
158class GUI_EXPORT QgsColorSchemeList : public QTreeView
159{
160 Q_OBJECT
161
162 public:
170 QgsColorSchemeList( QWidget *parent SIP_TRANSFERTHIS = nullptr, QgsColorScheme *scheme = nullptr, const QString &context = QString(), const QColor &baseColor = QColor() );
171
177 bool saveColorsToScheme();
178
184 bool importColorsFromGpl( QFile &file );
185
191 bool exportColorsToGpl( QFile &file );
192
197 bool isDirty() const;
198
203 QgsColorScheme *scheme();
204
205 public slots:
206
214 void setScheme( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
215
219 void removeSelection();
220
227 void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );
228
233 void pasteColors();
234
239 void copyColors();
240
245 void showImportColorsDialog();
246
251 void showExportColorsDialog();
252
253 signals:
254
259 void colorSelected( const QColor &color );
260
261 protected:
262 void keyPressEvent( QKeyEvent *event ) override;
263
264 void mousePressEvent( QMouseEvent *event ) override;
265
266 void mouseReleaseEvent( QMouseEvent *event ) override;
267
268 private:
269 QgsColorScheme *mScheme = nullptr;
270 QgsColorSchemeModel *mModel = nullptr;
271 QgsColorSwatchDelegate *mSwatchDelegate = nullptr;
272
273 QPoint mDragStartPosition;
274};
275
276#endif
An editable list of color swatches, taken from an associated QgsColorScheme.
void colorSelected(const QColor &color)
Emitted when a color is selected from the list.
A model for colors in a color scheme.
QgsNamedColorList colors() const
Returns a list of colors shown in the widget.
QColor baseColor() const
Gets the base color for the color scheme used by the model.
bool isDirty() const
Returns whether the color scheme model has been modified.
QString context() const
Gets the current color scheme context for the model.
Abstract base class for color schemes.
A delegate for showing a color swatch in a list.
Base class for any widget that can be shown as a 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