1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 19:53:54 +00:00
openmw/apps/opencs/view/world/infocreator.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

162 lines
5.2 KiB
C++
Raw Normal View History

2013-11-08 10:51:59 +00:00
#include "infocreator.hpp"
#include <algorithm>
2022-10-19 17:02:00 +00:00
#include <memory>
2013-11-08 10:51:59 +00:00
#include <QLabel>
2022-10-19 17:02:00 +00:00
#include <QString>
2013-11-08 10:51:59 +00:00
#include <QUuid>
2022-10-19 17:02:00 +00:00
#include <apps/opencs/model/world/columnbase.hpp>
#include <apps/opencs/model/world/idcollection.hpp>
#include <apps/opencs/view/world/genericcreator.hpp>
#include <components/misc/strings/lower.hpp>
#include "../../model/doc/document.hpp"
2013-11-08 10:51:59 +00:00
#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"
2013-11-08 10:51:59 +00:00
#include "../widget/droplineedit.hpp"
2022-10-19 17:02:00 +00:00
class QUndoStack;
2013-11-08 10:51:59 +00:00
std::string CSVWorld::InfoCreator::getId() const
{
const std::string topic = mTopic->text().toStdString();
2013-11-08 10:51:59 +00:00
std::string unique = QUuid::createUuid().toByteArray().data();
unique.erase(std::remove(unique.begin(), unique.end(), '-'), unique.end());
unique = unique.substr(1, unique.size() - 2);
return topic + '#' + unique;
2013-11-08 10:51:59 +00:00
}
void CSVWorld::InfoCreator::configureCreateCommand(CSMWorld::CreateCommand& command) const
{
2017-08-24 17:51:53 +00:00
CSMWorld::IdTable& table = dynamic_cast<CSMWorld::IdTable&>(*getData().getTableModel(getCollectionId()));
2013-11-08 10:51:59 +00:00
CSMWorld::CloneCommand* cloneCommand = dynamic_cast<CSMWorld::CloneCommand*>(&command);
2017-08-24 17:51:53 +00:00
if (getCollectionId() == CSMWorld::UniversalId::Type_TopicInfos)
{
if (!cloneCommand)
{
command.addValue(table.findColumnIndex(CSMWorld::Columns::ColumnId_Topic), mTopic->text());
command.addValue(table.findColumnIndex(CSMWorld::Columns::ColumnId_Rank), -1);
command.addValue(table.findColumnIndex(CSMWorld::Columns::ColumnId_Gender), -1);
command.addValue(table.findColumnIndex(CSMWorld::Columns::ColumnId_PcRank), -1);
}
else
{
cloneCommand->setOverrideValue(table.findColumnIndex(CSMWorld::Columns::ColumnId_Topic), mTopic->text());
}
2017-08-24 17:51:53 +00:00
}
else
{
if (!cloneCommand)
{
command.addValue(table.findColumnIndex(CSMWorld::Columns::ColumnId_Journal), mTopic->text());
}
else
cloneCommand->setOverrideValue(table.findColumnIndex(CSMWorld::Columns::ColumnId_Journal), mTopic->text());
2017-08-24 17:51:53 +00:00
}
2013-11-08 10:51:59 +00:00
}
CSVWorld::InfoCreator::InfoCreator(CSMWorld::Data& data, QUndoStack& undoStack, const CSMWorld::UniversalId& id,
CSMWorld::IdCompletionManager& completionManager)
2013-11-08 10:51:59 +00:00
: GenericCreator(data, undoStack, id)
{
// Determine if we're dealing with topics or journals.
CSMWorld::ColumnBase::Display displayType = CSMWorld::ColumnBase::Display_Topic;
QString labelText = "Topic";
if (getCollectionId().getType() == CSMWorld::UniversalId::Type_JournalInfos)
{
displayType = CSMWorld::ColumnBase::Display_Journal;
labelText = "Journal";
}
QLabel* label = new QLabel(labelText, this);
insertBeforeButtons(label, false);
// Add topic/journal ID input with auto-completion.
// Only existing topic/journal IDs are accepted so no ID validation is performed.
mTopic = new CSVWidget::DropLineEdit(displayType, this);
mTopic->setCompleter(completionManager.getCompleter(displayType).get());
2013-11-08 10:51:59 +00:00
insertBeforeButtons(mTopic, true);
setManualEditing(false);
connect(mTopic, &CSVWidget::DropLineEdit::textChanged, this, &InfoCreator::topicChanged);
connect(mTopic, &CSVWidget::DropLineEdit::returnPressed, this, &InfoCreator::inputReturnPressed);
2013-11-08 10:51:59 +00:00
}
void CSVWorld::InfoCreator::cloneMode(const std::string& originId, const CSMWorld::UniversalId::Type type)
{
CSMWorld::IdTable& infoTable = dynamic_cast<CSMWorld::IdTable&>(*getData().getTableModel(getCollectionId()));
int topicColumn = infoTable.findColumnIndex(getCollectionId().getType() == CSMWorld::UniversalId::Type_TopicInfos
? CSMWorld::Columns::ColumnId_Topic
: CSMWorld::Columns::ColumnId_Journal);
mTopic->setText(infoTable.data(infoTable.getModelIndex(originId, topicColumn)).toString());
GenericCreator::cloneMode(originId, type);
}
2013-11-08 10:51:59 +00:00
void CSVWorld::InfoCreator::reset()
{
mTopic->setText("");
GenericCreator::reset();
}
void CSVWorld::InfoCreator::setText(const std::string& text)
{
QString qText = QString::fromStdString(text);
mTopic->setText(qText);
}
2013-11-08 10:51:59 +00:00
std::string CSVWorld::InfoCreator::getErrors() const
{
// We ignore errors from GenericCreator here, because they can never happen in an InfoCreator.
std::string errors;
const ESM::RefId topic = ESM::RefId::stringRefId(mTopic->text().toStdString());
2013-11-08 10:51:59 +00:00
if ((getCollectionId().getType() == CSMWorld::UniversalId::Type_TopicInfos ? getData().getTopics()
: getData().getJournals())
.searchId(topic)
== -1)
{
errors += "Invalid Topic ID";
}
return errors;
}
void CSVWorld::InfoCreator::focus()
{
mTopic->setFocus();
}
void CSVWorld::InfoCreator::callReturnPressed()
{
emit inputReturnPressed();
}
2013-11-08 10:51:59 +00:00
void CSVWorld::InfoCreator::topicChanged()
{
update();
2015-03-11 14:54:45 +00:00
}
CSVWorld::Creator* CSVWorld::InfoCreatorFactory::makeCreator(
CSMDoc::Document& document, const CSMWorld::UniversalId& id) const
{
return new InfoCreator(document.getData(), document.getUndoStack(), id, document.getIdCompletionManager());
}