Merge branch 'master' of https://github.com/OpenMW/openmw into osg
Conflicts: apps/opencs/CMakeLists.txt apps/opencs/main.cpp apps/openmw/mwworld/player.hppc++11
commit
a5670b5133
@ -0,0 +1,113 @@
|
||||
#include "coloreditor.hpp"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QColor>
|
||||
#include <QColorDialog>
|
||||
#include <QDesktopWidget>
|
||||
#include <QPainter>
|
||||
#include <QRect>
|
||||
#include <QShowEvent>
|
||||
|
||||
#include "colorpickerpopup.hpp"
|
||||
|
||||
CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent, bool popupOnStart)
|
||||
: QPushButton(parent),
|
||||
mColor(color),
|
||||
mColorPicker(new ColorPickerPopup(this)),
|
||||
mPopupOnStart(popupOnStart)
|
||||
{
|
||||
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() + qRound(buttonRect.width() / 4.0),
|
||||
buttonRect.y() + qRound(buttonRect.height() / 4.0),
|
||||
buttonRect.width() / 2,
|
||||
buttonRect.height() / 2);
|
||||
QPainter painter(this);
|
||||
painter.fillRect(coloredRect, mColor);
|
||||
painter.setPen(Qt::black);
|
||||
painter.drawRect(coloredRect);
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::showEvent(QShowEvent *event)
|
||||
{
|
||||
QPushButton::showEvent(event);
|
||||
if (isVisible() && mPopupOnStart)
|
||||
{
|
||||
setChecked(true);
|
||||
showPicker();
|
||||
mPopupOnStart = false;
|
||||
}
|
||||
}
|
||||
|
||||
QColor CSVWidget::ColorEditor::color() const
|
||||
{
|
||||
return mColor;
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::setColor(const QColor &color)
|
||||
{
|
||||
mColor = color;
|
||||
update();
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::showPicker()
|
||||
{
|
||||
if (isChecked())
|
||||
{
|
||||
mColorPicker->showPicker(calculatePopupPosition(), mColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
mColorPicker->hide();
|
||||
}
|
||||
}
|
||||
|
||||
void CSVWidget::ColorEditor::pickerHid()
|
||||
{
|
||||
setChecked(false);
|
||||
emit pickingFinished();
|
||||
}
|
||||
|
||||
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,44 @@
|
||||
#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;
|
||||
ColorPickerPopup *mColorPicker;
|
||||
bool mPopupOnStart;
|
||||
|
||||
QPoint calculatePopupPosition();
|
||||
|
||||
public:
|
||||
ColorEditor(const QColor &color, QWidget *parent = 0, bool popupOnStart = false);
|
||||
|
||||
QColor color() const;
|
||||
void setColor(const QColor &color);
|
||||
|
||||
protected:
|
||||
virtual void paintEvent(QPaintEvent *event);
|
||||
virtual void showEvent(QShowEvent *event);
|
||||
|
||||
private slots:
|
||||
void showPicker();
|
||||
void pickerHid();
|
||||
void pickerColorChanged(const QColor &color);
|
||||
|
||||
signals:
|
||||
void pickingFinished();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,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);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
#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 mousePressEvent(QMouseEvent *event);
|
||||
virtual void hideEvent(QHideEvent *event);
|
||||
virtual bool eventFilter(QObject *object, QEvent *event);
|
||||
|
||||
signals:
|
||||
void hid();
|
||||
void colorChanged(const QColor &color);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,36 @@
|
||||
#include "colordelegate.hpp"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QPushButton>
|
||||
|
||||
#include "../widget/coloreditor.hpp"
|
||||
|
||||
CSVWorld::ColorDelegate::ColorDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document& document,
|
||||
QObject *parent)
|
||||
: CommandDelegate(dispatcher, document, parent)
|
||||
{}
|
||||
|
||||
void CSVWorld::ColorDelegate::paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
QRect coloredRect(option.rect.x() + qRound(option.rect.width() / 4.0),
|
||||
option.rect.y() + qRound(option.rect.height() / 4.0),
|
||||
option.rect.width() / 2,
|
||||
option.rect.height() / 2);
|
||||
painter->save();
|
||||
painter->fillRect(coloredRect, index.data().value<QColor>());
|
||||
painter->setPen(Qt::black);
|
||||
painter->drawRect(coloredRect);
|
||||
painter->restore();
|
||||
}
|
||||
|
||||
CSVWorld::CommandDelegate *CSVWorld::ColorDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document &document,
|
||||
QObject *parent) const
|
||||
{
|
||||
return new ColorDelegate(dispatcher, document, parent);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,37 @@
|
||||
#ifndef CSV_WORLD_COLORDELEGATE_HPP
|
||||
#define CSV_WORLD_COLORDELEGATE_HPP
|
||||
|
||||
#include "util.hpp"
|
||||
|
||||
class QRect;
|
||||
|
||||
namespace CSVWidget
|
||||
{
|
||||
class ColorEditButton;
|
||||
}
|
||||
|
||||
namespace CSVWorld
|
||||
{
|
||||
class ColorDelegate : public CommandDelegate
|
||||
{
|
||||
public:
|
||||
ColorDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document& document,
|
||||
QObject *parent);
|
||||
|
||||
virtual void paint(QPainter *painter,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
};
|
||||
|
||||
class ColorDelegateFactory : public CommandDelegateFactory
|
||||
{
|
||||
public:
|
||||
virtual CommandDelegate *makeDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document &document,
|
||||
QObject *parent) const;
|
||||
///< The ownership of the returned CommandDelegate is transferred to the caller.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue