2012-12-13 12:35:08 +00:00
|
|
|
|
|
|
|
#include "dialoguesubview.hpp"
|
|
|
|
|
2014-03-07 16:15:43 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <memory>
|
|
|
|
|
2012-12-30 13:01:52 +00:00
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QLabel>
|
2014-03-07 16:15:43 +00:00
|
|
|
#include <QSize>
|
2013-03-21 09:07:25 +00:00
|
|
|
#include <QAbstractItemModel>
|
2013-01-08 13:59:40 +00:00
|
|
|
#include <QDoubleSpinBox>
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QLineEdit>
|
2014-03-07 16:15:43 +00:00
|
|
|
#include <QEvent>
|
2013-01-08 13:59:40 +00:00
|
|
|
#include <QDataWidgetMapper>
|
2012-12-30 13:01:52 +00:00
|
|
|
|
|
|
|
#include "../../model/world/columnbase.hpp"
|
2013-01-19 13:29:14 +00:00
|
|
|
#include "../../model/world/idtable.hpp"
|
2014-03-08 14:27:43 +00:00
|
|
|
#include "../../model/world/columns.hpp"
|
2012-12-30 13:01:52 +00:00
|
|
|
|
2014-03-06 19:10:13 +00:00
|
|
|
#include "recordstatusdelegate.hpp"
|
|
|
|
#include "util.hpp"
|
|
|
|
|
2014-03-07 21:17:40 +00:00
|
|
|
/*
|
|
|
|
==============================DialogueDelegateDispatcherProxy==========================================
|
|
|
|
*/
|
2014-03-08 14:10:55 +00:00
|
|
|
CSVWorld::DialogueDelegateDispatcherProxy::refWrapper::refWrapper(const QModelIndex& index) :
|
2014-03-07 21:17:40 +00:00
|
|
|
mIndex(index)
|
|
|
|
{}
|
|
|
|
|
|
|
|
CSVWorld::DialogueDelegateDispatcherProxy::DialogueDelegateDispatcherProxy(QWidget* editor, CSMWorld::ColumnBase::Display display) :
|
|
|
|
mEditor(editor),
|
|
|
|
mDisplay(display),
|
|
|
|
mIndexWrapper(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::DialogueDelegateDispatcherProxy::editorDataCommited()
|
|
|
|
{
|
2014-03-08 15:50:42 +00:00
|
|
|
if (mIndexWrapper.get())
|
|
|
|
{
|
|
|
|
emit editorDataCommited(mEditor, mIndexWrapper->mIndex, mDisplay);
|
|
|
|
}
|
2014-03-07 21:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::DialogueDelegateDispatcherProxy::setIndex(const QModelIndex& index)
|
|
|
|
{
|
|
|
|
mIndexWrapper.reset(new refWrapper(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* CSVWorld::DialogueDelegateDispatcherProxy::getEditor() const
|
|
|
|
{
|
|
|
|
return mEditor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============================DialogueDelegateDispatcher==========================================
|
|
|
|
*/
|
|
|
|
|
2014-03-07 16:15:43 +00:00
|
|
|
CSVWorld::DialogueDelegateDispatcher::DialogueDelegateDispatcher(QObject* parent, CSMWorld::IdTable* table, QUndoStack& undoStack) :
|
|
|
|
mParent(parent),
|
|
|
|
mTable(table),
|
|
|
|
mUndoStack(undoStack)
|
2014-03-07 21:17:40 +00:00
|
|
|
{
|
|
|
|
}
|
2014-03-07 16:15:43 +00:00
|
|
|
|
|
|
|
CSVWorld::CommandDelegate* CSVWorld::DialogueDelegateDispatcher::makeDelegate(CSMWorld::ColumnBase::Display display)
|
|
|
|
{
|
|
|
|
CommandDelegate *delegate = NULL;
|
|
|
|
std::map<int, CommandDelegate*>::const_iterator delegateIt(mDelegates.find(display));
|
|
|
|
if (delegateIt == mDelegates.end())
|
|
|
|
{
|
|
|
|
delegate = CommandDelegateFactoryCollection::get().makeDelegate (
|
|
|
|
display, mUndoStack, mParent);
|
|
|
|
mDelegates.insert(std::make_pair<int, CommandDelegate*>(display, delegate));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
delegate = delegateIt->second;
|
|
|
|
}
|
|
|
|
return delegate;
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:17:40 +00:00
|
|
|
void CSVWorld::DialogueDelegateDispatcher::editorDataCommited(QWidget* editor, const QModelIndex& index, CSMWorld::ColumnBase::Display display)
|
2014-03-07 16:15:43 +00:00
|
|
|
{
|
2014-03-07 21:17:40 +00:00
|
|
|
setModelData(editor, mTable, index, display);
|
2014-03-07 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::DialogueDelegateDispatcher::setEditorData (QWidget* editor, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
CSMWorld::ColumnBase::Display display = static_cast<CSMWorld::ColumnBase::Display>
|
|
|
|
(mTable->headerData (index.column(), Qt::Horizontal, CSMWorld::ColumnBase::Role_Display).toInt());
|
|
|
|
|
|
|
|
std::map<int, CommandDelegate*>::const_iterator delegateIt(mDelegates.find(display));
|
|
|
|
if (delegateIt != mDelegates.end())
|
|
|
|
{
|
|
|
|
delegateIt->second->setEditorData(editor, index);
|
|
|
|
}
|
2014-03-07 21:17:40 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < mProxys.size(); ++i)
|
|
|
|
{
|
|
|
|
if (mProxys[i]->getEditor() == editor)
|
|
|
|
{
|
|
|
|
mProxys[i]->setIndex(index);
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 21:17:40 +00:00
|
|
|
void CSVWorld::DialogueDelegateDispatcher::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index, CSMWorld::ColumnBase::Display display) const
|
2014-03-07 16:15:43 +00:00
|
|
|
{
|
|
|
|
std::map<int, CommandDelegate*>::const_iterator delegateIt(mDelegates.find(display));
|
|
|
|
if (delegateIt != mDelegates.end())
|
|
|
|
{
|
|
|
|
delegateIt->second->setModelData(editor, model, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::DialogueDelegateDispatcher::paint (QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
//Does nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize CSVWorld::DialogueDelegateDispatcher::sizeHint (const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
{
|
|
|
|
return QSize(); //silencing warning, otherwise does nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* CSVWorld::DialogueDelegateDispatcher::makeEditor(CSMWorld::ColumnBase::Display display, const QModelIndex& index)
|
|
|
|
{
|
2014-03-08 14:27:43 +00:00
|
|
|
bool hasEnums = CSMWorld::Columns::hasEnums(static_cast<CSMWorld::Columns::ColumnId>(mTable->getColumnId(index.column() ) ) );
|
2014-03-08 15:50:42 +00:00
|
|
|
if (display == CSMWorld::ColumnBase::Display_Boolean)
|
|
|
|
{
|
|
|
|
hasEnums = true;
|
|
|
|
}
|
|
|
|
|
2014-03-07 16:15:43 +00:00
|
|
|
QWidget* editor = NULL;
|
|
|
|
std::map<int, CommandDelegate*>::iterator delegateIt(mDelegates.find(display));
|
|
|
|
if (delegateIt != mDelegates.end())
|
|
|
|
{
|
|
|
|
editor = delegateIt->second->createEditor(dynamic_cast<QWidget*>(mParent), QStyleOptionViewItem(), index);
|
2014-03-07 21:17:40 +00:00
|
|
|
DialogueDelegateDispatcherProxy* proxy = new DialogueDelegateDispatcherProxy(editor, display);
|
2014-03-08 14:27:43 +00:00
|
|
|
if (hasEnums) //combox is used for all enums
|
|
|
|
{
|
2014-03-08 15:50:42 +00:00
|
|
|
connect(editor, SIGNAL(currentIndexChanged (int)), proxy, SLOT(editorDataCommited()));
|
2014-03-08 14:27:43 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
connect(editor, SIGNAL(editingFinished()), proxy, SLOT(editorDataCommited()));
|
|
|
|
}
|
2014-03-07 21:17:40 +00:00
|
|
|
connect(proxy, SIGNAL(editorDataCommited(QWidget*, const QModelIndex&, CSMWorld::ColumnBase::Display)), this, SLOT(editorDataCommited(QWidget*, const QModelIndex&, CSMWorld::ColumnBase::Display)));
|
|
|
|
mProxys.push_back(proxy); //deleted in the destructor
|
2014-03-07 16:15:43 +00:00
|
|
|
}
|
|
|
|
return editor;
|
|
|
|
}
|
|
|
|
|
2014-03-07 21:17:40 +00:00
|
|
|
CSVWorld::DialogueDelegateDispatcher::~DialogueDelegateDispatcher()
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < mProxys.size(); ++i)
|
|
|
|
{
|
|
|
|
delete mProxys[i]; //unique_ptr could be handy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============================DialogueSubView==========================================
|
|
|
|
*/
|
|
|
|
|
2012-12-13 12:35:08 +00:00
|
|
|
CSVWorld::DialogueSubView::DialogueSubView (const CSMWorld::UniversalId& id, CSMDoc::Document& document,
|
2014-03-07 16:15:43 +00:00
|
|
|
bool createAndDelete) :
|
|
|
|
|
|
|
|
SubView (id),
|
2014-03-07 21:17:40 +00:00
|
|
|
mDispatcher(this, dynamic_cast<CSMWorld::IdTable*>(document.getData().getTableModel (id)), document.getUndoStack())
|
2014-03-07 16:15:43 +00:00
|
|
|
|
2012-12-13 12:35:08 +00:00
|
|
|
{
|
2012-12-30 13:01:52 +00:00
|
|
|
QWidget *widget = new QWidget (this);
|
|
|
|
|
|
|
|
setWidget (widget);
|
|
|
|
|
|
|
|
QGridLayout *layout = new QGridLayout;
|
|
|
|
|
|
|
|
widget->setLayout (layout);
|
|
|
|
|
2013-03-21 09:07:25 +00:00
|
|
|
QAbstractItemModel *model = document.getData().getTableModel (id);
|
2012-12-30 13:01:52 +00:00
|
|
|
|
|
|
|
int columns = model->columnCount();
|
|
|
|
|
2013-01-08 13:59:40 +00:00
|
|
|
mWidgetMapper = new QDataWidgetMapper (this);
|
|
|
|
mWidgetMapper->setModel (model);
|
2014-03-07 21:17:40 +00:00
|
|
|
mWidgetMapper->setItemDelegate(&mDispatcher);
|
2013-01-08 13:59:40 +00:00
|
|
|
|
2012-12-30 13:01:52 +00:00
|
|
|
for (int i=0; i<columns; ++i)
|
|
|
|
{
|
|
|
|
int flags = model->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Flags).toInt();
|
2012-12-13 12:35:08 +00:00
|
|
|
|
2012-12-30 13:01:52 +00:00
|
|
|
if (flags & CSMWorld::ColumnBase::Flag_Dialogue)
|
|
|
|
{
|
|
|
|
layout->addWidget (new QLabel (model->headerData (i, Qt::Horizontal).toString()), i, 0);
|
2013-01-08 13:59:40 +00:00
|
|
|
|
|
|
|
CSMWorld::ColumnBase::Display display = static_cast<CSMWorld::ColumnBase::Display>
|
|
|
|
(model->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Display).toInt());
|
|
|
|
|
2014-03-07 21:17:40 +00:00
|
|
|
mDispatcher.makeDelegate(display);
|
|
|
|
QWidget *widget = mDispatcher.makeEditor(display, (model->index (0, i)));
|
2013-01-08 13:59:40 +00:00
|
|
|
|
2014-03-07 16:15:43 +00:00
|
|
|
if (widget)
|
2013-01-08 13:59:40 +00:00
|
|
|
{
|
2014-03-07 16:15:43 +00:00
|
|
|
layout->addWidget (widget, i, 1);
|
|
|
|
mWidgetMapper->addMapping (widget, i);
|
2014-03-08 14:18:40 +00:00
|
|
|
if (! (model->flags (model->index (0, i)) & Qt::ItemIsEditable))
|
|
|
|
{
|
|
|
|
widget->setDisabled(true);
|
|
|
|
}
|
2013-01-08 13:59:40 +00:00
|
|
|
}
|
2012-12-30 13:01:52 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-08 13:59:40 +00:00
|
|
|
|
2013-01-19 13:29:14 +00:00
|
|
|
mWidgetMapper->setCurrentModelIndex (
|
|
|
|
dynamic_cast<CSMWorld::IdTable&> (*model).getModelIndex (id.getId(), 0));
|
2012-12-13 12:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::DialogueSubView::setEditLock (bool locked)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|