Merge branch 'globaltype' into next

This commit is contained in:
Marc Zinnschlag 2013-02-17 17:27:41 +01:00
commit bb990b96bf
12 changed files with 459 additions and 10 deletions

View file

@ -59,7 +59,7 @@ opencs_units (view/world
)
opencs_units_noqt (view/world
dialoguesubview util subviews
dialoguesubview util subviews enumdelegate vartypedelegate
)

View file

@ -29,7 +29,8 @@ namespace CSMWorld
Display_String,
Display_Integer,
Display_Float,
Display_Var
Display_Var,
Display_VarType
};
std::string mTitle;

View file

@ -79,7 +79,7 @@ namespace CSMWorld
int mType;
FixedRecordTypeColumn (int type)
: Column<ESXRecordT> ("Type", ColumnBase::Display_Integer, 0), mType (type) {}
: Column<ESXRecordT> ("Record Type", ColumnBase::Display_Integer, 0), mType (type) {}
virtual QVariant get (const Record<ESXRecordT>& record) const
{
@ -92,10 +92,11 @@ namespace CSMWorld
}
};
/// \attention A var type column must be immediately followed by a suitable value column.
template<typename ESXRecordT>
struct VarTypeColumn : public Column<ESXRecordT>
{
VarTypeColumn() : Column<ESXRecordT> ("Type", ColumnBase::Display_Integer) {}
VarTypeColumn() : Column<ESXRecordT> ("Type", ColumnBase::Display_VarType) {}
virtual QVariant get (const Record<ESXRecordT>& record) const
{

View file

@ -6,6 +6,10 @@
#include "../../model/doc/documentmanager.hpp"
#include "../../model/doc/document.hpp"
#include "../world/util.hpp"
#include "../world/enumdelegate.hpp"
#include "../world/vartypedelegate.hpp"
#include "view.hpp"
void CSVDoc::ViewManager::updateIndices()
@ -29,11 +33,16 @@ void CSVDoc::ViewManager::updateIndices()
CSVDoc::ViewManager::ViewManager (CSMDoc::DocumentManager& documentManager)
: mDocumentManager (documentManager)
{
mDelegateFactories = new CSVWorld::CommandDelegateFactoryCollection;
mDelegateFactories->add (CSMWorld::ColumnBase::Display_VarType,
new CSVWorld::VarTypeDelegateFactory (ESM::VT_None, ESM::VT_String, ESM::VT_Int, ESM::VT_Float));
}
CSVDoc::ViewManager::~ViewManager()
{
delete mDelegateFactories;
for (std::vector<View *>::iterator iter (mViews.begin()); iter!=mViews.end(); ++iter)
delete *iter;
}

View file

@ -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&);

View file

@ -0,0 +1,101 @@
#include "enumdelegate.hpp"
#include <stdexcept>
#include <QComboBox>
#include <QApplication>
#include <QUndoStack>
#include "../../model/world/commands.hpp"
void CSVWorld::EnumDelegate::setModelDataImp (QWidget *editor, QAbstractItemModel *model,
const QModelIndex& index) const
{
if (QComboBox *comboBox = dynamic_cast<QComboBox *> (editor))
{
QString value = comboBox->currentText();
for (std::vector<std::pair<int, QString> >::const_iterator iter (mValues.begin());
iter!=mValues.end(); ++iter)
if (iter->second==value)
{
addCommands (model, index, iter->first);
break;
}
}
}
void CSVWorld::EnumDelegate::addCommands (QAbstractItemModel *model,
const QModelIndex& index, int type) const
{
getUndoStack().push (new CSMWorld::ModifyCommand (*model, index, type));
}
CSVWorld::EnumDelegate::EnumDelegate (const std::vector<std::pair<int, QString> >& values,
QUndoStack& undoStack, QObject *parent)
: CommandDelegate (undoStack, parent), mValues (values)
{
}
QWidget *CSVWorld::EnumDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QComboBox *comboBox = new QComboBox (parent);
for (std::vector<std::pair<int, QString> >::const_iterator iter (mValues.begin());
iter!=mValues.end(); ++iter)
comboBox->addItem (iter->second);
return comboBox;
}
void CSVWorld::EnumDelegate::setEditorData (QWidget *editor, const QModelIndex& index) const
{
if (QComboBox *comboBox = dynamic_cast<QComboBox *> (editor))
{
int value = index.data (Qt::EditRole).toInt();
std::size_t size = mValues.size();
for (std::size_t i=0; i<size; ++i)
if (mValues[i].first==value)
{
comboBox->setCurrentIndex (i);
break;
}
}
}
void CSVWorld::EnumDelegate::paint (QPainter *painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QStyleOptionViewItemV4 option2 (option);
int value = index.data().toInt();
for (std::vector<std::pair<int, QString> >::const_iterator iter (mValues.begin());
iter!=mValues.end(); ++iter)
if (iter->first==value)
{
option2.text = iter->second;
QApplication::style()->drawControl (QStyle::CE_ItemViewItem, &option2, painter);
break;
}
}
CSVWorld::CommandDelegate *CSVWorld::EnumDelegateFactory::makeDelegate (QUndoStack& undoStack,
QObject *parent) const
{
return new EnumDelegate (mValues, undoStack, parent);
}
void CSVWorld::EnumDelegateFactory::add (int value, const QString& name)
{
mValues.push_back (std::make_pair (value, name));
}

