From eb5180ba8696f043cf91381728a36a86f41f9d44 Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 18:33:55 +0300 Subject: [PATCH] Save selected color in a model when picking is finished --- apps/opencs/view/widget/coloreditor.cpp | 19 ++++------- apps/opencs/view/widget/coloreditor.hpp | 9 +++-- .../opencs/view/world/colorpickerdelegate.cpp | 33 ++++--------------- .../opencs/view/world/colorpickerdelegate.hpp | 9 ----- apps/opencs/view/world/dialoguesubview.cpp | 6 ++++ apps/opencs/view/world/util.cpp | 28 +++++++++++++--- 6 files changed, 45 insertions(+), 59 deletions(-) diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp index bab98fcd5..eaef0f6d3 100644 --- a/apps/opencs/view/widget/coloreditor.cpp +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -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) diff --git a/apps/opencs/view/widget/coloreditor.hpp b/apps/opencs/view/widget/coloreditor.hpp index a0fa497b6..cca26fade 100644 --- a/apps/opencs/view/widget/coloreditor.hpp +++ b/apps/opencs/view/widget/coloreditor.hpp @@ -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(); }; } diff --git a/apps/opencs/view/world/colorpickerdelegate.cpp b/apps/opencs/view/world/colorpickerdelegate.cpp index 4491f392c..7490c07f5 100644 --- a/apps/opencs/view/world/colorpickerdelegate.cpp +++ b/apps/opencs/view/world/colorpickerdelegate.cpp @@ -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(), - getColoredRect(option).size(), - parent); -} - void CSVWorld::ColorPickerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - painter->fillRect(getColoredRect(option), index.data().value()); + QRect coloredRect = getColoredRect(option); + painter->fillRect(coloredRect, index.data().value()); } 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, diff --git a/apps/opencs/view/world/colorpickerdelegate.hpp b/apps/opencs/view/world/colorpickerdelegate.hpp index 147c2f424..f17923648 100644 --- a/apps/opencs/view/world/colorpickerdelegate.hpp +++ b/apps/opencs/view/world/colorpickerdelegate.hpp @@ -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; diff --git a/apps/opencs/view/world/dialoguesubview.cpp b/apps/opencs/view/world/dialoguesubview.cpp index 66e8fcb7a..541cf050f 100644 --- a/apps/opencs/view/world/dialoguesubview.cpp +++ b/apps/opencs/view/world/dialoguesubview.cpp @@ -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(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"); diff --git a/apps/opencs/view/world/util.cpp b/apps/opencs/view/world/util.cpp index e1d165a24..de088bffc 100644 --- a/apps/opencs/view/world/util.cpp +++ b/apps/opencs/view/world/util.cpp @@ -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(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(), 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(editor); + if (colorEditor != NULL) + { + colorEditor->setColor(index.data().value()); + return; + } + QByteArray n = editor->metaObject()->userProperty().name(); if (n == "dateTime") {