From 6ae7a03143b30483cc8ce17cd4bb8d5de18f45f4 Mon Sep 17 00:00:00 2001 From: Louis Moureaux Date: Wed, 2 Feb 2022 23:27:14 +0100 Subject: [PATCH] Fix showing unit context menus in the city view On my system, the event filter was not receiving the context menu event. Drop it altogether and connect customContextMenuRequested to a lambda that includes the previously used code. --- client/gui-qt/citydlg.cpp | 51 ++++++++++++++------------------------- client/gui-qt/citydlg.h | 13 ---------- 2 files changed, 18 insertions(+), 46 deletions(-) diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp index 4a23aa9ec5..46eaa533ed 100644 --- a/client/gui-qt/citydlg.cpp +++ b/client/gui-qt/citydlg.cpp @@ -843,36 +843,6 @@ void unit_list_item::sentry() } } -/** - Constructor - */ -unit_list_event_filter::unit_list_event_filter(QObject *parent) - : QObject(parent) -{ -} - -/** - Filters out context menu events and shows the unit context menu - */ -bool unit_list_event_filter::eventFilter(QObject *object, QEvent *event) -{ - auto *list = qobject_cast(object); - if (list != nullptr && event->type() == QEvent::ContextMenu) { - auto *menu_event = static_cast(event); - auto *item = list->itemAt(menu_event->pos()); - // Maybe there was no unit under the mouse - if (auto *unit_item = dynamic_cast(item)) { - // Maybe we can't give orders to this unit - if (auto *menu = unit_item->menu()) { - // OK, show the menu - menu->exec(menu_event->globalPos()); - return true; - } - } - } - return false; -} - cityIconInfoLabel::cityIconInfoLabel(QWidget *parent) : QWidget(parent) { auto f = fcFont::instance()->getFont(fonts::default_font); @@ -1417,14 +1387,27 @@ city_dialog::city_dialog(QWidget *parent) : QWidget(parent) ui.tabs_right->setTabText(0, _("General")); ui.tabs_right->setTabText(1, _("Citizens")); + const auto show_unit_actions_menu = [this](const QPoint &loc) { + auto *item = ui.present_units_list->itemAt(loc); + // Maybe there was no unit under the mouse + if (auto *unit_item = dynamic_cast(item)) { + // Maybe we can't give orders to this unit + if (auto *menu = unit_item->menu()) { + // OK, show the menu + menu->popup(ui.present_units_list->mapToGlobal(loc)); + } + } + }; + connect(ui.present_units_list, &QListWidget::itemDoubleClicked, [](QListWidgetItem *item) { if (auto *uitem = dynamic_cast(item)) { uitem->activate_and_close_dialog(); } }); - ui.present_units_list->installEventFilter( - new unit_list_event_filter(this)); + ui.present_units_list->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui.present_units_list, &QWidget::customContextMenuRequested, + show_unit_actions_menu); connect(ui.supported_units, &QListWidget::itemDoubleClicked, [](QListWidgetItem *item) { @@ -1432,7 +1415,9 @@ city_dialog::city_dialog(QWidget *parent) : QWidget(parent) uitem->activate_and_close_dialog(); } }); - ui.supported_units->installEventFilter(new unit_list_event_filter(this)); + ui.supported_units->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui.supported_units, &QWidget::customContextMenuRequested, + show_unit_actions_menu); ui.supported_units->oneliner = false; installEventFilter(this); } diff --git a/client/gui-qt/citydlg.h b/client/gui-qt/citydlg.h index 30dbe885d9..2991ca458e 100644 --- a/client/gui-qt/citydlg.h +++ b/client/gui-qt/citydlg.h @@ -129,19 +129,6 @@ class unit_list_item : public QObject, public QListWidgetItem { struct unit *m_unit; }; -/**************************************************************************** - Pops up unit context menu -****************************************************************************/ -class unit_list_event_filter : public QObject { - Q_OBJECT - -public: - explicit unit_list_event_filter(QObject *parent = nullptr); - -protected: - bool eventFilter(QObject *object, QEvent *event) override; -}; - /**************************************************************************** Single item on unit_info in city dialog representing one unit ****************************************************************************/