View file

@ -0,0 +1,57 @@
#ifndef CSV_WORLD_ENUMDELEGATE_H
#define CSV_WORLD_ENUMDELEGATE_H
#include <vector>
#include <QString>
#include <components/esm/defs.hpp>
#include "util.hpp"
namespace CSVWorld
{
/// \brief Integer value that represents an enum and is interacted with via a combobox
class EnumDelegate : public CommandDelegate
{
std::vector<std::pair<int, QString> > mValues;
private:
virtual void setModelDataImp (QWidget *editor, QAbstractItemModel *model,
const QModelIndex& index) const;
virtual void addCommands (QAbstractItemModel *model,
const QModelIndex& index, int type) const;
public:
EnumDelegate (const std::vector<std::pair<int, QString> >& values,
QUndoStack& undoStack, QObject *parent);
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
virtual void setEditorData (QWidget *editor, const QModelIndex& index) const;
virtual void paint (QPainter *painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
};
class EnumDelegateFactory : public CommandDelegateFactory
{
std::vector<std::pair<int, QString> > mValues;
public:
virtual CommandDelegate *makeDelegate (QUndoStack& undoStack, QObject *parent) const;
///< The ownership of the returned CommandDelegate is transferred to the caller.
void add (int value, const QString& name);
};
}
#endif

View file

@ -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);
}

View file

@ -1,6 +1,8 @@
#include "util.hpp"
#include <stdexcept>
#include <QUndoStack>
#include "../../model/world/commands.hpp"
@ -35,6 +37,70 @@ 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;
}
QUndoStack& CSVWorld::CommandDelegate::getUndoStack() const
{
return mUndoStack;
}
void CSVWorld::CommandDelegate::setModelDataImp (QWidget *editor, QAbstractItemModel *model,
const QModelIndex& index) const
{
NastyTableModelHack hack (*model);
QStyledItemDelegate::setModelData (editor, &hack, index);
mUndoStack.push (new CSMWorld::ModifyCommand (*model, index, hack.getData()));
}
CSVWorld::CommandDelegate::CommandDelegate (QUndoStack& undoStack, QObject *parent)
: QStyledItemDelegate (parent), mUndoStack (undoStack), mEditLock (false)
{}
@ -44,14 +110,18 @@ void CSVWorld::CommandDelegate::setModelData (QWidget *editor, QAbstractItemMode
{
if (!mEditLock)
{
NastyTableModelHack hack (*model);
QStyledItemDelegate::setModelData (editor, &hack, index);
mUndoStack.push (new CSMWorld::ModifyCommand (*model, index, hack.getData()));
setModelDataImp (editor, model, index);
}
///< \todo provide some kind of feedback to the user, indicating that editing is currently not possible.
}
void CSVWorld::CommandDelegate::setEditLock (bool locked)
void CSVWorld::CommandDelegate::setEditLock (bool locked)
{
mEditLock = locked;
}
bool CSVWorld::CommandDelegate::isEditLocked() const
{
return mEditLock;
}

View file

@ -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,19 +35,73 @@ 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
{
QUndoStack& mUndoStack;
bool mEditLock;
protected:
QUndoStack& getUndoStack() const;
virtual void setModelDataImp (QWidget *editor, QAbstractItemModel *model,
const QModelIndex& index) const;
public:
CommandDelegate (QUndoStack& undoStack, QObject *parent);
void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const;
virtual void setModelData (QWidget *editor, QAbstractItemModel *model,
const QModelIndex& index) const;
void setEditLock (bool locked);
bool isEditLocked() const;
};
}

