Freeciv-3.1: /fast/freeciv/nightly/S3_1/client/gui-qt/shortcuts.cpp Source File

Freeciv-3.1

Loading...
Searching...
No Matches
shortcuts.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18
19// Qt
20#include <QDialog>
21#include <QDialogButtonBox>
22#include <QGridLayout>
23#include <QLabel>
24#include <QMouseEvent>
25#include <QLineEdit>
26#include <QScrollArea>
27#include <QSettings>
28#include <QVBoxLayout>
29#include <QWidget>
30
31// client
32#include "options.h"
33
34// gui-qt
35#include "fc_client.h"
36#include "shortcuts.h"
37
38extern "C" void real_menus_init();
39
40 static QHash<int, const char *> key_map;
41static QString button_name(Qt::MouseButton bt);
42static QMap<shortcut_id, fc_shortcut*>* hash_copy(QMap<shortcut_id, fc_shortcut*> *h);
43 fc_shortcuts *fc_shortcuts::m_instance = 0;
44QMap<shortcut_id, fc_shortcut*> fc_shortcuts::hash = QMap<shortcut_id, fc_shortcut*>();
45
46enum {
47 RESPONSE_CANCEL,
48 RESPONSE_OK,
49 RESPONSE_APPLY,
50 RESPONSE_RESET,
51 RESPONSE_SAVE
52};
53
54 static bool use_default_shortcuts = false;
55
56 static int num_shortcuts = SC_NUM_SHORTCUTS;
57
58 fc_shortcut default_shortcuts[] = {
59 {SC_SCROLL_MAP, 0, Qt::RightButton, Qt::NoModifier, "Scroll map" },
60 {SC_CENTER_VIEW, Qt::Key_C, Qt::AllButtons, Qt::NoModifier,
61 _("Center View") },
62 {SC_FULLSCREEN, Qt::Key_Return, Qt::AllButtons, Qt::AltModifier,
63 _("Fullscreen") },
64 {SC_MINIMAP, Qt::Key_H, Qt::AllButtons, Qt::ShiftModifier,
65 _("Show minimap") },
66 {SC_CITY_OUTPUT, Qt::Key_V, Qt::AllButtons, Qt::ControlModifier,
67 _("City Output") },
68 {SC_MAP_GRID, Qt::Key_G, Qt::AllButtons, Qt::ControlModifier,
69 _("Map Grid") },
70 {SC_NAT_BORDERS, Qt::Key_B, Qt::AllButtons, Qt::ControlModifier,
71 _("National Borders") },
72 {SC_QUICK_BUY, 0, Qt::LeftButton, Qt::ControlModifier | Qt::ShiftModifier,
73 _("Quick buy from map") },
74 {SC_QUICK_SELECT, 0, Qt::LeftButton, Qt::ControlModifier,
75 _("Quick production select from map") },
76 {SC_SELECT_BUTTON, 0, Qt::LeftButton, Qt::NoModifier,
77 _("Select button") },
78 {SC_ADJUST_WORKERS, 0, Qt::LeftButton,
79 Qt::MetaModifier | Qt::ControlModifier, _("Adjust workers") },
80 {SC_APPEND_FOCUS, 0, Qt::LeftButton, Qt::MetaModifier,
81 _("Append focus") },
82 {SC_POPUP_INFO, 0, Qt::MiddleButton, Qt::NoModifier,
83 _("Popup tile info") },
84 {SC_WAKEUP_SENTRIES, 0, Qt::MiddleButton, Qt::ControlModifier,
85 _("Wakeup sentries") },
86 {SC_MAKE_LINK, 0, Qt::RightButton, Qt::ControlModifier | Qt::AltModifier,
87 _("Show link to tile") },
88 {SC_PASTE_PROD, 0, Qt::RightButton,
89 Qt::ShiftModifier | Qt::ControlModifier, _("Paste production") },
90 {SC_COPY_PROD, 0, Qt::RightButton,
91 Qt::ShiftModifier, _("Copy production") },
92 {SC_HIDE_WORKERS, 0, Qt::RightButton,
93 Qt::ShiftModifier | Qt::AltModifier, _("Show/hide workers") },
94 {SC_SHOW_UNITS, Qt::Key_Space, Qt::AllButtons, Qt::ControlModifier,
95 _("Units selection (for tile under mouse position)") },
96 {SC_TRADE_ROUTES, Qt::Key_D, Qt::AllButtons, Qt::ControlModifier,
97 _("City Trade Routes") },
98 {SC_CITY_PROD, Qt::Key_P, Qt::AllButtons, Qt::ControlModifier,
99 _("City Production Levels") },
100 {SC_CITY_NAMES, Qt::Key_N, Qt::AllButtons, Qt::ControlModifier,
101 _("City Names") },
102 {SC_DONE_MOVING, Qt::Key_Space, Qt::AllButtons, Qt::NoModifier,
103 _("Done Moving") },
104 {SC_GOTOAIRLIFT, Qt::Key_T, Qt::AllButtons, Qt::NoModifier,
105 _("Go to/Airlift to City...") },
106 {SC_AUTOEXPLORE, Qt::Key_X, Qt::AllButtons, Qt::NoModifier,
107 _("Auto Explore") },
108 {SC_PATROL, Qt::Key_Q, Qt::AllButtons, Qt::NoModifier,
109 _("Patrol") },
110 {SC_UNSENTRY_TILE, Qt::Key_D, Qt::AllButtons,
111 Qt::ShiftModifier | Qt::ControlModifier, _("Unsentry All On Tile") },
112 {SC_DO, Qt::Key_D, Qt::AllButtons, Qt::NoModifier, _("Do...") },
113 {SC_UPGRADE_UNIT, Qt::Key_U, Qt::AllButtons, Qt::ShiftModifier,
114 _("Upgrade") },
115 {SC_SETHOME, Qt::Key_H, Qt::AllButtons, Qt::NoModifier,
116 _("Set Home City") },
117 {SC_BUILDMINE, Qt::Key_M, Qt::AllButtons, Qt::NoModifier,
118 _("Build Mine") },
119 {SC_PLANT, Qt::Key_M, Qt::AllButtons, Qt::ShiftModifier,
120 _("Plant") },
121 {SC_BUILDIRRIGATION, Qt::Key_I, Qt::AllButtons, Qt::NoModifier,
122 _("Build Irrigation") },
123 {SC_CULTIVATE, Qt::Key_I, Qt::AllButtons, Qt::ShiftModifier,
124 _("Cultivate") },
125 {SC_BUILDROAD, Qt::Key_R, Qt::AllButtons, Qt::NoModifier,
126 _("Build Road") },
127 {SC_BUILDCITY, Qt::Key_B, Qt::AllButtons, Qt::NoModifier,
128 _("Build City") },
129 {SC_SENTRY, Qt::Key_S, Qt::AllButtons, Qt::NoModifier,
130 _("Sentry") },
131 {SC_FORTIFY, Qt::Key_F, Qt::AllButtons, Qt::NoModifier,
132 _("Fortify") },
133 {SC_GOTO, Qt::Key_G, Qt::AllButtons, Qt::NoModifier,
134 _("Go to Tile") },
135 {SC_WAIT, Qt::Key_W, Qt::AllButtons, Qt::NoModifier,
136 _("Wait") },
137 {SC_TRANSFORM, Qt::Key_O, Qt::AllButtons, Qt::NoModifier,
138 _("Transform") },
139 {SC_NUKE, Qt::Key_N, Qt::AllButtons, Qt::ShiftModifier,
140 _("Explode Nuclear") },
141 {SC_BOARD, Qt::Key_L, Qt::AllButtons, Qt::NoModifier,
142 _("Load") },
143 {SC_DEBOARD, Qt::Key_U, Qt::AllButtons, Qt::NoModifier,
144 _("Unload") },
145 {SC_BUY_MAP, 0, Qt::BackButton, Qt::NoModifier,
146 _("Quick buy current production from map") },
147 {SC_IFACE_LOCK, Qt::Key_L, Qt::AllButtons, Qt::ControlModifier
148 | Qt::ShiftModifier, _("Lock/unlock interface") },
149 {SC_AUTOMATE, Qt::Key_A, Qt::AllButtons, Qt::NoModifier,
150 _("Auto worker") },
151 {SC_PARADROP, Qt::Key_P, Qt::AllButtons, Qt::NoModifier,
152 _("Paradrop/clean pollution") },
153 {SC_POPUP_COMB_INF, Qt::Key_F1, Qt::AllButtons, Qt::ControlModifier,
154 _("Popup combat information") },
155 {SC_RELOAD_THEME, Qt::Key_F5, Qt::AllButtons, Qt::ControlModifier
156 | Qt::ShiftModifier, _("Reload theme") },
157 {SC_RELOAD_TILESET, Qt::Key_F6, Qt::AllButtons, Qt::ControlModifier
158 | Qt::ShiftModifier, _("Reload tileset") },
159 {SC_SHOW_FULLBAR, Qt::Key_F, Qt::AllButtons, Qt::ControlModifier,
160 _("Toggle city full bar visibility") },
161 {SC_ZOOM_IN, Qt::Key_Plus, Qt::AllButtons, Qt::NoModifier,
162 _("Reload zoomed in tileset") },
163 {SC_ZOOM_OUT, Qt::Key_Minus, Qt::AllButtons, Qt::NoModifier,
164 _("Reload zoomed out tileset") },
165 {SC_LOAD_LUA, Qt::Key_J, Qt::AllButtons, Qt::ControlModifier
166 | Qt::ShiftModifier, _("Load Lua script") },
167 {SC_RELOAD_LUA, Qt::Key_K, Qt::AllButtons, Qt::ControlModifier
168 | Qt::ShiftModifier, _("Load last loaded Lua script") },
169 {SC_ZOOM_RESET, Qt::Key_Backspace, Qt::AllButtons, Qt::ControlModifier,
170 _("Reload tileset with default scale") },
171 {SC_GOBUILDCITY, Qt::Key_B, Qt::AllButtons, Qt::ShiftModifier,
172 _("Go And Build City") },
173 {SC_GOJOINCITY, Qt::Key_J, Qt::AllButtons, Qt::ShiftModifier,
174 _("Go And Join City") },
175 {SC_STACK_SIZE, Qt::Key_Plus, Qt::AllButtons, Qt::ControlModifier,
176 _("Unit Stack Size") }
177};
178
179
180/**********************************************************************/
183QString shortcut_to_string(fc_shortcut *sc)
184{
185 QString ret, m, bn, k;
186
187 if (sc == nullptr) {
188 return "";
189 }
190 if (sc->mod != Qt::NoModifier) {
191 m = QKeySequence(sc->mod).toString(QKeySequence::NativeText);
192 }
193 if (sc->mouse != Qt::AllButtons) {
194 bn = button_name(sc->mouse);
195 }
196 if (sc->key != 0) {
197 k = QKeySequence(sc->key).toString(QKeySequence::NativeText);
198 }
199 ret = m + bn + k;
200
201 return ret;
202}
203
204/**********************************************************************/
207fc_shortcuts::fc_shortcuts()
208{
209 init_default(true);
210}
211
212/**********************************************************************/
215fc_shortcuts::~fc_shortcuts()
216{
217 qDeleteAll(hash.begin(), hash.end());
218 hash.clear();
219}
220
221
222/**********************************************************************/
225QString fc_shortcuts::get_desc(shortcut_id id)
226{
227 fc_shortcut *s;
228 s = hash.value(id);
229 return s->str;
230}
231
232/**********************************************************************/
235 fc_shortcut *fc_shortcuts::get_shortcut(shortcut_id id)
236{
237 fc_shortcut *s;
238 s = hash.value(id);
239 return s;
240}
241
242/**********************************************************************/
245 shortcut_id fc_shortcuts::get_id(fc_shortcut *sc)
246{
247 return hash.key(sc);
248}
249
250/**********************************************************************/
253 void fc_shortcuts::set_shortcut(fc_shortcut *s)
254{
255 fc_shortcut *sc;
256
257 sc = hash.value(s->id);
258 sc->key = s->key;
259 sc->mod = s->mod;
260 sc->mouse = s->mouse;
261}
262
263/**********************************************************************/
266 void fc_shortcuts::drop()
267{
268 if (m_instance) {
269 delete m_instance;
270 m_instance = nullptr;
271 }
272}
273
274/**********************************************************************/
277 fc_shortcuts *fc_shortcuts::sc()
278{
279 if (!m_instance) {
280 m_instance = new fc_shortcuts;
281 }
282
283 return m_instance;
284}
285
286/**********************************************************************/
290 bool fc_shortcuts::is_instantiated()
291{
292 return m_instance != nullptr;
293}
294
295/**********************************************************************/
298 void fc_shortcuts::init_default(bool read)
299{
300 int i;
301 bool suc = false;
302
303 hash.clear();
304
305 if (read) {
306 suc = read_shortcuts();
307 }
308
309 for (i = 0 ; i < num_shortcuts; i++) {
310 fc_shortcut *s = nullptr;
311
312 if (suc) {
313 s = fc_shortcuts::hash.value(static_cast< shortcut_id>(i + 1));
314 }
315 if (s == nullptr) {
316 s = new fc_shortcut();
317 s->id = default_shortcuts[i].id;
318 s->key = default_shortcuts[i].key;
319 s->mouse = default_shortcuts[i].mouse;
320 s->mod = default_shortcuts[i].mod;
321 s->str = default_shortcuts[i].str;
322 hash.insert(default_shortcuts[i].id, s);
323 }
324 }
325}
326
327/**********************************************************************/
330fc_shortcut_popup::fc_shortcut_popup(QWidget *parent): QDialog()
331{
332 QVBoxLayout *vb = new QVBoxLayout;
333 setParent(parent);
334 setAttribute(Qt::WA_DeleteOnClose);
335 setWindowFlags(Qt::Popup);
336 vb->addWidget(&edit);
337 setLayout(vb);
338 sc = nullptr;
339}
340
341/**********************************************************************/
344line_edit::line_edit(): QLineEdit()
345{
346 setContextMenuPolicy(Qt::NoContextMenu);
347 setWindowModality(Qt::WindowModal);
348}
349
350/**********************************************************************/
353 void line_edit::mousePressEvent(QMouseEvent *event)
354{
355 fc_shortcut_popup *fcp;
356 shc.mouse = event->button();
357 shc.mod = event->modifiers();
358 fcp = reinterpret_cast< fc_shortcut_popup *>(parentWidget());
359 fcp->sc->mouse = shc.mouse;
360 fcp->sc->mod = shc.mod;
361 fcp->sc->key = 0;
362 parentWidget()->close();
363}
364
365/**********************************************************************/
368 void line_edit::keyReleaseEvent(QKeyEvent *event)
369{
370 fc_shortcut_popup *fcp;
371
372 shc.key = event->key();
373 shc.mod = event->modifiers();
374 fcp = reinterpret_cast< fc_shortcut_popup *>(parentWidget());
375 fcp->sc->mouse = Qt::AllButtons;
376 fcp->sc->key = shc.key;
377 fcp->sc->mod = shc.mod;
378 parentWidget()->close();
379}
380
381/**********************************************************************/
384 void fc_shortcut_popup::run(fc_shortcut *s)
385{
386 QPoint p(50, 20);
387 edit.setReadOnly(true);
388 edit.setFocus();
389 setWindowModality(Qt::WindowModal);
390 sc = s;
391 move(QCursor::pos() - p);
392 show();
393}
394
395/**********************************************************************/
398 void fc_shortcut_popup::closeEvent(QCloseEvent *ev)
399{
400 fc_sc_button *scp;
401
402 if (sc != nullptr) {
403 if (!check_if_exist()) {
404 scp = reinterpret_cast< fc_sc_button *>(parentWidget());
405 scp->setText(shortcut_to_string(scp->sc));
406 fc_shortcuts::sc()->set_shortcut(sc);
407 }
408 }
409
410 QDialog::closeEvent(ev);
411}
412
413/**********************************************************************/
417 bool fc_shortcut_popup::check_if_exist()
418{
419 fc_shortcut *fsc;
420 QString desc;
421 int id = 0;
422
423 desc = "";
424 if (sc != nullptr) {
425 foreach (fsc, fc_shortcuts::sc()->hash) {
426 if (id == 0) {
427 id++;
428 continue;
429 }
430 if (*sc == *fsc) {
431 desc = fc_shortcuts::sc()->get_desc(static_cast< shortcut_id>(id + 1));
432 }
433 id++;
434 }
435 if (desc.isEmpty()) {
436 desc = gui()->menu_bar->shortcut_exist(sc);
437 }
438 if (!desc.isEmpty()) {
439 fc_sc_button *fsb;
440 fsb = qobject_cast<fc_sc_button*>(parentWidget());
441 fsb->show_info(desc);
442 return true;
443 }
444 }
445
446 return false;
447}
448
449/**********************************************************************/
452QString button_name(Qt::MouseButton bt)
453{
454 switch (bt) {
455 case Qt::NoButton:
456 return _("NoButton");
457 case Qt::LeftButton:
458 return _("LeftButton");
459 case Qt::RightButton:
460 return _("RightButton");
461 case Qt::MiddleButton:
462 return _("MiddleButton");
463 case Qt::BackButton:
464 return _("BackButton");
465 case Qt::ForwardButton:
466 return _("ForwardButton");
467 case Qt::TaskButton:
468 return _("TaskButton");
469 case Qt::ExtraButton4:
470 return _("ExtraButton4");
471 case Qt::ExtraButton5:
472 return _("ExtraButton5");
473 case Qt::ExtraButton6:
474 return _("ExtraButton6");
475 case Qt::ExtraButton7:
476 return _("ExtraButton7");
477 case Qt::ExtraButton8:
478 return _("ExtraButton8");
479 case Qt::ExtraButton9:
480 return _("ExtraButton9");
481 case Qt::ExtraButton10:
482 return _("ExtraButton10");
483 case Qt::ExtraButton11:
484 return _("ExtraButton11");
485 case Qt::ExtraButton12:
486 return _("ExtraButton12");
487 case Qt::ExtraButton13:
488 return _("ExtraButton13");
489 case Qt::ExtraButton14:
490 return _("ExtraButton14");
491 case Qt::ExtraButton15:
492 return _("ExtraButton15");
493 case Qt::ExtraButton16:
494 return _("ExtraButton16");
495 case Qt::ExtraButton17:
496 return _("ExtraButton17");
497 case Qt::ExtraButton18:
498 return _("ExtraButton18");
499 case Qt::ExtraButton19:
500 return _("ExtraButton19");
501 case Qt::ExtraButton20:
502 return _("ExtraButton20");
503 case Qt::ExtraButton21:
504 return _("ExtraButton21");
505 case Qt::ExtraButton22:
506 return _("ExtraButton22");
507 case Qt::ExtraButton23:
508 return _("ExtraButton23");
509 case Qt::ExtraButton24:
510 return _("ExtraButton24");
511 default:
512 return "";
513 }
514}
515
516/**********************************************************************/
519fc_sc_button::fc_sc_button(): QPushButton()
520{
521 sc = new fc_shortcut;
522}
523
524/**********************************************************************/
527 fc_sc_button::fc_sc_button(fc_shortcut *s): QPushButton()
528{
529 sc_orig = s;
530 sc = new fc_shortcut;
531 sc->id = sc_orig->id;
532 sc->key = sc_orig->key;
533 sc->mouse = sc_orig->mouse;
534 sc->mod = sc_orig->mod;
535 sc->str = sc_orig->str;
536 setText(shortcut_to_string(sc));
537}
538
539/**********************************************************************/
542 void fc_sc_button::show_info(QString str)
543{
544 err_message = str;
545 popup_error();
546}
547
548/**********************************************************************/
551 void fc_sc_button::popup_error()
552{
553 hud_message_box *scinfo;
554 QList<fc_shortcut_popup *> fsb_list;
555 QString title;
556
557 /* wait until shortcut popup is destroyed */
558 fsb_list = findChildren<fc_shortcut_popup *>();
559 if (fsb_list.count() > 0) {
560 QTimer::singleShot(20, this, SLOT(popup_error()));
561 return;
562 }
563
564 /* TRANS: Given shortcut(%1) is already assigned */
565 title = QString(_("%1 is already assigned to"))
566 .arg(shortcut_to_string(sc));
567 scinfo = new hud_message_box(gui()->central_wdg);
568 scinfo->setStandardButtons(QMessageBox::Ok);
569 scinfo->setDefaultButton(QMessageBox::Ok);
570 scinfo->set_text_title(err_message, title);
571 scinfo->setAttribute(Qt::WA_DeleteOnClose);
572 scinfo->show();
573}
574
575/**********************************************************************/
578fc_shortcuts_dialog::fc_shortcuts_dialog(QWidget *parent)
579 : QDialog(parent)
580{
581 setWindowTitle(_("Shortcuts options"));
582 hashcopy = hash_copy(&fc_shortcuts::hash);
583 init();
584}
585
586/**********************************************************************/
589fc_shortcuts_dialog::~fc_shortcuts_dialog()
590{
591}
592
593/**********************************************************************/
596 void fc_shortcuts_dialog::init()
597{
598 fc_shortcut *sc;
599 QPushButton *but;
600 QScrollArea *scroll;
601 QSize size;
602 QString desc;
603 QWidget *widget;
604 shortcut_id id;
605
606 widget = new QWidget(this);
607 scroll = new QScrollArea(this);
608 scroll->setWidgetResizable(true);
609 scroll_layout = new QVBoxLayout;
610 main_layout = new QVBoxLayout;
611 foreach (sc, fc_shortcuts::sc()->hash) {
612 id = fc_shortcuts::sc()->get_id(sc);
613 desc = fc_shortcuts::sc()->get_desc(id);
614 add_option(sc);
615 }
616 widget->setProperty("doomed", true);
617 widget->setLayout(scroll_layout);
618 scroll->setWidget(widget);
619 main_layout->addWidget(scroll);
620
621 button_box = new QDialogButtonBox();
622 but = new QPushButton(style()->standardIcon(QStyle::SP_DialogCancelButton),
623 _("Cancel"));
624 button_box->addButton(but, QDialogButtonBox::ActionRole);
625 QObject::connect(but, &QPushButton::clicked, [this]() {
626 apply_option(RESPONSE_CANCEL);
627 });
628
629 but = new QPushButton(style()->standardIcon(QStyle::SP_DialogResetButton),
630 _("Reset"));
631 button_box->addButton(but, QDialogButtonBox::ResetRole);
632 QObject::connect(but, &QPushButton::clicked, [this]() {
633 apply_option(RESPONSE_RESET);
634 });
635
636 but = new QPushButton(style()->standardIcon(QStyle::SP_DialogApplyButton),
637 _("Apply"));
638 button_box->addButton(but, QDialogButtonBox::ActionRole);
639 QObject::connect(but, &QPushButton::clicked, [this]() {
640 apply_option(RESPONSE_APPLY);
641 });
642
643 but = new QPushButton(style()->standardIcon(QStyle::SP_DialogSaveButton),
644 _("Save"));
645 button_box->addButton(but, QDialogButtonBox::ActionRole);
646 QObject::connect(but, &QPushButton::clicked, [this]() {
647 apply_option(RESPONSE_SAVE);
648 });
649
650 but = new QPushButton(style()->standardIcon(QStyle::SP_DialogOkButton),
651 _("Ok"));
652 button_box->addButton(but, QDialogButtonBox::ActionRole);
653 QObject::connect(but, &QPushButton::clicked, [this]() {
654 apply_option(RESPONSE_OK);
655 });
656
657 main_layout->addWidget(button_box);
658 setLayout(main_layout);
659 size = sizeHint();
660 size.setWidth(size.width() + 10
661 + style()->pixelMetric(QStyle::PM_ScrollBarExtent));
662 resize(size);
663 setAttribute(Qt::WA_DeleteOnClose);
664}
665
666/**********************************************************************/
669 void fc_shortcuts_dialog::add_option(fc_shortcut *sc)
670{
671 QHBoxLayout *hb;
672 QLabel *l;
673
674 l = new QLabel(sc->str);
675 hb = new QHBoxLayout();
676
677 fc_sc_button *fb = new fc_sc_button(sc);
678 connect(fb, &QAbstractButton::clicked, this, &fc_shortcuts_dialog::edit_shortcut);
679
680 hb->addWidget(l, 1, Qt::AlignLeft);
681 hb->addStretch();
682 hb->addWidget(fb, 1, Qt::AlignRight);
683
684 scroll_layout->addLayout(hb);
685}
686
687/**********************************************************************/
690 void fc_shortcuts_dialog::edit_shortcut()
691{
692 fc_sc_button *pb;
693 pb = qobject_cast<fc_sc_button *>(sender());
694 fc_shortcut_popup *sb = new fc_shortcut_popup(pb);
695 sb->run(pb->sc);
696}
697
698/**********************************************************************/
701 void fc_shortcuts_dialog::refresh()
702{
703 QLayout *layout;
704 QLayoutItem *item;
705 QWidget *widget;
706
707 layout = main_layout;
708 while ((item = layout->takeAt(0))) {
709 if (item->layout() != nullptr) {
710 // Nothing
711 } else if ((widget = item->widget()) != nullptr) {
712 widget->hide();
713 delete widget;
714 } else {
715 delete item;
716 }
717 }
718 delete main_layout;
719 init();
720}
721
722/**********************************************************************/
725 void fc_shortcuts_dialog::apply_option(int response)
726{
727 switch (response) {
728 case RESPONSE_APPLY:
729 real_menus_init();
730 gui()->menuBar()->setVisible(true);
731 break;
732 case RESPONSE_CANCEL:
733 fc_shortcuts::hash = *hashcopy;
734 fc_shortcuts::hash.detach();
735 close();
736 break;
737 case RESPONSE_OK:
738 real_menus_init();
739 gui()->menuBar()->setVisible(true);
740 close();
741 break;
742 case RESPONSE_SAVE:
743 write_shortcuts();
744 break;
745 case RESPONSE_RESET:
746 fc_shortcuts::sc()->init_default(false);
747 refresh();
748 break;
749 }
750}
751
752/**********************************************************************/
755 void popup_shortcuts_dialog()
756{
757 fc_shortcuts_dialog *sh = new fc_shortcuts_dialog(gui());
758 sh->show();
759}
760
761/**********************************************************************/
764QMap<shortcut_id, fc_shortcut *> *hash_copy(QMap<shortcut_id, fc_shortcut *> *h)
765{
766 QMap<shortcut_id, fc_shortcut*> *new_hash;
767 fc_shortcut *s;
768 fc_shortcut *sc;
769 int i;
770 shortcut_id id;
771 new_hash = new QMap<shortcut_id, fc_shortcut*>;
772
773 for (i = 1 ; i < num_shortcuts + 1; i++) {
774 sc = new fc_shortcut();
775 id = static_cast< shortcut_id>(i);
776 s = h->value(id);
777 sc->id = id;
778 sc->key = s->key;
779 sc->mouse = s->mouse;
780 sc->mod = s->mod;
781 sc->str = s->str;
782 new_hash->insert(sc->id, sc);
783 }
784
785 return new_hash;
786}
787
788/**********************************************************************/
791 void write_shortcuts()
792{
793 fc_shortcut *sc;
794 QMap<shortcut_id, fc_shortcut*> h = fc_shortcuts::hash;
795 // This can't be shared between freeciv versions as shortcut
796 // ids can change incompatible way between versions.
797 QString sname = "freeciv-qt-client-sc-"
798 + QString::number(MAJOR_NEW_OPTION_FILE_NAME) + "."
799 + QString::number(MINOR_NEW_OPTION_FILE_NAME);
800 QSettings s(QSettings::IniFormat, QSettings::UserScope,
801 sname);
802
803 if (!fc_shortcuts::is_instantiated()) {
804 // Shortcuts not even initialized yet. Happens e.g. when we quit from
805 // the main menu without ever visiting the game.
806 return;
807 }
808
809 s.beginWriteArray("Shortcuts");
810 for (int i = 0; i < num_shortcuts; ++i) {
811 s.setArrayIndex(i);
812 sc = h.value(static_cast< shortcut_id>(i + 1));
813 s.setValue("id", sc->id);
814 s.setValue("key", sc->key);
815 s.setValue("mouse", sc->mouse);
816 s.setValue("mod", QVariant(sc->mod));
817 }
818 s.endArray();
819}
820
821/**********************************************************************/
824 bool read_shortcuts()
825{
826 int num, i;
827 fc_shortcut *sc;
828
829 if (use_default_shortcuts) {
830 return false;
831 }
832
833 QString sname = "freeciv-qt-client-sc-"
834 + QString::number(MAJOR_NEW_OPTION_FILE_NAME) + "."
835 + QString::number(MINOR_NEW_OPTION_FILE_NAME);
836 QSettings s(QSettings::IniFormat, QSettings::UserScope,
837 sname);
838
839 num = s.beginReadArray("Shortcuts");
840 if (num == num_shortcuts) {
841 for (i = 0; i < num_shortcuts; ++i) {
842 s.setArrayIndex(i);
843 sc = new fc_shortcut();
844 sc->id = static_cast< shortcut_id>(s.value("id").toInt());
845 sc->key = s.value("key").toInt();
846 sc->mouse = static_cast<Qt::MouseButton>(s.value("mouse").toInt());
847 sc->mod = static_cast<Qt::KeyboardModifiers>(s.value("mod").toInt());
848 sc->str = default_shortcuts[i].str;
849 fc_shortcuts::hash.insert(sc->id, sc);
850 }
851 } else {
852 s.endArray();
853
854 return false;
855 }
856 s.endArray();
857
858 return true;
859}
860
861/**********************************************************************/
864 void shortcutreset()
865{
866 use_default_shortcuts = true;
867}
str
#define str
Definition astring.c:76
fc_sc_button
Definition shortcuts.h:173
fc_sc_button::fc_sc_button
fc_sc_button()
Definition shortcuts.cpp:519
fc_sc_button::sc_orig
fc_shortcut * sc_orig
Definition shortcuts.h:180
fc_sc_button::show_info
void show_info(QString str)
Definition shortcuts.cpp:542
fc_sc_button::popup_error
void popup_error()
Definition shortcuts.cpp:551
fc_sc_button::sc
fc_shortcut * sc
Definition shortcuts.h:179
fc_sc_button::err_message
QString err_message
Definition shortcuts.h:175
fc_shortcut_popup
Definition shortcuts.h:156
fc_shortcut_popup::closeEvent
void closeEvent(QCloseEvent *)
Definition shortcuts.cpp:398
fc_shortcut_popup::check_if_exist
bool check_if_exist()
Definition shortcuts.cpp:417
fc_shortcut_popup::sc
fc_shortcut * sc
Definition shortcuts.h:161
fc_shortcut_popup::edit
line_edit edit
Definition shortcuts.h:166
fc_shortcut_popup::run
void run(fc_shortcut *s)
Definition shortcuts.cpp:384
fc_shortcut_popup::fc_shortcut_popup
fc_shortcut_popup(QWidget *parent)
Definition shortcuts.cpp:330
fc_shortcuts_dialog
Definition shortcuts.h:190
fc_shortcuts_dialog::init
void init()
Definition shortcuts.cpp:596
fc_shortcuts_dialog::~fc_shortcuts_dialog
virtual ~fc_shortcuts_dialog()
Definition shortcuts.cpp:589
fc_shortcuts_dialog::add_option
void add_option(fc_shortcut *sc)
Definition shortcuts.cpp:669
fc_shortcuts_dialog::apply_option
void apply_option(int response)
Definition shortcuts.cpp:725
fc_shortcuts_dialog::fc_shortcuts_dialog
fc_shortcuts_dialog(QWidget *parent=0)
Definition shortcuts.cpp:578
fc_shortcuts_dialog::edit_shortcut
void edit_shortcut()
Definition shortcuts.cpp:690
fc_shortcuts_dialog::button_box
QDialogButtonBox * button_box
Definition shortcuts.h:194
fc_shortcuts_dialog::scroll_layout
QVBoxLayout * scroll_layout
Definition shortcuts.h:193
fc_shortcuts_dialog::hashcopy
QMap< shortcut_id, fc_shortcut * > * hashcopy
Definition shortcuts.h:195
fc_shortcuts_dialog::main_layout
QVBoxLayout * main_layout
Definition shortcuts.h:192
fc_shortcuts_dialog::refresh
void refresh()
Definition shortcuts.cpp:701
fc_shortcuts
Definition shortcuts.h:119
fc_shortcuts::m_instance
static fc_shortcuts * m_instance
Definition shortcuts.h:122
fc_shortcuts::hash
static QMap< shortcut_id, fc_shortcut * > hash
Definition shortcuts.h:129
fc_shortcuts::sc
static fc_shortcuts * sc()
Definition shortcuts.cpp:277
fc_shortcuts::init_default
static void init_default(bool read)
Definition shortcuts.cpp:298
fc_shortcuts::drop
static void drop()
Definition shortcuts.cpp:266
fc_shortcuts::get_desc
QString get_desc(shortcut_id id)
Definition shortcuts.cpp:225
fc_shortcuts::get_shortcut
fc_shortcut * get_shortcut(shortcut_id id)
Definition shortcuts.cpp:235
fc_shortcuts::is_instantiated
static bool is_instantiated()
Definition shortcuts.cpp:290
fc_shortcuts::set_shortcut
void set_shortcut(fc_shortcut *sc)
Definition shortcuts.cpp:253
fc_shortcuts::get_id
shortcut_id get_id(fc_shortcut *sc)
Definition shortcuts.cpp:245
fc_shortcuts::~fc_shortcuts
~fc_shortcuts()
Definition shortcuts.cpp:215
fc_shortcuts::fc_shortcuts
fc_shortcuts()
Definition shortcuts.cpp:207
hud_message_box
Definition hudwidget.h:56
hud_message_box::set_text_title
int set_text_title(QString s1, QString s2, bool return_exec=false)
Definition hudwidget.cpp:126
line_edit::mousePressEvent
void mousePressEvent(QMouseEvent *event)
Definition shortcuts.cpp:353
line_edit::shc
fc_shortcut shc
Definition shortcuts.h:146
line_edit::keyReleaseEvent
void keyReleaseEvent(QKeyEvent *event)
Definition shortcuts.cpp:368
line_edit::line_edit
line_edit()
Definition shortcuts.cpp:344
id
int int id
Definition editgui_g.h:28
event
enum event_type event
Definition events.c:81
fc_client.h
_
#define _(String)
Definition fcintl.h:67
layout
static PangoLayout * layout
Definition canvas.c:331
real_menus_init
void real_menus_init(void)
Definition menu.c:2836
RESPONSE_SAVE
@ RESPONSE_SAVE
Definition optiondlg.c:66
RESPONSE_APPLY
@ RESPONSE_APPLY
Definition optiondlg.c:63
RESPONSE_CANCEL
@ RESPONSE_CANCEL
Definition optiondlg.c:61
RESPONSE_OK
@ RESPONSE_OK
Definition optiondlg.c:62
RESPONSE_RESET
@ RESPONSE_RESET
Definition optiondlg.c:64
title
const char * title
Definition repodlgs.c:1313
hash
static GHashTable * hash
Definition wldlg.c:320
gui
static mpgui * gui
Definition mpgui_qt.cpp:52
options.h
MAJOR_NEW_OPTION_FILE_NAME
#define MAJOR_NEW_OPTION_FILE_NAME
Definition options.h:37
MINOR_NEW_OPTION_FILE_NAME
#define MINOR_NEW_OPTION_FILE_NAME
Definition options.h:41
key_map
static QHash< int, const char * > key_map
Definition shortcuts.cpp:40
popup_shortcuts_dialog
void popup_shortcuts_dialog()
Definition shortcuts.cpp:755
default_shortcuts
fc_shortcut default_shortcuts[]
Definition shortcuts.cpp:58
read_shortcuts
bool read_shortcuts()
Definition shortcuts.cpp:824
shortcutreset
void shortcutreset()
Definition shortcuts.cpp:864
shortcut_to_string
QString shortcut_to_string(fc_shortcut *sc)
Definition shortcuts.cpp:183
hash_copy
static QMap< shortcut_id, fc_shortcut * > * hash_copy(QMap< shortcut_id, fc_shortcut * > *h)
Definition shortcuts.cpp:764
real_menus_init
void real_menus_init()
Definition menu.c:2836
use_default_shortcuts
static bool use_default_shortcuts
Definition shortcuts.cpp:54
button_name
static QString button_name(Qt::MouseButton bt)
Definition shortcuts.cpp:452
write_shortcuts
void write_shortcuts()
Definition shortcuts.cpp:791
num_shortcuts
static int num_shortcuts
Definition shortcuts.cpp:56
RESPONSE_SAVE
@ RESPONSE_SAVE
Definition shortcuts.cpp:51
RESPONSE_APPLY
@ RESPONSE_APPLY
Definition shortcuts.cpp:49
RESPONSE_CANCEL
@ RESPONSE_CANCEL
Definition shortcuts.cpp:47
RESPONSE_OK
@ RESPONSE_OK
Definition shortcuts.cpp:48
RESPONSE_RESET
@ RESPONSE_RESET
Definition shortcuts.cpp:50
shortcuts.h
shortcut_id
shortcut_id
Definition shortcuts.h:33
SC_PARADROP
@ SC_PARADROP
Definition shortcuts.h:82
SC_UPGRADE_UNIT
@ SC_UPGRADE_UNIT
Definition shortcuts.h:63
SC_BUILDIRRIGATION
@ SC_BUILDIRRIGATION
Definition shortcuts.h:67
SC_BUILDCITY
@ SC_BUILDCITY
Definition shortcuts.h:70
SC_DONE_MOVING
@ SC_DONE_MOVING
Definition shortcuts.h:57
SC_BUILDROAD
@ SC_BUILDROAD
Definition shortcuts.h:69
SC_UNSENTRY_TILE
@ SC_UNSENTRY_TILE
Definition shortcuts.h:61
SC_CULTIVATE
@ SC_CULTIVATE
Definition shortcuts.h:68
SC_DO
@ SC_DO
Definition shortcuts.h:62
SC_CENTER_VIEW
@ SC_CENTER_VIEW
Definition shortcuts.h:36
SC_POPUP_COMB_INF
@ SC_POPUP_COMB_INF
Definition shortcuts.h:83
SC_SENTRY
@ SC_SENTRY
Definition shortcuts.h:71
SC_GOBUILDCITY
@ SC_GOBUILDCITY
Definition shortcuts.h:92
SC_NUKE
@ SC_NUKE
Definition shortcuts.h:76
SC_HIDE_WORKERS
@ SC_HIDE_WORKERS
Definition shortcuts.h:52
SC_AUTOEXPLORE
@ SC_AUTOEXPLORE
Definition shortcuts.h:59
SC_POPUP_INFO
@ SC_POPUP_INFO
Definition shortcuts.h:47
SC_SETHOME
@ SC_SETHOME
Definition shortcuts.h:64
SC_RELOAD_LUA
@ SC_RELOAD_LUA
Definition shortcuts.h:90
SC_PLANT
@ SC_PLANT
Definition shortcuts.h:66
SC_BUILDMINE
@ SC_BUILDMINE
Definition shortcuts.h:65
SC_FULLSCREEN
@ SC_FULLSCREEN
Definition shortcuts.h:37
SC_TRADE_ROUTES
@ SC_TRADE_ROUTES
Definition shortcuts.h:54
SC_CITY_OUTPUT
@ SC_CITY_OUTPUT
Definition shortcuts.h:39
SC_TRANSFORM
@ SC_TRANSFORM
Definition shortcuts.h:75
SC_STACK_SIZE
@ SC_STACK_SIZE
Definition shortcuts.h:94
SC_SHOW_UNITS
@ SC_SHOW_UNITS
Definition shortcuts.h:53
SC_PASTE_PROD
@ SC_PASTE_PROD
Definition shortcuts.h:50
SC_WAKEUP_SENTRIES
@ SC_WAKEUP_SENTRIES
Definition shortcuts.h:48
SC_MAKE_LINK
@ SC_MAKE_LINK
Definition shortcuts.h:49
SC_FORTIFY
@ SC_FORTIFY
Definition shortcuts.h:72
SC_APPEND_FOCUS
@ SC_APPEND_FOCUS
Definition shortcuts.h:46
SC_ZOOM_IN
@ SC_ZOOM_IN
Definition shortcuts.h:87
SC_GOJOINCITY
@ SC_GOJOINCITY
Definition shortcuts.h:93
SC_MAP_GRID
@ SC_MAP_GRID
Definition shortcuts.h:40
SC_MINIMAP
@ SC_MINIMAP
Definition shortcuts.h:38
SC_QUICK_BUY
@ SC_QUICK_BUY
Definition shortcuts.h:42
SC_CITY_PROD
@ SC_CITY_PROD
Definition shortcuts.h:55
SC_RELOAD_THEME
@ SC_RELOAD_THEME
Definition shortcuts.h:84
SC_PATROL
@ SC_PATROL
Definition shortcuts.h:60
SC_GOTO
@ SC_GOTO
Definition shortcuts.h:73
SC_RELOAD_TILESET
@ SC_RELOAD_TILESET
Definition shortcuts.h:85
SC_BUY_MAP
@ SC_BUY_MAP
Definition shortcuts.h:79
SC_SCROLL_MAP
@ SC_SCROLL_MAP
Definition shortcuts.h:35
SC_GOTOAIRLIFT
@ SC_GOTOAIRLIFT
Definition shortcuts.h:58
SC_WAIT
@ SC_WAIT
Definition shortcuts.h:74
SC_QUICK_SELECT
@ SC_QUICK_SELECT
Definition shortcuts.h:43
SC_IFACE_LOCK
@ SC_IFACE_LOCK
Definition shortcuts.h:80
SC_DEBOARD
@ SC_DEBOARD
Definition shortcuts.h:78
SC_ZOOM_RESET
@ SC_ZOOM_RESET
Definition shortcuts.h:91
SC_ZOOM_OUT
@ SC_ZOOM_OUT
Definition shortcuts.h:88
SC_ADJUST_WORKERS
@ SC_ADJUST_WORKERS
Definition shortcuts.h:45
SC_SELECT_BUTTON
@ SC_SELECT_BUTTON
Definition shortcuts.h:44
SC_AUTOMATE
@ SC_AUTOMATE
Definition shortcuts.h:81
SC_CITY_NAMES
@ SC_CITY_NAMES
Definition shortcuts.h:56
SC_COPY_PROD
@ SC_COPY_PROD
Definition shortcuts.h:51
SC_LOAD_LUA
@ SC_LOAD_LUA
Definition shortcuts.h:89
SC_BOARD
@ SC_BOARD
Definition shortcuts.h:77
SC_NAT_BORDERS
@ SC_NAT_BORDERS
Definition shortcuts.h:41
SC_SHOW_FULLBAR
@ SC_SHOW_FULLBAR
Definition shortcuts.h:86
read_shortcuts
bool read_shortcuts()
Definition shortcuts.cpp:824
shortcut_to_string
QString shortcut_to_string(fc_shortcut *sc)
Definition shortcuts.cpp:183
SC_NUM_SHORTCUTS
#define SC_NUM_SHORTCUTS
Definition shortcuts.h:97
write_shortcuts
void write_shortcuts()
Definition shortcuts.cpp:791
size
size_t size
Definition specvec.h:72
fc_shortcut
Definition shortcuts.h:104
fc_shortcut::key
int key
Definition shortcuts.h:106
fc_shortcut::str
QString str
Definition shortcuts.h:109
fc_shortcut::mod
Qt::KeyboardModifiers mod
Definition shortcuts.h:108
fc_shortcut::mouse
Qt::MouseButton mouse
Definition shortcuts.h:107
fc_shortcut::id
shortcut_id id
Definition shortcuts.h:105
item
Definition climisc.h:80
move
Definition widget_window.c:39
widget
Definition widget.h:112
show
#define show(id)
Definition widget.h:235