From 33e36ee9227d88e334c8dbc9f0fd8017216d54ee Mon Sep 17 00:00:00 2001 From: Stanislav Bas Date: Sat, 30 May 2015 15:51:33 +0300 Subject: [PATCH] Add the ID completion delegate --- apps/opencs/CMakeLists.txt | 2 +- .../view/world/idcompletiondelegate.cpp | 41 +++++++++++++++++++ .../view/world/idcompletiondelegate.hpp | 36 ++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 apps/opencs/view/world/idcompletiondelegate.cpp create mode 100644 apps/opencs/view/world/idcompletiondelegate.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 99ba54ac1..efbe77ccb 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -69,7 +69,7 @@ opencs_units (view/world opencs_units_noqt (view/world subviews enumdelegate vartypedelegate recordstatusdelegate idtypedelegate datadisplaydelegate - scripthighlighter idvalidator dialoguecreator physicssystem + scripthighlighter idvalidator dialoguecreator physicssystem idcompletiondelegate ) opencs_units (view/widget diff --git a/apps/opencs/view/world/idcompletiondelegate.cpp b/apps/opencs/view/world/idcompletiondelegate.cpp new file mode 100644 index 000000000..0f309054b --- /dev/null +++ b/apps/opencs/view/world/idcompletiondelegate.cpp @@ -0,0 +1,41 @@ +#include "idcompletiondelegate.hpp" + +#include "../../model/world/idcompletionmanager.hpp" + +CSVWorld::IdCompletionDelegate::IdCompletionDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document& document, + QObject *parent) + : CommandDelegate(dispatcher, document, parent) +{} + +QWidget *CSVWorld::IdCompletionDelegate::createEditor(QWidget *parent, + const QStyleOptionViewItem &option, + const QModelIndex &index) const +{ + return createEditor(parent, option, index, CSMWorld::ColumnBase::Display_None); +} + +QWidget *CSVWorld::IdCompletionDelegate::createEditor(QWidget *parent, + const QStyleOptionViewItem &option, + const QModelIndex &index, + CSMWorld::ColumnBase::Display display) const +{ + int columnIdData = index.data(CSMWorld::ColumnBase::Role_ColumnId).toInt(); + CSMWorld::Columns::ColumnId columnId = static_cast(columnIdData); + CSMWorld::IdCompletionManager &completionManager = getDocument().getIdCompletionManager(); + + QWidget *editor = CSVWorld::CommandDelegate::createEditor(parent, option, index, display); + QLineEdit *lineEditor = qobject_cast(editor); + if (lineEditor != NULL && completionManager.isCompleterExistFor(columnId)) + { + lineEditor->setCompleter(completionManager.getCompleter(columnId).get()); + } + return editor; +} + +CSVWorld::CommandDelegate *CSVWorld::IdCompletionDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher, + CSMDoc::Document& document, + QObject *parent) const +{ + return new IdCompletionDelegate(dispatcher, document, parent); +} \ No newline at end of file diff --git a/apps/opencs/view/world/idcompletiondelegate.hpp b/apps/opencs/view/world/idcompletiondelegate.hpp new file mode 100644 index 000000000..11ecc1cb9 --- /dev/null +++ b/apps/opencs/view/world/idcompletiondelegate.hpp @@ -0,0 +1,36 @@ +#ifndef CSV_WORLD_IDCOMPLETIONMANAGER_HPP +#define CSV_WORLD_IDCOMPLETIONMANAGER_HPP + +#include "util.hpp" + +namespace CSVWorld +{ + /// \brief Enables the Id completion for a column + class IdCompletionDelegate : public CommandDelegate + { + public: + IdCompletionDelegate(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; + }; + + class IdCompletionDelegateFactory : 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 \ No newline at end of file