~openmw/openmw/openmw-packaging2

« back to all changes in this revision

Viewing changes to apps/opencs/view/widget/colorpickerpopup.cpp

  • Committer: Scott Howard
  • Date: 2015-11-27 08:01:08 UTC
  • Revision ID: showard@debian.org-20151127080108-vby93jqgdvxj8d6d
Cron update. Git hash: 37c8fea

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "colorpickerpopup.hpp"
 
2
 
 
3
#include <QColorDialog>
 
4
#include <QPushButton>
 
5
#include <QEvent>
 
6
#include <QKeyEvent>
 
7
#include <QMouseEvent>
 
8
#include <QLayout>
 
9
#include <QStyleOption>
 
10
 
 
11
CSVWidget::ColorPickerPopup::ColorPickerPopup(QWidget *parent) 
 
12
    : QFrame(parent)
 
13
{
 
14
    setWindowFlags(Qt::Popup);
 
15
    setFrameStyle(QFrame::Box | QFrame::Plain);
 
16
    hide();
 
17
 
 
18
    mColorPicker = new QColorDialog(this);
 
19
    mColorPicker->setWindowFlags(Qt::Widget);
 
20
    mColorPicker->setOptions(QColorDialog::NoButtons | QColorDialog::DontUseNativeDialog);
 
21
    mColorPicker->installEventFilter(this);
 
22
    mColorPicker->open();
 
23
    connect(mColorPicker,
 
24
            SIGNAL(currentColorChanged(const QColor &)),
 
25
            this,
 
26
            SIGNAL(colorChanged(const QColor &)));
 
27
 
 
28
    QVBoxLayout *layout = new QVBoxLayout(this);
 
29
    layout->addWidget(mColorPicker);
 
30
    layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
 
31
    layout->setContentsMargins(0, 0, 0, 0);
 
32
    setLayout(layout);
 
33
    setFixedSize(mColorPicker->size());
 
34
}
 
35
 
 
36
void CSVWidget::ColorPickerPopup::showPicker(const QPoint &position, const QColor &initialColor)
 
37
{
 
38
    QRect geometry = this->geometry();
 
39
    geometry.moveTo(position);
 
40
    setGeometry(geometry);
 
41
 
 
42
    mColorPicker->setCurrentColor(initialColor);
 
43
    show();
 
44
}
 
45
 
 
46
void CSVWidget::ColorPickerPopup::mousePressEvent(QMouseEvent *event)
 
47
{
 
48
    QPushButton *button = qobject_cast<QPushButton *>(parentWidget());
 
49
    if (button != NULL)
 
50
    {
 
51
        QStyleOptionButton option;
 
52
        option.init(button);
 
53
        QRect buttonRect = option.rect;
 
54
        buttonRect.moveTo(button->mapToGlobal(buttonRect.topLeft()));
 
55
 
 
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()))
 
59
        {
 
60
            setAttribute(Qt::WA_NoMouseReplay);
 
61
        }
 
62
    }
 
63
    QFrame::mousePressEvent(event);
 
64
}
 
65
 
 
66
void CSVWidget::ColorPickerPopup::hideEvent(QHideEvent *event)
 
67
{
 
68
    QFrame::hideEvent(event);
 
69
    emit hid();
 
70
}
 
71
 
 
72
bool CSVWidget::ColorPickerPopup::eventFilter(QObject *object, QEvent *event)
 
73
{
 
74
    if (object == mColorPicker && event->type() == QEvent::KeyPress)
 
75
    {
 
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)
 
80
        {
 
81
            hide();
 
82
            return true;
 
83
        }
 
84
    }
 
85
    return QFrame::eventFilter(object, event);
 
86
}