~openmw/openmw/openmw-packaging2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "colorpickerpopup.hpp"

#include <QColorDialog>
#include <QPushButton>
#include <QEvent>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QLayout>
#include <QStyleOption>

CSVWidget::ColorPickerPopup::ColorPickerPopup(QWidget *parent) 
    : QFrame(parent)
{
    setWindowFlags(Qt::Popup);
    setFrameStyle(QFrame::Box | QFrame::Plain);
    hide();

    mColorPicker = new QColorDialog(this);
    mColorPicker->setWindowFlags(Qt::Widget);
    mColorPicker->setOptions(QColorDialog::NoButtons | QColorDialog::DontUseNativeDialog);
    mColorPicker->installEventFilter(this);
    mColorPicker->open();
    connect(mColorPicker,
            SIGNAL(currentColorChanged(const QColor &)),
            this,
            SIGNAL(colorChanged(const QColor &)));

    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(mColorPicker);
    layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);
    setFixedSize(mColorPicker->size());
}

void CSVWidget::ColorPickerPopup::showPicker(const QPoint &position, const QColor &initialColor)
{
    QRect geometry = this->geometry();
    geometry.moveTo(position);
    setGeometry(geometry);

    mColorPicker->setCurrentColor(initialColor);
    show();
}

void CSVWidget::ColorPickerPopup::mousePressEvent(QMouseEvent *event)
{
    QPushButton *button = qobject_cast<QPushButton *>(parentWidget());
    if (button != NULL)
    {
        QStyleOptionButton option;
        option.init(button);
        QRect buttonRect = option.rect;
        buttonRect.moveTo(button->mapToGlobal(buttonRect.topLeft()));

        // If the mouse is pressed above the pop-up parent,
        // the pop-up will be hidden and the pressed signal won't be repeated for the parent
        if (buttonRect.contains(event->globalPos()) || buttonRect.contains(event->pos()))
        {
            setAttribute(Qt::WA_NoMouseReplay);
        }
    }
    QFrame::mousePressEvent(event);
}

void CSVWidget::ColorPickerPopup::hideEvent(QHideEvent *event)
{
    QFrame::hideEvent(event);
    emit hid();
}

bool CSVWidget::ColorPickerPopup::eventFilter(QObject *object, QEvent *event)
{
    if (object == mColorPicker && event->type() == QEvent::KeyPress)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        // Prevent QColorDialog from closing when Escape is pressed.
        // Instead, hide the popup.
        if (keyEvent->key() == Qt::Key_Escape)
        {
            hide();
            return true;
        }
    }
    return QFrame::eventFilter(object, event);
}