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