QGIS API Documentation  2.6.0-Brighton
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscolorswatchgrid.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorswatchgrid.h
3  ------------------
4  Date : July 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 QGSCOLORSWATCHGRID_H
16 #define QGSCOLORSWATCHGRID_H
17 
18 #include "qgscolorscheme.h"
19 #include <QWidget>
20 #include <QWidgetAction>
21 
29 class GUI_EXPORT QgsColorSwatchGrid: public QWidget
30 {
31  Q_OBJECT
32 
33  public:
34 
40  QgsColorSwatchGrid( QgsColorScheme* scheme, QString context = QString(), QWidget *parent = 0 );
41 
42  virtual ~QgsColorSwatchGrid();
43 
44  //Reimplemented to set fixed size on widget
45  virtual QSize minimumSizeHint() const;
46 
47  //Reimplemented to set fixed size on widget
48  virtual QSize sizeHint() const;
49 
54  QString context() const { return mContext; }
55 
60  void setContext( const QString context );
61 
66  QColor baseColor() const { return mBaseColor; }
67 
72  void setBaseColor( const QColor baseColor );
73 
77  QgsNamedColorList* colors() { return &mColors; }
78 
79  public slots:
80 
83  void refreshColors();
84 
85  signals:
86 
90  void colorChanged( const QColor color );
91 
94  void hovered();
95 
96  protected:
97 
98  //reimplemented QWidget events
99  void paintEvent( QPaintEvent * event );
100  void mouseMoveEvent( QMouseEvent * event );
101  void mousePressEvent( QMouseEvent * event );
102  void mouseReleaseEvent( QMouseEvent * event );
103  void keyPressEvent( QKeyEvent* event );
104  void focusInEvent( QFocusEvent* event );
105  void focusOutEvent( QFocusEvent* event );
106 
107  private:
108  QgsColorScheme* mScheme;
109  QString mContext;
110  QgsNamedColorList mColors;
111  QColor mBaseColor;
112 
113  bool mDrawBoxDepressed;
114  int mCurrentHoverBox;
115 
116  bool mFocused;
117  int mCurrentFocusBox;
118 
119  int mWidth;
120 
121  bool mPressedOnWidget;
122 
126  int calculateHeight() const;
127 
131  void draw( QPainter &painter );
132 
137  int swatchForPosition( const QPoint &position ) const;
138 
142  void updateTooltip( const int colorIdx );
143 
147  const QPixmap &transparentBackground();
148 };
149 
150 
158 class GUI_EXPORT QgsColorSwatchGridAction: public QWidgetAction
159 {
160  Q_OBJECT
161 
162  public:
163 
170  QgsColorSwatchGridAction( QgsColorScheme* scheme, QMenu* menu = 0, QString context = QString(), QWidget *parent = 0 );
171 
172  virtual ~QgsColorSwatchGridAction();
173 
178  void setBaseColor( const QColor baseColor );
179 
184  QColor baseColor() const;
185 
190  QString context() const;
191 
196  void setContext( const QString context );
197 
198  public slots:
199 
202  void refreshColors();
203 
204  signals:
205 
209  void colorChanged( const QColor color );
210 
211  private:
212  QMenu* mMenu;
213  QgsColorSwatchGrid* mColorSwatchGrid;
214 
215  //used to supress recursion with hover events
216  bool mSuppressRecurse;
217 
218  private slots:
219 
222  void setColor( const QColor &color );
223 
226  void onHover();
227 };
228 
229 #endif