diff --git a/apps/opencs/view/world/startscriptcreator.cpp b/apps/opencs/view/world/startscriptcreator.cpp index 69b1b3ff1..57ac34b8f 100644 --- a/apps/opencs/view/world/startscriptcreator.cpp +++ b/apps/opencs/view/world/startscriptcreator.cpp @@ -1,20 +1,130 @@ #include "startscriptcreator.hpp" -CSVWorld::StartScriptCreator::StartScriptCreator(CSMWorld::Data &data, QUndoStack &undoStack, const CSMWorld::UniversalId &id, bool relaxedIdRules): - GenericCreator (data, undoStack, id, true) -{} +#include + +#include "../../model/doc/document.hpp" + +#include "../../model/world/columns.hpp" +#include "../../model/world/commands.hpp" +#include "../../model/world/data.hpp" +#include "../../model/world/idcompletionmanager.hpp" +#include "../../model/world/idtable.hpp" + +#include "../widget/droplineedit.hpp" + +std::string CSVWorld::StartScriptCreator::getId() const +{ + return mScript->text().toUtf8().constData(); +} + +CSMWorld::IdTable& CSVWorld::StartScriptCreator::getStartScriptsTable() const +{ + return dynamic_cast ( + *getData().getTableModel(getCollectionId()) + ); +} + +void CSVWorld::StartScriptCreator::configureCreateCommand(CSMWorld::CreateCommand& command) const +{ + CSMWorld::IdTable& table = getStartScriptsTable(); + int column = table.findColumnIndex(CSMWorld::Columns::ColumnId_Id); + + // Set script ID to be added to start scripts table. + command.addValue(column, mScript->text()); +} + +CSVWorld::StartScriptCreator::StartScriptCreator( + CSMWorld::Data &data, + QUndoStack &undoStack, + const CSMWorld::UniversalId &id, + CSMWorld::IdCompletionManager& completionManager +) : GenericCreator(data, undoStack, id, true) +{ + setManualEditing(false); + + // Add script ID input label. + QLabel *label = new QLabel("Script ID", this); + insertBeforeButtons(label, false); + + // Add script ID input with auto-completion. + CSMWorld::ColumnBase::Display displayType = CSMWorld::ColumnBase::Display_Script; + mScript = new CSVWidget::DropLineEdit(displayType, this); + mScript->setCompleter(completionManager.getCompleter(displayType).get()); + insertBeforeButtons(mScript, true); + + connect(mScript, SIGNAL (textChanged(const QString&)), this, SLOT (scriptChanged())); +} + +void CSVWorld::StartScriptCreator::cloneMode( + const std::string& originId, + const CSMWorld::UniversalId::Type type) +{ + CSVWorld::GenericCreator::cloneMode(originId, type); + + // Look up cloned record in start scripts table and set script ID text. + CSMWorld::IdTable& table = getStartScriptsTable(); + int column = table.findColumnIndex(CSMWorld::Columns::ColumnId_Id); + mScript->setText(table.data(table.getModelIndex(originId, column)).toString()); +} std::string CSVWorld::StartScriptCreator::getErrors() const { - std::string errors; + std::string scriptId = getId(); - errors = getIdValidatorResult(); - if (errors.length() > 0) - return errors; - else if (getData().getScripts().searchId(getId()) == -1) - errors = "Script ID not found"; - else if (getData().getStartScripts().searchId(getId()) > -1 ) - errors = "Script with this ID already registered as Start Script"; + // Check user input for any errors. + std::string errors; + if (scriptId.empty()) + { + if (!errors.empty()) + { + errors += "
"; + } + errors += "No Script ID entered"; + } + else if (getData().getScripts().searchId(scriptId) == -1) + { + if (!errors.empty()) + { + errors += "
"; + } + errors += "Script ID not found"; + } + else if (getData().getStartScripts().searchId(scriptId) > -1) + { + if (!errors.empty()) + { + errors += "
"; + } + errors += "Script with this ID already registered as Start Script"; + } return errors; } + +void CSVWorld::StartScriptCreator::focus() +{ + mScript->setFocus(); +} + +void CSVWorld::StartScriptCreator::reset() +{ + CSVWorld::GenericCreator::reset(); + mScript->setText(""); +} + +void CSVWorld::StartScriptCreator::scriptChanged() +{ + update(); +} + +CSVWorld::Creator *CSVWorld::StartScriptCreatorFactory::makeCreator( + CSMDoc::Document& document, + const CSMWorld::UniversalId& id) const +{ + return new StartScriptCreator( + document.getData(), + document.getUndoStack(), + id, + document.getIdCompletionManager() + ); +} diff --git a/apps/opencs/view/world/startscriptcreator.hpp b/apps/opencs/view/world/startscriptcreator.hpp index 07fe8ff3d..745dce819 100644 --- a/apps/opencs/view/world/startscriptcreator.hpp +++ b/apps/opencs/view/world/startscriptcreator.hpp @@ -3,23 +3,77 @@ #include "genericcreator.hpp" -namespace CSVWorld { +namespace CSMWorld +{ + class IdCompletionManager; + class IdTable; +} + +namespace CSVWidget +{ + class DropLineEdit; +} +namespace CSVWorld +{ + /// \brief Record creator for start scripts. class StartScriptCreator : public GenericCreator { Q_OBJECT + CSVWidget::DropLineEdit *mScript; + + private: + + /// \return script ID entered by user. + virtual std::string getId() const; + + /// \return reference to table containing start scripts. + CSMWorld::IdTable& getStartScriptsTable() const; + + /// \brief Add user input to command for creating start script. + /// \param command Creation command to configure. + virtual void configureCreateCommand(CSMWorld::CreateCommand& command) const; + public: - StartScriptCreator(CSMWorld::Data& data, QUndoStack& undoStack, - const CSMWorld::UniversalId& id, bool relaxedIdRules = false); + StartScriptCreator( + CSMWorld::Data& data, + QUndoStack& undoStack, + const CSMWorld::UniversalId& id, + CSMWorld::IdCompletionManager& completionManager); + + /// \brief Set script ID input widget to ID of record to be cloned. + /// \param originId Script ID to be cloned. + /// \param type Type of record to be cloned. + virtual void cloneMode( + const std::string& originId, + const CSMWorld::UniversalId::Type type); + + /// \return Formatted error descriptions for current user input. 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. - }; -} + /// \brief Set focus to script ID input widget. + virtual void focus(); + + /// \brief Clear script ID input widget. + virtual void reset(); + private slots: + + /// \brief Check user input for any errors. + void scriptChanged(); + }; + /// \brief Creator factory for start script record creator. + class StartScriptCreatorFactory : public CreatorFactoryBase + { + public: + + virtual Creator *makeCreator( + CSMDoc::Document& document, + const CSMWorld::UniversalId& id) const; + }; +} #endif // STARTSCRIPTCREATOR_HPP diff --git a/apps/opencs/view/world/subviews.cpp b/apps/opencs/view/world/subviews.cpp index 88375caaf..650f344ed 100644 --- a/apps/opencs/view/world/subviews.cpp +++ b/apps/opencs/view/world/subviews.cpp @@ -52,7 +52,7 @@ void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager) new CSVDoc::SubViewFactoryWithCreator >); manager.add (CSMWorld::UniversalId::Type_StartScripts, - new CSVDoc::SubViewFactoryWithCreator >); + new CSVDoc::SubViewFactoryWithCreator); manager.add (CSMWorld::UniversalId::Type_Cells, new CSVDoc::SubViewFactoryWithCreator >); @@ -136,8 +136,7 @@ void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager) CreatorFactory > (false)); manager.add (CSMWorld::UniversalId::Type_StartScript, - new CSVDoc::SubViewFactoryWithCreator > (false)); + new CSVDoc::SubViewFactoryWithCreator(false)); manager.add (CSMWorld::UniversalId::Type_Skill, new CSVDoc::SubViewFactoryWithCreator (false)); @@ -177,7 +176,7 @@ void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager) manager.add (CSMWorld::UniversalId::Type_MetaData, new CSVDoc::SubViewFactory); - + //preview manager.add (CSMWorld::UniversalId::Type_Preview, new CSVDoc::SubViewFactory); }