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"
|
2016-06-09 19:42:40 +00:00
|
|
|
#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"
|
|
|
|
|
2015-03-06 03:36:13 +00:00
|
|
|
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);
|
|
|
|
|
2015-06-12 13:10:12 +00:00
|
|
|
horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
|
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, mDispatcher, document, 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
|
|
|
|
2015-10-28 09:30:30 +00:00
|
|
|
if (editable)
|
|
|
|
{
|
|
|
|
if (!fixedRows)
|
|
|
|
{
|
|
|
|
mAddNewRowAction = new QAction(tr("Add new row"), this);
|
2022-08-23 02:28:58 +00:00
|
|
|
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
|
|
|
|
2016-06-09 19:42:40 +00:00
|
|
|
mRemoveRowAction = new QAction(tr("Remove rows"), this);
|
2022-08-23 02:28:58 +00:00
|
|
|
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-10-28 09:30:30 +00:00
|
|
|
}
|
2015-07-04 16:27:42 +00:00
|
|
|
|
2015-10-28 09:30:30 +00:00
|
|
|
mEditIdAction = new TableEditIdAction(*this, this);
|
2022-08-23 02:28:58 +00:00
|
|
|
connect(mEditIdAction, &QAction::triggered, this, &NestedTable::editCell);
|
2015-10-28 09:30:30 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2015-10-28 09:30:30 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-10-28 09:30:30 +00:00
|
|
|
if (mAddNewRowAction && mRemoveRowAction)
|
|
|
|
{
|
|
|
|
menu.addAction(mAddNewRowAction);
|
2016-06-09 19:42:40 +00:00
|
|
|
menu.addAction(mRemoveRowAction);
|
2015-10-28 09:30:30 +00:00
|
|
|
}
|
2014-07-02 11:13:03 +00:00
|
|
|
|
2014-07-01 18:52:27 +00:00
|
|
|
menu.exec(event->globalPos());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSVWorld::NestedTable::removeRowActionTriggered()
|
|
|
|
{
|
2016-06-09 19:42:40 +00:00
|
|
|
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()
|
|
|
|
{
|
2016-05-27 02:44:02 +00:00
|
|
|
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(), "");
|
|
|
|
}
|