Loading...
Searching...
No Matches
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 ROWS 6
47#define SPACE 4
48#define MARGINY 19
49#define MARGINX 4
50
51namespace te
52{
53 namespace qt
54 {
55 namespace widgets
56 {
57 /*!
58 \class ColorKeyInfo
59
60 \brief Associate a rect to a color.
61 */
63 {
64 public:
65
66 ColorKeyInfo(const QColor& color, const QRectF& rect)
67 {
68 m_color = color;
69 m_rect = rect;
70 }
71
72 QColor m_color;
73 QRectF m_rect;
74 };
75
76 /*!
77 \class ColorPickerPopup
78
79 \brief Create a frame with a default color palette.
80 */
81 class ColorPickerPopup : public QFrame
82 {
83 Q_OBJECT
84
85 public:
86
87 ColorPickerPopup(QWidget* parent = 0) : QFrame(parent)
88 {
89 setWindowFlags(Qt::Popup);
90 setFrameShape(QFrame::StyledPanel);
91 setFrameShadow(QFrame::Plain);
92 setStyleSheet("QFrame{background-color: rgb(255, 255, 255);border-color: rgb(0, 0, 0);}");
93
94 // Create default colors
95 int i = 0;
96 for(int g = 0; g < 4; ++g)
97 for(int r = 0; r < 4; ++r)
98 for(int b = 0; b < 3; ++b)
99 m_stdrgb[i++] = qRgb(r * 255 / 3, g * 255 / 3, b * 255 / 2);
100
101 int width = COLS;
102 int height = 48 / COLS;
103
104 width = width * COLORSIZE + SPACE + MARGINX + 4;
105 height = height * COLORSIZE + SPACE + MARGINY + 32;
106 resize(width, height);
107
108 setMouseTracking(true);
109 }
110
111 void paintButton(QPainter* painter, const QRectF& rc, const QString& text, bool hover)
112 {
113 !hover ? painter->setBrush(Qt::lightGray) : painter->setBrush(Qt::gray);
114 painter->drawRect(rc);
115 painter->setPen(Qt::black);
116 painter->drawText(rc, text, Qt::AlignHCenter | Qt::AlignVCenter);
117 }
118
119 void paintEvent(QPaintEvent* /*e*/)
120 {
121 QPainter painter(this);
122
123 painter.drawRect(0, 0, width() - 1, height() - 1);
124 painter.drawText(QRect(10, 1, this->width(), 19), tr("Basic Colors"), Qt::AlignLeft | Qt::AlignVCenter);
125
126 int col, row;
127 m_colorInfos.clear();
128 painter.setPen(Qt::darkGray);
129 for(int i = 0; i < 48; ++i)
130 {
131 QRectF rc;
132
133 col = (int)(i / ROWS);
134 row = i % ROWS;
135
136 rc.setLeft(col * COLORSIZE + SPACE + MARGINX);
137 rc.setTop(row * COLORSIZE + SPACE + MARGINY);
138
139 rc.setWidth(COLORSIZE - SPACE);
140 rc.setHeight(COLORSIZE - SPACE);
141
142 QColor color(m_stdrgb[i]);
143 painter.setBrush(color);
144 painter.drawRect(rc);
145 m_colorInfos.append(ColorKeyInfo(color, rc));
146 }
147
148 QPointF mousePos = mapFromGlobal(QCursor::pos());
149
150 m_buttonKeyRect = QRect(SPACE, (row + 1) * COLORSIZE + SPACE + MARGINY + 5, width() - 2 * SPACE, 20);
151
152 paintButton(&painter, m_buttonKeyRect, tr("More Colors..."), m_buttonKeyRect.contains(mousePos));
153
154 for(int i = 0; i < m_colorInfos.count(); ++i)
155 {
156 if(m_colorInfos[i].m_rect.contains(mousePos))
157 {
158 m_hoverColor = m_colorInfos[i].m_color;
159
160 QRectF rc = m_colorInfos[i].m_rect;
161 rc.setTop(rc.top() - 2);
162 rc.setLeft(rc.left() - 2);
163 rc.setBottom(rc.bottom() + 2);
164 rc.setRight(rc.right() + 2);
165
166 painter.setBrush(m_hoverColor);
167 painter.drawRect(rc);
168
169 break;
170 }
171 }
172 }
173
174 void mouseMoveEvent(QMouseEvent* /*e*/)
175 {
176 repaint();
177 }
178
179 void mousePressEvent(QMouseEvent* e)
180 {
181 if(!rect().contains(e->pos()))
182 close();
183
184 for(int i = 0; i < m_colorInfos.count(); ++i)
185 {
186 if(m_colorInfos[i].m_rect.contains(e->pos()))
187 {
188 m_hoverColor = m_colorInfos.at(i).m_color;
190 close();
191 }
192 }
193
194#if (QT_VERSION >= 0x050000)
195 if(m_buttonKeyRect.contains(e->localPos()))
196#else
197 if(m_buttonKeyRect.contains(e->posF()))
198#endif
199 {
200 // Show original color dialog
201 QColor color = QColorDialog::getColor(m_currentColor, this, "", QColorDialog::ShowAlphaChannel);
202 if(!color.isValid())
203 return;
204
205 m_hoverColor = color;
207 close();
208 }
209 }
210
211 void setSelected(QColor color)
212 {
213 m_currentColor = color;
214 }
215
216 signals:
217
218 void selected(const QColor& color);
219
220 public:
221
224 QRgb m_stdrgb[48];
225 QList<ColorKeyInfo> m_colorInfos;
227 };
228
229 /*!
230 \class ColorPickerToolButton
231
232 \brief Custom tool button used to pick a color.
233
234 \ingroup widgets
235 */
236 class TEQTWIDGETSEXPORT ColorPickerToolButton : public QToolButton
237 {
238 Q_OBJECT
239
240 public:
241
242 ColorPickerToolButton(QWidget* parent = 0);
243
245
246 void setColor(const QColor& color);
247
248 QColor getColor() const;
249
250 protected:
251
252 void resizeEvent(QResizeEvent* e);
253
254 void mousePressEvent(QMouseEvent* e);
255
257
258 protected slots:
259
260 void onPopupSelected(const QColor& color);
261
262 signals:
263
264 void colorChanged(const QColor&);
265
266 private:
267
271 };
272
273 } // end namespace widgets
274 } // end namespace qt
275} // end namespace te
276
277#endif // __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H
#define ROWS
#define SPACE
#define MARGINX
#define MARGINY
#define COLORSIZE
#define COLS
Associate a rect to a color.
ColorKeyInfo(const QColor &color, const QRectF &rect)
Create a frame with a default color palette.
void paintButton(QPainter *painter, const QRectF &rc, const QString &text, bool hover)
void selected(const QColor &color)
Custom tool button used to pick a color.
void setColor(const QColor &color)
void onPopupSelected(const QColor &color)
TerraLib.
#define slots
#define TEQTWIDGETSEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:63