forked from mirror/openmw-tes3mp
added delegate factory
This commit is contained in:
parent
f19fbaa293
commit
eb6590f7d8
5 changed files with 118 additions and 2 deletions
|
@ -6,6 +6,8 @@
|
|||
#include "../../model/doc/documentmanager.hpp"
|
||||
#include "../../model/doc/document.hpp"
|
||||
|
||||
#include "../world/util.hpp"
|
||||
|
||||
#include "view.hpp"
|
||||
|
||||
void CSVDoc::ViewManager::updateIndices()
|
||||
|
@ -29,11 +31,13 @@ void CSVDoc::ViewManager::updateIndices()
|
|||
CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager)
|
||||
: mDocumentManager (documentManager)
|
||||
{
|
||||
|
||||
mDelegateFactories = new CSVWorld::CommandDelegateFactoryCollection;
|
||||
}
|
||||
|
||||
CSVDoc::ViewManager::~ViewManager()
|
||||
{
|
||||
delete mDelegateFactories;
|
||||
|
||||
for (std::vector<View *>::iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
|
||||
delete *iter;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,11 @@ namespace CSMDoc
|
|||
class DocumentManager;
|
||||
}
|
||||
|
||||
namespace CSVWorld
|
||||
{
|
||||
class CommandDelegateFactoryCollection;
|
||||
}
|
||||
|
||||
namespace CSVDoc
|
||||
{
|
||||
class View;
|
||||
|
@ -21,6 +26,7 @@ namespace CSVDoc
|
|||
|
||||
CSMDoc::DocumentManager& mDocumentManager;
|
||||
std::vector<View *> mViews;
|
||||
CSVWorld::CommandDelegateFactoryCollection *mDelegateFactories;
|
||||
|
||||
// not implemented
|
||||
ViewManager (const ViewManager&);
|
||||
|
|
|
@ -102,7 +102,12 @@ CSVWorld::Table::Table (const CSMWorld::UniversalId& id, CSMWorld::Data& data, Q
|
|||
|
||||
if (flags & CSMWorld::ColumnBase::Flag_Table)
|
||||
{
|
||||
CommandDelegate *delegate = new CommandDelegate (undoStack, this);
|
||||
CSMWorld::ColumnBase::Display display = static_cast<CSMWorld::ColumnBase::Display> (
|
||||
mModel->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Display).toInt());
|
||||
|
||||
CommandDelegate *delegate = CommandDelegateFactoryCollection::get().makeDelegate (display,
|
||||
undoStack, this);
|
||||
|
||||
mDelegates.push_back (delegate);
|
||||
setItemDelegateForColumn (i, delegate);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
|
||||
#include "util.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
#include <QUndoStack>
|
||||
|
||||
#include "../../model/world/commands.hpp"
|
||||
|
@ -35,6 +37,57 @@ QVariant CSVWorld::NastyTableModelHack::getData() const
|
|||
return mData;
|
||||
}
|
||||
|
||||
|
||||
CSVWorld::CommandDelegateFactory::~CommandDelegateFactory() {}
|
||||
|
||||
|
||||
CSVWorld::CommandDelegateFactoryCollection *CSVWorld::CommandDelegateFactoryCollection::sThis = 0;
|
||||
|
||||
CSVWorld::CommandDelegateFactoryCollection::CommandDelegateFactoryCollection()
|
||||
{
|
||||
if (sThis)
|
||||
throw std::logic_error ("multiple instances of CSVWorld::CommandDelegateFactoryCollection");
|
||||
|
||||
sThis = this;
|
||||
}
|
||||
|
||||
CSVWorld::CommandDelegateFactoryCollection::~CommandDelegateFactoryCollection()
|
||||
{
|
||||
sThis = 0;
|
||||
|
||||
for (std::map<CSMWorld::ColumnBase::Display, CommandDelegateFactory *>::iterator iter (
|
||||
mFactories.begin());
|
||||
iter!=mFactories.end(); ++iter)
|
||||
delete iter->second;
|
||||
}
|
||||
|
||||
void CSVWorld::CommandDelegateFactoryCollection::add (CSMWorld::ColumnBase::Display display,
|
||||
CommandDelegateFactory *factory)
|
||||
{
|
||||
mFactories.insert (std::make_pair (display, factory));
|
||||
}
|
||||
|
||||
CSVWorld::CommandDelegate *CSVWorld::CommandDelegateFactoryCollection::makeDelegate (
|
||||
CSMWorld::ColumnBase::Display display, QUndoStack& undoStack, QObject *parent) const
|
||||
{
|
||||
std::map<CSMWorld::ColumnBase::Display, CommandDelegateFactory *>::const_iterator iter =
|
||||
mFactories.find (display);
|
||||
|
||||
if (iter!=mFactories.end())
|
||||
return iter->second->makeDelegate (undoStack, parent);
|
||||
|
||||
return new CommandDelegate (undoStack, parent);
|
||||
}
|
||||
|
||||
const CSVWorld::CommandDelegateFactoryCollection& CSVWorld::CommandDelegateFactoryCollection::get()
|
||||
{
|
||||
if (!sThis)
|
||||
throw std::logic_error ("no instance of CSVWorld::CommandDelegateFactoryCollection");
|
||||
|
||||
return *sThis;
|
||||
}
|
||||
|
||||
|
||||
CSVWorld::CommandDelegate::CommandDelegate (QUndoStack& undoStack, QObject *parent)
|
||||
: QStyledItemDelegate (parent), mUndoStack (undoStack), mEditLock (false)
|
||||
{}
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
#ifndef CSV_WORLD_UTIL_H
|
||||
#define CSV_WORLD_UTIL_H
|
||||
|
||||
#include <map>
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
#include "../../model/world/columnbase.hpp"
|
||||
|
||||
class QUndoStack;
|
||||
|
||||
namespace CSVWorld
|
||||
|
@ -31,6 +35,50 @@ namespace CSVWorld
|
|||
QVariant getData() const;
|
||||
};
|
||||
|
||||
class CommandDelegate;
|
||||
|
||||
class CommandDelegateFactory
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~CommandDelegateFactory();
|
||||
|
||||
virtual CommandDelegate *makeDelegate (QUndoStack& undoStack, QObject *parent) const = 0;
|
||||
///< The ownership of the returned CommandDelegate is transferred to the caller.
|
||||
};
|
||||
|
||||
class CommandDelegateFactoryCollection
|
||||
{
|
||||
static CommandDelegateFactoryCollection *sThis;
|
||||
std::map<CSMWorld::ColumnBase::Display, CommandDelegateFactory *> mFactories;
|
||||
|
||||
private:
|
||||
|
||||
// not implemented
|
||||
CommandDelegateFactoryCollection (const CommandDelegateFactoryCollection&);
|
||||
CommandDelegateFactoryCollection& operator= (const CommandDelegateFactoryCollection&);
|
||||
|
||||
public:
|
||||
|
||||
CommandDelegateFactoryCollection();
|
||||
|
||||
~CommandDelegateFactoryCollection();
|
||||
|
||||
void add (CSMWorld::ColumnBase::Display display, CommandDelegateFactory *factory);
|
||||
///< The ownership of \æ factory is transferred to *this.
|
||||
///
|
||||
/// This function must not be called more than once per value of \æ display.
|
||||
|
||||
CommandDelegate *makeDelegate (CSMWorld::ColumnBase::Display display, QUndoStack& undoStack,
|
||||
QObject *parent) const;
|
||||
///< The ownership of the returned CommandDelegate is transferred to the caller.
|
||||
///
|
||||
/// If no factory is registered for \a display, a CommandDelegate will be returned.
|
||||
|
||||
static const CommandDelegateFactoryCollection& get();
|
||||
|
||||
};
|
||||
|
||||
///< \brief Use commands instead of manipulating the model directly
|
||||
class CommandDelegate : public QStyledItemDelegate
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue