forked from mirror/openmw-tes3mp
Merge remote-tracking branch 'upstream/master'
commit
c9a8d53240
@ -0,0 +1,110 @@
|
||||
#include "idcompletionmanager.hpp"
|
||||
|
||||
#include <boost/make_shared.hpp>
|
||||
|
||||
#include <QCompleter>
|
||||
|
||||
#include "../../view/widget/completerpopup.hpp"
|
||||
|
||||
#include "data.hpp"
|
||||
#include "idtablebase.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
std::map<CSMWorld::ColumnBase::Display, CSMWorld::UniversalId::Type> generateModelTypes()
|
||||
{
|
||||
std::map<CSMWorld::ColumnBase::Display, CSMWorld::UniversalId::Type> types;
|
||||
|
||||
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_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_Weapon ] = CSMWorld::UniversalId::Type_Referenceable;
|
||||
|
||||
return types;
|
||||
}
|
||||
|
||||
typedef std::map<CSMWorld::ColumnBase::Display,
|
||||
CSMWorld::UniversalId::Type>::const_iterator ModelTypeConstIterator;
|
||||
}
|
||||
|
||||
const std::map<CSMWorld::ColumnBase::Display, CSMWorld::UniversalId::Type>
|
||||
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);
|
||||
}
|
||||
return types;
|
||||
}
|
||||
|
||||
CSMWorld::IdCompletionManager::IdCompletionManager(CSMWorld::Data &data)
|
||||
{
|
||||
generateCompleters(data);
|
||||
}
|
||||
|
||||
bool CSMWorld::IdCompletionManager::hasCompleterFor(CSMWorld::ColumnBase::Display display) const
|
||||
{
|
||||
return mCompleters.find(display) != mCompleters.end();
|
||||
}
|
||||
|
||||
boost::shared_ptr<QCompleter> CSMWorld::IdCompletionManager::getCompleter(CSMWorld::ColumnBase::Display display)
|
||||
{
|
||||
if (!hasCompleterFor(display))
|
||||
{
|
||||
throw std::logic_error("This column doesn't have an ID completer");
|
||||
}
|
||||
return mCompleters[display];
|
||||
}
|
||||
|
||||
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);
|
||||
if (table != NULL)
|
||||
{
|
||||
int idColumn = table->searchColumnIndex(CSMWorld::Columns::ColumnId_Id);
|
||||
if (idColumn != -1)
|
||||
{
|
||||
boost::shared_ptr<QCompleter> completer = boost::make_shared<QCompleter>(table);
|
||||
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);
|
||||
|
||||
mCompleters[current->first] = completer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
#ifndef CSM_WORLD_IDCOMPLETIONMANAGER_HPP
|
||||
#define CSM_WORLD_IDCOMPLETIONMANAGER_HPP
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
#include "columnbase.hpp"
|
||||
#include "universalid.hpp"
|
||||
|
||||
class QCompleter;
|
||||
|
||||
namespace CSMWorld
|
||||
{
|
||||
class Data;
|
||||
|
||||
/// \brief Creates and stores all ID completers
|
||||
class IdCompletionManager
|
||||
{
|
||||
static const std::map<ColumnBase::Display, UniversalId::Type> sCompleterModelTypes;
|
||||
|
||||
std::map<ColumnBase::Display, boost::shared_ptr<QCompleter> > mCompleters;
|
||||
|
||||
// Don't allow copying
|
||||
IdCompletionManager(const IdCompletionManager &);
|
||||
IdCompletionManager &operator = (const IdCompletionManager &);
|
||||
|
||||
void generateCompleters(Data &data);
|
||||
|
||||
public:
|
||||
static std::vector<ColumnBase::Display> getDisplayTypes();
|
||||
|
||||
IdCompletionManager(Data &data);
|
||||
|
||||
bool hasCompleterFor(ColumnBase::Display display) const;
|
||||
boost::shared_ptr<QCompleter> getCompleter(ColumnBase::Display display);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,28 @@
|
||||
#include "completerpopup.hpp"
|
||||
|
||||
CSVWidget::CompleterPopup::CompleterPopup(QWidget *parent)
|
||||
: QListView(parent)
|
||||
{
|
||||
setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
}
|
||||
|
||||
int CSVWidget::CompleterPopup::sizeHintForRow(int row) const
|
||||
{
|
||||
if (model() == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
if (row < 0 || row >= model()->rowCount())
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
ensurePolished();
|
||||
QModelIndex index = model()->index(row, modelColumn());
|
||||
QStyleOptionViewItem option = viewOptions();
|
||||
QAbstractItemDelegate *delegate = itemDelegate(index);
|
||||
return delegate->sizeHint(option, index).height();
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#ifndef CSV_WIDGET_COMPLETERPOPUP_HPP
|
||||
#define CSV_WIDGET_COMPLETERPOPUP_HPP
|
||||
|
||||
#include <QListView>
|
||||
|
||||
namespace CSVWidget
|
||||
{
|
||||
class CompleterPopup : public QListView
|
||||
{
|
||||
public:
|
||||
CompleterPopup(QWidget *parent = 0);
|
||||
|
||||
virtual int sizeHintForRow(int row) const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,39 @@
|
||||
#include "idcompletiondelegate.hpp"
|
||||
|
||||
#include "../../model/world/idcompletionmanager.hpp"
|
||||
|
||||
CSVWorld::IdCompletionDelegate::IdCompletionDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document& document,
|
||||
QObject *parent)
|
||||
: CommandDelegate(dispatcher, document, parent)
|
||||
{}
|
||||
|
||||
QWidget *CSVWorld::IdCompletionDelegate::createEditor(QWidget *parent,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
return createEditor(parent, option, index, getDisplayTypeFromIndex(index));
|
||||
}
|
||||
|
||||
QWidget *CSVWorld::IdCompletionDelegate::createEditor(QWidget *parent,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index,
|
||||
CSMWorld::ColumnBase::Display display) const
|
||||
{
|
||||
if (!index.data(Qt::EditRole).isValid() && !index.data(Qt::DisplayRole).isValid())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CSMWorld::IdCompletionManager &completionManager = getDocument().getIdCompletionManager();
|
||||
DropLineEdit *editor = new DropLineEdit(parent);
|
||||
editor->setCompleter(completionManager.getCompleter(display).get());
|
||||
return editor;
|
||||
}
|
||||
|
||||
CSVWorld::CommandDelegate *CSVWorld::IdCompletionDelegateFactory::makeDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document& document,
|
||||
QObject *parent) const
|
||||
{
|
||||
return new IdCompletionDelegate(dispatcher, document, parent);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
#ifndef CSV_WORLD_IDCOMPLETIONDELEGATE_HPP
|
||||
#define CSV_WORLD_IDCOMPLETIONDELEGATE_HPP
|
||||
|
||||
#include "util.hpp"
|
||||
|
||||
namespace CSVWorld
|
||||
{
|
||||
/// \brief Enables the Id completion for a column
|
||||
class IdCompletionDelegate : public CommandDelegate
|
||||
{
|
||||
public:
|
||||
IdCompletionDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document& document,
|
||||
QObject *parent);
|
||||
|
||||
virtual QWidget *createEditor (QWidget *parent,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
|
||||
virtual QWidget *createEditor (QWidget *parent,
|
||||
const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index,
|
||||
CSMWorld::ColumnBase::Display display) const;
|
||||
};
|
||||
|
||||
class IdCompletionDelegateFactory : public CommandDelegateFactory
|
||||
{
|
||||
public:
|
||||
virtual CommandDelegate *makeDelegate(CSMWorld::CommandDispatcher *dispatcher,
|
||||
CSMDoc::Document& document,
|
||||
QObject *parent) const;
|
||||
///< The ownership of the returned CommandDelegate is transferred to the caller.
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue