1
#include "colorpickerpopup.hpp"
3
#include <QColorDialog>
9
#include <QStyleOption>
11
CSVWidget::ColorPickerPopup::ColorPickerPopup(QWidget *parent)
14
setWindowFlags(Qt::Popup);
15
setFrameStyle(QFrame::Box | QFrame::Plain);
18
mColorPicker = new QColorDialog(this);
19
mColorPicker->setWindowFlags(Qt::Widget);
20
mColorPicker->setOptions(QColorDialog::NoButtons | QColorDialog::DontUseNativeDialog);
21
mColorPicker->installEventFilter(this);
24
SIGNAL(currentColorChanged(const QColor &)),
26
SIGNAL(colorChanged(const QColor &)));
28
QVBoxLayout *layout = new QVBoxLayout(this);
29
layout->addWidget(mColorPicker);
30
layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
31
layout->setContentsMargins(0, 0, 0, 0);
33
setFixedSize(mColorPicker->size());
36
void CSVWidget::ColorPickerPopup::showPicker(const QPoint &position, const QColor &initialColor)
38
QRect geometry = this->geometry();
39
geometry.moveTo(position);
40
setGeometry(geometry);
42
mColorPicker->setCurrentColor(initialColor);
46
void CSVWidget::ColorPickerPopup::mousePressEvent(QMouseEvent *event)
48
QPushButton *button = qobject_cast<QPushButton *>(parentWidget());
51
QStyleOptionButton option;
53
QRect buttonRect = option.rect;
54
buttonRect.moveTo(button->mapToGlobal(buttonRect.topLeft()));
56
// If the mouse is pressed above the pop-up parent,
57
// the pop-up will be hidden and the pressed signal won't be repeated for the parent
58
if (buttonRect.contains(event->globalPos()) || buttonRect.contains(event->pos()))
60
setAttribute(Qt::WA_NoMouseReplay);
63
QFrame::mousePressEvent(event);
66
void CSVWidget::ColorPickerPopup::hideEvent(QHideEvent *event)
68
QFrame::hideEvent(event);
72
bool CSVWidget::ColorPickerPopup::eventFilter(QObject *object, QEvent *event)
74
if (object == mColorPicker && event->type() == QEvent::KeyPress)
76
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
77
// Prevent QColorDialog from closing when Escape is pressed.
78
// Instead, hide the popup.
79
if (keyEvent->key() == Qt::Key_Escape)
85
return QFrame::eventFilter(object, event);