2014-06-29 19:12:31 +00:00
|
|
|
#include "nestedtable.hpp"
|
|
|
|
|
2014-06-30 12:12:57 +00:00
|
|
|
#include <QHeaderView>
|
2014-06-30 18:06:18 +00:00
|
|
|
#include <QContextMenuEvent>
|
2014-07-01 18:52:27 +00:00
|
|
|
#include <QMenu>
|
2014-07-07 10:07:29 +00:00
|
|
|
#include <QDebug>
|
2014-06-30 12:12:57 +00:00
|
|
|
|
2015-07-04 16:27:42 +00:00
|
|
|
#include "../../model/world/nestedtableproxymodel.hpp"
|
|
|
|
#include "../../model/world/universalid.hpp"
|
|
|
|
#include "../../model/world/commands.hpp"
|
|
|
|
#include "../../model/world/commanddispatcher.hpp"
|
|
|
|
|
|
|
|
#include "tableeditidaction.hpp"
|
|
|
|
#include "util.hpp"
|
|
|
|
|
2015-03-06 03:36:13 +00:00
|
|
|
CSVWorld::NestedTable::NestedTable(CSMDoc::Document& document,
|
2015-04-24 23:39:37 +00:00
|
|
|
CSMWorld::UniversalId id,
|
2015-03-30 05:41:55 +00:00
|
|
|
CSMWorld::NestedTableProxyModel* model,
|
2014-06-30 11:09:10 +00:00
|
|
|
QWidget* parent)
|
2015-06-21 18:35:00 +00:00
|
|
|
: DragRecordTable(document, parent),
|
2014-07-01 18:52:27 +00:00
|
|
|
mModel(model)
|
2014-06-29 19:12:31 +00:00
|
|
|
{
|
2015-04-24 23:39:37 +00:00
|
|
|
mDispatcher = new CSMWorld::CommandDispatcher (document, id, this);
|
2014-06-30 12:12:57 +00:00
|
|
|
|
|
|
|
setSelectionBehavior (QAbstractItemView::SelectRows);
|
|
|
|
setSelectionMode (QAbstractItemView::ExtendedSelection);
|
|
|
|
|
2015-06-12 13:10:12 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
|
|
|
|
horizontalHeader()->setSectionResizeMode (QHeaderView::Interactive);
|
|
|
|
#else
|
2014-06-30 12:12:57 +00:00
|
|
|
horizontalHeader()->setResizeMode (QHeaderView::Interactive);
|
2015-06-12 13:10:12 +00:00
|
|
|
#endif
|
2014-06-30 12:12:57 +00:00
|
|
|
verticalHeader()->hide();
|
2014-06-29 19:12:31 +00:00
|
|
|
|
2014-06-30 11:09:10 +00:00
|
|
|
int columns = model->columnCount(QModelIndex());
|
2014-06-29 19:12:31 +00:00
|
|
|
|
|
|
|
for(int i = 0 ; i < columns; ++i)
|
|
|
|
{
|
|
|
|
CSMWorld::ColumnBase::Display display = static_cast<CSMWorld::ColumnBase::Display> (
|
2014-06-30 11:09:10 +00:00
|
|
|
model->headerData (i, Qt::Horizontal, CSMWorld::ColumnBase::Role_Display).toInt());
|
2014-07-02 11:13:03 +00:00
|
|
|
|
2014-06-29 19:12:31 +00:00
|
|
|
CommandDelegate *delegate = CommandDelegateFactoryCollection::get().makeDelegate(display,
|
2015-04-24 23:39:37 +00:00
|
|
|
mDispatcher,
|
2015-03-06 03:36:13 +00:00
|
|
|
document,
|
2014-06-29 19:12:31 +00:00
|
|
|
this);
|
2015-03-06 03:36:13 +00:00
|
|
|
|
2014-06-30 11:09:10 +00:00
|
|
|
setItemDelegateForColumn(i, delegate);
|
2014-06-29 19:12:31 +00:00
|
|
|
}
|
2014-06-30 12:12:57 +00:00
|
|
|
|
|
|
|
setModel(model);
|
2014-07-01 18:52:27 +00:00
|
|
|
|
|
|
|
mAddNewRowAction = new QAction (tr ("Add new row"), this);
|
2014-07-02 11:13:03 +00:00
|
|
|
|
|
|
|
connect(mAddNewRowAction, SIGNAL(triggered()),
|
2014-07-01 18:52:27 +00:00
|
|
|
this, SLOT(addNewRowActionTriggered()));
|
2014-07-02 11:13:03 +00:00
|
|
|
|
2014-07-01 18:52:27 +00:00
|
|
|
mRemoveRowAction = new QAction (tr ("Remove row"), this);
|
2014-07-02 11:13:03 +00:00
|
|
|
|
2014-07-01 18:52:27 +00:00
|
|
|
connect(mRemoveRowAction, SIGNAL(triggered()),
|
|
|
|
this, SLOT(removeRowActionTriggered()));
|
2015-07-04 16:27:42 +00:00
|
|
|
|
|
|
|
mEditIdAction = new TableEditIdAction(*this, this);
|
|
|
|
connect(mEditIdAction, SIGNAL(triggered()), this, SLOT(editCell()));
|
2014-06-29 19:12:31 +00:00
|
|
|
}
|
2014-06-30 11:09:10 +00:00
|
|
|
|
2015-06-21 18:35:00 +00:00
|
|
|
std::vector<CSMWorld::UniversalId> CSVWorld::NestedTable::getDraggedRecords() const
|
2014-06-30 11:09:10 +00:00
|
|
|
{
|
2015-06-21 18:35:00 +00:00
|
|
|
// No drag support for nested tables
|
|
|
|
return std::vector<CSMWorld::UniversalId>();
|
2014-06-30 11:09:10 +00:00
|
|
|
}
|
2014-06-30 18:06:18 +00:00
|
|
|
|
|
|
|
void CSVWorld::NestedTable::contextMenuEvent (QContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
QModelIndexList selectedRows = selectionModel()->selectedRows();
|
2014-07-02 11:13:03 +00:00
|
|
|
|
2014-07-01 18:52:27 +00:00
|
|
|
QMenu menu(this);
|
|
|
|
|
2015-07-04 16:27:42 +00:00
|
|
|
int currentRow = rowAt(event->y());
|
|
|
|
int currentColumn = columnAt(event->x());
|
|
|
|
if (mEditIdAction->isValidIdCell(currentRow, currentColumn))
|
|
|
|
{
|
|
|
|
mEditIdAction->setCell(currentRow, currentColumn);
|
|
|
|
menu.addAction(mEditIdAction);
|
|
|
|
menu.addSeparator();
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:52:27 +00:00
|
|
|
if (selectionModel()->selectedRows().size() == 1)
|
|
|
|
menu.addAction(mRemoveRowAction);
|
|
|
|
|
|
|
|
menu.addAction(mAddNewRowAction);
|
2014-07-02 11:13:03 +00:00
|
|
|
|
2014-07-01 18:52:27 +00:00
|
|
|
menu.exec (event->globalPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::NestedTable::removeRowActionTriggered()
|
|
|
|
{
|
2015-06-21 18:35:00 +00:00
|
|
|
mDocument.getUndoStack().push(new CSMWorld::DeleteNestedCommand(*(mModel->model()),
|
|
|
|
mModel->getParentId(),
|
|
|
|
selectionModel()->selectedRows().begin()->row(),
|
|
|
|
mModel->getParentColumn()));
|
2014-07-01 18:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::NestedTable::addNewRowActionTriggered()
|
|
|
|
{
|
2015-06-21 18:35:00 +00:00
|
|
|
mDocument.getUndoStack().push(new CSMWorld::AddNestedCommand(*(mModel->model()),
|
|
|
|
mModel->getParentId(),
|
|
|
|
selectionModel()->selectedRows().size(),
|
|
|
|
mModel->getParentColumn()));
|
2014-06-30 18:06:18 +00:00
|
|
|
}
|
2015-07-04 16:27:42 +00:00
|
|
|
|
|
|
|
void CSVWorld::NestedTable::editCell()
|
|
|
|
{
|
|
|
|
emit editRequest(mEditIdAction->getCurrentId(), "");
|
|
|
|
}
|