View file

@ -0,0 +1,103 @@
#include "vartypedelegate.hpp"
#include <QUndoStack>
#include "../../model/world/commands.hpp"
void CSVWorld::VarTypeDelegate::addCommands (QAbstractItemModel *model, const QModelIndex& index, int type)
const
{
QModelIndex next = model->index (index.row(), index.column()+1);
QVariant old = model->data (next);
QVariant value;
switch (type)
{
case ESM::VT_Short:
case ESM::VT_Int:
case ESM::VT_Long:
value = old.toInt();
break;
case ESM::VT_Float:
value = old.toFloat();
break;
case ESM::VT_String:
value = old.toString();
break;
default: break; // ignore the rest
}
getUndoStack().beginMacro (
"Modify " + model->headerData (index.column(), Qt::Horizontal, Qt::DisplayRole).toString());
getUndoStack().push (new CSMWorld::ModifyCommand (*model, index, type));
getUndoStack().push (new CSMWorld::ModifyCommand (*model, next, value));
getUndoStack().endMacro();
}
CSVWorld::VarTypeDelegate::VarTypeDelegate (const std::vector<std::pair<int, QString> >& values,
QUndoStack& undoStack, QObject *parent)
: EnumDelegate (values, undoStack, parent)
{}
CSVWorld::VarTypeDelegateFactory::VarTypeDelegateFactory (ESM::VarType type0,
ESM::VarType type1, ESM::VarType type2, ESM::VarType type3)
{
if (type0!=ESM::VT_Unknown)
add (type0);
if (type1!=ESM::VT_Unknown)
add (type1);
if (type2!=ESM::VT_Unknown)
add (type2);
if (type3!=ESM::VT_Unknown)
add (type3);
}
CSVWorld::CommandDelegate *CSVWorld::VarTypeDelegateFactory::makeDelegate (QUndoStack& undoStack,
QObject *parent) const
{
return new VarTypeDelegate (mValues, undoStack, parent);
}
void CSVWorld::VarTypeDelegateFactory::add (ESM::VarType type)
{
struct Name
{
ESM::VarType mType;
const char *mName;
};
static const Name sNames[] =
{
{ ESM::VT_None, "empty" },
{ ESM::VT_Short, "short" },
{ ESM::VT_Int, "long" },
{ ESM::VT_Long, "long" },
{ ESM::VT_Float, "float" },
{ ESM::VT_String, "string" },
{ ESM::VT_Unknown, 0 } // end marker
};
for (int i=0; sNames[i].mName; ++i)
if (sNames[i].mType==type)
{
mValues.push_back (std::make_pair (type, sNames[i].mName));
return;
}
throw std::logic_error ("Unsupported variable type");
}

View file

@ -0,0 +1,38 @@
#ifndef CSV_WORLD_VARTYPEDELEGATE_H
#define CSV_WORLD_VARTYPEDELEGATE_H
#include "enumdelegate.hpp"
namespace CSVWorld
{
class VarTypeDelegate : public EnumDelegate
{
private:
virtual void addCommands (QAbstractItemModel *model,
const QModelIndex& index, int type) const;
public:
VarTypeDelegate (const std::vector<std::pair<int, QString> >& values,
QUndoStack& undoStack, QObject *parent);
};
class VarTypeDelegateFactory : public CommandDelegateFactory
{
std::vector<std::pair<int, QString> > mValues;
public:
VarTypeDelegateFactory (ESM::VarType type0 = ESM::VT_Unknown,
ESM::VarType type1 = ESM::VT_Unknown, ESM::VarType type2 = ESM::VT_Unknown,
ESM::VarType type3 = ESM::VT_Unknown);
virtual CommandDelegate *makeDelegate (QUndoStack& undoStack, QObject *parent) const;
///< The ownership of the returned CommandDelegate is transferred to the caller.
void add (ESM::VarType type);
};
}
#endif