Save selected color in a model when picking is finished

This commit is contained in:
Stanislav Bas 2015-06-12 18:33:55 +03:00
parent 4096d2851c
commit eb5180ba86
6 changed files with 45 additions and 59 deletions

View file

@ -9,12 +9,9 @@
#include "colorpickerpopup.hpp" #include "colorpickerpopup.hpp"
CSVWidget::ColorEditor::ColorEditor(const QColor &color, CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent)
const QSize &coloredRectSize,
QWidget *parent)
: QPushButton(parent), : QPushButton(parent),
mColor(color), mColor(color),
mColoredRectSize(coloredRectSize),
mColorPicker(new ColorPickerPopup(this)) mColorPicker(new ColorPickerPopup(this))
{ {
setCheckable(true); setCheckable(true);
@ -28,10 +25,10 @@ void CSVWidget::ColorEditor::paintEvent(QPaintEvent *event)
QPushButton::paintEvent(event); QPushButton::paintEvent(event);
QRect buttonRect = rect(); QRect buttonRect = rect();
QRect coloredRect(buttonRect.x() + (buttonRect.width() - mColoredRectSize.width()) / 2, QRect coloredRect(qRound(buttonRect.x() + buttonRect.width() / 4.0),
buttonRect.y() + (buttonRect.height() - mColoredRectSize.height()) / 2, qRound(buttonRect.y() + buttonRect.height() / 4.0),
mColoredRectSize.width(), qRound(buttonRect.width() / 2.0),
mColoredRectSize.height()); qRound(buttonRect.height() / 2.0));
QPainter painter(this); QPainter painter(this);
painter.fillRect(coloredRect, mColor); painter.fillRect(coloredRect, mColor);
} }
@ -46,11 +43,6 @@ void CSVWidget::ColorEditor::setColor(const QColor &color)
mColor = color; mColor = color;
} }
void CSVWidget::ColorEditor::setColoredRectSize(const QSize &size)
{
mColoredRectSize = size;
}
void CSVWidget::ColorEditor::showPicker() void CSVWidget::ColorEditor::showPicker()
{ {
if (isChecked()) if (isChecked())
@ -73,6 +65,7 @@ void CSVWidget::ColorEditor::pickerHid()
{ {
setChecked(false); setChecked(false);
} }
emit pickingFinished();
} }
void CSVWidget::ColorEditor::pickerColorChanged(const QColor &color) void CSVWidget::ColorEditor::pickerColorChanged(const QColor &color)

View file

@ -16,19 +16,15 @@ namespace CSVWidget
Q_OBJECT Q_OBJECT
QColor mColor; QColor mColor;
QSize mColoredRectSize;
ColorPickerPopup *mColorPicker; ColorPickerPopup *mColorPicker;
QPoint calculatePopupPosition(); QPoint calculatePopupPosition();
public: public:
ColorEditor(const QColor &color, ColorEditor(const QColor &color, QWidget *parent = 0);
const QSize &coloredRectSize,
QWidget *parent = 0);
QColor color() const; QColor color() const;
void setColor(const QColor &color); void setColor(const QColor &color);
void setColoredRectSize(const QSize &size);
protected: protected:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
@ -37,6 +33,9 @@ namespace CSVWidget
void showPicker(); void showPicker();
void pickerHid(); void pickerHid();
void pickerColorChanged(const QColor &color); void pickerColorChanged(const QColor &color);
signals:
void pickingFinished();
}; };
} }

View file

@ -11,41 +11,20 @@ CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *
: CommandDelegate(dispatcher, document, parent) : CommandDelegate(dispatcher, document, parent)
{} {}
QWidget *CSVWorld::ColorPickerDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
return createEditor(parent, option, index, getDisplayTypeFromIndex(index));
}
QWidget *CSVWorld::ColorPickerDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index,
CSMWorld::ColumnBase::Display display) const
{
if (display != CSMWorld::ColumnBase::Display_Colour)
{
throw std::logic_error("Wrong column for ColorPickerDelegate");
}
return new CSVWidget::ColorEditor(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
{ {
painter->fillRect(getColoredRect(option), index.data().value<QColor>()); QRect coloredRect = getColoredRect(option);
painter->fillRect(coloredRect, index.data().value<QColor>());
} }
QRect CSVWorld::ColorPickerDelegate::getColoredRect(const QStyleOptionViewItem &option) const QRect CSVWorld::ColorPickerDelegate::getColoredRect(const QStyleOptionViewItem &option) const
{ {
return QRect(option.rect.x() + option.rect.width() / 4, return QRect(qRound(option.rect.x() + option.rect.width() / 4.0),
option.rect.y() + option.rect.height() / 4, qRound(option.rect.y() + option.rect.height() / 4.0),
option.rect.width() / 2, qRound(option.rect.width() / 2.0),
option.rect.height() / 2); qRound(option.rect.height() / 2.0));
} }
CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher, CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher,

View file

@ -21,15 +21,6 @@ namespace CSVWorld
CSMDoc::Document& document, CSMDoc::Document& document,
QObject *parent); QObject *parent);
virtual QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
virtual QWidget *createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index,
CSMWorld::ColumnBase::Display display) const;
virtual void paint(QPainter *painter, virtual void paint(QPainter *painter,
const QStyleOptionViewItem &option, const QStyleOptionViewItem &option,
const QModelIndex &index) const; const QModelIndex &index) const;

