From c7241c692f61bf37ee52e5a3b49ecfc11b241814 Mon Sep 17 00:00:00 2001 From: Rob Cutmore Date: Wed, 26 Jul 2017 19:43:37 -0400 Subject: [PATCH] Editor: Do int to color conversion in ColorEditor --- apps/opencs/view/widget/coloreditor.cpp | 23 +++++++++++++++++++++-- apps/opencs/view/widget/coloreditor.hpp | 10 +++++++++- apps/opencs/view/world/util.cpp | 12 +++--------- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/apps/opencs/view/widget/coloreditor.cpp b/apps/opencs/view/widget/coloreditor.cpp index 7ef1ec7b1..4cccf504d 100644 --- a/apps/opencs/view/widget/coloreditor.cpp +++ b/apps/opencs/view/widget/coloreditor.cpp @@ -10,9 +10,20 @@ #include "colorpickerpopup.hpp" -CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent, bool popupOnStart) +CSVWidget::ColorEditor::ColorEditor(const QColor &color, QWidget *parent, const bool popupOnStart) + : ColorEditor(parent, popupOnStart) +{ + setColor(color); +} + +CSVWidget::ColorEditor::ColorEditor(const int colorInt, QWidget *parent, const bool popupOnStart) + : ColorEditor(parent, popupOnStart) +{ + setColor(colorInt); +} + +CSVWidget::ColorEditor::ColorEditor(QWidget *parent, const bool popupOnStart) : QPushButton(parent), - mColor(color), mColorPicker(new ColorPickerPopup(this)), mPopupOnStart(popupOnStart) { @@ -59,6 +70,14 @@ void CSVWidget::ColorEditor::setColor(const QColor &color) update(); } +void CSVWidget::ColorEditor::setColor(const int colorInt) +{ + // Color RGB values are stored in given integer. + // First byte is red, second byte is green, third byte is blue. + QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff); + setColor(color); +} + void CSVWidget::ColorEditor::showPicker() { if (isChecked()) diff --git a/apps/opencs/view/widget/coloreditor.hpp b/apps/opencs/view/widget/coloreditor.hpp index 61232cb13..850cf6b2f 100644 --- a/apps/opencs/view/widget/coloreditor.hpp +++ b/apps/opencs/view/widget/coloreditor.hpp @@ -22,15 +22,23 @@ namespace CSVWidget QPoint calculatePopupPosition(); public: - ColorEditor(const QColor &color, QWidget *parent = 0, bool popupOnStart = false); + ColorEditor(const QColor &color, QWidget *parent = 0, const bool popupOnStart = false); + ColorEditor(const int colorInt, QWidget *parent = 0, const bool popupOnStart = false); QColor color() const; void setColor(const QColor &color); + /// \brief Set color using given int value. + /// \param colorInt RGB color value encoded as an integer. + void setColor(const int colorInt); + protected: virtual void paintEvent(QPaintEvent *event); virtual void showEvent(QShowEvent *event); + private: + ColorEditor(QWidget *parent = 0, const bool popupOnStart = false); + private slots: void showPicker(); void pickerHid(); diff --git a/apps/opencs/view/world/util.cpp b/apps/opencs/view/world/util.cpp index 22d295761..7c1436d26 100644 --- a/apps/opencs/view/world/util.cpp +++ b/apps/opencs/view/world/util.cpp @@ -181,9 +181,7 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO // (the third parameter of ColorEditor's constructor) else if (display == CSMWorld::ColumnBase::Display_Colour) { - int colorInt = index.data().toInt(); - QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff); - return new CSVWidget::ColorEditor(color, parent, true); + return new CSVWidget::ColorEditor(index.data().toInt(), parent, true); } return createEditor (parent, option, index, display); } @@ -207,9 +205,7 @@ QWidget *CSVWorld::CommandDelegate::createEditor (QWidget *parent, const QStyleO { case CSMWorld::ColumnBase::Display_Colour: { - int colorInt = variant.toInt(); - QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff); - return new CSVWidget::ColorEditor(color, parent); + return new CSVWidget::ColorEditor(variant.toInt(), parent); } case CSMWorld::ColumnBase::Display_Integer: { @@ -322,9 +318,7 @@ void CSVWorld::CommandDelegate::setEditorData (QWidget *editor, const QModelInde CSVWidget::ColorEditor *colorEditor = qobject_cast(editor); if (colorEditor != NULL) { - int colorInt = variant.toInt(); - QColor color = QColor(colorInt & 0xff, (colorInt >> 8) & 0xff, (colorInt >> 16) & 0xff); - colorEditor->setColor(color); + colorEditor->setColor(variant.toInt()); return; }