1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-07 15:23:22 +02:00
GPXSee/src/GUI/colorbox.cpp

71 lines
1.7 KiB
C++
Raw Normal View History

2016-12-06 01:48:26 +01:00
#include <QStylePainter>
#include <QStyleOptionComboBox>
#include <QMouseEvent>
#include <QColorDialog>
#include <QComboBox>
#include "colorbox.h"
ColorBox::ColorBox(QWidget *parent) : QWidget(parent)
{
_color = Qt::red;
_alpha = true;
2016-12-06 01:48:26 +01:00
setSizePolicy(QSizePolicy::QSizePolicy::Minimum, QSizePolicy::Fixed);
}
QSize ColorBox::sizeHint() const
{
static QSize size;
if (size.isValid())
return size;
QComboBox cb;
size = cb.sizeHint();
return size;
}
void ColorBox::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStylePainter painter(this);
QStyleOptionComboBox option;
2016-12-06 21:01:06 +01:00
option.initFrom(this);
2016-12-06 01:48:26 +01:00
#if defined(Q_OS_MAC) || defined(Q_OS_WIN32)
painter.setBrush(_color);
painter.drawPrimitive(QStyle::PE_Frame, option);
#else // Q_OS_MAC || Q_OS_WIN32
2016-12-06 21:01:06 +01:00
// Fallback for some broken QT4 styles that do not draw the background
2016-12-06 01:48:26 +01:00
painter.setBrush(_color);
2016-12-06 21:01:06 +01:00
painter.setPen(Qt::NoPen);
painter.drawRect(event->rect().adjusted(2, 2, -2, -2));
// If works (QT5 and most QT4 styles) overpaints the previous rectangle
2016-12-06 01:48:26 +01:00
option.palette.setBrush(QPalette::Base, _color);
2016-12-06 21:01:06 +01:00
painter.drawPrimitive(QStyle::PE_PanelLineEdit, option);
2016-12-06 01:48:26 +01:00
painter.drawPrimitive(QStyle::PE_FrameLineEdit, option);
#endif // Q_OS_MAC || Q_OS_WIN32
}
void ColorBox::mousePressEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton)
return;
QColorDialog::ColorDialogOptions options = _alpha
? QColorDialog::ShowAlphaChannel : (QColorDialog::ColorDialogOptions)0;
QColor color = QColorDialog::getColor(_color, this, QString(), options);
2016-12-06 01:48:26 +01:00
if (color.isValid()) {
_color = color;
update();
emit colorChanged(_color);
}
}
void ColorBox::setColor(const QColor &color)
{
_color = color;
update();
}