QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
26 class QMimeData;
27 class QgsPanelWidget;
28 
36 class 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 
68 class GUI_EXPORT QgsColorSchemeModel: public QAbstractItemModel
69 {
70  Q_OBJECT
71 
72  public:
73 
81  explicit QgsColorSchemeModel( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor(), QObject *parent SIP_TRANSFERTHIS = nullptr );
82 
83  //reimplemented QAbstractItemModel methods
84  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
85  QModelIndex parent( const QModelIndex &index ) const override;
86  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
87  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
88  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
89  Qt::ItemFlags flags( const QModelIndex &index ) const override;
90  bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
91  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
92  Qt::DropActions supportedDropActions() const override;
93  QStringList mimeTypes() const override;
94  bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
95  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
96  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
97  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
98 
103  QgsNamedColorList colors() const { return mColors; }
104 
111  void setScheme( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
112 
118  QString context() const { return mContext; }
119 
125  QColor baseColor() const { return mBaseColor; }
126 
133  void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );
134 
139  bool isDirty() const { return mIsDirty; }
140 
141  private:
142 
143  enum Columns
144  {
145  ColorSwatch = 0,
146  ColorLabel
147  };
148 
149  QgsNamedColorList mColors;
150  QgsColorScheme *mScheme = nullptr;
151  QString mContext;
152  QColor mBaseColor;
153  bool mIsDirty;
154 };
155 
163 class GUI_EXPORT QgsColorSchemeList: public QTreeView
164 {
165  Q_OBJECT
166 
167  public:
168 
176  QgsColorSchemeList( QWidget *parent SIP_TRANSFERTHIS = nullptr, QgsColorScheme *scheme = nullptr, const QString &context = QString(), const QColor &baseColor = QColor() );
177 
183  bool saveColorsToScheme();
184 
190  bool importColorsFromGpl( QFile &file );
191 
197  bool exportColorsToGpl( QFile &file );
198 
203  bool isDirty() const;
204 
210  QgsColorScheme *scheme();
211 
212  public slots:
213 
221  void setScheme( QgsColorScheme *scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
222 
226  void removeSelection();
227 
234  void addColor( const QColor &color, const QString &label = QString(), bool allowDuplicate = false );
235 
240  void pasteColors();
241 
246  void copyColors();
247 
253  void showImportColorsDialog();
254 
260  void showExportColorsDialog();
261 
262  signals:
263 
268  void colorSelected( const QColor &color );
269 
270  protected:
271 
272  void keyPressEvent( QKeyEvent *event ) override;
273 
274  void mousePressEvent( QMouseEvent *event ) override;
275 
276  void mouseReleaseEvent( QMouseEvent *event ) override;
277 
278  private:
279  QgsColorScheme *mScheme = nullptr;
280  QgsColorSchemeModel *mModel = nullptr;
281  QgsColorSwatchDelegate *mSwatchDelegate = nullptr;
282 
283  QPoint mDragStartPosition;
284 
285 };
286 
287 #endif
QgsNamedColorList colors() const
Returns a list of colors shown in the widget.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QColor baseColor() const
Gets the base color for the color scheme used by the model.
Abstract base class for color schemes.
QString context() const
Gets the current color scheme context for the model.
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.
An editable list of color swatches, taken from an associated QgsColorScheme.
bool isDirty() const
Returns whether the color scheme model has been modified.
A delegate for showing a color swatch in a list.
A model for colors in a color scheme.