1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00
OpenMW/apps/opencs/view/world/tableheadermouseeventhandler.cpp

66 lines
1.9 KiB
C++
Raw Normal View History

2021-09-19 19:07:54 +02:00
#include "tableheadermouseeventhandler.hpp"
#include "dragrecordtable.hpp"
#include <QMenu>
#include <QPoint>
2022-06-16 21:29:55 +02:00
#include <QMouseEvent>
2021-09-19 19:07:54 +02:00
namespace CSVWorld
{
TableHeaderMouseEventHandler::TableHeaderMouseEventHandler(DragRecordTable * parent)
: QWidget(parent)
, table(*parent)
, header(*table.horizontalHeader())
{
header.setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
connect(
2022-02-24 16:01:40 +01:00
&header, &QHeaderView::customContextMenuRequested, [this](const QPoint & position) { showContextMenu(position); });
2021-09-19 19:07:54 +02: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))
{
const auto & index = table.indexAt(clickEvent.pos());
table.setColumnHidden(index.column(), true);
clickEvent.accept();
return true;
}
}
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)
{
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 16:01:40 +01:00
connect(action, &QAction::triggered, [this, &action, &i]() {
2021-09-19 19:07:54 +02:00
table.setColumnHidden(i, !action->isChecked());
action->setChecked(!action->isChecked());
action->toggle();
});
}
return *menu;
}
} // namespace CSVWorld