1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 13:23:55 +00:00
openmw/apps/opencs/model/world/idcompletionmanager.cpp

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

125 lines
5.4 KiB
C++
Raw Normal View History

2015-05-30 12:31:44 +00:00
#include "idcompletionmanager.hpp"
2022-10-19 17:02:00 +00:00
#include <QAbstractItemModel>
2015-05-30 12:31:44 +00:00
#include <QCompleter>
2022-10-19 17:02:00 +00:00
#include <algorithm>
#include <stdexcept>
#include <utility>
#include <apps/opencs/model/world/columnbase.hpp>
#include <apps/opencs/model/world/columns.hpp>
#include <apps/opencs/model/world/universalid.hpp>
#include "../../view/widget/completerpopup.hpp"
2015-05-30 12:31:44 +00:00
#include "data.hpp"
#include "idtablebase.hpp"
2022-10-19 17:02:00 +00:00
class QAbstractItemView;
2015-05-30 12:31:44 +00:00
namespace
{
std::map<CSMWorld::ColumnBase::Display, CSMWorld::UniversalId::Type> generateModelTypes()
2015-05-30 12:31:44 +00:00
{
std::map<CSMWorld::ColumnBase::Display, CSMWorld::UniversalId::Type> types;
2015-05-30 12:31:44 +00:00
types[CSMWorld::ColumnBase::Display_BodyPart] = CSMWorld::UniversalId::Type_BodyPart;
types[CSMWorld::ColumnBase::Display_Cell] = CSMWorld::UniversalId::Type_Cell;
types[CSMWorld::ColumnBase::Display_Class] = CSMWorld::UniversalId::Type_Class;
types[CSMWorld::ColumnBase::Display_CreatureLevelledList] = CSMWorld::UniversalId::Type_Referenceable;
types[CSMWorld::ColumnBase::Display_Creature] = CSMWorld::UniversalId::Type_Referenceable;
types[CSMWorld::ColumnBase::Display_Enchantment] = CSMWorld::UniversalId::Type_Enchantment;
types[CSMWorld::ColumnBase::Display_Faction] = CSMWorld::UniversalId::Type_Faction;
types[CSMWorld::ColumnBase::Display_GlobalVariable] = CSMWorld::UniversalId::Type_Global;
types[CSMWorld::ColumnBase::Display_Icon] = CSMWorld::UniversalId::Type_Icon;
types[CSMWorld::ColumnBase::Display_Journal] = CSMWorld::UniversalId::Type_Journal;
types[CSMWorld::ColumnBase::Display_Mesh] = CSMWorld::UniversalId::Type_Mesh;
types[CSMWorld::ColumnBase::Display_Miscellaneous] = CSMWorld::UniversalId::Type_Referenceable;
types[CSMWorld::ColumnBase::Display_Npc] = CSMWorld::UniversalId::Type_Referenceable;
types[CSMWorld::ColumnBase::Display_Race] = CSMWorld::UniversalId::Type_Race;
types[CSMWorld::ColumnBase::Display_Region] = CSMWorld::UniversalId::Type_Region;
types[CSMWorld::ColumnBase::Display_Referenceable] = CSMWorld::UniversalId::Type_Referenceable;
types[CSMWorld::ColumnBase::Display_Script] = CSMWorld::UniversalId::Type_Script;
types[CSMWorld::ColumnBase::Display_Skill] = CSMWorld::UniversalId::Type_Skill;
types[CSMWorld::ColumnBase::Display_Sound] = CSMWorld::UniversalId::Type_Sound;
types[CSMWorld::ColumnBase::Display_SoundRes] = CSMWorld::UniversalId::Type_SoundRes;
types[CSMWorld::ColumnBase::Display_Spell] = CSMWorld::UniversalId::Type_Spell;
types[CSMWorld::ColumnBase::Display_Static] = CSMWorld::UniversalId::Type_Referenceable;
types[CSMWorld::ColumnBase::Display_Texture] = CSMWorld::UniversalId::Type_Texture;
types[CSMWorld::ColumnBase::Display_Topic] = CSMWorld::UniversalId::Type_Topic;
types[CSMWorld::ColumnBase::Display_Weapon] = CSMWorld::UniversalId::Type_Referenceable;
2015-05-30 12:31:44 +00:00
return types;
}
typedef std::map<CSMWorld::ColumnBase::Display, CSMWorld::UniversalId::Type>::const_iterator ModelTypeConstIterator;
2015-05-30 12:31:44 +00:00
}
const std::map<CSMWorld::ColumnBase::Display, CSMWorld::UniversalId::Type>
2015-05-30 12:31:44 +00:00
CSMWorld::IdCompletionManager::sCompleterModelTypes = generateModelTypes();
std::vector<CSMWorld::ColumnBase::Display> CSMWorld::IdCompletionManager::getDisplayTypes()
{
std::vector<CSMWorld::ColumnBase::Display> types;
ModelTypeConstIterator current = sCompleterModelTypes.begin();
ModelTypeConstIterator end = sCompleterModelTypes.end();
for (; current != end; ++current)
{
types.push_back(current->first);
}
2016-02-22 20:48:25 +00:00
// Hack for Display_InfoCondVar
types.push_back(CSMWorld::ColumnBase::Display_InfoCondVar);
return types;
}
2015-05-30 12:31:44 +00:00
CSMWorld::IdCompletionManager::IdCompletionManager(CSMWorld::Data& data)
{
generateCompleters(data);
}
bool CSMWorld::IdCompletionManager::hasCompleterFor(CSMWorld::ColumnBase::Display display) const
2015-05-30 12:31:44 +00:00
{
return mCompleters.find(display) != mCompleters.end();
2015-05-30 12:31:44 +00:00
}
std::shared_ptr<QCompleter> CSMWorld::IdCompletionManager::getCompleter(CSMWorld::ColumnBase::Display display)
2015-05-30 12:31:44 +00:00
{
if (!hasCompleterFor(display))
2015-05-30 12:31:44 +00:00
{
throw std::logic_error("This column doesn't have an ID completer");
}
return mCompleters[display];
2015-05-30 12:31:44 +00:00
}
void CSMWorld::IdCompletionManager::generateCompleters(CSMWorld::Data& data)
{
ModelTypeConstIterator current = sCompleterModelTypes.begin();
ModelTypeConstIterator end = sCompleterModelTypes.end();
for (; current != end; ++current)
{
QAbstractItemModel* model = data.getTableModel(current->second);
CSMWorld::IdTableBase* table = dynamic_cast<CSMWorld::IdTableBase*>(model);
2018-10-09 06:21:12 +00:00
if (table != nullptr)
2015-05-30 12:31:44 +00:00
{
int idColumn = table->searchColumnIndex(CSMWorld::Columns::ColumnId_Id);
if (idColumn != -1)
{
std::shared_ptr<QCompleter> completer = std::make_shared<QCompleter>(table);
2015-05-30 12:31:44 +00:00
completer->setCompletionColumn(idColumn);
// The completion role must be Qt::DisplayRole to get the ID values from the model
completer->setCompletionRole(Qt::DisplayRole);
completer->setCaseSensitivity(Qt::CaseInsensitive);
QAbstractItemView* popup = new CSVWidget::CompleterPopup();
completer->setPopup(popup); // The completer takes ownership of the popup
completer->setMaxVisibleItems(10);
2016-02-22 20:48:25 +00:00
2015-05-30 12:31:44 +00:00
mCompleters[current->first] = completer;
}
}
}
}