Skip to content

An OpenMW mod that lets you play the whole Elder Scrolls III: Morrowind as a top-down isometric game — with full online co-op or MMO modes.

C++
2.6 KB
Raw Blame
#include "colorpickerpopup.hpp"

#include <QColorDialog>
#include <QEvent>
#include <QKeyEvent>
#include <QPushButton>
#include <QStyleOptionButton>
#include <QVBoxLayout>

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

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

    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);

    // Calling getColor() creates a blocking dialog that will continue execution once the user chooses OK or Cancel
    QColor color = mColorPicker->getColor(initialColor);
    if (color.isValid())
        mColorPicker->setCurrentColor(color);
}

void CSVWidget::ColorPickerPopup::mousePressEvent(QMouseEvent* event)
{
    QPushButton* button = qobject_cast<QPushButton*>(parentWidget());
    if (button != nullptr)
    {
        QStyleOptionButton option;
        option.initFrom(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->globalPosition().toPoint()) || buttonRect.contains(event->position().toPoint()))
        {
            setAttribute(Qt::WA_NoMouseReplay);
        }
    }
    QFrame::mousePressEvent(event);
}

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);
}