All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColorPickerToolButton.h
Go to the documentation of this file.
1 /* Copyright (C) 2011-2012 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/qt/widgets/utils/ColorPickerToolButton.h
22 
23  \brief Custom widget used to pick a color.
24 */
25 
26 #ifndef __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H
27 #define __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H
28 
29 // TerrLib
30 #include "../Config.h"
31 
32 // Qt
33 #include <QtGui/QApplication>
34 #include <QtGui/QColor>
35 #include <QtGui/QColorDialog>
36 #include <QtGui/QFrame>
37 #include <QtGui/QGridLayout>
38 #include <QtCore/QHash>
39 #include <QtGui/QMouseEvent>
40 #include <QtGui/QPainter>
41 #include <QtCore/QRectF>
42 #include <QtGui/QToolButton>
43 
44 #define COLORSIZE 20
45 #define COLS 8
46 #define SPACE 4
47 #define MARGINY 19
48 #define MARGINX 4
49 
50 namespace te
51 {
52  namespace qt
53  {
54  namespace widgets
55  {
56  /*!
57  \class ColorKeyInfo
58 
59  \brief Associate a rect to a color.
60  */
62  {
63  public:
64 
65  ColorKeyInfo(const QColor& color, const QRectF& rect)
66  {
67  m_color = color;
68  m_rect = rect;
69  }
70 
71  QColor m_color;
72  QRectF m_rect;
73  };
74 
75  /*!
76  \class ColorPickerPopup
77 
78  \brief Create a frame with a default color palette.
79  */
80  class ColorPickerPopup : public QFrame
81  {
82  Q_OBJECT
83 
84  public:
85 
86  ColorPickerPopup(QWidget* parent = 0) : QFrame(parent)
87  {
88  setWindowFlags(Qt::Popup);
89  setFrameShape(QFrame::StyledPanel);
90  setFrameShadow(QFrame::Plain);
91  setStyleSheet("QFrame{background-color: rgb(255, 255, 255);border-color: rgb(0, 0, 0);}");
92 
93  // Create default colors
94  int i = 0;
95  for(int g = 0; g < 4; ++g)
96  for(int r = 0; r < 4; ++r)
97  for(int b = 0; b < 3; ++b)
98  m_stdrgb[i++] = qRgb(r * 255 / 3, g * 255 / 3, b * 255 / 2);
99 
100  int width = COLS;
101  int height = 48 / COLS;
102 
103  width = width * COLORSIZE + SPACE + MARGINX + 4;
104  height = height * COLORSIZE + SPACE + MARGINY + 32;
105  resize(width, height);
106 
107  setMouseTracking(true);
108  }
109 
110  void paintButton(QPainter* painter, const QRectF& rc, const QString& text, bool hover)
111  {
112  !hover ? painter->setBrush(Qt::lightGray) : painter->setBrush(Qt::gray);
113  painter->drawRect(rc);
114  painter->setPen(Qt::black);
115  painter->drawText(rc, text, Qt::AlignHCenter | Qt::AlignVCenter);
116  }
117 
118  void paintEvent(QPaintEvent* /*e*/)
119  {
120  QPainter painter(this);
121 
122  painter.drawRect(0, 0, width() - 1, height() - 1);
123  painter.drawText(QRect(10, 1, this->width(), 19), tr("Basic Colors"), Qt::AlignLeft | Qt::AlignVCenter);
124 
125  int col, row;
126  m_colorInfos.clear();
127  painter.setPen(Qt::darkGray);
128  for(int i = 0; i < 48; ++i)
129  {
130  QRectF rc;
131  col = i % COLS;
132  row = (int)(i / COLS);
133 
134  rc.setLeft(col * COLORSIZE + SPACE + MARGINX);
135  rc.setTop(row * COLORSIZE + SPACE + MARGINY);
136 
137  rc.setWidth(COLORSIZE - SPACE);
138  rc.setHeight(COLORSIZE - SPACE);
139 
140  QColor color(m_stdrgb[i]);
141  painter.setBrush(color);
142  painter.drawRect(rc);
143  m_colorInfos.append(ColorKeyInfo(color, rc));
144  }
145 
146  QPointF mousePos = mapFromGlobal(QCursor::pos());
147 
148  m_buttonKeyRect = QRect(SPACE, (row + 1) * COLORSIZE + SPACE + MARGINY + 5, width() - 2 * SPACE, 20);
149 
150  paintButton(&painter, m_buttonKeyRect, tr("More Colors..."), m_buttonKeyRect.contains(mousePos));
151 
152  for(int i = 0; i < m_colorInfos.count(); ++i)
153  {
154  if(m_colorInfos[i].m_rect.contains(mousePos))
155  {
156  m_hoverColor = m_colorInfos[i].m_color;
157 
158  QRectF rc = m_colorInfos[i].m_rect;
159  rc.setTop(rc.top() - 2);
160  rc.setLeft(rc.left() - 2);
161  rc.setBottom(rc.bottom() + 2);
162  rc.setRight(rc.right() + 2);
163 
164  painter.setBrush(m_hoverColor);
165  painter.drawRect(rc);
166 
167  break;
168  }
169  }
170  }
171 
172  void mouseMoveEvent(QMouseEvent* /*e*/)
173  {
174  repaint();
175  }
176 
177  void mousePressEvent(QMouseEvent* e)
178  {
179  if(!rect().contains(e->pos()))
180  close();
181 
182  for(int i = 0; i < m_colorInfos.count(); ++i)
183  {
184  if(m_colorInfos[i].m_rect.contains(e->pos()))
185  {
186  m_hoverColor = m_colorInfos.at(i).m_color;
187  emit selected(m_hoverColor);
188  close();
189  }
190  }
191 
192  if(m_buttonKeyRect.contains(e->posF()))
193  {
194  // Show original color dialog
195  QColor color = QColorDialog::getColor(Qt::white, this, "", QColorDialog::ShowAlphaChannel);
196  if(!color.isValid())
197  return;
198 
199  m_hoverColor = color;
200  emit selected(m_hoverColor);
201  close();
202  }
203  }
204 
205  signals:
206 
207  void selected(const QColor& color);
208 
209  public:
210 
211  QColor m_hoverColor;
212  QRgb m_stdrgb[48];
213  QList<ColorKeyInfo> m_colorInfos;
215  };
216 
217  /*!
218  \class ColorPickerToolButton
219 
220  \brief Custom tool button used to pick a color.
221 
222  \ingroup widgets
223  */
224  class TEQTWIDGETSEXPORT ColorPickerToolButton : public QToolButton
225  {
226  Q_OBJECT
227 
228  public:
229 
230  ColorPickerToolButton(QWidget* parent = 0);
231 
233 
234  void setColor(const QColor& color);
235 
236  QColor getColor() const;
237 
238  protected:
239 
240  void resizeEvent(QResizeEvent* e);
241 
242  void mousePressEvent(QMouseEvent* e);
243 
244  void updateIcon();
245 
246  protected slots:
247 
248  void onPopupSelected(const QColor& color);
249 
250  signals:
251 
252  void colorChanged(const QColor&);
253 
254  private:
255 
258  };
259 
260  } // end namespace widgets
261  } // end namespace qt
262 } // end namespace te
263 
264 #endif // __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H
#define TEQTWIDGETSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:101
Create a frame with a default color palette.
void selected(const QColor &color)
#define MARGINX
#define COLS
#define SPACE
void paintButton(QPainter *painter, const QRectF &rc, const QString &text, bool hover)
#define COLORSIZE
ColorKeyInfo(const QColor &color, const QRectF &rect)
Associate a rect to a color.
Custom tool button used to pick a color.
#define MARGINY