From a54efbcfa00990eccdcc78e31d7b26f1fede15cd Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sun, 10 Aug 2014 18:46:32 +0200 Subject: [PATCH] made FilterCreator redundant by generalising GenericCreator --- apps/opencs/CMakeLists.txt | 2 +- apps/opencs/view/filter/filtercreator.cpp | 77 -------------- apps/opencs/view/filter/filtercreator.hpp | 43 -------- apps/opencs/view/world/genericcreator.cpp | 119 ++++++++++++++++++---- apps/opencs/view/world/genericcreator.hpp | 14 ++- apps/opencs/view/world/subviews.cpp | 2 +- 6 files changed, 116 insertions(+), 141 deletions(-) delete mode 100644 apps/opencs/view/filter/filtercreator.cpp delete mode 100644 apps/opencs/view/filter/filtercreator.hpp diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index 3928b06d8..a5fe77dd9 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -131,7 +131,7 @@ opencs_hdrs_noqt (model/filter ) opencs_units (view/filter - filtercreator filterbox recordfilterbox editwidget + filterbox recordfilterbox editwidget ) set (OPENCS_US diff --git a/apps/opencs/view/filter/filtercreator.cpp b/apps/opencs/view/filter/filtercreator.cpp deleted file mode 100644 index 5bca0434b..000000000 --- a/apps/opencs/view/filter/filtercreator.cpp +++ /dev/null @@ -1,77 +0,0 @@ - -#include "filtercreator.hpp" - -#include -#include - -#include "../../model/filter/filter.hpp" - -#include "../../model/world/data.hpp" -#include "../../model/world/commands.hpp" -#include "../../model/world/columns.hpp" -#include "../../model/world/idtable.hpp" - -std::string CSVFilter::FilterCreator::getNamespace() const -{ - switch (mScope->currentIndex()) - { - case CSMFilter::Filter::Scope_Project: return "project::"; - case CSMFilter::Filter::Scope_Session: return "session::"; - } - - return ""; -} - -void CSVFilter::FilterCreator::update() -{ - mNamespace->setText (QString::fromUtf8 (getNamespace().c_str())); - GenericCreator::update(); -} - -std::string CSVFilter::FilterCreator::getId() const -{ - return getNamespace() + GenericCreator::getId(); -} - -void CSVFilter::FilterCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const -{ - int index = - dynamic_cast (*getData().getTableModel (getCollectionId())). - findColumnIndex (CSMWorld::Columns::ColumnId_Scope); - - command.addValue (index, mScope->currentIndex()); -} - -CSVFilter::FilterCreator::FilterCreator (CSMWorld::Data& data, QUndoStack& undoStack, - const CSMWorld::UniversalId& id) -: GenericCreator (data, undoStack, id) -{ - mNamespace = new QLabel ("::", this); - insertAtBeginning (mNamespace, false); - - mScope = new QComboBox (this); - - mScope->addItem ("Project"); - mScope->addItem ("Session"); - /// \todo re-enable for OpenMW 1.1 - // mScope->addItem ("Content"); - - connect (mScope, SIGNAL (currentIndexChanged (int)), this, SLOT (setScope2 (int))); - - insertAtBeginning (mScope, false); - - QLabel *label = new QLabel ("Scope", this); - insertAtBeginning (label, false); - - mScope->setCurrentIndex (1); -} - -void CSVFilter::FilterCreator::reset() -{ - GenericCreator::reset(); -} - -void CSVFilter::FilterCreator::setScope2 (int index) -{ - update(); -} diff --git a/apps/opencs/view/filter/filtercreator.hpp b/apps/opencs/view/filter/filtercreator.hpp deleted file mode 100644 index 144b5adf4..000000000 --- a/apps/opencs/view/filter/filtercreator.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef CSV_FILTER_FILTERCREATOR_H -#define CSV_FILTER_FILTERCREATOR_H - -class QComboBox; -class QLabel; - -#include "../world/genericcreator.hpp" - -namespace CSVFilter -{ - class FilterCreator : public CSVWorld::GenericCreator - { - Q_OBJECT - - QComboBox *mScope; - QLabel *mNamespace; - - private: - - std::string getNamespace() const; - - protected: - - void update(); - - virtual std::string getId() const; - - virtual void configureCreateCommand (CSMWorld::CreateCommand& command) const; - - public: - - FilterCreator (CSMWorld::Data& data, QUndoStack& undoStack, - const CSMWorld::UniversalId& id); - - virtual void reset(); - - private slots: - - void setScope2 (int index); - }; -} - -#endif diff --git a/apps/opencs/view/world/genericcreator.cpp b/apps/opencs/view/world/genericcreator.cpp index 31c216e2c..d23fc2803 100644 --- a/apps/opencs/view/world/genericcreator.cpp +++ b/apps/opencs/view/world/genericcreator.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include "../../model/world/commands.hpp" #include "../../model/world/data.hpp" @@ -22,6 +24,9 @@ void CSVWorld::GenericCreator::update() mId->setToolTip (QString::fromUtf8 (mErrors.c_str())); mCreate->setEnabled (mErrors.empty() && !mLocked); + + if (mNamespace) + mNamespace->setText (QString::fromUtf8 (getNamespace().c_str())); } void CSVWorld::GenericCreator::setManualEditing (bool enabled) @@ -41,7 +46,7 @@ void CSVWorld::GenericCreator::insertBeforeButtons (QWidget *widget, bool stretc std::string CSVWorld::GenericCreator::getId() const { - return mId->text().toUtf8().constData(); + return getNamespace() + mId->text().toUtf8().constData(); } void CSVWorld::GenericCreator::configureCreateCommand (CSMWorld::CreateCommand& command) const {} @@ -56,16 +61,37 @@ const CSMWorld::UniversalId& CSVWorld::GenericCreator::getCollectionId() const return mListId; } -CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, - const CSMWorld::UniversalId& id, bool relaxedIdRules): +std::string CSVWorld::GenericCreator::getNamespace() const +{ + CSMWorld::Scope scope = CSMWorld::Scope_Content; + + if (mScope) + { + scope = static_cast (mScope->itemData (mScope->currentIndex()).toInt()); + } + else + { + if (mScopes & CSMWorld::Scope_Project) + scope = CSMWorld::Scope_Project; + else if (mScopes & CSMWorld::Scope_Session) + scope = CSMWorld::Scope_Session; + } + + switch (scope) + { + case CSMWorld::Scope_Content: return ""; + case CSMWorld::Scope_Project: return "project::"; + case CSMWorld::Scope_Session: return "session::"; + } - mData (data), - mUndoStack (undoStack), - mListId (id), - mLocked (false), - mCloneMode(false), - mClonedType(CSMWorld::UniversalId::Type_None) + return ""; +} +CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, + const CSMWorld::UniversalId& id, bool relaxedIdRules) +: mData (data), mUndoStack (undoStack), mListId (id), mLocked (false), mCloneMode (false), + mClonedType (CSMWorld::UniversalId::Type_None), mScopes (CSMWorld::Scope_Content), mScope (0), + mScopeLabel (0), mNamespace (0) { mLayout = new QHBoxLayout; mLayout->setContentsMargins (0, 0, 0, 0); @@ -128,29 +154,28 @@ void CSVWorld::GenericCreator::create() { if (!mLocked) { + std::string id = getId(); + if (mCloneMode) { - std::string id = getId(); std::auto_ptr command (new CSMWorld::CloneCommand ( dynamic_cast (*mData.getTableModel(mListId)), mClonedId, id, mClonedType)); mUndoStack.push(command.release()); - emit done(); - emit requestFocus(id); - } else { - std::string id = getId(); - + } + else + { std::auto_ptr command (new CSMWorld::CreateCommand ( dynamic_cast (*mData.getTableModel (mListId)), id)); configureCreateCommand (*command); mUndoStack.push (command.release()); - - emit done(); - emit requestFocus (id); } + + emit done(); + emit requestFocus(id); } } @@ -165,3 +190,61 @@ void CSVWorld::GenericCreator::cloneMode(const std::string& originId, void CSVWorld::GenericCreator::toggleWidgets(bool active) { } + +void CSVWorld::GenericCreator::setScope (unsigned int scope) +{ + mScopes = scope; + int count = (mScopes & CSMWorld::Scope_Content) + (mScopes & CSMWorld::Scope_Project) + + (mScopes & CSMWorld::Scope_Session); + + // namespace widget + if (count>1 || (count>0 && !(mScopes & CSMWorld::Scope_Content))) + { + if (!mNamespace) + { + mNamespace = new QLabel ("::", this); + insertAtBeginning (mNamespace, false); + } + } + else + { + delete mNamespace; + mNamespace = 0; + } + + // scope selector widget + if (count>1) + { + mScope = new QComboBox (this); + insertAtBeginning (mScope, false); + + if (mScopes & CSMWorld::Scope_Content) + mScope->addItem ("Content", static_cast (CSMWorld::Scope_Content)); + + if (mScopes & CSMWorld::Scope_Project) + mScope->addItem ("Project", static_cast (CSMWorld::Scope_Project)); + + if (mScopes & CSMWorld::Scope_Session) + mScope->addItem ("Session", static_cast (CSMWorld::Scope_Session)); + + connect (mScope, SIGNAL (currentIndexChanged (int)), this, SLOT (scopeChanged (int))); + + mScopeLabel = new QLabel ("Scope", this); + insertAtBeginning (mScopeLabel, false); + + mScope->setCurrentIndex (0); + } + else + { + delete mScope; + mScope = 0; + + delete mScopeLabel; + mScopeLabel = 0; + } +} + +void CSVWorld::GenericCreator::scopeChanged (int index) +{ + update(); +} \ No newline at end of file diff --git a/apps/opencs/view/world/genericcreator.hpp b/apps/opencs/view/world/genericcreator.hpp index 714853f98..fdd743489 100644 --- a/apps/opencs/view/world/genericcreator.hpp +++ b/apps/opencs/view/world/genericcreator.hpp @@ -5,6 +5,8 @@ class QString; class QPushButton; class QLineEdit; class QHBoxLayout; +class QComboBox; +class QLabel; #include "creator.hpp" @@ -31,6 +33,10 @@ namespace CSVWorld bool mLocked; std::string mClonedId; CSMWorld::UniversalId::Type mClonedType; + unsigned int mScopes; + QComboBox *mScope; + QLabel *mScopeLabel; + QLabel *mNamespace; protected: bool mCloneMode; @@ -54,6 +60,8 @@ namespace CSVWorld const CSMWorld::UniversalId& getCollectionId() const; + std::string getNamespace() const; + public: GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, @@ -65,18 +73,22 @@ namespace CSVWorld virtual void toggleWidgets (bool active = true); - virtual void cloneMode(const std::string& originId, + virtual void cloneMode(const std::string& originId, const CSMWorld::UniversalId::Type type); virtual std::string getErrors() const; ///< Return formatted error descriptions for the current state of the creator. if an empty /// string is returned, there is no error. + virtual void setScope (unsigned int scope); + private slots: void textChanged (const QString& text); void create(); + + void scopeChanged (int index); }; } diff --git a/apps/opencs/view/world/subviews.cpp b/apps/opencs/view/world/subviews.cpp index 9deaee329..1d90a26bb 100644 --- a/apps/opencs/view/world/subviews.cpp +++ b/apps/opencs/view/world/subviews.cpp @@ -93,7 +93,7 @@ void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager) manager.add (CSMWorld::UniversalId::Type_Filters, new CSVDoc::SubViewFactoryWithCreator >); + CreatorFactory >); manager.add (CSMWorld::UniversalId::Type_DebugProfiles, new CSVDoc::SubViewFactoryWithCreator