ColorPickerToolButton.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 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 <QApplication>
34 #include <QColor>
35 #include <QColorDialog>
36 #include <QFrame>
37 #include <QGridLayout>
38 #include <QHash>
39 #include <QMouseEvent>
40 #include <QPainter>
41 #include <QRectF>
42 #include <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 (QT_VERSION >= 0x050000)
193  if(m_buttonKeyRect.contains(e->localPos()))
194 #else
195  if(m_buttonKeyRect.contains(e->posF()))
196 #endif
197  {
198  // Show original color dialog
199  QColor color = QColorDialog::getColor(m_currentColor, this, "", QColorDialog::ShowAlphaChannel);
200  if(!color.isValid())
201  return;
202 
203  m_hoverColor = color;
204  emit selected(m_hoverColor);
205  close();
206  }
207  }
208 
209  void setSelected(QColor color)
210  {
211  m_currentColor = color;
212  }
213 
214  signals:
215 
216  void selected(const QColor& color);
217 
218  public:
219 
220  QColor m_hoverColor;
222  QRgb m_stdrgb[48];
223  QList<ColorKeyInfo> m_colorInfos;
225  };
226 
227  /*!
228  \class ColorPickerToolButton
229 
230  \brief Custom tool button used to pick a color.
231 
232  \ingroup widgets
233  */
234  class TEQTWIDGETSEXPORT ColorPickerToolButton : public QToolButton
235  {
236  Q_OBJECT
237 
238  public:
239 
240  ColorPickerToolButton(QWidget* parent = 0);
241 
243 
244  void setColor(const QColor& color);
245 
246  QColor getColor() const;
247 
248  protected:
249 
250  void resizeEvent(QResizeEvent* e);
251 
252  void mousePressEvent(QMouseEvent* e);
253 
254  void updateIcon();
255 
256  protected slots:
257 
258  void onPopupSelected(const QColor& color);
259 
260  signals:
261 
262  void colorChanged(const QColor&);
263 
264  private:
265 
268  bool m_isOpen;
269  };
270 
271  } // end namespace widgets
272  } // end namespace qt
273 } // end namespace te
274 
275 #endif // __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H
#define COLS
Create a frame with a default color palette.
void paintButton(QPainter *painter, const QRectF &rc, const QString &text, bool hover)
#define COLORSIZE
Custom tool button used to pick a color.
URI C++ Library.
#define MARGINX
void selected(const QColor &color)
Associate a rect to a color.
#define MARGINY
#define SPACE
#define TEQTWIDGETSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63
ColorKeyInfo(const QColor &color, const QRectF &rect)