Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performing actions from the city screen #862

Merged
merged 2 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 25 additions & 40 deletions client/gui-qt/citydlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ bool unit_list_item::can_issue_orders() const
*/
void unit_list_item::disband()
{
if (!can_issue_orders()) {
if (can_issue_orders()) {
auto *units = unit_list_new();
unit_list_append(units, m_unit);
popup_disband_dialog(units);
Expand All @@ -759,11 +759,11 @@ void unit_list_item::disband()
}

/**
Loads unit into some tranport
Loads unit into some transport
*/
void unit_list_item::load()
{
if (!can_issue_orders()) {
if (can_issue_orders()) {
qtg_request_transport(m_unit, unit_tile(m_unit));
}
}
Expand All @@ -773,7 +773,7 @@ void unit_list_item::load()
*/
void unit_list_item::unload()
{
if (!can_issue_orders()) {
if (can_issue_orders()) {
request_unit_unload(m_unit);
}
}
Expand All @@ -783,7 +783,7 @@ void unit_list_item::unload()
*/
void unit_list_item::unload_all()
{
if (!can_issue_orders()) {
if (can_issue_orders()) {
request_unit_unload_all(m_unit);
}
}
Expand All @@ -793,7 +793,7 @@ void unit_list_item::unload_all()
*/
void unit_list_item::upgrade()
{
if (!can_issue_orders()) {
if (can_issue_orders()) {
auto *units = unit_list_new();
unit_list_append(units, m_unit);
popup_upgrade_dialog(units);
Expand All @@ -802,7 +802,7 @@ void unit_list_item::upgrade()
}

/**
Changes homecity for given unit
Changes home city for given unit
*/
void unit_list_item::change_homecity()
{
Expand Down 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