QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
24 class QMimeData;
25 
33 {
34  Q_OBJECT
35 
36  public:
37  QgsColorSwatchDelegate( QWidget *parent = nullptr );
38  void paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const override;
39  QSize sizeHint( const QStyleOptionViewItem & option, const QModelIndex & index ) const override;
40  bool editorEvent( QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index ) override;
41 
42  private:
43  QWidget* mParent;
44 
48  const QPixmap &transparentBackground() const;
49 };
50 
51 
58 class GUI_EXPORT QgsColorSchemeModel: public QAbstractItemModel
59 {
60  Q_OBJECT
61 
62  public:
63 
70  explicit QgsColorSchemeModel( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor(), QObject* parent = nullptr );
71 
73 
74  //reimplemented QAbstractItemModel methods
75  QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
76  QModelIndex parent( const QModelIndex &index ) const override;
77  int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
78  int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
79  QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
80  Qt::ItemFlags flags( const QModelIndex & index ) const override;
81  bool setData( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole ) override;
82  QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const override;
83  Qt::DropActions supportedDropActions() const override;
84  QStringList mimeTypes() const override;
85  bool removeRows( int row, int count, const QModelIndex & parent = QModelIndex() ) override;
86  bool insertRows( int row, int count, const QModelIndex &parent = QModelIndex() ) override;
87  QMimeData *mimeData( const QModelIndexList &indexes ) const override;
88  bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ) override;
89 
93  QgsNamedColorList colors() const { return mColors; }
94 
100  void setScheme( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
101 
106  QString context() const { return mContext; }
107 
112  QColor baseColor() const { return mBaseColor; }
113 
118  void addColor( const QColor &color, const QString &label = QString() );
119 
123  bool isDirty() const { return mIsDirty; }
124 
125  private:
126 
127  enum Columns
128  {
129  ColorSwatch = 0,
130  ColorLabel
131  };
132 
133  QgsNamedColorList mColors;
134  QgsColorScheme* mScheme;
135  QString mContext;
136  QColor mBaseColor;
137  bool mIsDirty;
138 };
139 
146 class GUI_EXPORT QgsColorSchemeList: public QTreeView
147 {
148  Q_OBJECT
149 
150  public:
151 
158  QgsColorSchemeList( QWidget *parent = nullptr, QgsColorScheme* scheme = nullptr, const QString &context = QString(), const QColor &baseColor = QColor() );
159 
160  virtual ~QgsColorSchemeList();
161 
166  bool saveColorsToScheme();
167 
172  bool importColorsFromGpl( QFile &file );
173 
178  bool exportColorsToGpl( QFile &file );
179 
183  bool isDirty() const;
184 
189  QgsColorScheme* scheme();
190 
191  public slots:
192 
199  void setScheme( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
200 
203  void removeSelection();
204 
209  void addColor( const QColor &color, const QString &label = QString() );
210 
214  void pasteColors();
215 
219  void copyColors();
220 
221  signals:
222 
226  void colorSelected( const QColor &color );
227 
228  protected:
229 
230  void keyPressEvent( QKeyEvent* event ) override;
231 
232  void mousePressEvent( QMouseEvent* event ) override;
233 
234  void mouseReleaseEvent( QMouseEvent* event ) override;
235 
236  private:
237  QgsColorScheme* mScheme;
238  QgsColorSchemeModel* mModel;
239  QgsColorSwatchDelegate* mSwatchDelegate;
240 
241  QPoint mDragStartPosition;
242 
243 };
244 
245 #endif
static unsigned index
virtual int rowCount(const QModelIndex &parent) const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const=0
QgsNamedColorList colors() const
Returns a list of colors shown in the widget.
QColor baseColor() const
Get the base color for the color scheme used by the model.
Abstract base class for color schemes.
QString context() const
Get the current color scheme context for the model.
virtual bool insertRows(int row, int count, const QModelIndex &parent)
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
virtual QStringList mimeTypes() const
virtual Qt::DropActions supportedDropActions() const
virtual QVariant data(const QModelIndex &index, int role) const=0
virtual bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
virtual void mouseReleaseEvent(QMouseEvent *event)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const=0
An editable list of color swatches, taken from an associated QgsColorScheme.
virtual bool removeRows(int row, int count, const QModelIndex &parent)
bool isDirty() const
Returns whether the color scheme model has been modified.
typedef DropActions
virtual int columnCount(const QModelIndex &parent) const=0
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const=0
virtual bool setData(const QModelIndex &index, const QVariant &value, int role)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual void mousePressEvent(QMouseEvent *event)
virtual void keyPressEvent(QKeyEvent *event)
QObject * parent() const
A delegate for showing a color swatch in a list.
A model for colors in a color scheme.
typedef ItemFlags