diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 4a5a64f604..f8f3ea0360 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 0000000000..4960d1ef1b --- /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 0000000000..e1a8cce9da --- /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 aa9f2e9371..c74a1828f3 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 e93e0e87de..147c2f4243 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