From ab27a9c0b02e62176d6c7d1f353374a937568e0f Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 10 Oct 2023 01:00:08 +0300 Subject: [PATCH 36/36] Qt: Make OK and Cancel buttons gettext translatable See osdn #44799 Signed-off-by: Marko Lindqvist --- client/gui-qt/citydlg.cpp | 8 +++----- client/gui-qt/cityrep.cpp | 3 +-- client/gui-qt/dialogs.cpp | 35 ++++++++++++++++++++++------------- client/gui-qt/gui_main.cpp | 3 +-- client/gui-qt/hudwidget.cpp | 32 ++++++++++++++++++++++++++++++++ client/gui-qt/hudwidget.h | 5 +++++ client/gui-qt/menu.cpp | 13 ++++++------- client/gui-qt/repodlgs.cpp | 18 +++++++----------- client/gui-qt/shortcuts.cpp | 3 +-- 9 files changed, 78 insertions(+), 42 deletions(-) diff --git a/client/gui-qt/citydlg.cpp b/client/gui-qt/citydlg.cpp index f283baa4d6..cc4af74939 100644 --- a/client/gui-qt/citydlg.cpp +++ b/client/gui-qt/citydlg.cpp @@ -630,7 +630,7 @@ void impr_item::mouseDoubleClickEvent(QMouseEvent *event) city_improvement_name_translation(dlgcity, impr), price); ask->set_text_title(buf, (_("Sell improvement?"))); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); + ask->add_default_buttons(0); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, [=]() { struct city *pcity = game_city_by_number(city_id); @@ -2596,8 +2596,7 @@ void city_dialog::cma_remove() ask = new hud_message_box(city_dlg); ask->set_text_title(_("Remove this preset?"), cmafec_preset_get_descr(i)); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, this, CAPTURE_DEFAULT_THIS () { @@ -3357,8 +3356,7 @@ void city_dialog::buy() "Buy %s for %d gold?", value), name, value); ask->set_text_title(buf, buf2); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, this, [=]() { struct city *pcity = game_city_by_number(city_id); diff --git a/client/gui-qt/cityrep.cpp b/client/gui-qt/cityrep.cpp index 0abbf32b0d..3921c399b4 100644 --- a/client/gui-qt/cityrep.cpp +++ b/client/gui-qt/cityrep.cpp @@ -667,8 +667,7 @@ void city_widget::display_list_menu(const QPoint &) _("Are you sure you want to sell those %s?"), imprname); sell_ask = false; - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->set_text_title(buf, _("Sell?")); ask->setAttribute(Qt::WA_DeleteOnClose); city_id = pcity_mid->id; diff --git a/client/gui-qt/dialogs.cpp b/client/gui-qt/dialogs.cpp index ddb8776e56..5d4299669f 100644 --- a/client/gui-qt/dialogs.cpp +++ b/client/gui-qt/dialogs.cpp @@ -1197,6 +1197,19 @@ void popup_notify_goto_dialog(const char *headline, const char *lines, ask->show(); } +/***********************************************************************//** + Add "OK" button for messagebox. Translated with gettext() +***************************************************************************/ +static void ok_button_for_messagebox(QMessageBox *box) +{ + QPushButton *button = new QPushButton(_("OK"), box); + QStyle *style = QApplication::style(); + + button->setIcon(style->standardIcon(QStyle::SP_DialogOkButton)); + + box->addButton(button, QMessageBox::AcceptRole); +} + /***********************************************************************//** Popup a dialog to display connection message from server. ***************************************************************************/ @@ -1205,7 +1218,7 @@ void popup_connect_msg(const char *headline, const char *message) QMessageBox *msg = new QMessageBox(gui()->central_wdg); msg->setText(message); - msg->setStandardButtons(QMessageBox::Ok); + ok_button_for_messagebox(msg); msg->setWindowTitle(headline); msg->setAttribute(Qt::WA_DeleteOnClose); msg->show(); @@ -1309,8 +1322,7 @@ void popup_revolution_dialog(struct government *gov) if (0 > client.conn.playing->revolution_finishes) { ask = new hud_message_box(gui()->central_wdg); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->set_text_title(_("You say you wanna revolution?"), _("Revolution!")); ask->setAttribute(Qt::WA_DeleteOnClose); @@ -3674,7 +3686,7 @@ void popup_incite_dialog(struct unit *actor, struct city *tcity, int cost, fc_snprintf(buf2, ARRAY_SIZE(buf2), _("You can't incite a revolt in %s."), city_name_get(tcity)); impossible->set_text_title(buf2, "!"); - impossible->setStandardButtons(QMessageBox::Ok); + impossible->add_default_buttons(0, HUD_BUTTON_OK); impossible->setAttribute(Qt::WA_DeleteOnClose); impossible->show(); } else if (cost <= client_player()->economic.gold) { @@ -3683,8 +3695,7 @@ void popup_incite_dialog(struct unit *actor, struct city *tcity, int cost, fc_snprintf(buf2, ARRAY_SIZE(buf2), PL_("Incite a revolt for %d gold?\n%s", "Incite a revolt for %d gold?\n%s", cost), cost, buf); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->set_text_title(buf2, _("Incite a Revolt!")); ask->setAttribute(Qt::WA_DeleteOnClose); QObject::connect(ask, &hud_message_box::accepted, [=]() { @@ -3701,7 +3712,7 @@ void popup_incite_dialog(struct unit *actor, struct city *tcity, int cost, "Inciting a revolt costs %d gold.\n%s", cost), cost, buf); too_much->set_text_title(buf2, "!"); - too_much->setStandardButtons(QMessageBox::Ok); + too_much->add_default_buttons(0, HUD_BUTTON_OK); too_much->setAttribute(Qt::WA_DeleteOnClose); too_much->show(); } @@ -3736,8 +3747,7 @@ void popup_bribe_dialog(struct unit *actor, struct unit *tunit, int cost, "Bribe unit for %d gold?\n%s", cost), cost, buf); ask->set_text_title(buf2, _("Bribe Enemy Unit")); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); QObject::connect(ask, &hud_message_box::accepted, [=]() { request_do_action(act_id, diplomat_id, diplomat_target_id, 0, ""); @@ -4358,7 +4368,7 @@ void show_tileset_error(bool fatal, const char *tset_name, const char *msg) } ask->setText(buf); - ask->setStandardButtons(QMessageBox::Ok); + ok_button_for_messagebox(ask); ask->setWindowTitle(_("Tileset error")); if (std_gui != nullptr) { @@ -4393,11 +4403,10 @@ void popup_upgrade_dialog(struct unit_list *punits) if (!get_units_upgrade_info(buf, sizeof(buf), punits)) { title = _("Upgrade Unit!"); - ask->setStandardButtons(QMessageBox::Ok); + ask->add_default_buttons(0, HUD_BUTTON_OK); } else { title = _("Upgrade Obsolete Units"); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); } ask->set_text_title(buf, title); ask->setAttribute(Qt::WA_DeleteOnClose); diff --git a/client/gui-qt/gui_main.cpp b/client/gui-qt/gui_main.cpp index 8833b813ab..2ba6436716 100644 --- a/client/gui-qt/gui_main.cpp +++ b/client/gui-qt/gui_main.cpp @@ -566,8 +566,7 @@ void popup_quit_dialog() { hud_message_box *ask = new hud_message_box(gui()->central_wdg); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->set_text_title(_("Are you sure you want to quit?"), _("Quit?")); ask->setAttribute(Qt::WA_DeleteOnClose); QObject::connect(ask, &hud_message_box::accepted, [=]() { diff --git a/client/gui-qt/hudwidget.cpp b/client/gui-qt/hudwidget.cpp index 9f813cafce..f0cf15b4c0 100644 --- a/client/gui-qt/hudwidget.cpp +++ b/client/gui-qt/hudwidget.cpp @@ -107,6 +107,38 @@ hud_message_box::~hud_message_box() delete fm_title; } +/************************************************************************//** + Add requested default button implementations. Labels of these + buttons are translated with gettext() +****************************************************************************/ +void hud_message_box::add_default_buttons(int def, int but) +{ + QPushButton *button; + QStyle *style = QApplication::style(); + + if (but & HUD_BUTTON_OK) { + button = new QPushButton(_("OK"), this); + + button->setIcon(style->standardIcon(QStyle::SP_DialogOkButton)); + addButton(button, QMessageBox::AcceptRole); + + if (def == HUD_BUTTON_OK) { + setDefaultButton(button); + } + } + + if (but & HUD_BUTTON_CANCEL) { + button = new QPushButton(_("Cancel"), this); + + button->setIcon(style->standardIcon(QStyle::SP_DialogCancelButton)); + addButton(button, QMessageBox::RejectRole); + + if (def == HUD_BUTTON_CANCEL) { + setDefaultButton(button); + } + } +} + /************************************************************************//** Key press event for hud message box ****************************************************************************/ diff --git a/client/gui-qt/hudwidget.h b/client/gui-qt/hudwidget.h index 8a7a8a3fb0..f6034ac593 100644 --- a/client/gui-qt/hudwidget.h +++ b/client/gui-qt/hudwidget.h @@ -49,6 +49,9 @@ struct unit_list; void show_new_turn_info(); bool has_player_unit_type(Unit_type_id utype); +#define HUD_BUTTON_OK (1) +#define HUD_BUTTON_CANCEL (1 << 1) + /**************************************************************************** Custom message box with animated background ****************************************************************************/ @@ -61,6 +64,8 @@ public: hud_message_box(QWidget *parent); ~hud_message_box(); int set_text_title(QString s1, QString s2, bool return_exec = false); + void add_default_buttons(int def = HUD_BUTTON_CANCEL, + int but = HUD_BUTTON_OK | HUD_BUTTON_CANCEL); protected: void paintEvent(QPaintEvent *event); diff --git a/client/gui-qt/menu.cpp b/client/gui-qt/menu.cpp index c0b0ccfc24..00dff02464 100644 --- a/client/gui-qt/menu.cpp +++ b/client/gui-qt/menu.cpp @@ -3822,7 +3822,7 @@ void mr_menu::save_image() } map_saved = mapview.store->map_pixmap.save(img_name, "png"); map_canvas_resized(current_width, current_height); - saved->setStandardButtons(QMessageBox::Ok); + saved->add_default_buttons(0, HUD_BUTTON_OK); saved->setDefaultButton(QMessageBox::Cancel); saved->setAttribute(Qt::WA_DeleteOnClose); if (map_saved) { @@ -3878,8 +3878,7 @@ void mr_menu::back_to_menu() if (is_server_running()) { ask = new hud_message_box(gui()->central_wdg); ask->set_text_title(_("Leaving a local game will end it!"), "Leave game"); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, [=]() { @@ -3898,15 +3897,15 @@ void mr_menu::back_to_menu() **************************************************************************/ bool mr_menu::confirm_disruptive_selection() { - hud_message_box* ask = new hud_message_box(gui()->central_wdg); + hud_message_box *ask = new hud_message_box(gui()->central_wdg); ask->setIcon(QMessageBox::Warning); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); + return (ask->set_text_title(_("Selection will cancel current assignments!"), _("Confirm Disruptive Selection"), true) - == QMessageBox::Ok); + == QMessageBox::AcceptRole); } /**********************************************************************//** diff --git a/client/gui-qt/repodlgs.cpp b/client/gui-qt/repodlgs.cpp index f63da073c5..4f05bb8fa8 100644 --- a/client/gui-qt/repodlgs.cpp +++ b/client/gui-qt/repodlgs.cpp @@ -224,8 +224,7 @@ void unittype_item::upgrade_units() utype_name_translation(upgrade), price, buf); s2 = QString(buf2); ask->set_text_title(s2, _("Upgrade Obsolete Units")); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, [=]() { dsend_packet_unit_type_upgrade(&client.conn, type); @@ -1459,8 +1458,7 @@ void eco_report::disband_units() utype_name_translation(utype_by_number(utype)), counter); ask->set_text_title(title, _("Disband Units")); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, [=]() { struct unit_type *putype = utype_by_number(utype); @@ -1473,7 +1471,7 @@ void eco_report::disband_units() result = new hud_message_box(gui()->central_wdg); result->set_text_title(buf, _("Disband Results")); - result->setStandardButtons(QMessageBox::Ok); + result->add_default_buttons(0, HUD_BUTTON_OK); result->setAttribute(Qt::WA_DeleteOnClose); result->show(); }); @@ -1500,8 +1498,7 @@ void eco_report::sell_buildings() improvement_name_translation(pimpr), counter); ask->set_text_title(title, _("Sell Improvements")); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, [=]() { char buf[1024]; @@ -1516,7 +1513,7 @@ void eco_report::sell_buildings() result = new hud_message_box(gui()->central_wdg); result->set_text_title(buf, _("Sell-Off: Results")); - result->setStandardButtons(QMessageBox::Ok); + result->add_default_buttons(0, HUD_BUTTON_OK); result->setAttribute(Qt::WA_DeleteOnClose); result->show(); }); @@ -1543,8 +1540,7 @@ void eco_report::sell_redundant() improvement_name_translation(pimpr), counter); ask->set_text_title(title, _("Sell Improvements")); - ask->setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok); - ask->setDefaultButton(QMessageBox::Cancel); + ask->add_default_buttons(); ask->setAttribute(Qt::WA_DeleteOnClose); connect(ask, &hud_message_box::accepted, [=]() { char buf[1024]; @@ -1559,7 +1555,7 @@ void eco_report::sell_redundant() result = new hud_message_box(gui()->central_wdg); result->set_text_title(buf, _("Sell-Off: Results")); - result->setStandardButtons(QMessageBox::Ok); + result->add_default_buttons(0, HUD_BUTTON_OK); result->setAttribute(Qt::WA_DeleteOnClose); result->show(); }); diff --git a/client/gui-qt/shortcuts.cpp b/client/gui-qt/shortcuts.cpp index 453ea43c42..bef5fdabcb 100644 --- a/client/gui-qt/shortcuts.cpp +++ b/client/gui-qt/shortcuts.cpp @@ -566,8 +566,7 @@ void fc_sc_button::popup_error() title = QString(_("%1 is already assigned to")) .arg(shortcut_to_string(sc)); scinfo = new hud_message_box(gui()->central_wdg); - scinfo->setStandardButtons(QMessageBox::Ok); - scinfo->setDefaultButton(QMessageBox::Ok); + scinfo->add_default_buttons(HUD_BUTTON_OK, HUD_BUTTON_OK); scinfo->set_text_title(err_message, title); scinfo->setAttribute(Qt::WA_DeleteOnClose); scinfo->show(); -- 2.42.0