26 #ifndef __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H 27 #define __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H 30 #include "../Config.h" 33 #include <QApplication> 35 #include <QColorDialog> 37 #include <QGridLayout> 39 #include <QMouseEvent> 42 #include <QToolButton> 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);}");
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);
102 int height = 48 /
COLS;
106 resize(width, height);
108 setMouseTracking(
true);
111 void paintButton(QPainter* painter,
const QRectF& rc,
const QString& text,
bool hover)
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);
121 QPainter painter(
this);
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);
127 m_colorInfos.clear();
128 painter.setPen(Qt::darkGray);
129 for(
int i = 0; i < 48; ++i)
133 col = (int)(i /
ROWS);
142 QColor color(m_stdrgb[i]);
143 painter.setBrush(color);
144 painter.drawRect(rc);
148 QPointF mousePos = mapFromGlobal(QCursor::pos());
152 paintButton(&painter, m_buttonKeyRect, tr(
"More Colors..."), m_buttonKeyRect.contains(mousePos));
154 for(
int i = 0; i < m_colorInfos.count(); ++i)
156 if(m_colorInfos[i].
m_rect.contains(mousePos))
158 m_hoverColor = m_colorInfos[i].m_color;
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);
166 painter.setBrush(m_hoverColor);
167 painter.drawRect(rc);
181 if(!rect().contains(e->pos()))
184 for(
int i = 0; i < m_colorInfos.count(); ++i)
186 if(m_colorInfos[i].
m_rect.contains(e->pos()))
188 m_hoverColor = m_colorInfos.at(i).m_color;
189 emit selected(m_hoverColor);
194 #if (QT_VERSION >= 0x050000) 195 if(m_buttonKeyRect.contains(e->localPos()))
197 if(m_buttonKeyRect.contains(e->posF()))
201 QColor color = QColorDialog::getColor(m_currentColor,
this,
"", QColorDialog::ShowAlphaChannel);
205 m_hoverColor = color;
206 emit selected(m_hoverColor);
213 m_currentColor = color;
218 void selected(
const QColor& color);
246 void setColor(
const QColor& color);
248 QColor getColor()
const;
252 void resizeEvent(QResizeEvent* e);
254 void mousePressEvent(QMouseEvent* e);
260 void onPopupSelected(
const QColor& color);
264 void colorChanged(
const QColor&);
277 #endif // __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H