1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-03-29 00:06:43 +00:00

Create a custom item editor for color columns

This commit is contained in:
Stanislav Bas 2015-06-09 15:20:50 +03:00
parent d7fb497255
commit ef88b28c26
5 changed files with 100 additions and 17 deletions

View file

@ -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 scenetooltoggle2 completerpopup coloreditbutton
) )
opencs_units (view/render opencs_units (view/render

View file

@ -0,0 +1,42 @@
#include "coloreditbutton.hpp"
#include <QColor>
#include <QPainter>
#include <QRect>
CSVWidget::ColorEditButton::ColorEditButton(const QColor &color,
const QSize &coloredRectSize,
QWidget *parent)
: QPushButton(parent),
mColor(color),
mColoredRectSize(coloredRectSize)
{}
void CSVWidget::ColorEditButton::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::ColorEditButton::color() const
{
return mColor;
}
void CSVWidget::ColorEditButton::setColor(const QColor &color)
{
mColor = color;
}
void CSVWidget::ColorEditButton::setColoredRectSize(const QSize &size)
{
mColoredRectSize = size;
}

View file

@ -0,0 +1,30 @@
#ifndef CSV_WIDGET_COLOREDITBUTTON_HPP
#define CSV_WIDGET_COLOREDITBUTTON_HPP
#include <QPushButton>
class QColor;
class QSize;
namespace CSVWidget
{
class ColorEditButton : public QPushButton
{
QColor mColor;
QSize mColoredRectSize;
public:
ColorEditButton(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);
};
}
#endif

View file

@ -3,6 +3,8 @@
#include <QPainter> #include <QPainter>
#include <QPushButton> #include <QPushButton>
#include "../widget/coloreditbutton.hpp"
CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher,
CSMDoc::Document& document, CSMDoc::Document& document,
QObject *parent) QObject *parent)
@ -26,20 +28,24 @@ QWidget *CSVWorld::ColorPickerDelegate::createEditor(QWidget *parent,
throw std::logic_error("Wrong column for ColorPickerDelegate"); throw std::logic_error("Wrong column for ColorPickerDelegate");
} }
return CommandDelegate::createEditor(parent, option, index, display); return new CSVWidget::ColorEditButton(index.data().value<QColor>(),
getColoredRect(option).size(),
parent);
} }
void CSVWorld::ColorPickerDelegate::paint(QPainter *painter, void CSVWorld::ColorPickerDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option, const QStyleOptionViewItem &option,
const QModelIndex &index) const const QModelIndex &index) const
{ {
QColor color = index.data().value<QColor>(); painter->fillRect(getColoredRect(option), index.data().value<QColor>());
QRect rect(option.rect.x() + option.rect.width() / 4, }
option.rect.y() + option.rect.height() / 4,
option.rect.width() / 2, QRect CSVWorld::ColorPickerDelegate::getColoredRect(const QStyleOptionViewItem &option) const
option.rect.height() / 2); {
return QRect(option.rect.x() + option.rect.width() / 4,
painter->fillRect(rect, color); option.rect.y() + option.rect.height() / 4,
option.rect.width() / 2,
option.rect.height() / 2);
} }
CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher, CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher,
@ -47,4 +53,6 @@ CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CS
QObject *parent) const QObject *parent) const
{ {
return new ColorPickerDelegate(dispatcher, document, parent); return new ColorPickerDelegate(dispatcher, document, parent);
} }

View file

@ -3,10 +3,19 @@
#include "util.hpp" #include "util.hpp"
class QRect;
namespace CSVWidget
{
class ColorEditButton;
}
namespace CSVWorld namespace CSVWorld
{ {
class ColorPickerDelegate : public CommandDelegate class ColorPickerDelegate : public CommandDelegate
{ {
QRect getColoredRect(const QStyleOptionViewItem &option) const;
public: public:
ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher,
CSMDoc::Document& document, CSMDoc::Document& document,
@ -23,13 +32,7 @@ namespace CSVWorld
virtual void paint(QPainter *painter, virtual void paint(QPainter *painter,
const QStyleOptionViewItem &option, const QStyleOptionViewItem &option,
const QModelIndex &index) const;/* const QModelIndex &index) const;
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
virtual void setModelData(QWidget *editor,
QAbstractItemModel &model,
const QModelIndex &index) const;*/
}; };
class ColorPickerDelegateFactory : public CommandDelegateFactory class ColorPickerDelegateFactory : public CommandDelegateFactory