diff --git a/apps/opencs/CMakeLists.txt b/apps/opencs/CMakeLists.txt index bad1e13762..67cbcc772a 100644 --- a/apps/opencs/CMakeLists.txt +++ b/apps/opencs/CMakeLists.txt @@ -10,9 +10,10 @@ set (OPENCS_SRC model/tools/tools.cpp model/tools/operation.cpp model/tools/stage.cpp model/tools/verifier.cpp model/tools/mandatoryid.cpp - view/doc/viewmanager.cpp view/doc/view.cpp view/doc/operations.cpp view/doc/operation.cpp + view/doc/viewmanager.cpp view/doc/view.cpp view/doc/operations.cpp view/doc/operation.cpp view/doc/subviewfactory.cpp + view/doc/subview.cpp - view/world/subview.cpp view/world/table.cpp view/world/tablesubview.cpp + view/world/table.cpp view/world/tablesubview.cpp view/world/subviews.cpp ) set (OPENCS_HDR @@ -27,9 +28,10 @@ set (OPENCS_HDR model/tools/tools.hpp model/tools/operation.hpp model/tools/stage.hpp model/tools/verifier.hpp model/tools/mandatoryid.hpp - view/doc/viewmanager.hpp view/doc/view.hpp view/doc/operations.hpp view/doc/operation.hpp + view/doc/viewmanager.hpp view/doc/view.hpp view/doc/operations.hpp view/doc/operation.hpp view/doc/subviewfactory.hpp + view/doc/subview.hpp view/doc/subviewfactoryimp.hpp - view/world/subview.hpp view/world/table.hpp view/world/tablesubview.hpp + view/world/table.hpp view/world/tablesubview.hpp view/world/subviews.hpp ) set (OPENCS_US diff --git a/apps/opencs/view/world/subview.cpp b/apps/opencs/view/doc/subview.cpp similarity index 65% rename from apps/opencs/view/world/subview.cpp rename to apps/opencs/view/doc/subview.cpp index 23f075980a..1c356fa736 100644 --- a/apps/opencs/view/world/subview.cpp +++ b/apps/opencs/view/doc/subview.cpp @@ -1,7 +1,7 @@ #include "subview.hpp" -CSVWorld::SubView::SubView (const CSMWorld::UniversalId& id) : mUniversalId (id) +CSVDoc::SubView::SubView (const CSMWorld::UniversalId& id) : mUniversalId (id) { /// \todo add a button to the title bar that clones this sub view @@ -12,7 +12,7 @@ CSVWorld::SubView::SubView (const CSMWorld::UniversalId& id) : mUniversalId (id) setMinimumHeight (60); } -CSMWorld::UniversalId CSVWorld::SubView::getUniversalId() const +CSMWorld::UniversalId CSVDoc::SubView::getUniversalId() const { return mUniversalId; } diff --git a/apps/opencs/view/doc/subview.hpp b/apps/opencs/view/doc/subview.hpp new file mode 100644 index 0000000000..50ec511ab1 --- /dev/null +++ b/apps/opencs/view/doc/subview.hpp @@ -0,0 +1,41 @@ +#ifndef CSV_DOC_SUBVIEW_H +#define CSV_DOC_SUBVIEW_H + +#include "../../model/doc/document.hpp" + +#include "../../model/world/universalid.hpp" + +#include "subviewfactory.hpp" + +#include + +class QUndoStack; + +namespace CSMWorld +{ + class Data; +} + +namespace CSVDoc +{ + class SubView : public QDockWidget + { + Q_OBJECT + + CSMWorld::UniversalId mUniversalId; + + // not implemented + SubView (const SubView&); + SubView& operator= (SubView&); + + public: + + SubView (const CSMWorld::UniversalId& id); + + CSMWorld::UniversalId getUniversalId() const; + + virtual void setEditLock (bool locked) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/apps/opencs/view/doc/subviewfactory.cpp b/apps/opencs/view/doc/subviewfactory.cpp new file mode 100644 index 0000000000..8576f6b1d1 --- /dev/null +++ b/apps/opencs/view/doc/subviewfactory.cpp @@ -0,0 +1,38 @@ + +#include "subviewfactory.hpp" + +#include + +#include + +CSVDoc::SubViewFactoryBase::SubViewFactoryBase() {} + +CSVDoc::SubViewFactoryBase::~SubViewFactoryBase() {} + + +CSVDoc::SubViewFactoryManager::SubViewFactoryManager() {} + +CSVDoc::SubViewFactoryManager::~SubViewFactoryManager() +{ + for (std::map::iterator iter (mSubViewFactories.begin()); + iter!=mSubViewFactories.end(); ++iter) + delete iter->second; +} + +void CSVDoc::SubViewFactoryManager::add (const CSMWorld::UniversalId::Type& id, SubViewFactoryBase *factory) +{ + assert (mSubViewFactories.find (id)==mSubViewFactories.end()); + + mSubViewFactories.insert (std::make_pair (id, factory)); +} + +CSVDoc::SubView *CSVDoc::SubViewFactoryManager::makeSubView (const CSMWorld::UniversalId& id, + CSMDoc::Document& document) +{ + std::map::iterator iter = mSubViewFactories.find (id.getType()); + + if (iter==mSubViewFactories.end()) + throw std::runtime_error ("Failed to create a sub view for: " + id.toString()); + + return iter->second->makeSubView (id, document); +} \ No newline at end of file diff --git a/apps/opencs/view/doc/subviewfactory.hpp b/apps/opencs/view/doc/subviewfactory.hpp new file mode 100644 index 0000000000..1f7c154806 --- /dev/null +++ b/apps/opencs/view/doc/subviewfactory.hpp @@ -0,0 +1,55 @@ +#ifndef CSV_DOC_SUBVIEWFACTORY_H +#define CSV_DOC_SUBVIEWFACTORY_H + +#include + +#include "../../model/world/universalid.hpp" + +namespace CSMDoc +{ + class Document; +} + +namespace CSVDoc +{ + class SubView; + + class SubViewFactoryBase + { + // not implemented + SubViewFactoryBase (const SubViewFactoryBase&); + SubViewFactoryBase& operator= (const SubViewFactoryBase&); + + public: + + SubViewFactoryBase(); + + virtual ~SubViewFactoryBase(); + + virtual SubView *makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document) = 0; + ///< The ownership of the returned sub view is not transferred. + }; + + class SubViewFactoryManager + { + std::map mSubViewFactories; + + // not implemented + SubViewFactoryManager (const SubViewFactoryManager&); + SubViewFactoryManager& operator= (const SubViewFactoryManager&); + + public: + + SubViewFactoryManager(); + + ~SubViewFactoryManager(); + + void add (const CSMWorld::UniversalId::Type& id, SubViewFactoryBase *factory); + ///< The ownership of \a factory is transferred to this. + + SubView *makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document); + ///< The ownership of the returned sub view is not transferred. + }; +} + +#endif diff --git a/apps/opencs/view/doc/subviewfactoryimp.hpp b/apps/opencs/view/doc/subviewfactoryimp.hpp new file mode 100644 index 0000000000..d16e0b2b7b --- /dev/null +++ b/apps/opencs/view/doc/subviewfactoryimp.hpp @@ -0,0 +1,50 @@ +#ifndef CSV_DOC_SUBVIEWFACTORYIMP_H +#define CSV_DOC_SUBVIEWFACTORYIMP_H + +#include "../../model/doc/document.hpp" + +#include "subviewfactory.hpp" + +namespace CSVDoc +{ + template + class SubViewFactory : public SubViewFactoryBase + { + public: + + virtual CSVDoc::SubView *makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document); + }; + + template + CSVDoc::SubView *SubViewFactory::makeSubView (const CSMWorld::UniversalId& id, + CSMDoc::Document& document) + { + return new SubViewT (id, document); + } + + template + class SubViewFactoryWithCreateFlag : public SubViewFactoryBase + { + bool mCreateAndDelete; + + public: + + SubViewFactoryWithCreateFlag (bool createAndDelete); + + virtual CSVDoc::SubView *makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document); + }; + + template + SubViewFactoryWithCreateFlag::SubViewFactoryWithCreateFlag (bool createAndDelete) + : mCreateAndDelete (createAndDelete) + {} + + template + CSVDoc::SubView *SubViewFactoryWithCreateFlag::makeSubView (const CSMWorld::UniversalId& id, + CSMDoc::Document& document) + { + return new SubViewT (id, document, mCreateAndDelete); + } +} + +#endif \ No newline at end of file diff --git a/apps/opencs/view/doc/view.cpp b/apps/opencs/view/doc/view.cpp index c1659b388e..47053192b0 100644 --- a/apps/opencs/view/doc/view.cpp +++ b/apps/opencs/view/doc/view.cpp @@ -10,10 +10,11 @@ #include "../../model/doc/document.hpp" -#include "../world/tablesubview.hpp" +#include "../world/subviews.hpp" #include "viewmanager.hpp" #include "operations.hpp" +#include "subview.hpp" void CSVDoc::View::closeEvent (QCloseEvent *event) { @@ -120,15 +121,11 @@ CSVDoc::View::View (ViewManager& viewManager, CSMDoc::Document *document, int to setupUi(); - mSubViewFactories.insert (std::make_pair (CSMWorld::UniversalId (CSMWorld::UniversalId::Type_Globals), - new CSVWorld::SubViewFactoryWithCreateFlag (true))); + CSVWorld::addSubViewFactories (mSubViewFactory); } CSVDoc::View::~View() { - for (std::map::iterator iter (mSubViewFactories.begin()); - iter!=mSubViewFactories.end(); ++iter) - delete iter->second; } const CSMDoc::Document *CSVDoc::View::getDocument() const @@ -165,9 +162,9 @@ void CSVDoc::View::updateDocumentState() if (!(state & operations[i])) mOperations->quitOperation (operations[i]); - QList subViews = findChildren(); + QList subViews = findChildren(); - for (QList::iterator iter (subViews.begin()); iter!=subViews.end(); ++iter) + for (QList::iterator iter (subViews.begin()); iter!=subViews.end(); ++iter) (*iter)->setEditLock (state & CSMDoc::State_Locked); } @@ -186,12 +183,7 @@ void CSVDoc::View::addSubView (const CSMWorld::UniversalId& id) /// \todo add an user setting to reuse sub views (on a per document basis or on a per top level view basis) - std::map::iterator iter = mSubViewFactories.find (id); - - if (iter==mSubViewFactories.end()) - throw std::logic_error ("can't create subview for " + id.toString()); - - CSVWorld::SubView *view = iter->second->makeSubView (id, *mDocument); + SubView *view = mSubViewFactory.makeSubView (id, *mDocument); addDockWidget (Qt::TopDockWidgetArea, view); view->show(); } diff --git a/apps/opencs/view/doc/view.hpp b/apps/opencs/view/doc/view.hpp index c86c3b3625..f7d4dd3732 100644 --- a/apps/opencs/view/doc/view.hpp +++ b/apps/opencs/view/doc/view.hpp @@ -6,6 +6,8 @@ #include +#include "subviewfactory.hpp" + class QAction; namespace CSMDoc @@ -18,11 +20,6 @@ namespace CSMWorld class UniversalId; } -namespace CSVWorld -{ - struct SubViewFactoryBase; -} - namespace CSVDoc { class ViewManager; @@ -42,7 +39,7 @@ namespace CSVDoc QAction *mVerify; std::vector mEditingActions; Operations *mOperations; - std::map mSubViewFactories; + SubViewFactoryManager mSubViewFactory; // not implemented View (const View&); diff --git a/apps/opencs/view/world/subview.hpp b/apps/opencs/view/world/subview.hpp deleted file mode 100644 index ff130828b6..0000000000 --- a/apps/opencs/view/world/subview.hpp +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef CSV_WORLD_SUBVIEW_H -#define CSV_WORLD_SUBVIEW_H - -#include "../../model/doc/document.hpp" - -#include "../../model/world/universalid.hpp" - -#include - -class QUndoStack; - -namespace CSMWorld -{ - class Data; -} - -namespace CSVWorld -{ - class SubView : public QDockWidget - { - Q_OBJECT - - CSMWorld::UniversalId mUniversalId; - - // not implemented - SubView (const SubView&); - SubView& operator= (SubView&); - - public: - - SubView (const CSMWorld::UniversalId& id); - - CSMWorld::UniversalId getUniversalId() const; - - virtual void setEditLock (bool locked) = 0; - }; - - struct SubViewFactoryBase - { - virtual SubView *makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document) - = 0; - }; - - template - struct SubViewFactory : public SubViewFactoryBase - { - virtual SubView *makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document); - }; - - template - SubView *SubViewFactory::makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document) - { - return new SubViewT (id, document.getData(), document.getUndoStack()); - } - - template - struct SubViewFactoryWithCreateFlag : public SubViewFactoryBase - { - bool mCreateAndDelete; - - SubViewFactoryWithCreateFlag (bool createAndDelete); - - virtual SubView *makeSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document); - }; - - template - SubViewFactoryWithCreateFlag::SubViewFactoryWithCreateFlag (bool createAndDelete) - : mCreateAndDelete (createAndDelete) - {} - - template - SubView *SubViewFactoryWithCreateFlag::makeSubView (const CSMWorld::UniversalId& id, - CSMDoc::Document& document) - { - return new SubViewT (id, document.getData(), document.getUndoStack(), mCreateAndDelete); - } -} - -#endif \ No newline at end of file diff --git a/apps/opencs/view/world/subviews.cpp b/apps/opencs/view/world/subviews.cpp new file mode 100644 index 0000000000..fdc0cb31d1 --- /dev/null +++ b/apps/opencs/view/world/subviews.cpp @@ -0,0 +1,12 @@ + +#include "subviews.hpp" + +#include "../doc/subviewfactoryimp.hpp" + +#include "tablesubview.hpp" + +void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager) +{ + manager.add (CSMWorld::UniversalId::Type_Globals, + new CSVDoc::SubViewFactoryWithCreateFlag (true)); +} \ No newline at end of file diff --git a/apps/opencs/view/world/subviews.hpp b/apps/opencs/view/world/subviews.hpp new file mode 100644 index 0000000000..51e4cb0830 --- /dev/null +++ b/apps/opencs/view/world/subviews.hpp @@ -0,0 +1,14 @@ +#ifndef CSV_WORLD_SUBVIEWS_H +#define CSV_WORLD_SUBVIEWS_H + +namespace CSVDoc +{ + class SubViewFactoryManager; +} + +namespace CSVWorld +{ + void addSubViewFactories (CSVDoc::SubViewFactoryManager& manager); +} + +#endif diff --git a/apps/opencs/view/world/tablesubview.cpp b/apps/opencs/view/world/tablesubview.cpp index 3bc555a2d0..4abd531d01 100644 --- a/apps/opencs/view/world/tablesubview.cpp +++ b/apps/opencs/view/world/tablesubview.cpp @@ -1,13 +1,15 @@ #include "tablesubview.hpp" +#include "../../model/doc/document.hpp" + #include "table.hpp" -CSVWorld::TableSubView::TableSubView (const CSMWorld::UniversalId& id, CSMWorld::Data& data, QUndoStack& undoStack, +CSVWorld::TableSubView::TableSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document, bool createAndDelete) : SubView (id) { - setWidget (mTable = new Table (id, data, undoStack, createAndDelete)); + setWidget (mTable = new Table (id, document.getData(), document.getUndoStack(), createAndDelete)); } void CSVWorld::TableSubView::setEditLock (bool locked) diff --git a/apps/opencs/view/world/tablesubview.hpp b/apps/opencs/view/world/tablesubview.hpp index b45b3c2796..9b0785c470 100644 --- a/apps/opencs/view/world/tablesubview.hpp +++ b/apps/opencs/view/world/tablesubview.hpp @@ -1,22 +1,26 @@ #ifndef CSV_WORLD_TABLESUBVIEW_H #define CSV_WORLD_TABLESUBVIEW_H -#include "subview.hpp" +#include "../doc/subview.hpp" class QUndoStack; +namespace CSMDoc +{ + class Document; +} + namespace CSVWorld { class Table; - class TableSubView : public SubView + class TableSubView : public CSVDoc::SubView { Table *mTable; public: - TableSubView (const CSMWorld::UniversalId& id, CSMWorld::Data& data, QUndoStack& undoStack, - bool createAndDelete); + TableSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document, bool createAndDelete); virtual void setEditLock (bool locked); };