View file

@ -33,6 +33,8 @@
#include "../../model/world/commands.hpp" #include "../../model/world/commands.hpp"
#include "../../model/doc/document.hpp" #include "../../model/doc/document.hpp"
#include "../widget/coloreditor.hpp"
#include "recordstatusdelegate.hpp" #include "recordstatusdelegate.hpp"
#include "util.hpp" #include "util.hpp"
#include "tablebottombox.hpp" #include "tablebottombox.hpp"
@ -331,6 +333,10 @@ QWidget* CSVWorld::DialogueDelegateDispatcher::makeEditor(CSMWorld::ColumnBase::
{ {
connect(editor, SIGNAL(editingFinished()), proxy, SLOT(editorDataCommited())); connect(editor, SIGNAL(editingFinished()), proxy, SLOT(editorDataCommited()));
} }
else if (qobject_cast<CSVWidget::ColorEditor *>(editor))
{
connect(editor, SIGNAL(pickingFinished()), proxy, SLOT(editorDataCommited()));
}
else // throw an exception because this is a coding error else // throw an exception because this is a coding error
throw std::logic_error ("Dialogue editor type missing"); throw std::logic_error ("Dialogue editor type missing");

View file

@ -17,6 +17,7 @@
#include "../../model/world/commands.hpp" #include "../../model/world/commands.hpp"
#include "../../model/world/tablemimedata.hpp" #include "../../model/world/tablemimedata.hpp"
#include "../../model/world/commanddispatcher.hpp" #include "../../model/world/commanddispatcher.hpp"
#include "../widget/coloreditor.hpp"
#include "dialoguespinbox.hpp" #include "dialoguespinbox.hpp"
#include "scriptedit.hpp" #include "scriptedit.hpp"
@ -123,10 +124,19 @@ void CSVWorld::CommandDelegate::setModelDataImp (QWidget *editor, QAbstractItemM
if (!mCommandDispatcher) if (!mCommandDispatcher)
return; return;
NastyTableModelHack hack (*model); QVariant new_;
QStyledItemDelegate::setModelData (editor, &hack, index); // Color columns use a custom editor, so we need explicitly extract a data from it
CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor);
QVariant new_ = hack.getData(); if (colorEditor != NULL)
{
new_ = colorEditor->color();
}
else
{
NastyTableModelHack hack (*model);
QStyledItemDelegate::setModelData (editor, &hack, index);
new_ = hack.getData();
}
if ((model->data (index)!=new_) && (model->flags(index) & Qt::ItemIsEditable)) if ((model->data (index)!=new_) && (model->flags(index) & Qt::ItemIsEditable))
mCommandDispatcher->executeModify (model, index, new_); mCommandDispatcher->executeModify (model, index, new_);
@ -184,7 +194,7 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO
{ {
case CSMWorld::ColumnBase::Display_Colour: case CSMWorld::ColumnBase::Display_Colour:
return new QLineEdit(parent); return new CSVWidget::ColorEditor(index.data().value<QColor>(), parent);
case CSMWorld::ColumnBase::Display_Integer: case CSMWorld::ColumnBase::Display_Integer:
{ {
@ -284,6 +294,14 @@ void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelInde
} }
} }
// Color columns use a custom editor, so we need explicitly set a data for it
CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor);
if (colorEditor != NULL)
{
colorEditor->setColor(index.data().value<QColor>());
return;
}
QByteArray n = editor->metaObject()->userProperty().name(); QByteArray n = editor->metaObject()->userProperty().name();
if (n == "dateTime") { if (n == "dateTime") {