Skip to content

Commit

Permalink
Fix showing unit context menus in the city view
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lmoureaux committed Feb 5, 2022
1 parent 29efe22 commit 6ae7a03
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
51 changes: 18 additions & 33 deletions client/gui-qt/citydlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QListWidget *>(object);
if (list != nullptr && event->type() == QEvent::ContextMenu) {
auto *menu_event = static_cast<QContextMenuEvent *>(event);
auto *item = list->itemAt(menu_event->pos());
// Maybe there was no unit under the mouse
if (auto *unit_item = dynamic_cast<unit_list_item *>(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);
Expand Down Expand Up @@ -1417,22 +1387,37 @@ 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<unit_list_item *>(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<unit_list_item *>(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) {
if (auto *uitem = dynamic_cast<unit_list_item *>(item)) {
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);
}
Expand Down
13 changes: 0 additions & 13 deletions client/gui-qt/citydlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************************************************************************/
Expand Down

0 comments on commit 6ae7a03

Please sign in to comment.