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

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

252 lines
8.5 KiB
C++
Raw Normal View History

#include "tablesubview.hpp"
#include <QApplication>
2021-07-24 20:00:25 +00:00
#include <QCheckBox>
#include <QDropEvent>
#include <QEvent>
2021-07-24 20:00:25 +00:00
#include <QHBoxLayout>
#include <QHeaderView>
2021-07-24 20:00:25 +00:00
#include <QPushButton>
#include <QScreen>
2013-07-23 19:59:02 +00:00
#include <QVBoxLayout>
#include <QVariant>
2013-07-23 19:59:02 +00:00
2022-10-19 17:02:00 +00:00
#include <algorithm>
#include <utility>
#include <variant>
2022-10-19 17:02:00 +00:00
#include <apps/opencs/view/doc/subview.hpp>
2024-04-25 07:57:43 +00:00
#include <components/misc/scalableicon.hpp>
2012-12-11 12:22:43 +00:00
#include "../../model/doc/document.hpp"
#include "../../model/world/tablemimedata.hpp"
2012-12-11 12:22:43 +00:00
#include "../doc/sizehint.hpp"
#include "../doc/view.hpp"
#include "../filter/filterbox.hpp"
2023-01-29 15:25:25 +00:00
#include "../filter/filterdata.hpp"
#include "table.hpp"
2013-07-25 12:29:56 +00:00
#include "tablebottombox.hpp"
2012-12-11 12:22:43 +00:00
CSVWorld::TableSubView::TableSubView(
const CSMWorld::UniversalId& id, CSMDoc::Document& document, const CreatorFactoryBase& creatorFactory, bool sorting)
2021-07-24 20:00:25 +00:00
: SubView(id)
, mShowOptions(false)
, mOptions(0)
{
2013-07-23 19:59:02 +00:00
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(mBottom = new TableBottomBox(creatorFactory, document, id, this), 0);
2013-07-26 10:42:15 +00:00
2014-03-06 12:51:21 +00:00
layout->insertWidget(0, mTable = new Table(id, mBottom->canCreateAndDelete(), sorting, document), 2);
2013-07-25 12:29:56 +00:00
2014-04-13 14:40:41 +00:00
mFilterBox = new CSVFilter::FilterBox(document.getData(), this);
2021-07-24 20:00:25 +00:00
QHBoxLayout* hLayout = new QHBoxLayout;
hLayout->insertWidget(0, mFilterBox);
mOptions = new QWidget;
QHBoxLayout* optHLayout = new QHBoxLayout;
QCheckBox* autoJump = new QCheckBox("Auto Jump");
autoJump->setToolTip(
"Whether to jump to the modified record."
"\nCan be useful in finding the moved or modified"
"\nobject instance while 3D editing.");
autoJump->setCheckState(Qt::Unchecked);
connect(autoJump, &QCheckBox::stateChanged, mTable, &Table::jumpAfterModChanged);
2021-07-24 20:00:25 +00:00
optHLayout->insertWidget(0, autoJump);
optHLayout->setContentsMargins(QMargins(0, 3, 0, 0));
mOptions->setLayout(optHLayout);
mOptions->resize(mOptions->width(), mFilterBox->height());
mOptions->hide();
QPushButton* opt = new QPushButton();
2024-04-25 07:57:43 +00:00
opt->setIcon(Misc::ScalableIcon::load(":startup/configure"));
2021-07-24 20:00:25 +00:00
opt->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
opt->setToolTip("Open additional options for this subview.");
connect(opt, &QPushButton::clicked, this, &TableSubView::toggleOptions);
2021-07-24 20:00:25 +00:00
QVBoxLayout* buttonLayout = new QVBoxLayout; // work around margin issues
buttonLayout->setContentsMargins(QMargins(0 /*left*/, 3 /*top*/, 3 /*right*/, 0 /*bottom*/));
buttonLayout->insertWidget(0, opt, 0, Qt::AlignVCenter | Qt::AlignRight);
hLayout->insertWidget(1, mOptions);
hLayout->insertLayout(2, buttonLayout);
layout->insertLayout(0, hLayout);
CSVDoc::SizeHintWidget* widget = new CSVDoc::SizeHintWidget;
2013-07-23 19:59:02 +00:00
widget->setLayout(layout);
setWidget(widget);
QScreen* screen = CSVDoc::View::getWidgetScreen(pos());
// prefer height of the screen and full width of the table
const QRect rect = screen->geometry();
int frameHeight = 40; // set a reasonable default
QWidget* topLevel = QApplication::topLevelAt(pos());
if (topLevel)
frameHeight = topLevel->frameGeometry().height() - topLevel->height();
widget->setSizeHint(QSize(mTable->horizontalHeader()->length(), rect.height() - frameHeight));
connect(mTable, &Table::editRequest, this, &TableSubView::editRequest);
2013-07-25 12:29:56 +00:00
connect(mTable, &Table::selectionSizeChanged, mBottom, &TableBottomBox::selectionSizeChanged);
connect(mTable, &Table::tableSizeChanged, mBottom, &TableBottomBox::tableSizeChanged);
2013-07-25 12:29:56 +00:00
mTable->tableSizeUpdate();
mTable->selectionSizeUpdate();
mTable->viewport()->installEventFilter(this);
2014-02-21 11:48:39 +00:00
mBottom->installEventFilter(this);
2014-04-13 14:40:41 +00:00
mFilterBox->installEventFilter(this);
if (mBottom->canCreateAndDelete())
2014-01-19 10:44:47 +00:00
{
connect(mTable, &Table::createRequest, mBottom, &TableBottomBox::createRequest);
2014-01-27 18:40:05 +00:00
connect(
mTable, &Table::cloneRequest, this, qOverload<const CSMWorld::UniversalId&>(&TableSubView::cloneRequest));
2014-01-27 18:40:05 +00:00
connect(this, qOverload<const std::string&, const CSMWorld::UniversalId::Type>(&TableSubView::cloneRequest),
mBottom, &TableBottomBox::cloneRequest);
connect(mTable, &Table::createRecordsDirectlyRequest, mBottom, &TableBottomBox::createRecordsDirectlyRequest);
connect(mTable, &Table::touchRequest, mBottom, &TableBottomBox::touchRequest);
connect(mTable, &Table::extendedDeleteConfigRequest, mBottom, &TableBottomBox::extendedDeleteConfigRequest);
connect(mTable, &Table::extendedRevertConfigRequest, mBottom, &TableBottomBox::extendedRevertConfigRequest);
2014-01-19 10:44:47 +00:00
}
connect(mBottom, &TableBottomBox::requestFocus, mTable, &Table::requestFocus);
connect(mFilterBox, &CSVFilter::FilterBox::recordFilterChanged, mTable, &Table::recordFilterChanged);
connect(mFilterBox, &CSVFilter::FilterBox::recordDropped, this, &TableSubView::createFilterRequest);
connect(mTable, &Table::closeRequest, this, qOverload<>(&TableSubView::closeRequest));
}
void CSVWorld::TableSubView::setEditLock(bool locked)
{
mTable->setEditLock(locked);
2013-07-28 12:51:47 +00:00
mBottom->setEditLock(locked);
}
void CSVWorld::TableSubView::editRequest(const CSMWorld::UniversalId& id, const std::string& hint)
{
focusId(id, hint);
}
2013-07-25 12:29:56 +00:00
void CSVWorld::TableSubView::setStatusBar(bool show)
{
mBottom->setStatusBar(show);
2014-01-19 10:44:47 +00:00
}
2014-04-13 14:40:41 +00:00
void CSVWorld::TableSubView::useHint(const std::string& hint)
{
if (hint.empty())
return;
if (hint[0] == 'f' && hint.size() >= 2)
mFilterBox->setRecordFilter(hint.substr(2));
}
2014-01-21 07:27:29 +00:00
void CSVWorld::TableSubView::cloneRequest(const CSMWorld::UniversalId& toClone)
2014-01-19 10:44:47 +00:00
{
2014-01-27 12:08:14 +00:00
emit cloneRequest(toClone.getId(), toClone.getType());
2014-01-19 10:44:47 +00:00
}
void CSVWorld::TableSubView::createFilterRequest(std::vector<CSMWorld::UniversalId>& types,
const std::pair<QVariant, std::string>& columnSearchData, Qt::DropAction action)
{
2023-01-29 15:25:25 +00:00
std::vector<CSVFilter::FilterData> sourceFilter;
std::vector<std::string> refIdColumns = mTable->getColumnsWithDisplay(
CSMWorld::TableMimeData::convertEnums(CSMWorld::UniversalId::Type_Referenceable));
bool hasRefIdDisplay = !refIdColumns.empty();
for (std::vector<CSMWorld::UniversalId>::iterator it(types.begin()); it != types.end(); ++it)
{
CSMWorld::UniversalId::Type type = it->getType();
std::vector<std::string> col = mTable->getColumnsWithDisplay(CSMWorld::TableMimeData::convertEnums(type));
if (!col.empty())
{
2023-01-29 15:25:25 +00:00
CSVFilter::FilterData filterData;
filterData.searchData = it->getId();
2024-01-12 06:04:56 +00:00
filterData.columns = std::move(col);
2023-01-29 15:25:25 +00:00
sourceFilter.emplace_back(filterData);
}
if (hasRefIdDisplay && CSMWorld::TableMimeData::isReferencable(type))
{
2023-01-29 15:25:25 +00:00
CSVFilter::FilterData filterData;
filterData.searchData = it->getId();
filterData.columns = refIdColumns;
sourceFilter.emplace_back(filterData);
}
}
2023-01-29 15:25:25 +00:00
if (!sourceFilter.empty())
mFilterBox->createFilterRequest(sourceFilter, action);
else
{
2023-01-29 15:25:25 +00:00
std::vector<CSVFilter::FilterData> sourceFilterByValue;
QVariant qData = columnSearchData.first;
std::string searchColumn = columnSearchData.second;
std::vector<std::string> searchColumns;
searchColumns.emplace_back(searchColumn);
2023-01-29 15:25:25 +00:00
CSVFilter::FilterData filterData;
filterData.searchData = qData;
2024-01-21 16:20:37 +00:00
filterData.columns = std::move(searchColumns);
2023-01-29 15:25:25 +00:00
sourceFilterByValue.emplace_back(filterData);
mFilterBox->createFilterRequest(sourceFilterByValue, action);
}
2014-02-21 11:48:39 +00:00
}
bool CSVWorld::TableSubView::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::Drop)
{
2015-01-15 11:13:53 +00:00
if (QDropEvent* drop = dynamic_cast<QDropEvent*>(event))
{
2016-10-15 16:34:54 +00:00
const CSMWorld::TableMimeData* tableMimeData
= dynamic_cast<const CSMWorld::TableMimeData*>(drop->mimeData());
if (!tableMimeData) // May happen when non-records (e.g. plain text) are dragged and dropped
2015-01-15 11:13:53 +00:00
return false;
2016-10-15 16:34:54 +00:00
bool handled = tableMimeData->holdsType(CSMWorld::UniversalId::Type_Filter);
2015-01-15 11:13:53 +00:00
if (handled)
{
2016-10-15 16:34:54 +00:00
mFilterBox->setRecordFilter(tableMimeData->returnMatching(CSMWorld::UniversalId::Type_Filter).getId());
2015-01-15 11:13:53 +00:00
}
return handled;
}
}
2014-02-21 11:48:39 +00:00
return false;
}
2021-07-24 20:00:25 +00:00
void CSVWorld::TableSubView::toggleOptions()
{
if (mShowOptions)
{
mShowOptions = false;
mOptions->hide();
}
else
{
mShowOptions = true;
mOptions->show();
}
}
void CSVWorld::TableSubView::requestFocus(const std::string& id)
{
mTable->requestFocus(id);
}