Create a custom item editor for color columns

pull/35/head
Stanislav Bas 10 years ago
parent d7fb497255
commit ef88b28c26

@ -75,7 +75,7 @@ opencs_units_noqt (view/world
opencs_units (view/widget
scenetoolbar scenetool scenetoolmode pushbutton scenetooltoggle scenetoolrun modebutton
scenetooltoggle2 completerpopup
scenetooltoggle2 completerpopup coloreditbutton
)
opencs_units (view/render

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

@ -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

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

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

Loading…
Cancel
Save