Skip to content

Commit

Permalink
Fix invoking SC_NUKE from the unit control bar
Browse files Browse the repository at this point in the history
It's better to route everything routed through the shortcuts handling code.

Closes #1650.

(cherry picked from commit 0906331)
  • Loading branch information
lmoureaux committed Jan 4, 2023
1 parent ee6be29 commit 08b56ff
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 34 deletions.
4 changes: 3 additions & 1 deletion client/hudwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "movement.h"
#include "nation.h"
#include "research.h"
#include "shortcuts.h"
#include "tile.h"
#include "tilespec.h"
#include "unit.h"
Expand Down Expand Up @@ -897,7 +898,8 @@ void hud_action::mouse_right_clicked() {}
*/
void hud_action::mouse_clicked()
{
king()->menu_bar->execute_shortcut(action_shortcut);
// Only works if there's an action in the menu!
fc_shortcuts::sc()->invoke(action_shortcut, nullptr);
}

/**
Expand Down
24 changes: 0 additions & 24 deletions client/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,30 +1288,6 @@ void mr_menu::set_tile_for_order(tile *ptile)
}
}

/**
Finds QAction bounded to given shortcut and triggers it
*/
void mr_menu::execute_shortcut(int sid)
{
if (sid == SC_GOTO) {
queen()->mapview_wdg->menu_click = true;
slot_unit_goto();
return;
}

auto fcs = fc_shortcuts::sc()->get_shortcut(static_cast<shortcut_id>(sid));
auto menu_list = findChildren<QMenu *>();
for (const QMenu *m : qAsConst(menu_list)) {
QList<QAction *> actions = m->actions();
for (QAction *a : qAsConst(actions)) {
if (a->shortcut() == fcs.keys && a->isEnabled()) {
a->activate(QAction::Trigger);
return;
}
}
}
}

/**
Returns string assigned to shortcut or empty string if doesnt exist
*/
Expand Down
1 change: 0 additions & 1 deletion client/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class mr_menu : public QMenuBar {
void update_roads_menu();
void update_bases_menu();
void set_tile_for_order(struct tile *ptile);
void execute_shortcut(int sid);
bool shortcut_exists(const fc_shortcut &fcs, QString &where);
QString shortcut_2_menustring(int sid);
QAction *minimap_status = nullptr;
Expand Down
23 changes: 15 additions & 8 deletions client/shortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,20 @@ void fc_shortcuts::create_no_action_shortcuts(map_view *parent)
}
}

/**
* Invokes the action for a shortcut.
*/
void fc_shortcuts::invoke(shortcut_id id, map_view *mapview)
{
if (m_actions.count(id) > 0 && m_actions[id] != nullptr) {
m_actions[id]->trigger();
} else if (mapview) {
// Shortcuts with no action are handled by the mapview directly.
// Eventually we should create actions and add them to menus...
mapview->shortcut_pressed(id);
}
}

/**
* If the mouse event corresponds to a registered shortcut, fire the
* corresponding action.
Expand All @@ -292,14 +306,7 @@ void fc_shortcuts::maybe_route_mouse_shortcut(QMouseEvent *event,
for (const auto &[id, shortcut] : shortcuts()) {
if (shortcut.type == fc_shortcut::mouse && shortcut.buttons == buttons
&& shortcut.modifiers == modifiers) {
// Found a matching shortcut
if (m_actions.count(id) > 0 && m_actions[id] != nullptr) {
m_actions[id]->trigger();
} else {
// Shortcuts with no action are handled by the mapview directly.
// Eventually we should create actions and add them to menus...
mapview->shortcut_pressed(id);
}
invoke(id, mapview);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions client/shortcuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class fc_shortcuts : public QObject {

void link_action(shortcut_id id, QAction *action);
void create_no_action_shortcuts(map_view *parent);
void invoke(shortcut_id id, map_view *mapview);
void maybe_route_mouse_shortcut(QMouseEvent *event, map_view *mapview);

static fc_shortcuts *sc();
Expand Down

0 comments on commit 08b56ff

Please sign in to comment.