QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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:
71
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
141 enum Columns
142 {
143 ColorSwatch = 0,
144 ColorLabel
145 };
146
147 QgsNamedColorList mColors;
148 QgsColorScheme *mScheme = nullptr;
149 QString mContext;
150 QColor mBaseColor;
151 bool mIsDirty;
152};
153
160class GUI_EXPORT QgsColorSchemeList: public QTreeView
161{
162 Q_OBJECT
163
164 public:
165
173 QgsColorSchemeList( QWidget *parent SIP_TRANSFERTHIS = nullptr, QgsColorScheme *scheme = nullptr, const QString &context = QString(), const QColor &baseColor = QColor() );
174
180 bool saveColorsToScheme();
181
187 bool importColorsFromGpl( QFile &file );
188
194 bool exportColorsToGpl( QFile &file );
195
200 bool isDirty() const;
201
206 QgsColorScheme *scheme();
207
208 public slots:
209
217 void setScheme( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
218
222 void removeSelection();
223
230 void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );
231
236 void pasteColors();
237
242 void copyColors();
243
248 void showImportColorsDialog();
249
254 void showExportColorsDialog();
255
256 signals:
257
262 void colorSelected( const QColor &color );
263
264 protected:
265
266 void keyPressEvent( QKeyEvent *event ) override;
267
268 void mousePressEvent( QMouseEvent *event ) override;
269
270 void mouseReleaseEvent( QMouseEvent *event ) override;
271
272 private:
273 QgsColorScheme *mScheme = nullptr;
274 QgsColorSchemeModel *mModel = nullptr;
275 QgsColorSwatchDelegate *mSwatchDelegate = nullptr;
276
277 QPoint mDragStartPosition;
278
279};
280
281#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