From 00fcb79f082be9ecd7b6041d8ff576c1bc08c556 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sun, 28 Jul 2013 13:43:16 +0200 Subject: [PATCH] moved record creation to GenericCreator class (now taking ID from user instead of using a procedurally generated one) --- apps/opencs/view/world/creator.cpp | 2 +- apps/opencs/view/world/creator.hpp | 15 ++++-- apps/opencs/view/world/genericcreator.cpp | 66 ++++++++++++++++++++--- apps/opencs/view/world/genericcreator.hpp | 29 +++++++++- apps/opencs/view/world/table.cpp | 16 ------ apps/opencs/view/world/table.hpp | 2 - apps/opencs/view/world/tablebottombox.cpp | 5 +- apps/opencs/view/world/tablebottombox.hpp | 3 +- apps/opencs/view/world/tablesubview.cpp | 2 +- 9 files changed, 104 insertions(+), 36 deletions(-) diff --git a/apps/opencs/view/world/creator.cpp b/apps/opencs/view/world/creator.cpp index c0378151ed..d753a2b476 100644 --- a/apps/opencs/view/world/creator.cpp +++ b/apps/opencs/view/world/creator.cpp @@ -7,7 +7,7 @@ CSVWorld::CreatorFactoryBase::~CreatorFactoryBase() {} CSVWorld::Creator *CSVWorld::NullCreatorFactory::makeCreator (CSMWorld::Data& data, - QUndoStack& undoStack) const + QUndoStack& undoStack, const CSMWorld::UniversalId& id) const { return 0; } \ No newline at end of file diff --git a/apps/opencs/view/world/creator.hpp b/apps/opencs/view/world/creator.hpp index 9f586bcba6..564a5584e0 100644 --- a/apps/opencs/view/world/creator.hpp +++ b/apps/opencs/view/world/creator.hpp @@ -8,6 +8,7 @@ class QUndoStack; namespace CSMWorld { class Data; + class UniversalId; } namespace CSVWorld @@ -35,7 +36,8 @@ namespace CSVWorld virtual ~CreatorFactoryBase(); - virtual Creator *makeCreator (CSMWorld::Data& data, QUndoStack& undoStack) const = 0; + virtual Creator *makeCreator (CSMWorld::Data& data, QUndoStack& undoStack, + const CSMWorld::UniversalId& id) const = 0; ///< The ownership of the returned Creator is transferred to the caller. /// /// \note The function can return a 0-pointer, which means no UI for creating/deleting @@ -47,7 +49,8 @@ namespace CSVWorld { public: - virtual Creator *makeCreator (CSMWorld::Data& data, QUndoStack& undoStack) const; + virtual Creator *makeCreator (CSMWorld::Data& data, QUndoStack& undoStack, + const CSMWorld::UniversalId& id) const; ///< The ownership of the returned Creator is transferred to the caller. /// /// \note The function always returns 0. @@ -58,7 +61,8 @@ namespace CSVWorld { public: - virtual Creator *makeCreator (CSMWorld::Data& data, QUndoStack& undoStac) const; + virtual Creator *makeCreator (CSMWorld::Data& data, QUndoStack& undoStack, + const CSMWorld::UniversalId& id) const; ///< The ownership of the returned Creator is transferred to the caller. /// /// \note The function can return a 0-pointer, which means no UI for creating/deleting @@ -66,9 +70,10 @@ namespace CSVWorld }; template - Creator *CreatorFactory::makeCreator (CSMWorld::Data& data, QUndoStack& undoStack) const + Creator *CreatorFactory::makeCreator (CSMWorld::Data& data, QUndoStack& undoStack, + const CSMWorld::UniversalId& id) const { - return new CreatorT (data, undoStack); + return new CreatorT (data, undoStack, id); } } diff --git a/apps/opencs/view/world/genericcreator.cpp b/apps/opencs/view/world/genericcreator.cpp index 9a855fc4b9..10cbf9447b 100644 --- a/apps/opencs/view/world/genericcreator.cpp +++ b/apps/opencs/view/world/genericcreator.cpp @@ -4,26 +4,78 @@ #include #include #include +#include -CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack) +#include "../../model/world/commands.hpp" +#include "../../model/world/data.hpp" +#include "../../model/world/idtable.hpp" + +void CSVWorld::GenericCreator::update() +{ + mErrors = getErrors(); + + mCreate->setToolTip (QString::fromUtf8 (mErrors.c_str())); + + mCreate->setEnabled (mErrors.empty()); +} + +CSVWorld::GenericCreator::GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, + const CSMWorld::UniversalId& id) +: mData (data), mUndoStack (undoStack), mListId (id) { QHBoxLayout *layout = new QHBoxLayout; - QLineEdit *name = new QLineEdit; - layout->addWidget (name, 1); + mId = new QLineEdit; + layout->addWidget (mId, 1); - QPushButton *createButton = new QPushButton ("Create"); - layout->addWidget (createButton); + mCreate = new QPushButton ("Create"); + layout->addWidget (mCreate); QPushButton *cancelButton = new QPushButton ("Cancel"); layout->addWidget (cancelButton); - connect (cancelButton, SIGNAL (clicked (bool)), this, SIGNAL (done())); - setLayout (layout); + + connect (cancelButton, SIGNAL (clicked (bool)), this, SIGNAL (done())); + connect (mCreate, SIGNAL (clicked (bool)), this, SLOT (create())); + + connect (mId, SIGNAL (textChanged (const QString&)), this, SLOT (textChanged (const QString&))); } void CSVWorld::GenericCreator::reset() { + mId->setText (""); + update(); +} +std::string CSVWorld::GenericCreator::getErrors() const +{ + std::string errors; + + std::string id = mId->text().toUtf8().constData(); + + if (id.empty()) + { + errors = "Missing ID"; + } + else + { + + } + + return errors; +} + +void CSVWorld::GenericCreator::textChanged (const QString& text) +{ + update(); +} + +void CSVWorld::GenericCreator::create() +{ + mUndoStack.push (new CSMWorld::CreateCommand ( + dynamic_cast (*mData.getTableModel (mListId)), + mId->text().toUtf8().constData())); + + emit done(); } \ No newline at end of file diff --git a/apps/opencs/view/world/genericcreator.hpp b/apps/opencs/view/world/genericcreator.hpp index 553315ae34..a89b4c31f2 100644 --- a/apps/opencs/view/world/genericcreator.hpp +++ b/apps/opencs/view/world/genericcreator.hpp @@ -1,19 +1,46 @@ #ifndef CSV_WORLD_GENERICCREATOR_H #define CSV_WORLD_GENERICCREATOR_H +class QPushButton; +class QLineEdit; + #include "creator.hpp" +#include "../../model/world/universalid.hpp" + namespace CSVWorld { class GenericCreator : public Creator { Q_OBJECT + CSMWorld::Data& mData; + QUndoStack& mUndoStack; + CSMWorld::UniversalId mListId; + QPushButton *mCreate; + QLineEdit *mId; + std::string mErrors; + + private: + + void update(); + public: - GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack); + GenericCreator (CSMWorld::Data& data, QUndoStack& undoStack, + const CSMWorld::UniversalId& id); virtual void reset(); + + 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. + + private slots: + + void textChanged (const QString& text); + + void create(); }; } diff --git a/apps/opencs/view/world/table.cpp b/apps/opencs/view/world/table.cpp index 441828e69c..c35d7358a0 100644 --- a/apps/opencs/view/world/table.cpp +++ b/apps/opencs/view/world/table.cpp @@ -187,22 +187,6 @@ CSMWorld::UniversalId CSVWorld::Table::getUniversalId (int row) const mProxyModel->data (mProxyModel->index (row, 0)).toString().toStdString()); } -#include /// \todo remove -void CSVWorld::Table::createRecord() -{ - if (!mEditLock) - { - /// \todo ask the user for an ID instead. - static int index = 0; - - std::ostringstream stream; - stream << "id" << index++; - - mUndoStack.push (new CSMWorld::CreateCommand (*mProxyModel, stream.str())); - } - -} - void CSVWorld::Table::revertRecord() { if (!mEditLock) diff --git a/apps/opencs/view/world/table.hpp b/apps/opencs/view/world/table.hpp index 913f048894..2a59270b89 100644 --- a/apps/opencs/view/world/table.hpp +++ b/apps/opencs/view/world/table.hpp @@ -71,8 +71,6 @@ namespace CSVWorld private slots: - void createRecord(); - void revertRecord(); void deleteRecord(); diff --git a/apps/opencs/view/world/tablebottombox.cpp b/apps/opencs/view/world/tablebottombox.cpp index ae2b3d9208..f796d89600 100644 --- a/apps/opencs/view/world/tablebottombox.cpp +++ b/apps/opencs/view/world/tablebottombox.cpp @@ -40,7 +40,7 @@ void CSVWorld::TableBottomBox::updateStatus() } CSVWorld::TableBottomBox::TableBottomBox (const CreatorFactoryBase& creatorFactory, - CSMWorld::Data& data, QUndoStack& undoStack, QWidget *parent) + CSMWorld::Data& data, QUndoStack& undoStack, const CSMWorld::UniversalId& id, QWidget *parent) : QWidget (parent), mShowStatusBar (false), mCreating (false) { for (int i=0; i<4; ++i) @@ -60,7 +60,7 @@ CSVWorld::TableBottomBox::TableBottomBox (const CreatorFactoryBase& creatorFacto setLayout (mLayout); - mCreator = creatorFactory.makeCreator (data, undoStack); + mCreator = creatorFactory.makeCreator (data, undoStack, id); mLayout->addWidget (mCreator); @@ -139,6 +139,7 @@ void CSVWorld::TableBottomBox::tableSizeChanged (int size, int deleted, int modi void CSVWorld::TableBottomBox::createRequest() { + mCreator->reset(); mLayout->setCurrentWidget (mCreator); setVisible (true); mCreating = true; diff --git a/apps/opencs/view/world/tablebottombox.hpp b/apps/opencs/view/world/tablebottombox.hpp index 50741167c1..62c552c93a 100644 --- a/apps/opencs/view/world/tablebottombox.hpp +++ b/apps/opencs/view/world/tablebottombox.hpp @@ -11,6 +11,7 @@ class QUndoStack; namespace CSMWorld { class Data; + class UniversalId; } namespace CSVWorld @@ -41,7 +42,7 @@ namespace CSVWorld public: TableBottomBox (const CreatorFactoryBase& creatorFactory, CSMWorld::Data& data, - QUndoStack& undoStack, QWidget *parent = 0); + QUndoStack& undoStack, const CSMWorld::UniversalId& id, QWidget *parent = 0); virtual ~TableBottomBox(); diff --git a/apps/opencs/view/world/tablesubview.cpp b/apps/opencs/view/world/tablesubview.cpp index a76042842d..8a828c9bd2 100644 --- a/apps/opencs/view/world/tablesubview.cpp +++ b/apps/opencs/view/world/tablesubview.cpp @@ -18,7 +18,7 @@ CSVWorld::TableSubView::TableSubView (const CSMWorld::UniversalId& id, CSMDoc::D layout->setContentsMargins (QMargins (0, 0, 0, 0)); layout->addWidget (mBottom = - new TableBottomBox (creatorFactory, document.getData(), document.getUndoStack(), this), 0); + new TableBottomBox (creatorFactory, document.getData(), document.getUndoStack(), id, this), 0); layout->insertWidget (0, mTable = new Table (id, document.getData(), document.getUndoStack(), mBottom->canCreateAndDelete()), 2);