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

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

66 lines
2.1 KiB
C++
Raw Normal View History

2021-09-19 17:07:54 +00:00
#include "tableheadermouseeventhandler.hpp"
#include "dragrecordtable.hpp"
#include <QMenu>
2022-06-16 19:29:55 +00:00
#include <QMouseEvent>
2022-09-22 18:26:05 +00:00
#include <QPoint>
2021-09-19 17:07:54 +00:00
namespace CSVWorld
{
2022-09-22 18:26:05 +00:00
TableHeaderMouseEventHandler::TableHeaderMouseEventHandler(DragRecordTable* parent)
: QWidget(parent)
, table(*parent)
, header(*table.horizontalHeader())
{
header.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(&header, &QHeaderView::customContextMenuRequested,
[this](const QPoint& position) { showContextMenu(position); });
2021-09-19 17:07:54 +00:00
2022-09-22 18:26:05 +00:00
header.viewport()->installEventFilter(this);
}
2021-09-19 17:07:54 +00:00
2022-09-22 18:26:05 +00:00
bool TableHeaderMouseEventHandler::eventFilter(QObject* tableWatched, QEvent* event)
2021-09-19 17:07:54 +00:00
{
2022-09-22 18:26:05 +00:00
if (event->type() == QEvent::Type::MouseButtonPress)
2021-09-19 17:07:54 +00:00
{
2022-09-22 18:26:05 +00:00
auto& clickEvent = static_cast<QMouseEvent&>(*event);
if ((clickEvent.button() == Qt::MiddleButton))
{
const auto& index = table.indexAt(clickEvent.pos());
table.setColumnHidden(index.column(), true);
clickEvent.accept();
return true;
}
2021-09-19 17:07:54 +00:00
}
2022-09-22 18:26:05 +00:00
return false;
2021-09-19 17:07:54 +00:00
}
2022-09-22 18:26:05 +00:00
void TableHeaderMouseEventHandler::showContextMenu(const QPoint& position)
{
auto& menu{ createContextMenu() };
menu.popup(header.viewport()->mapToGlobal(position));
}
2021-09-19 17:07:54 +00:00
2022-09-22 18:26:05 +00:00
QMenu& TableHeaderMouseEventHandler::createContextMenu()
2021-09-19 17:07:54 +00:00
{
2022-09-22 18:26:05 +00:00
auto* menu = new QMenu(this);
for (int i = 0; i < table.model()->columnCount(); ++i)
{
const auto& name = table.model()->headerData(i, Qt::Horizontal, Qt::DisplayRole);
QAction* action{ new QAction(name.toString(), this) };
action->setCheckable(true);
action->setChecked(!table.isColumnHidden(i));
menu->addAction(action);
2021-09-19 17:07:54 +00:00
2022-09-22 18:26:05 +00:00
connect(action, &QAction::triggered, [this, &action, &i]() {
table.setColumnHidden(i, !action->isChecked());
action->setChecked(!action->isChecked());
action->toggle();
});
}
return *menu;
2021-09-19 17:07:54 +00:00
}
} // namespace CSVWorld