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

View file

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

View file

@ -11,41 +11,20 @@ CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *
: 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,
const QStyleOptionViewItem &option,
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
{
return QRect(option.rect.x() + option.rect.width() / 4,
option.rect.y() + option.rect.height() / 4,
option.rect.width() / 2,
option.rect.height() / 2);
return QRect(qRound(option.rect.x() + option.rect.width() / 4.0),
qRound(option.rect.y() + option.rect.height() / 4.0),
qRound(option.rect.width() / 2.0),
qRound(option.rect.height() / 2.0));
}
CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher,

View file

@ -21,15 +21,6 @@ namespace CSVWorld
CSMDoc::Document& document,
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,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;

View file

@ -33,6 +33,8 @@
#include "../../model/world/commands.hpp"
#include "../../model/doc/document.hpp"
#include "../widget/coloreditor.hpp"
#include "recordstatusdelegate.hpp"
#include "util.hpp"
#include "tablebottombox.hpp"
@ -331,6 +333,10 @@ QWidget* CSVWorld::DialogueDelegateDispatcher::makeEditor(CSMWorld::ColumnBase::
{
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
throw std::logic_error ("Dialogue editor type missing");

View file

@ -17,6 +17,7 @@
#include "../../model/world/commands.hpp"
#include "../../model/world/tablemimedata.hpp"
#include "../../model/world/commanddispatcher.hpp"
#include "../widget/coloreditor.hpp"
#include "dialoguespinbox.hpp"
#include "scriptedit.hpp"
@ -123,10 +124,19 @@ void CSVWorld::CommandDelegate::setModelDataImp (QWidget *editor, QAbstractItemM
if (!mCommandDispatcher)
return;
NastyTableModelHack hack (*model);
QStyledItemDelegate::setModelData (editor, &hack, index);
QVariant new_ = hack.getData();
QVariant new_;
// Color columns use a custom editor, so we need explicitly extract a data from it
CSVWidget::ColorEditor *colorEditor = qobject_cast<CSVWidget::ColorEditor *>(editor);
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))
mCommandDispatcher->executeModify (model, index, new_);
@ -184,7 +194,7 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO
{
case CSMWorld::ColumnBase::Display_Colour:
return new QLineEdit(parent);
return new CSVWidget::ColorEditor(index.data().value<QColor>(), parent);
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();
if (n == "dateTime") {