Human-friendly links
[GPXSee.git] / src / colorbox.cpp
blob95e3cc7909272f3e44582a10680d1302eccf987a
1 #include <QStylePainter>
2 #include <QStyleOptionComboBox>
3 #include <QMouseEvent>
4 #include <QColorDialog>
5 #include <QComboBox>
6 #include "colorbox.h"
9 ColorBox::ColorBox(QWidget *parent) : QWidget(parent)
11 _color = Qt::red;
12 setSizePolicy(QSizePolicy::QSizePolicy::Minimum, QSizePolicy::Fixed);
15 QSize ColorBox::sizeHint() const
17 static QSize size;
18 if (size.isValid())
19 return size;
21 QComboBox cb;
22 size = cb.sizeHint();
23 return size;
26 void ColorBox::paintEvent(QPaintEvent *event)
28 Q_UNUSED(event);
30 QStylePainter painter(this);
32 QStyleOptionComboBox option;
33 option.initFrom(this);
35 #if defined(Q_OS_MAC) || defined(Q_OS_WIN32)
36 painter.setBrush(_color);
37 painter.drawPrimitive(QStyle::PE_Frame, option);
38 #else // Q_OS_MAC || Q_OS_WIN32
39 // Fallback for some broken QT4 styles that do not draw the background
40 painter.setBrush(_color);
41 painter.setPen(Qt::NoPen);
42 painter.drawRect(event->rect().adjusted(2, 2, -2, -2));
43 // If works (QT5 and most QT4 styles) overpaints the previous rectangle
44 option.palette.setBrush(QPalette::Base, _color);
45 painter.drawPrimitive(QStyle::PE_PanelLineEdit, option);
46 painter.drawPrimitive(QStyle::PE_FrameLineEdit, option);
47 #endif // Q_OS_MAC || Q_OS_WIN32
50 void ColorBox::mousePressEvent(QMouseEvent *event)
52 if (event->button() != Qt::LeftButton)
53 return;
55 QColor color = QColorDialog::getColor(_color, this, QString(),
56 QColorDialog::ShowAlphaChannel);
57 if (color.isValid()) {
58 _color = color;
59 update();
60 emit colorChanged(_color);
64 void ColorBox::setColor(const QColor &color)
66 _color = color;
67 update();