QGIS API Documentation  2.14.0-Essen
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 
185  public slots:
186 
192  void setScheme( QgsColorScheme* scheme, const QString &context = QString(), const QColor &baseColor = QColor() );
193 
196  void removeSelection();
197 
202  void addColor( const QColor &color, const QString &label = QString() );
203 
207  void pasteColors();
208 
212  void copyColors();
213 
214  signals:
215 
219  void colorSelected( const QColor &color );
220 
221  protected:
222 
223  void keyPressEvent( QKeyEvent* event ) override;
224 
225  void mousePressEvent( QMouseEvent* event ) override;
226 
227  void mouseReleaseEvent( QMouseEvent* event ) override;
228 
229  private:
230  QgsColorScheme* mScheme;
231  QgsColorSchemeModel* mModel;
232  QgsColorSwatchDelegate* mSwatchDelegate;
233 
234  QPoint mDragStartPosition;
235 
236 };
237 
238 #endif
static unsigned index
virtual int rowCount(const QModelIndex &parent) const =0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const =0
QString context() const
Get the current color scheme context for the model.
Abstract base class for color schemes.
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
bool isDirty() const
Returns whether the color scheme model has been modified.
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)
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)
QgsNamedColorList colors() const
Returns a list of colors shown in the widget.
virtual void keyPressEvent(QKeyEvent *event)
QObject * parent() const
QColor baseColor() const
Get the base color for the color scheme used by the model.
A delegate for showing a color swatch in a list.
A model for colors in a color scheme.
typedef ItemFlags