Improve start script creation (Fixes #3024)

Improved start script creation by updating input to be a drop target
and adding auto-completion.
coverity_scan
Rob Cutmore 9 years ago
parent 07a4c0bf73
commit b93b97575e

@ -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 <QLabel>
#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<CSMWorld::IdTable&> (
*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 += "<br>";
}
errors += "No Script ID entered";
}
else if (getData().getScripts().searchId(scriptId) == -1)
{
if (!errors.empty())
{
errors += "<br>";
}
errors += "Script ID not found";
}
else if (getData().getStartScripts().searchId(scriptId) > -1)
{
if (!errors.empty())
{
errors += "<br>";
}
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()
);
}

@ -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

@ -52,7 +52,7 @@ void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager)
new CSVDoc::SubViewFactoryWithCreator<TableSubView, CreatorFactory<GenericCreator> >);
manager.add (CSMWorld::UniversalId::Type_StartScripts,
new CSVDoc::SubViewFactoryWithCreator<TableSubView, CreatorFactory<StartScriptCreator> >);
new CSVDoc::SubViewFactoryWithCreator<TableSubView, StartScriptCreatorFactory>);
manager.add (CSMWorld::UniversalId::Type_Cells,
new CSVDoc::SubViewFactoryWithCreator<TableSubView, CreatorFactory<CellCreator> >);
@ -136,8 +136,7 @@ void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager)
CreatorFactory<GenericCreator> > (false));
manager.add (CSMWorld::UniversalId::Type_StartScript,
new CSVDoc::SubViewFactoryWithCreator<DialogueSubView,
CreatorFactory<StartScriptCreator> > (false));
new CSVDoc::SubViewFactoryWithCreator<DialogueSubView, StartScriptCreatorFactory>(false));
manager.add (CSMWorld::UniversalId::Type_Skill,
new CSVDoc::SubViewFactoryWithCreator<DialogueSubView, NullCreatorFactory > (false));
@ -177,7 +176,7 @@ void CSVWorld::addSubViewFactories (CSVDoc::SubViewFactoryManager& manager)
manager.add (CSMWorld::UniversalId::Type_MetaData,
new CSVDoc::SubViewFactory<SimpleDialogueSubView >);
//preview
manager.add (CSMWorld::UniversalId::Type_Preview, new CSVDoc::SubViewFactory<PreviewSubView>);
}

Loading…
Cancel
Save