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> 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);}");
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);
101 int height = 48 /
COLS;
105 resize(width, height);
107 setMouseTracking(
true);
110 void paintButton(QPainter* painter,
const QRectF& rc,
const QString& text,
bool hover)
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);
120 QPainter painter(
this);
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);
126 m_colorInfos.clear();
127 painter.setPen(Qt::darkGray);
128 for(
int i = 0; i < 48; ++i)
132 row = (int)(i /
COLS);
140 QColor color(m_stdrgb[i]);
141 painter.setBrush(color);
142 painter.drawRect(rc);
146 QPointF mousePos = mapFromGlobal(QCursor::pos());
150 paintButton(&painter, m_buttonKeyRect, tr(
"More Colors..."), m_buttonKeyRect.contains(mousePos));
152 for(
int i = 0; i < m_colorInfos.count(); ++i)
154 if(m_colorInfos[i].
m_rect.contains(mousePos))
156 m_hoverColor = m_colorInfos[i].m_color;
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);
164 painter.setBrush(m_hoverColor);
165 painter.drawRect(rc);
179 if(!rect().contains(e->pos()))
182 for(
int i = 0; i < m_colorInfos.count(); ++i)
184 if(m_colorInfos[i].
m_rect.contains(e->pos()))
186 m_hoverColor = m_colorInfos.at(i).m_color;
187 emit selected(m_hoverColor);
192 #if (QT_VERSION >= 0x050000) 193 if(m_buttonKeyRect.contains(e->localPos()))
195 if(m_buttonKeyRect.contains(e->posF()))
199 QColor color = QColorDialog::getColor(m_currentColor,
this,
"", QColorDialog::ShowAlphaChannel);
203 m_hoverColor = color;
204 emit selected(m_hoverColor);
211 m_currentColor = color;
216 void selected(
const QColor& color);
244 void setColor(
const QColor& color);
246 QColor getColor()
const;
250 void resizeEvent(QResizeEvent* e);
252 void mousePressEvent(QMouseEvent* e);
258 void onPopupSelected(
const QColor& color);
262 void colorChanged(
const QColor&);
275 #endif // __TERRALIB_QT_WIDGETS_UTILS_INTERNAL_COLORPICKERTOOLBUTTON_H