GCC Code Coverage Report


Directory: ./
File: src/gui/shortcut-factory.cc
Date: 2025-04-14 11:18:00
Exec Total Coverage
Lines: 0 76 0.0%
Branches: 0 176 0.0%

Line Branch Exec Source
1 // Copyright (c) 2015-2018, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of gepetto-viewer.
5 // gepetto-viewer is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // gepetto-viewer is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // gepetto-viewer. If not, see <http://www.gnu.org/licenses/>.
16
17 #include <QCoreApplication>
18 #include <QFormLayout>
19 #include <QGroupBox>
20 #include <QLabel>
21 #include <QScrollArea>
22 #include <QSettings>
23 #include <QVBoxLayout>
24 #include <gepetto/gui/shortcut-factory.hh>
25 #include <iostream>
26
27 namespace gepetto {
28 namespace gui {
29 ✗ ShortcutFactory::ShortcutFactory() : widgetsBindings_(MapBindings()) {
30 ✗ readShortcutsFile();
31 }
32
33 ✗ ShortcutFactory::~ShortcutFactory() { writeShortcutsFile(); }
34
35 ✗ void ShortcutFactory::addBinding(QString widgetName, QString actionName,
36 QAction* action) {
37 ✗ if (widgetsBindings_.find(widgetName) == widgetsBindings_.end()) {
38 ✗ widgetsBindings_.insert(std::make_pair(widgetName, BindingList()));
39 }
40 ✗ widgetsBindings_[widgetName].push_back(Binding(actionName, action));
41 ✗ if (saved_.find(widgetName + "-" + actionName) != saved_.end()) {
42 ✗ action->setShortcut(saved_[widgetName + "-" + actionName]);
43 }
44 }
45
46 ✗ void ShortcutFactory::open() {
47 ✗ QScrollArea* s = new QScrollArea(NULL);
48 ✗ QWidget* w = new QWidget(s, Qt::Window);
49 ✗ QVBoxLayout* l = new QVBoxLayout(w);
50
51 ✗ l->addWidget(
52 ✗ new QLabel("Double click on the button to change the shortcut."));
53 ✗ s->setWidget(w);
54 ✗ s->setAttribute(Qt::WA_DeleteOnClose);
55 ✗ s->setWidgetResizable(true);
56 ✗ s->resize(400, 500);
57 ✗ for (MapBindings::iterator it = widgetsBindings_.begin();
58 ✗ it != widgetsBindings_.end(); ++it) {
59 ✗ QGroupBox* g = new QGroupBox((*it).first, w);
60 ✗ QFormLayout* f = new QFormLayout(g);
61 ✗ for (BindingList::iterator itL = (*it).second.begin();
62 ✗ itL != (*it).second.end(); ++itL) {
63 ✗ f->addRow((*itL).first, new ShortcutButton((*itL).second, g));
64 }
65 ✗ l->addWidget(g);
66 }
67 ✗ s->show();
68 }
69
70 ✗ void ShortcutFactory::writeShortcutsFile() {
71 QSettings shortcut(QSettings::SystemScope,
72 ✗ QCoreApplication::organizationName(),
73 ✗ QString::fromStdString("shortcuts"));
74 ✗ if (!shortcut.isWritable()) {
75 ✗ return;
76 }
77 ✗ for (MapBindings::iterator it = widgetsBindings_.begin();
78 ✗ it != widgetsBindings_.end(); ++it) {
79 ✗ shortcut.beginGroup((*it).first);
80 ✗ for (BindingList::iterator itL = (*it).second.begin();
81 ✗ itL != (*it).second.end(); ++itL) {
82 ✗ shortcut.setValue((*itL).first, (*itL).second->shortcut().toString());
83 }
84 ✗ shortcut.endGroup();
85 }
86 }
87
88 ✗ void ShortcutFactory::readShortcutsFile() {
89 ✗ QSettings robot(QSettings::SystemScope, QCoreApplication::organizationName(),
90 ✗ QString::fromStdString("shortcuts"));
91 ✗ if (robot.status() != QSettings::NoError) {
92 ✗ return;
93 }
94 ✗ foreach (QString name, robot.childGroups()) {
95 ✗ robot.beginGroup(name);
96 ✗ foreach (QString child, robot.childKeys()) {
97 ✗ saved_.insert(std::make_pair(name + "-" + child,
98 ✗ robot.value(child, "").toString()));
99 }
100 ✗ robot.endGroup();
101 }
102 }
103
104 ✗ ShortcutFactory::ShortcutButton::ShortcutButton(QAction* action,
105 ✗ QWidget* parent)
106 ✗ : QPushButton(parent) {
107 ✗ action_ = action;
108 ✗ hasFocus_ = false;
109 ✗ modifiers_.push_back(Qt::Key_Shift);
110 ✗ modifiers_.push_back(Qt::Key_Control);
111 ✗ modifiers_.push_back(Qt::Key_Meta);
112 ✗ modifiers_.push_back(Qt::Key_Alt);
113
114 ✗ setText(action->shortcut().toString());
115 }
116
117 ✗ ShortcutFactory::ShortcutButton::~ShortcutButton() {
118 ✗ if (hasFocus_) releaseKeyboard();
119 }
120
121 ✗ void ShortcutFactory::ShortcutButton::mouseDoubleClickEvent(
122 QMouseEvent* event) {
123 ✗ if (!hasFocus_ && event->button() == Qt::LeftButton) {
124 ✗ grabKeyboard();
125 ✗ setText("Changing");
126 ✗ hasFocus_ = true;
127 }
128 }
129
130 ✗ void ShortcutFactory::ShortcutButton::keyPressEvent(QKeyEvent* event) {
131 ✗ if (hasFocus_) {
132 ✗ if (modifiers_.indexOf(event->key()) == -1) {
133 ✗ int seq = event->key() + event->modifiers();
134
135 ✗ action_->setShortcut(seq);
136 ✗ setText(action_->shortcut().toString(QKeySequence::NativeText));
137 ✗ hasFocus_ = false;
138 ✗ releaseKeyboard();
139 }
140 }
141 }
142 } // namespace gui
143 } // namespace gepetto
144