diff options
Diffstat (limited to 'src/gui_qt.cc')
| -rw-r--r-- | src/gui_qt.cc | 120 |
1 files changed, 101 insertions, 19 deletions
diff --git a/src/gui_qt.cc b/src/gui_qt.cc index d4bc3d8..8607e16 100644 --- a/src/gui_qt.cc +++ b/src/gui_qt.cc @@ -173,12 +173,13 @@ public: : parent_(nullptr) { } - void add_item(std::string const& id, std::string const& label) override { - item_.emplace_back(id, label); + void add_item(std::string const& id, std::string const& label, + Shortcut const& shortcut) override { + item_.emplace_back(id, label, shortcut); } void add_separator() override { - item_.emplace_back("", ""); + item_.emplace_back("", "", Shortcut()); } GuiMenu* add_menu(std::string const& label) override; @@ -202,16 +203,68 @@ public: } protected: + struct Entry { + std::string const id; + std::string const label; + Shortcut const shortcut; + + Entry(std::string const& id, std::string const& label, + Shortcut const& shortcut) + : id(id), label(label), shortcut(shortcut) { + } + }; + bool find(const std::string& id) const { - for (auto const& pair : item_) { - if (pair.first == id) return true; + for (auto const& entry : item_) { + if (entry.id == id) return true; } return false; } + static QKeySequence make_key_sequence(Shortcut const& shortcut) { + assert(shortcut.key); + int mod = 0; + if (shortcut.mask & CTRL) mod |= Qt::CTRL; + if (shortcut.mask & ALT) mod |= Qt::ALT; + if (shortcut.mask & SHIFT) mod |= Qt::SHIFT; + if (shortcut.mask & META) mod |= Qt::META; + int key = 0; + if (shortcut.function) { + key = Qt::Key_F1 + (shortcut.key - 1); + if (key > Qt::Key_F35) { + return QKeySequence(); + } + } else { + if (shortcut.key == ESC) { + key = Qt::Key_Escape; + } else if (shortcut.key == RETURN) { + key = Qt::Key_Return; + } else if (shortcut.key == UP) { + key = Qt::Key_Up; + } else if (shortcut.key == LEFT) { + key = Qt::Key_Left; + } else if (shortcut.key == RIGHT) { + key = Qt::Key_Right; + } else if (shortcut.key == DOWN) { + key = Qt::Key_Down; + } else if (shortcut.key >= 0x20 && shortcut.key <= 0x60) { + key = shortcut.key; + } else if (shortcut.key > 0x60 && shortcut.key < 0x7b) { + key = shortcut.key - 32; + } else if (shortcut.key == '\t') { + key = Qt::Key_Tab; + } else if (shortcut.key == 0x08) { + key = Qt::Key_Backspace; + } else { + return QKeySequence(); + } + } + return QKeySequence(mod + key); + } + QtCommonMenu* const parent_; std::string const label_; - std::vector<std::pair<std::string, std::string>> item_; + std::vector<Entry> item_; std::unordered_set<std::string> disabled_; std::vector<std::unique_ptr<QtChildGuiMenu>> children_; }; @@ -220,7 +273,8 @@ class QtGuiMenu : public QtCommonMenu { public: QtGuiMenu(); - void add_item(std::string const& id, std::string const& label) override; + void add_item(std::string const& id, std::string const& label, + Shortcut const& shotcut) override; void add_separator() override; GuiMenu* add_menu(std::string const& label) override; bool enable_item(const std::string& id, bool enable) override; @@ -599,12 +653,16 @@ public: : QtCommonMenu(parent, label), menu_(nullptr) { } - void add_item(std::string const& id, std::string const& label) override { + void add_item(std::string const& id, std::string const& label, + Shortcut const& shortcut) override { if (!menu_) { - QtCommonMenu::add_item(id, label); + QtCommonMenu::add_item(id, label, shortcut); return; } auto action = menu_->addAction(QString::fromStdString(label)); + if (shortcut.key != 0) { + action->setShortcut(make_key_sequence(shortcut)); + } parent_->add_action(id, action); } @@ -637,13 +695,16 @@ public: assert(menu); menu_ = menu; - for (auto const& pair : item_) { - if (pair.first.empty() && pair.second.empty()) { + for (auto const& entry : item_) { + if (entry.id.empty() && entry.label.empty()) { menu_->addSeparator(); continue; } - auto action = menu_->addAction(QString::fromStdString(pair.second)); - parent_->add_action(pair.first, action); + auto action = menu_->addAction(QString::fromStdString(entry.label)); + if (entry.shortcut.key) { + action->setShortcut(make_key_sequence(entry.shortcut)); + } + parent_->add_action(entry.id, action); } item_.clear(); @@ -666,12 +727,17 @@ QtGuiMenu::QtGuiMenu() : QtCommonMenu(), menubar_(nullptr) { } -void QtGuiMenu::add_item(std::string const& id, std::string const& label) { +void QtGuiMenu::add_item(std::string const& id, std::string const& label, + Shortcut const& shortcut) { if (!menubar_) { - QtCommonMenu::add_item(id, label); + QtCommonMenu::add_item(id, label, shortcut); return; } - add_action(id, menubar_->addAction(QString::fromStdString(label))); + auto action = menubar_->addAction(QString::fromStdString(label)); + if (shortcut.key != 0) { + action->setShortcut(make_key_sequence(shortcut)); + } + add_action(id, action); } void QtGuiMenu::add_separator() { @@ -725,9 +791,11 @@ void QtGuiMenu::setup(QMenuBar* menubar) { assert(menubar); assert(!menubar_); menubar_ = menubar; - for (auto const& pair : item_) { - auto action = menubar_->addAction(QString::fromStdString(pair.second)); - add_action(pair.first, action); + for (auto const& entry : item_) { + auto action = menubar_->addAction(QString::fromStdString(entry.label)); + if (entry.shortcut.key) { + } + add_action(entry.id, action); } item_.clear(); @@ -1779,6 +1847,20 @@ GuiMenu* GuiMenu::create() { } // static +const uint32_t GuiMenu::CTRL = 1 << 0; +const uint32_t GuiMenu::ALT = 1 << 1; +const uint32_t GuiMenu::SHIFT = 1 << 2; +const uint32_t GuiMenu::META = 1 << 3; + +// static +const char GuiMenu::ESC = 0x1b; +const char GuiMenu::UP = 1; +const char GuiMenu::LEFT = 2; +const char GuiMenu::RIGHT = 3; +const char GuiMenu::DOWN = 4; +const char GuiMenu::RETURN = '\n'; + +// static GuiStatusBar* GuiStatusBar::create() { return new QtGuiStatusBar(); } |
