1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-23 01:23:52 +00:00
openmw/apps/opencs/view/world/nestedtable.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
4.3 KiB
C++
Raw Normal View History

2014-06-29 19:12:31 +00:00
#include "nestedtable.hpp"
2014-06-30 18:06:18 +00:00
#include <QContextMenuEvent>
2014-06-30 12:12:57 +00:00
#include <QHeaderView>
2014-07-01 18:52:27 +00:00
#include <QMenu>
2014-06-30 12:12:57 +00:00
2016-07-27 05:53:21 +00:00
#include "../../model/prefs/shortcut.hpp"
2015-07-04 16:27:42 +00:00
#include "../../model/world/commanddispatcher.hpp"
#include "../../model/world/commandmacro.hpp"
2015-07-04 16:27:42 +00:00
#include "../../model/world/commands.hpp"
#include "../../model/world/nestedtableproxymodel.hpp"
#include "../../model/world/universalid.hpp"
2022-10-19 17:02:00 +00:00
#include <apps/opencs/model/doc/document.hpp>
#include <apps/opencs/model/world/columnbase.hpp>
#include <apps/opencs/view/world/dragrecordtable.hpp>
2015-07-04 16:27:42 +00:00
#include "tableeditidaction.hpp"
#include "util.hpp"
CSVWorld::NestedTable::NestedTable(CSMDoc::Document& document, CSMWorld::UniversalId id,
2015-03-30 05:41:55 +00:00
CSMWorld::NestedTableProxyModel* model, QWidget* parent, bool editable, bool fixedRows)
2015-06-21 18:35:00 +00:00
: DragRecordTable(document, parent)
2018-10-09 06:21:12 +00:00
, mAddNewRowAction(nullptr)
, mRemoveRowAction(nullptr)
, mEditIdAction(nullptr)
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);
horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
2014-06-30 12:12:57 +00:00
verticalHeader()->hide();
2014-06-29 19:12:31 +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>(
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, mDispatcher, document, this);
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
if (editable)
{
if (!fixedRows)
{
mAddNewRowAction = new QAction(tr("Add new row"), this);
connect(mAddNewRowAction, &QAction::triggered, this, &NestedTable::addNewRowActionTriggered);
2016-07-27 05:53:21 +00:00
CSMPrefs::Shortcut* addRowShortcut = new CSMPrefs::Shortcut("table-add", this);
addRowShortcut->associateAction(mAddNewRowAction);
2014-07-02 11:13:03 +00:00
mRemoveRowAction = new QAction(tr("Remove rows"), this);
connect(mRemoveRowAction, &QAction::triggered, this, &NestedTable::removeRowActionTriggered);
2016-07-27 05:53:21 +00:00
CSMPrefs::Shortcut* removeRowShortcut = new CSMPrefs::Shortcut("table-remove", this);
removeRowShortcut->associateAction(mRemoveRowAction);
}
2015-07-04 16:27:42 +00:00
mEditIdAction = new TableEditIdAction(*this, this);
connect(mEditIdAction, &QAction::triggered, this, &NestedTable::editCell);
}
2014-06-29 19:12:31 +00:00
}
2015-06-21 18:35:00 +00:00
std::vector<CSMWorld::UniversalId> CSVWorld::NestedTable::getDraggedRecords() const
{
2015-06-21 18:35:00 +00:00
// No drag support for nested tables
return std::vector<CSMWorld::UniversalId>();
}
2014-06-30 18:06:18 +00:00
void CSVWorld::NestedTable::contextMenuEvent(QContextMenuEvent* event)
{
if (!mEditIdAction)
return;
2014-06-30 18:06:18 +00:00
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();
}
if (mAddNewRowAction && mRemoveRowAction)
{
menu.addAction(mAddNewRowAction);
menu.addAction(mRemoveRowAction);
}
2014-07-02 11:13:03 +00:00
2014-07-01 18:52:27 +00:00
menu.exec(event->globalPos());
}
void CSVWorld::NestedTable::removeRowActionTriggered()
{
CSMWorld::CommandMacro macro(
mDocument.getUndoStack(), selectionModel()->selectedRows().size() > 1 ? tr("Remove rows") : "");
// Remove rows in reverse order
for (int i = selectionModel()->selectedRows().size() - 1; i >= 0; --i)
{
macro.push(new CSMWorld::DeleteNestedCommand(*(mModel->model()), mModel->getParentId(),
selectionModel()->selectedRows()[i].row(), mModel->getParentColumn()));
}
2014-07-01 18:52:27 +00:00
}
void CSVWorld::NestedTable::addNewRowActionTriggered()
{
int row = 0;
if (!selectionModel()->selectedRows().empty())
row = selectionModel()->selectedRows().back().row() + 1;
2015-06-21 18:35:00 +00:00
mDocument.getUndoStack().push(
new CSMWorld::AddNestedCommand(*(mModel->model()), mModel->getParentId(), row, 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(), "");
}