mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-20 07:23:51 +00:00
Add color picker popup to choose color
This commit is contained in:
parent
ef88b28c26
commit
e257c915bf
6 changed files with 253 additions and 5 deletions
|
@ -75,7 +75,7 @@ opencs_units_noqt (view/world
|
||||||
|
|
||||||
opencs_units (view/widget
|
opencs_units (view/widget
|
||||||
scenetoolbar scenetool scenetoolmode pushbutton scenetooltoggle scenetoolrun modebutton
|
scenetoolbar scenetool scenetoolmode pushbutton scenetooltoggle scenetoolrun modebutton
|
||||||
scenetooltoggle2 completerpopup coloreditbutton
|
scenetooltoggle2 completerpopup coloreditor colorpickerpopup
|
||||||
)
|
)
|
||||||
|
|
||||||
opencs_units (view/render
|
opencs_units (view/render
|
||||||
|
|
111
apps/opencs/view/widget/coloreditor.cpp
Normal file
111
apps/opencs/view/widget/coloreditor.cpp
Normal file
|
@ -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;
|
||||||
|
}
|
43
apps/opencs/view/widget/coloreditor.hpp
Normal file
43
apps/opencs/view/widget/coloreditor.hpp
Normal file
|
@ -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
|
63
apps/opencs/view/widget/colorpickerpopup.cpp
Normal file
63
apps/opencs/view/widget/colorpickerpopup.cpp
Normal file
|
@ -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);
|
||||||
|
}
|
31
apps/opencs/view/widget/colorpickerpopup.hpp
Normal file
31
apps/opencs/view/widget/colorpickerpopup.hpp
Normal file
|
@ -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
|
|
@ -3,7 +3,7 @@
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
|
|
||||||
#include "../widget/coloreditbutton.hpp"
|
#include "../widget/coloreditor.hpp"
|
||||||
|
|
||||||
CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||||
CSMDoc::Document& document,
|
CSMDoc::Document& document,
|
||||||
|
@ -28,9 +28,9 @@ QWidget *CSVWorld::ColorPickerDelegate::createEditor(QWidget *parent,
|
||||||
throw std::logic_error("Wrong column for ColorPickerDelegate");
|
throw std::logic_error("Wrong column for ColorPickerDelegate");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new CSVWidget::ColorEditButton(index.data().value<QColor>(),
|
return new CSVWidget::ColorEditor(index.data().value<QColor>(),
|
||||||
getColoredRect(option).size(),
|
getColoredRect(option).size(),
|
||||||
parent);
|
parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSVWorld::ColorPickerDelegate::paint(QPainter *painter,
|
void CSVWorld::ColorPickerDelegate::paint(QPainter *painter,
|
||||||
|
|
Loading…
Reference in a new issue