1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-23 20:23:54 +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>
2021-09-19 17:07:54 +00:00
#include <QPoint>
namespace CSVWorld
{
TableHeaderMouseEventHandler::TableHeaderMouseEventHandler(DragRecordTable* parent)
: QWidget(parent)
, table(*parent)
, header(*table.horizontalHeader())
{
header.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
2022-02-24 15:01:40 +00:00
connect(&header, &QHeaderView::customContextMenuRequested,
[this](const QPoint& position) { showContextMenu(position); });
2021-09-19 17:07:54 +00:00
header.viewport()->installEventFilter(this);
}
bool TableHeaderMouseEventHandler::eventFilter(QObject* tableWatched, QEvent* event)
{
if (event->type() == QEvent::Type::MouseButtonPress)
{
auto& clickEvent = static_cast<QMouseEvent&>(*event);
if ((clickEvent.button() == Qt::MiddleButton))
2022-09-22 18:26:05 +00:00
{
2021-09-19 17:07:54 +00:00
const auto& index = table.indexAt(clickEvent.pos());
table.setColumnHidden(index.column(), true);
clickEvent.accept();
return true;
2022-09-22 18:26:05 +00:00
}
2021-09-19 17:07:54 +00:00
}
return false;
}
void TableHeaderMouseEventHandler::showContextMenu(const QPoint& position)
{
auto& menu{ createContextMenu() };
menu.popup(header.viewport()->mapToGlobal(position));
}
QMenu& TableHeaderMouseEventHandler::createContextMenu()
{
auto* menu = new QMenu(this);
for (int i = 0; i < table.model()->columnCount(); ++i)
2022-09-22 18:26:05 +00:00
{
2021-09-19 17:07:54 +00:00
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);
2022-02-24 15:01:40 +00:00
connect(action, &QAction::triggered, [this, &action, &i]() {
2021-09-19 17:07:54 +00:00
table.setColumnHidden(i, !action->isChecked());
action->setChecked(!action->isChecked());
action->toggle();
});
2022-09-22 18:26:05 +00:00
}
2021-09-19 17:07:54 +00:00
return *menu;
}
} // namespace CSVWorld