Editor: use combo box when creating pathgrids

Instead of using QLineEdit for user input, use a QComboBox populated
with valid choices. This prevents user from being able to create a
pathgrid for a non-existent cell.
This commit is contained in:
Rob Cutmore 2017-03-18 07:49:46 -04:00
parent f151eccc23
commit 95d164a6e6
2 changed files with 61 additions and 6 deletions

View file

@ -1,28 +1,74 @@
#include "pathgridcreator.hpp" #include "pathgridcreator.hpp"
#include <QComboBox>
#include <QLabel>
#include <QSortFilterProxyModel>
#include <QStringListModel>
#include "../../model/world/data.hpp" #include "../../model/world/data.hpp"
std::string CSVWorld::PathgridCreator::getId() const
{
return mCell->currentText().toUtf8().constData();
}
CSVWorld::PathgridCreator::PathgridCreator( CSVWorld::PathgridCreator::PathgridCreator(
CSMWorld::Data& data, CSMWorld::Data& data,
QUndoStack& undoStack, QUndoStack& undoStack,
const CSMWorld::UniversalId& id, const CSMWorld::UniversalId& id,
bool relaxedIdRules bool relaxedIdRules
) : GenericCreator(data, undoStack, id, relaxedIdRules) ) : GenericCreator(data, undoStack, id, relaxedIdRules)
{} {
setManualEditing(false);
QLabel *label = new QLabel("Cell ID", this);
insertBeforeButtons(label, false);
// Create combo box with case-insensitive sorting.
mCell = new QComboBox(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel;
QStringListModel *listModel = new QStringListModel;
proxyModel->setSourceModel(listModel);
proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
mCell->setModel(proxyModel);
insertBeforeButtons(mCell, true);
// Populate combo box with cells that don't have a pathgrid yet.
const CSMWorld::IdCollection<CSMWorld::Pathgrid>& pathgrids = getData().getPathgrids();
const CSMWorld::IdCollection<CSMWorld::Cell>& cells = getData().getCells();
const int cellCount = cells.getSize();
for (int i = 0; i < cellCount; ++i)
{
std::string cellId = cells.getId(i);
if (pathgrids.searchId(cellId) == -1)
{
mCell->addItem(QString::fromStdString(cellId));
}
}
mCell->model()->sort(0);
mCell->setCurrentIndex(0);
}
std::string CSVWorld::PathgridCreator::getErrors() const std::string CSVWorld::PathgridCreator::getErrors() const
{ {
std::string pathgridId = getId(); std::string cellId = getId();
// Check user input for any errors. // Check user input for any errors.
// The last two checks, cell with existing pathgrid and non-existent cell,
// shouldn't be needed but we absolutely want to make sure they never happen.
std::string errors; std::string errors;
if (pathgridId.empty()) if (cellId.empty())
{ {
errors = "No Pathgrid ID entered"; errors = "No cell ID selected";
} }
else if (getData().getPathgrids().searchId(pathgridId) > -1) else if (getData().getPathgrids().searchId(cellId) > -1)
{ {
errors = "Pathgrid with this ID already exists"; errors = "Pathgrid for selected cell ID already exists";
}
else if (getData().getCells().searchId(cellId) == -1)
{
errors = "Cell with selected cell ID does not exist";
} }
return errors; return errors;

View file

@ -1,6 +1,8 @@
#ifndef PATHGRIDCREATOR_HPP #ifndef PATHGRIDCREATOR_HPP
#define PATHGRIDCREATOR_HPP #define PATHGRIDCREATOR_HPP
class QComboBox;
#include "genericcreator.hpp" #include "genericcreator.hpp"
namespace CSVWorld namespace CSVWorld
@ -10,6 +12,13 @@ namespace CSVWorld
{ {
Q_OBJECT Q_OBJECT
QComboBox *mCell;
private:
/// \return Cell ID selected by user.
virtual std::string getId() const;
public: public:
PathgridCreator( PathgridCreator(