From d7fb49725569e6fed93672f86036e6037cd112b8 Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Mon, 8 Jun 2015 21:33:23 +0300 Subject: [PATCH 01/12] Color values are displayed as colored rectangles in tables --- apps/opencs/CMakeLists.txt | 1 + apps/opencs/view/doc/viewmanager.cpp | 4 ++ .../opencs/view/world/colorpickerdelegate.cpp | 50 +++++++++++++++++++ .../opencs/view/world/colorpickerdelegate.hpp | 45 +++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 apps/opencs/view/world/colorpickerdelegate.cpp create mode 100644 apps/opencs/view/world/colorpickerdelegate.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 37d13223a..4a5a64f60 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -70,6 +70,7 @@ opencs_units (view/world opencs_units_noqt (view/world subviews enumdelegate vartypedelegate recordstatusdelegate idtypedelegate datadisplaydelegate scripthighlighter idvalidator dialoguecreator physicssystem idcompletiondelegate + colorpickerdelegate ) opencs_units (view/widget diff --git a/apps/opencs/view/doc/viewmanager.cpp b/apps/opencs/view/doc/viewmanager.cpp index b2f681df1..e5bb7fe81 100644 --- a/apps/opencs/view/doc/viewmanager.cpp +++ b/apps/opencs/view/doc/viewmanager.cpp @@ -19,6 +19,7 @@ #include "../world/recordstatusdelegate.hpp" #include "../world/idtypedelegate.hpp" #include "../world/idcompletiondelegate.hpp" +#include "../world/colorpickerdelegate.hpp" #include "../../model/settings/usersettings.hpp" @@ -63,6 +64,9 @@ CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager) mDelegateFactories->add (CSMWorld::ColumnBase::Display_RefRecordType, new CSVWorld::IdTypeDelegateFactory()); + mDelegateFactories->add (CSMWorld::ColumnBase::Display_Colour, + new CSVWorld::ColorPickerDelegateFactory()); + std::vector idCompletionColumns = CSMWorld::IdCompletionManager::getDisplayTypes(); for (std::vector::const_iterator current = idCompletionColumns.begin(); current != idCompletionColumns.end(); diff --git a/apps/opencs/view/world/colorpickerdelegate.cpp b/apps/opencs/view/world/colorpickerdelegate.cpp new file mode 100644 index 000000000..aa9f2e937 --- /dev/null +++ b/apps/opencs/view/world/colorpickerdelegate.cpp @@ -0,0 +1,50 @@ +#include "colorpickerdelegate.hpp" + +#include +#include + +CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document& document, + QObject *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 CommandDelegate::createEditor(parent, option, index, display); +} + +void CSVWorld::ColorPickerDelegate::paint(QPainter *painter, + const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + QColor color = index.data().value(); + 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); +} + +CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document &document, + QObject *parent) const +{ + return new ColorPickerDelegate(dispatcher, document, parent); +} \ No newline at end of file diff --git a/apps/opencs/view/world/colorpickerdelegate.hpp b/apps/opencs/view/world/colorpickerdelegate.hpp new file mode 100644 index 000000000..e93e0e87d --- /dev/null +++ b/apps/opencs/view/world/colorpickerdelegate.hpp @@ -0,0 +1,45 @@ +#ifndef CSV_WORLD_COLORPICKERDELEGATE_HPP +#define CSV_WORLD_COLORPICKERDELEGATE_HPP + +#include "util.hpp" + +namespace CSVWorld +{ + class ColorPickerDelegate : public CommandDelegate + { + public: + ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, + 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;/* + + virtual void setEditorData(QWidget *editor, const QModelIndex &index) const; + + virtual void setModelData(QWidget *editor, + QAbstractItemModel &model, + const QModelIndex &index) const;*/ + }; + + class ColorPickerDelegateFactory : public CommandDelegateFactory + { + public: + virtual CommandDelegate *makeDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document &document, + QObject *parent) const; + ///< The ownership of the returned CommandDelegate is transferred to the caller. + }; +} + +#endif From ef88b28c26c8031ba29a4f81a44eb6a4391f362c Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Tue, 9 Jun 2015 15:20:50 +0300 Subject: [PATCH 02/12] Create a custom item editor for color columns --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/view/widget/coloreditbutton.cpp | 42 +++++++++++++++++++ apps/opencs/view/widget/coloreditbutton.hpp | 30 +++++++++++++ .../opencs/view/world/colorpickerdelegate.cpp | 26 ++++++++---- .../opencs/view/world/colorpickerdelegate.hpp | 17 ++++---- 5 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 apps/opencs/view/widget/coloreditbutton.cpp create mode 100644 apps/opencs/view/widget/coloreditbutton.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 4a5a64f60..f8f3ea036 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -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 diff --git a/apps/opencs/view/widget/coloreditbutton.cpp b/apps/opencs/view/widget/coloreditbutton.cpp new file mode 100644 index 000000000..4960d1ef1 --- /dev/null +++ b/apps/opencs/view/widget/coloreditbutton.cpp @@ -0,0 +1,42 @@ +#include "coloreditbutton.hpp" + +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/apps/opencs/view/widget/coloreditbutton.hpp b/apps/opencs/view/widget/coloreditbutton.hpp new file mode 100644 index 000000000..e1a8cce9d --- /dev/null +++ b/apps/opencs/view/widget/coloreditbutton.hpp @@ -0,0 +1,30 @@ +#ifndef CSV_WIDGET_COLOREDITBUTTON_HPP +#define CSV_WIDGET_COLOREDITBUTTON_HPP + +#include + +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 diff --git a/apps/opencs/view/world/colorpickerdelegate.cpp b/apps/opencs/view/world/colorpickerdelegate.cpp index aa9f2e937..c74a1828f 100644 --- a/apps/opencs/view/world/colorpickerdelegate.cpp +++ b/apps/opencs/view/world/colorpickerdelegate.cpp @@ -3,6 +3,8 @@ #include #include +#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(), + getColoredRect(option).size(), + parent); } void CSVWorld::ColorPickerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - QColor color = index.data().value(); - 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()); +} + +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); -} \ No newline at end of file +} + + diff --git a/apps/opencs/view/world/colorpickerdelegate.hpp b/apps/opencs/view/world/colorpickerdelegate.hpp index e93e0e87d..147c2f424 100644 --- a/apps/opencs/view/world/colorpickerdelegate.hpp +++ b/apps/opencs/view/world/colorpickerdelegate.hpp @@ -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 From e257c915bfc75db5474d0e31d307664bbe745e60 Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 00:00:23 +0300 Subject: [PATCH 03/12] Add color picker popup to choose color --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/view/widget/coloreditor.cpp | 111 ++++++++++++++++++ apps/opencs/view/widget/coloreditor.hpp | 43 +++++++ apps/opencs/view/widget/colorpickerpopup.cpp | 63 ++++++++++ apps/opencs/view/widget/colorpickerpopup.hpp | 31 +++++ .../opencs/view/world/colorpickerdelegate.cpp | 8 +- 6 files changed, 253 insertions(+), 5 deletions(-) create mode 100644 apps/opencs/view/widget/coloreditor.cpp create mode 100644 apps/opencs/view/widget/coloreditor.hpp create mode 100644 apps/opencs/view/widget/colorpickerpopup.cpp create mode 100644 apps/opencs/view/widget/colorpickerpopup.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index f8f3ea036..83a20ecaa 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -75,7 +75,7 @@ opencs_units_noqt (view/world opencs_units (view/widget scenetoolbar scenetool scenetoolmode pushbutton scenetooltoggle scenetoolrun modebutton - scenetooltoggle2 completerpopup coloreditbutton + scenetooltoggle2 completerpopup coloreditor colorpickerpopup ) opencs_units (view/render diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp new file mode 100644 index 000000000..bab98fcd5 --- /dev/null +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -0,0 +1,111 @@ +#include "coloreditor.hpp" + +#include +#include +#include +#include +#include +#include + +#include "colorpickerpopup.hpp" + +CSVWidget::ColorEditor::ColorEditor(const QColor &color, + const QSize &coloredRectSize, + QWidget *parent) + : QPushButton(parent), + mColor(color), + mColoredRectSize(coloredRectSize), + mColorPicker(new ColorPickerPopup(this)) +{ + setCheckable(true); + connect(this, SIGNAL(clicked()), this, SLOT(showPicker())); + connect(mColorPicker, SIGNAL(hid()), this, SLOT(pickerHid())); + connect(mColorPicker, SIGNAL(colorChanged(const QColor &)), this, SLOT(pickerColorChanged(const QColor &))); +} + +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()); + QPainter painter(this); + painter.fillRect(coloredRect, mColor); +} + +QColor CSVWidget::ColorEditor::color() const +{ + return mColor; +} + +void CSVWidget::ColorEditor::setColor(const QColor &color) +{ + mColor = color; +} + +void CSVWidget::ColorEditor::setColoredRectSize(const QSize &size) +{ + mColoredRectSize = size; +} + +void CSVWidget::ColorEditor::showPicker() +{ + if (isChecked()) + { + mColorPicker->showPicker(calculatePopupPosition(), mColor); + } + else + { + mColorPicker->hide(); + } +} + +void CSVWidget::ColorEditor::pickerHid() +{ + // If the popup is hidden and mouse isn't above the editor, + // reset the editor checked state manually + QPoint globalEditorPosition = mapToGlobal(QPoint(0, 0)); + QRect globalEditorRect(globalEditorPosition, geometry().size()); + if (!globalEditorRect.contains(QCursor::pos())) + { + setChecked(false); + } +} + +void CSVWidget::ColorEditor::pickerColorChanged(const QColor &color) +{ + mColor = color; + update(); +} + +QPoint CSVWidget::ColorEditor::calculatePopupPosition() +{ + QRect editorGeometry = geometry(); + QRect popupGeometry = mColorPicker->geometry(); + QRect screenGeometry = QApplication::desktop()->screenGeometry(); + + // Center the popup horizontally relative to the editor + int localPopupX = (editorGeometry.width() - popupGeometry.width()) / 2; + // Popup position need to be specified in global coords + QPoint popupPosition = mapToGlobal(QPoint(localPopupX, editorGeometry.height())); + + // Make sure that the popup isn't out of the screen + if (popupPosition.x() < screenGeometry.left()) + { + popupPosition.setX(screenGeometry.left() + 1); + } + else if (popupPosition.x() + popupGeometry.width() > screenGeometry.right()) + { + popupPosition.setX(screenGeometry.right() - popupGeometry.width() - 1); + } + if (popupPosition.y() + popupGeometry.height() > screenGeometry.bottom()) + { + // Place the popup above the editor + popupPosition.setY(popupPosition.y() - popupGeometry.height() - editorGeometry.height() - 1); + } + + return popupPosition; +} diff --git a/apps/opencs/view/widget/coloreditor.hpp b/apps/opencs/view/widget/coloreditor.hpp new file mode 100644 index 000000000..a0fa497b6 --- /dev/null +++ b/apps/opencs/view/widget/coloreditor.hpp @@ -0,0 +1,43 @@ +#ifndef CSV_WIDGET_COLOREDITOR_HPP +#define CSV_WIDGET_COLOREDITOR_HPP + +#include + +class QColor; +class QPoint; +class QSize; + +namespace CSVWidget +{ + class ColorPickerPopup; + + class ColorEditor : public QPushButton + { + Q_OBJECT + + QColor mColor; + QSize mColoredRectSize; + ColorPickerPopup *mColorPicker; + + QPoint calculatePopupPosition(); + + public: + ColorEditor(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); + + private slots: + void showPicker(); + void pickerHid(); + void pickerColorChanged(const QColor &color); + }; +} + +#endif diff --git a/apps/opencs/view/widget/colorpickerpopup.cpp b/apps/opencs/view/widget/colorpickerpopup.cpp new file mode 100644 index 000000000..dd53f1bec --- /dev/null +++ b/apps/opencs/view/widget/colorpickerpopup.cpp @@ -0,0 +1,63 @@ +#include "colorpickerpopup.hpp" + +#include +#include +#include +#include + +CSVWidget::ColorPickerPopup::ColorPickerPopup(QWidget *parent) + : QFrame(parent) +{ + setWindowFlags(Qt::Popup); + setFrameStyle(QFrame::Box | QFrame::Plain); + hide(); + + mColorPicker = new QColorDialog(this); + mColorPicker->setWindowFlags(Qt::Widget); + mColorPicker->setOptions(QColorDialog::NoButtons | QColorDialog::DontUseNativeDialog); + mColorPicker->installEventFilter(this); + mColorPicker->open(); + connect(mColorPicker, + SIGNAL(currentColorChanged(const QColor &)), + this, + SIGNAL(colorChanged(const QColor &))); + + QVBoxLayout *layout = new QVBoxLayout(this); + layout->addWidget(mColorPicker); + layout->setAlignment(Qt::AlignTop | Qt::AlignLeft); + layout->setContentsMargins(0, 0, 0, 0); + setLayout(layout); + setFixedSize(mColorPicker->size()); +} + +void CSVWidget::ColorPickerPopup::showPicker(const QPoint &position, const QColor &initialColor) +{ + QRect geometry = this->geometry(); + geometry.moveTo(position); + setGeometry(geometry); + + mColorPicker->setCurrentColor(initialColor); + show(); +} + +void CSVWidget::ColorPickerPopup::hideEvent(QHideEvent *event) +{ + QFrame::hideEvent(event); + emit hid(); +} + +bool CSVWidget::ColorPickerPopup::eventFilter(QObject *object, QEvent *event) +{ + if (object == mColorPicker && event->type() == QEvent::KeyPress) + { + QKeyEvent *keyEvent = static_cast(event); + // Prevent QColorDialog from closing when Escape is pressed. + // Instead, hide the popup. + if (keyEvent->key() == Qt::Key_Escape) + { + hide(); + return true; + } + } + return QFrame::eventFilter(object, event); +} diff --git a/apps/opencs/view/widget/colorpickerpopup.hpp b/apps/opencs/view/widget/colorpickerpopup.hpp new file mode 100644 index 000000000..a7aec3bad --- /dev/null +++ b/apps/opencs/view/widget/colorpickerpopup.hpp @@ -0,0 +1,31 @@ +#ifndef CSVWIDGET_COLORPICKERPOPUP_HPP +#define CSVWIDGET_COLORPICKERPOPUP_HPP + +#include + +class QColorDialog; + +namespace CSVWidget +{ + class ColorPickerPopup : public QFrame + { + Q_OBJECT + + QColorDialog *mColorPicker; + + public: + explicit ColorPickerPopup(QWidget *parent); + + void showPicker(const QPoint &position, const QColor &initialColor); + + protected: + virtual void hideEvent(QHideEvent *event); + virtual bool eventFilter(QObject *object, QEvent *event); + + signals: + void hid(); + void colorChanged(const QColor &color); + }; +} + +#endif diff --git a/apps/opencs/view/world/colorpickerdelegate.cpp b/apps/opencs/view/world/colorpickerdelegate.cpp index c74a1828f..4491f392c 100644 --- a/apps/opencs/view/world/colorpickerdelegate.cpp +++ b/apps/opencs/view/world/colorpickerdelegate.cpp @@ -3,7 +3,7 @@ #include #include -#include "../widget/coloreditbutton.hpp" +#include "../widget/coloreditor.hpp" CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, CSMDoc::Document& document, @@ -28,9 +28,9 @@ QWidget *CSVWorld::ColorPickerDelegate::createEditor(QWidget *parent, throw std::logic_error("Wrong column for ColorPickerDelegate"); } - return new CSVWidget::ColorEditButton(index.data().value(), - getColoredRect(option).size(), - parent); + return new CSVWidget::ColorEditor(index.data().value(), + getColoredRect(option).size(), + parent); } void CSVWorld::ColorPickerDelegate::paint(QPainter *painter, From 4096d2851c66a574743c4a6d2ca4c61305affdab Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 14:09:25 +0300 Subject: [PATCH 04/12] Remove outdated ColorEditButton files --- apps/opencs/view/widget/coloreditbutton.cpp | 42 --------------------- apps/opencs/view/widget/coloreditbutton.hpp | 30 --------------- 2 files changed, 72 deletions(-) delete mode 100644 apps/opencs/view/widget/coloreditbutton.cpp delete mode 100644 apps/opencs/view/widget/coloreditbutton.hpp diff --git a/apps/opencs/view/widget/coloreditbutton.cpp b/apps/opencs/view/widget/coloreditbutton.cpp deleted file mode 100644 index 4960d1ef1..000000000 --- a/apps/opencs/view/widget/coloreditbutton.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "coloreditbutton.hpp" - -#include -#include -#include - -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; -} \ No newline at end of file diff --git a/apps/opencs/view/widget/coloreditbutton.hpp b/apps/opencs/view/widget/coloreditbutton.hpp deleted file mode 100644 index e1a8cce9d..000000000 --- a/apps/opencs/view/widget/coloreditbutton.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef CSV_WIDGET_COLOREDITBUTTON_HPP -#define CSV_WIDGET_COLOREDITBUTTON_HPP - -#include - -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 From eb5180ba8696f043cf91381728a36a86f41f9d44 Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 18:33:55 +0300 Subject: [PATCH 05/12] 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") { From caeba1b8878a91504ac39ceba9eff2d96b6ff1ba Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 20:34:35 +0300 Subject: [PATCH 06/12] Draw the frame around rect in ColorEditor and ColorPickerDelegate --- apps/opencs/view/widget/coloreditor.cpp | 2 ++ apps/opencs/view/world/colorpickerdelegate.cpp | 17 ++++++++--------- apps/opencs/view/world/colorpickerdelegate.hpp | 2 -- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp index eaef0f6d3..e6e3264be 100644 --- a/apps/opencs/view/widget/coloreditor.cpp +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -31,6 +31,8 @@ void CSVWidget::ColorEditor::paintEvent(QPaintEvent *event) qRound(buttonRect.height() / 2.0)); QPainter painter(this); painter.fillRect(coloredRect, mColor); + painter.setPen(Qt::black); + painter.drawRect(coloredRect); } QColor CSVWidget::ColorEditor::color() const diff --git a/apps/opencs/view/world/colorpickerdelegate.cpp b/apps/opencs/view/world/colorpickerdelegate.cpp index 7490c07f5..4feaa170e 100644 --- a/apps/opencs/view/world/colorpickerdelegate.cpp +++ b/apps/opencs/view/world/colorpickerdelegate.cpp @@ -15,16 +15,15 @@ void CSVWorld::ColorPickerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - QRect coloredRect = getColoredRect(option); + QRect coloredRect(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)); + painter->save(); painter->fillRect(coloredRect, index.data().value()); -} - -QRect CSVWorld::ColorPickerDelegate::getColoredRect(const QStyleOptionViewItem &option) const -{ - 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)); + painter->setPen(Qt::black); + painter->drawRect(coloredRect); + painter->restore(); } 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 f17923648..a8e6d87fc 100644 --- a/apps/opencs/view/world/colorpickerdelegate.hpp +++ b/apps/opencs/view/world/colorpickerdelegate.hpp @@ -14,8 +14,6 @@ namespace CSVWorld { class ColorPickerDelegate : public CommandDelegate { - QRect getColoredRect(const QStyleOptionViewItem &option) const; - public: ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, CSMDoc::Document& document, From e516d37cbf22e047cca941c2be7f48deab75dccd Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 22:03:47 +0300 Subject: [PATCH 07/12] MapColourColumn::set() uses the correct color value --- apps/opencs/model/world/columnimp.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/opencs/model/world/columnimp.hpp b/apps/opencs/model/world/columnimp.hpp index 8758d924b..a8ae5dfa1 100644 --- a/apps/opencs/model/world/columnimp.hpp +++ b/apps/opencs/model/world/columnimp.hpp @@ -694,7 +694,7 @@ namespace CSMWorld QColor colour = data.value(); - record2.mMapColor = colour.rgb() & 0xffffff; + record2.mMapColor = (colour.blue() << 16) | (colour.green() << 8) | colour.red(); record.setModified (record2); } From f19d07b4047daad44c7add15784659c167c1531e Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 23:09:31 +0300 Subject: [PATCH 08/12] Rename ColorPickerDelegate to ColorDelegate --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/view/doc/viewmanager.cpp | 4 +-- apps/opencs/view/world/colordelegate.cpp | 36 +++++++++++++++++++ ...orpickerdelegate.hpp => colordelegate.hpp} | 14 ++++---- .../opencs/view/world/colorpickerdelegate.cpp | 36 ------------------- 5 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 apps/opencs/view/world/colordelegate.cpp rename apps/opencs/view/world/{colorpickerdelegate.hpp => colordelegate.hpp} (64%) delete mode 100644 apps/opencs/view/world/colorpickerdelegate.cpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 83a20ecaa..88edf7bb1 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -70,7 +70,7 @@ opencs_units (view/world opencs_units_noqt (view/world subviews enumdelegate vartypedelegate recordstatusdelegate idtypedelegate datadisplaydelegate scripthighlighter idvalidator dialoguecreator physicssystem idcompletiondelegate - colorpickerdelegate + colordelegate ) opencs_units (view/widget diff --git a/apps/opencs/view/doc/viewmanager.cpp b/apps/opencs/view/doc/viewmanager.cpp index e5bb7fe81..969914851 100644 --- a/apps/opencs/view/doc/viewmanager.cpp +++ b/apps/opencs/view/doc/viewmanager.cpp @@ -19,7 +19,7 @@ #include "../world/recordstatusdelegate.hpp" #include "../world/idtypedelegate.hpp" #include "../world/idcompletiondelegate.hpp" -#include "../world/colorpickerdelegate.hpp" +#include "../world/colordelegate.hpp" #include "../../model/settings/usersettings.hpp" @@ -65,7 +65,7 @@ CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager) new CSVWorld::IdTypeDelegateFactory()); mDelegateFactories->add (CSMWorld::ColumnBase::Display_Colour, - new CSVWorld::ColorPickerDelegateFactory()); + new CSVWorld::ColorDelegateFactory()); std::vector idCompletionColumns = CSMWorld::IdCompletionManager::getDisplayTypes(); for (std::vector::const_iterator current = idCompletionColumns.begin(); diff --git a/apps/opencs/view/world/colordelegate.cpp b/apps/opencs/view/world/colordelegate.cpp new file mode 100644 index 000000000..ae71f965a --- /dev/null +++ b/apps/opencs/view/world/colordelegate.cpp @@ -0,0 +1,36 @@ +#include "colordelegate.hpp" + +#include +#include + +#include "../widget/coloreditor.hpp" + +CSVWorld::ColorDelegate::ColorDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document& document, + QObject *parent) + : CommandDelegate(dispatcher, document, parent) +{} + +void CSVWorld::ColorDelegate::paint(QPainter *painter, + const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + QRect coloredRect(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)); + painter->save(); + painter->fillRect(coloredRect, index.data().value()); + painter->setPen(Qt::black); + painter->drawRect(coloredRect); + painter->restore(); +} + +CSVWorld::CommandDelegate *CSVWorld::ColorDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document &document, + QObject *parent) const +{ + return new ColorDelegate(dispatcher, document, parent); +} + + diff --git a/apps/opencs/view/world/colorpickerdelegate.hpp b/apps/opencs/view/world/colordelegate.hpp similarity index 64% rename from apps/opencs/view/world/colorpickerdelegate.hpp rename to apps/opencs/view/world/colordelegate.hpp index a8e6d87fc..87051e86d 100644 --- a/apps/opencs/view/world/colorpickerdelegate.hpp +++ b/apps/opencs/view/world/colordelegate.hpp @@ -1,5 +1,5 @@ -#ifndef CSV_WORLD_COLORPICKERDELEGATE_HPP -#define CSV_WORLD_COLORPICKERDELEGATE_HPP +#ifndef CSV_WORLD_COLORDELEGATE_HPP +#define CSV_WORLD_COLORDELEGATE_HPP #include "util.hpp" @@ -12,19 +12,19 @@ namespace CSVWidget namespace CSVWorld { - class ColorPickerDelegate : public CommandDelegate + class ColorDelegate : public CommandDelegate { public: - ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, - CSMDoc::Document& document, - QObject *parent); + ColorDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document& document, + QObject *parent); virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; }; - class ColorPickerDelegateFactory : public CommandDelegateFactory + class ColorDelegateFactory : public CommandDelegateFactory { public: virtual CommandDelegate *makeDelegate(CSMWorld::CommandDispatcher *dispatcher, diff --git a/apps/opencs/view/world/colorpickerdelegate.cpp b/apps/opencs/view/world/colorpickerdelegate.cpp deleted file mode 100644 index 4feaa170e..000000000 --- a/apps/opencs/view/world/colorpickerdelegate.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include "colorpickerdelegate.hpp" - -#include -#include - -#include "../widget/coloreditor.hpp" - -CSVWorld::ColorPickerDelegate::ColorPickerDelegate(CSMWorld::CommandDispatcher *dispatcher, - CSMDoc::Document& document, - QObject *parent) - : CommandDelegate(dispatcher, document, parent) -{} - -void CSVWorld::ColorPickerDelegate::paint(QPainter *painter, - const QStyleOptionViewItem &option, - const QModelIndex &index) const -{ - QRect coloredRect(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)); - painter->save(); - painter->fillRect(coloredRect, index.data().value()); - painter->setPen(Qt::black); - painter->drawRect(coloredRect); - painter->restore(); -} - -CSVWorld::CommandDelegate *CSVWorld::ColorPickerDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher, - CSMDoc::Document &document, - QObject *parent) const -{ - return new ColorPickerDelegate(dispatcher, document, parent); -} - - From b06d1f008f0aed4a6152ef7d0b28a99ad6c5b7eb Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Fri, 12 Jun 2015 23:34:08 +0300 Subject: [PATCH 09/12] Redraw the color editor immediately after the color is updated --- apps/opencs/view/widget/coloreditor.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp index e6e3264be..f06187992 100644 --- a/apps/opencs/view/widget/coloreditor.cpp +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -43,6 +43,7 @@ QColor CSVWidget::ColorEditor::color() const void CSVWidget::ColorEditor::setColor(const QColor &color) { mColor = color; + update(); } void CSVWidget::ColorEditor::showPicker() From a294e24a85672ee7c31c9d08f22d7aab6a4f6902 Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Sat, 13 Jun 2015 00:37:10 +0300 Subject: [PATCH 10/12] Reset ColorEditor checked state after the popup is hidden --- apps/opencs/view/widget/coloreditor.cpp | 9 +------- apps/opencs/view/widget/colorpickerpopup.cpp | 23 ++++++++++++++++++++ apps/opencs/view/widget/colorpickerpopup.hpp | 1 + 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp index f06187992..b31225962 100644 --- a/apps/opencs/view/widget/coloreditor.cpp +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -60,14 +60,7 @@ void CSVWidget::ColorEditor::showPicker() void CSVWidget::ColorEditor::pickerHid() { - // If the popup is hidden and mouse isn't above the editor, - // reset the editor checked state manually - QPoint globalEditorPosition = mapToGlobal(QPoint(0, 0)); - QRect globalEditorRect(globalEditorPosition, geometry().size()); - if (!globalEditorRect.contains(QCursor::pos())) - { - setChecked(false); - } + setChecked(false); emit pickingFinished(); } diff --git a/apps/opencs/view/widget/colorpickerpopup.cpp b/apps/opencs/view/widget/colorpickerpopup.cpp index dd53f1bec..8e71ce39e 100644 --- a/apps/opencs/view/widget/colorpickerpopup.cpp +++ b/apps/opencs/view/widget/colorpickerpopup.cpp @@ -1,9 +1,12 @@ #include "colorpickerpopup.hpp" #include +#include #include #include +#include #include +#include CSVWidget::ColorPickerPopup::ColorPickerPopup(QWidget *parent) : QFrame(parent) @@ -40,6 +43,26 @@ void CSVWidget::ColorPickerPopup::showPicker(const QPoint &position, const QColo show(); } +void CSVWidget::ColorPickerPopup::mousePressEvent(QMouseEvent *event) +{ + QPushButton *button = qobject_cast(parentWidget()); + if (button != NULL) + { + QStyleOptionButton option; + option.init(button); + QRect buttonRect = option.rect; + buttonRect.moveTo(button->mapToGlobal(buttonRect.topLeft())); + + // If the mouse is pressed above the pop-up parent, + // the pop-up will be hidden and the pressed signal won't be repeated for the parent + if (buttonRect.contains(event->globalPos()) || buttonRect.contains(event->pos())) + { + setAttribute(Qt::WA_NoMouseReplay); + } + } + QFrame::mousePressEvent(event); +} + void CSVWidget::ColorPickerPopup::hideEvent(QHideEvent *event) { QFrame::hideEvent(event); diff --git a/apps/opencs/view/widget/colorpickerpopup.hpp b/apps/opencs/view/widget/colorpickerpopup.hpp index a7aec3bad..602bbdb6d 100644 --- a/apps/opencs/view/widget/colorpickerpopup.hpp +++ b/apps/opencs/view/widget/colorpickerpopup.hpp @@ -19,6 +19,7 @@ namespace CSVWidget void showPicker(const QPoint &position, const QColor &initialColor); protected: + virtual void mousePressEvent(QMouseEvent *event); virtual void hideEvent(QHideEvent *event); virtual bool eventFilter(QObject *object, QEvent *event); From ed0942422370e5e99dc3f4eb6dfd4e74e38c0aff Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Wed, 17 Jun 2015 00:45:43 +0300 Subject: [PATCH 11/12] Correct the position of the colored rectangle in ColorEditor and ColorDelegate --- apps/opencs/view/widget/coloreditor.cpp | 8 ++++---- apps/opencs/view/world/colordelegate.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp index b31225962..798502196 100644 --- a/apps/opencs/view/widget/coloreditor.cpp +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -25,10 +25,10 @@ void CSVWidget::ColorEditor::paintEvent(QPaintEvent *event) QPushButton::paintEvent(event); QRect buttonRect = rect(); - 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)); + QRect coloredRect(buttonRect.x() + qRound(buttonRect.width() / 4.0), + buttonRect.y() + qRound(buttonRect.height() / 4.0), + buttonRect.width() / 2, + buttonRect.height() / 2); QPainter painter(this); painter.fillRect(coloredRect, mColor); painter.setPen(Qt::black); diff --git a/apps/opencs/view/world/colordelegate.cpp b/apps/opencs/view/world/colordelegate.cpp index ae71f965a..1a89fc675 100644 --- a/apps/opencs/view/world/colordelegate.cpp +++ b/apps/opencs/view/world/colordelegate.cpp @@ -15,10 +15,10 @@ void CSVWorld::ColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - QRect coloredRect(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)); + QRect coloredRect(option.rect.x() + qRound(option.rect.width() / 4.0), + option.rect.y() + qRound(option.rect.height() / 4.0), + option.rect.width() / 2, + option.rect.height() / 2); painter->save(); painter->fillRect(coloredRect, index.data().value()); painter->setPen(Qt::black); From cf487581f79a917c4c68b1fe3b65d38110a3d520 Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Thu, 18 Jun 2015 12:50:46 +0300 Subject: [PATCH 12/12] Show the pop-up of ColorEditor immediately after the editor creation in tables --- apps/opencs/view/widget/coloreditor.cpp | 17 +++++++++++++++-- apps/opencs/view/widget/coloreditor.hpp | 6 ++++-- apps/opencs/view/world/util.cpp | 6 ++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp index 798502196..7ef1ec7b1 100644 --- a/apps/opencs/view/widget/coloreditor.cpp +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -6,13 +6,15 @@ #include #include #include +#include #include "colorpickerpopup.hpp" -CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent) +CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent, bool popupOnStart) : QPushButton(parent), mColor(color), - mColorPicker(new ColorPickerPopup(this)) + mColorPicker(new ColorPickerPopup(this)), + mPopupOnStart(popupOnStart) { setCheckable(true); connect(this, SIGNAL(clicked()), this, SLOT(showPicker())); @@ -35,6 +37,17 @@ void CSVWidget::ColorEditor::paintEvent(QPaintEvent *event) painter.drawRect(coloredRect); } +void CSVWidget::ColorEditor::showEvent(QShowEvent *event) +{ + QPushButton::showEvent(event); + if (isVisible() && mPopupOnStart) + { + setChecked(true); + showPicker(); + mPopupOnStart = false; + } +} + QColor CSVWidget::ColorEditor::color() const { return mColor; diff --git a/apps/opencs/view/widget/coloreditor.hpp b/apps/opencs/view/widget/coloreditor.hpp index cca26fade..61232cb13 100644 --- a/apps/opencs/view/widget/coloreditor.hpp +++ b/apps/opencs/view/widget/coloreditor.hpp @@ -17,17 +17,19 @@ namespace CSVWidget QColor mColor; ColorPickerPopup *mColorPicker; + bool mPopupOnStart; QPoint calculatePopupPosition(); public: - ColorEditor(const QColor &color, QWidget *parent = 0); + ColorEditor(const QColor &color, QWidget *parent = 0, bool popupOnStart = false); QColor color() const; void setColor(const QColor &color); protected: - void paintEvent(QPaintEvent *event); + virtual void paintEvent(QPaintEvent *event); + virtual void showEvent(QShowEvent *event); private slots: void showPicker(); diff --git a/apps/opencs/view/world/util.cpp b/apps/opencs/view/world/util.cpp index de088bffc..5974987c0 100644 --- a/apps/opencs/view/world/util.cpp +++ b/apps/opencs/view/world/util.cpp @@ -172,6 +172,12 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO { return QStyledItemDelegate::createEditor(parent, option, index); } + // For tables the pop-up of the color editor should appear immediately after the editor creation + // (the third parameter of ColorEditor's constructor) + else if (display == CSMWorld::ColumnBase::Display_Colour) + { + return new CSVWidget::ColorEditor(index.data().value(), parent, true); + } return createEditor (parent, option, index, display); }