hotkeys.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <QKeySequence>
  2. #include <QSettings>
  3. #include "hotkeys.hxx"
  4. #include <map>
  5. struct Hotkey
  6. {
  7. Hotkey() : shortcut(NULL), context(Qt::WindowShortcut) {}
  8. QKeySequence keyseq;
  9. QShortcut* shortcut;
  10. Qt::ShortcutContext context;
  11. };
  12. typedef std::map<QString, Hotkey> HotkeyMap;
  13. typedef std::map<QString, HotkeyMap> HotkeyGroupMap;
  14. HotkeyGroupMap hotkey_groups;
  15. void SaveHotkeys(QSettings& settings)
  16. {
  17. settings.beginGroup("Shortcuts");
  18. for (HotkeyGroupMap::iterator group = hotkey_groups.begin(); group != hotkey_groups.end(); ++group)
  19. {
  20. settings.beginGroup(group->first);
  21. for (HotkeyMap::iterator hotkey = group->second.begin(); hotkey != group->second.end(); ++hotkey)
  22. {
  23. settings.beginGroup(hotkey->first);
  24. settings.setValue(QString("KeySeq"), hotkey->second.keyseq.toString());
  25. settings.setValue(QString("Context"), hotkey->second.context);
  26. settings.endGroup();
  27. }
  28. settings.endGroup();
  29. }
  30. settings.endGroup();
  31. }
  32. void LoadHotkeys(QSettings& settings)
  33. {
  34. settings.beginGroup("Shortcuts");
  35. // Make sure NOT to use a reference here because it would become invalid once we call beginGroup()
  36. QStringList groups = settings.childGroups();
  37. for (QList<QString>::iterator group = groups.begin(); group != groups.end(); ++group)
  38. {
  39. settings.beginGroup(*group);
  40. QStringList hotkeys = settings.childGroups();
  41. for (QList<QString>::iterator hotkey = hotkeys.begin(); hotkey != hotkeys.end(); ++hotkey)
  42. {
  43. settings.beginGroup(*hotkey);
  44. // RegisterHotkey assigns default keybindings, so use old values as default parameters
  45. Hotkey& hk = hotkey_groups[*group][*hotkey];
  46. hk.keyseq = QKeySequence::fromString(settings.value("KeySeq", hk.keyseq.toString()).toString());
  47. hk.context = (Qt::ShortcutContext)settings.value("Context", hk.context).toInt();
  48. if (hk.shortcut)
  49. hk.shortcut->setKey(hk.keyseq);
  50. settings.endGroup();
  51. }
  52. settings.endGroup();
  53. }
  54. settings.endGroup();
  55. }
  56. void RegisterHotkey(const QString& group, const QString& action, const QKeySequence& default_keyseq, Qt::ShortcutContext default_context)
  57. {
  58. if (hotkey_groups[group].find(action) == hotkey_groups[group].end())
  59. {
  60. hotkey_groups[group][action].keyseq = default_keyseq;
  61. hotkey_groups[group][action].context = default_context;
  62. }
  63. }
  64. QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget)
  65. {
  66. Hotkey& hk = hotkey_groups[group][action];
  67. if (!hk.shortcut)
  68. hk.shortcut = new QShortcut(hk.keyseq, widget, NULL, NULL, hk.context);
  69. return hk.shortcut;
  70. }
  71. GHotkeysDialog::GHotkeysDialog(QWidget* parent): QDialog(parent)
  72. {
  73. ui.setupUi(this);
  74. for (HotkeyGroupMap::iterator group = hotkey_groups.begin(); group != hotkey_groups.end(); ++group)
  75. {
  76. QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group->first));
  77. for (HotkeyMap::iterator hotkey = group->second.begin(); hotkey != group->second.end(); ++hotkey)
  78. {
  79. QStringList columns;
  80. columns << hotkey->first << hotkey->second.keyseq.toString();
  81. QTreeWidgetItem* item = new QTreeWidgetItem(columns);
  82. toplevel_item->addChild(item);
  83. }
  84. ui.treeWidget->addTopLevelItem(toplevel_item);
  85. }
  86. // TODO: Make context configurable as well (hiding the column for now)
  87. ui.treeWidget->setColumnCount(2);
  88. ui.treeWidget->resizeColumnToContents(0);
  89. ui.treeWidget->resizeColumnToContents(1);
  90. }