forked from mirror/openmw-tes3mp
Add color picker popup to choose color
parent
ef88b28c26
commit
e257c915bf
@ -0,0 +1,111 @@
|
||||
#include "coloreditor.hpp"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QColor>
|
||||
#include <QColorDialog>
|
||||
#include <QDesktopWidget>
|
||||
#include <QPainter>
|
||||
#include <QRect>
|
||||
|
||||
#include "colorpickerpopup.hpp"
|
||||
|
||||
CSVWidget::ColorEditor::ColorEditor(const QColor &color,
|
||||
const QSize &coloredRectSize,
|
||||
QWidget *parent)
|
||||
: QPushButton(parent),
|
||||
mColor(color),
|
||||
mColoredRectSize(coloredRectSize),
|
||||
mColorPicker(new ColorPickerPopup(this))
|
||||
{
|
||||
setCheckable(true);
|
||||
connect(this, SIGNAL(clicked()), this, SLOT(showPicker()));
|
||||
connect(mColorPicker, SIGNAL(hid()), this, SLOT(pickerHid()));
|
||||
connect(mColorPicker, SIGNAL(colorChanged(const QColor &)), this, SLOT(pickerColorChanged(const QColor &)));
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPushButton::paintEvent(event);
|
||||
|
||||
QRect buttonRect = rect();
|
||||
QRect coloredRect(buttonRect.x() + (buttonRect.width() - mColoredRectSize.width()) / 2,
|
||||
buttonRect.y() + (buttonRect.height() - mColoredRectSize.height()) / 2,
|
||||
mColoredRectSize.width(),
|
||||
mColoredRectSize.height());
|
||||
QPainter painter(this);
|
||||
painter.fillRect(coloredRect, mColor);
|
||||
}
|
||||
|
||||
QColor CSVWidget::ColorEditor::color() const
|
||||
{
|
||||
return mColor;
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::setColor(const QColor &color)
|
||||
{
|
||||
mColor = color;
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::setColoredRectSize(const QSize &size)
|
||||
{
|
||||
mColoredRectSize = size;
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::showPicker()
|
||||
{
|
||||
if (isChecked())
|
||||
{
|
||||
mColorPicker->showPicker(calculatePopupPosition(), mColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
mColorPicker->hide();
|
||||
}
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::pickerHid()
|
||||
{
|
||||
// If the popup is hidden and mouse isn't above the editor,
|
||||
// reset the editor checked state manually
|
||||
QPoint globalEditorPosition = mapToGlobal(QPoint(0, 0));
|
||||
QRect globalEditorRect(globalEditorPosition, geometry().size());
|
||||
if (!globalEditorRect.contains(QCursor::pos()))
|
||||
{
|
||||
setChecked(false);
|
||||
}
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::pickerColorChanged(const QColor &color)
|
||||
{
|
||||
mColor = color;
|
||||
update();
|
||||
}
|
||||
|
||||
QPoint CSVWidget::ColorEditor::calculatePopupPosition()
|
||||
{
|
||||
QRect editorGeometry = geometry();
|
||||
QRect popupGeometry = mColorPicker->geometry();
|
||||
QRect screenGeometry = QApplication::desktop()->screenGeometry();
|
||||
|
||||
// Center the popup horizontally relative to the editor
|
||||
int localPopupX = (editorGeometry.width() - popupGeometry.width()) / 2;
|
||||
// Popup position need to be specified in global coords
|
||||
QPoint popupPosition = mapToGlobal(QPoint(localPopupX, editorGeometry.height()));
|
||||
|
||||
// Make sure that the popup isn't out of the screen
|
||||
if (popupPosition.x() < screenGeometry.left())
|
||||
{
|
||||
popupPosition.setX(screenGeometry.left() + 1);
|
||||
}
|
||||
else if (popupPosition.x() + popupGeometry.width() > screenGeometry.right())
|
||||
{
|
||||
popupPosition.setX(screenGeometry.right() - popupGeometry.width() - 1);
|
||||
}
|
||||
if (popupPosition.y() + popupGeometry.height() > screenGeometry.bottom())
|
||||
{
|
||||
// Place the popup above the editor
|
||||
popupPosition.setY(popupPosition.y() - popupGeometry.height() - editorGeometry.height() - 1);
|
||||
}
|
||||
|
||||
return popupPosition;
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
#ifndef CSV_WIDGET_COLOREDITOR_HPP
|
||||
#define CSV_WIDGET_COLOREDITOR_HPP
|
||||
|
||||
#include <QPushButton>
|
||||
|
||||
class QColor;
|
||||
class QPoint;
|
||||
class QSize;
|
||||
|
||||
namespace CSVWidget
|
||||
{
|
||||
class ColorPickerPopup;
|
||||
|
||||
class ColorEditor : public QPushButton
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
QColor mColor;
|
||||
QSize mColoredRectSize;
|
||||
ColorPickerPopup *mColorPicker;
|
||||
|
||||
QPoint calculatePopupPosition();
|
||||
|
||||
public:
|
||||
ColorEditor(const QColor &color,
|
||||
const QSize &coloredRectSize,
|
||||
QWidget *parent = 0);
|
||||
|
||||
QColor color() const;
|
||||
void setColor(const QColor &color);
|
||||
void setColoredRectSize(const QSize &size);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event);
|
||||
|
||||
private slots:
|
||||
void showPicker();
|
||||
void pickerHid();
|
||||
void pickerColorChanged(const QColor &color);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,63 @@
|
||||
#include "colorpickerpopup.hpp"
|
||||
|
||||
#include <QColorDialog>
|
||||
#include <QEvent>
|
||||
#include <QKeyEvent>
|
||||
#include <QLayout>
|
||||
|
||||
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::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);
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
#ifndef CSVWIDGET_COLORPICKERPOPUP_HPP
|
||||
#define CSVWIDGET_COLORPICKERPOPUP_HPP
|
||||
|
||||
#include <QFrame>
|
||||
|
||||
class QColorDialog;
|
||||
|
||||
namespace CSVWidget
|
||||
{
|
||||
class ColorPickerPopup : public QFrame
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
QColorDialog *mColorPicker;
|
||||
|
||||
public:
|
||||
explicit ColorPickerPopup(QWidget *parent);
|
||||
|
||||
void showPicker(const QPoint &position, const QColor &initialColor);
|
||||
|
||||
protected:
|
||||
virtual void hideEvent(QHideEvent *event);
|
||||
virtual bool eventFilter(QObject *object, QEvent *event);
|
||||
|
||||
signals:
|
||||
void hid();
|
||||
void colorChanged(const QColor &color);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue