GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/gui/shortcut-factory.cc Lines: 1 74 1.4 %
Date: 2020-05-14 11:23:33 Branches: 2 194 1.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 <iostream>
18
#include <QScrollArea>
19
#include <QLabel>
20
#include <QSettings>
21
#include <QCoreApplication>
22
#include <QVBoxLayout>
23
#include <QFormLayout>
24
#include <QGroupBox>
25
26
#include <gepetto/gui/shortcut-factory.hh>
27
28
namespace gepetto {
29
  namespace gui {
30
    ShortcutFactory::ShortcutFactory()
31
      : widgetsBindings_ (MapBindings())
32
    {
33
      readShortcutsFile();
34
    }
35
36
    ShortcutFactory::~ShortcutFactory()
37
    {
38
      writeShortcutsFile();
39
    }
40
41
    void ShortcutFactory::addBinding(QString widgetName, QString actionName, QAction* action)
42
    {
43
      if (widgetsBindings_.find(widgetName) == widgetsBindings_.end()) {
44
	widgetsBindings_.insert(std::make_pair(widgetName, BindingList()));
45
      }
46
      widgetsBindings_[widgetName].push_back(Binding(actionName, action));
47
      if (saved_.find(widgetName + "-" + actionName) != saved_.end()) {
48
	action->setShortcut(saved_[widgetName + "-" + actionName]);
49
      }
50
    }
51
52
    void ShortcutFactory::open()
53
    {
54
      QScrollArea* s = new QScrollArea(NULL);
55
      QWidget* w = new QWidget(s, Qt::Window);
56
      QVBoxLayout* l = new QVBoxLayout(w);
57
58
      l->addWidget(new QLabel("Double click on the button to change the shortcut."));
59
      s->setWidget(w);
60
      s->setAttribute(Qt::WA_DeleteOnClose);
61
      s->setWidgetResizable(true);
62
      s->resize(400, 500);
63
      for (MapBindings::iterator it = widgetsBindings_.begin(); it != widgetsBindings_.end(); ++it) {
64
	QGroupBox* g = new QGroupBox((*it).first, w);
65
	QFormLayout* f = new QFormLayout(g);
66
	for (BindingList::iterator itL = (*it).second.begin(); itL != (*it).second.end(); ++itL) {
67
	  f->addRow((*itL).first, new ShortcutButton((*itL).second, g));
68
	}
69
	l->addWidget(g);
70
      }
71
      s->show();
72
    }
73
74
    void ShortcutFactory::writeShortcutsFile()
75
    {
76
      QSettings shortcut (QSettings::SystemScope,
77
                       QCoreApplication::organizationName(),
78
                       QString::fromStdString("shortcuts"));
79
      if (!shortcut.isWritable()) {
80
          return;
81
        }
82
      for (MapBindings::iterator it = widgetsBindings_.begin(); it != widgetsBindings_.end(); ++it) {
83
	shortcut.beginGroup((*it).first);
84
	for (BindingList::iterator itL = (*it).second.begin(); itL != (*it).second.end(); ++itL) {
85
	  shortcut.setValue((*itL).first, (*itL).second->shortcut().toString());
86
	}
87
	shortcut.endGroup();
88
      }
89
    }
90
91
    void ShortcutFactory::readShortcutsFile()
92
    {
93
      QSettings robot (QSettings::SystemScope,
94
          QCoreApplication::organizationName(),
95
          QString::fromStdString("shortcuts"));
96
      if (robot.status() != QSettings::NoError) {
97
	return ;
98
      }
99
      foreach (QString name, robot.childGroups()) {
100
	robot.beginGroup(name);
101
	foreach (QString child, robot.childKeys()) {
102
	  saved_.insert(std::make_pair(name + "-" + child,
103
							      robot.value(child, "").toString()));
104
	}
105
	robot.endGroup();
106
      }
107
    }
108
109
    ShortcutFactory::ShortcutButton::ShortcutButton(QAction* action, QWidget* parent)
110
      : QPushButton(parent)
111
    {
112
      action_ = action;
113
      hasFocus_ = false;
114
      modifiers_.push_back(Qt::Key_Shift);
115
      modifiers_.push_back(Qt::Key_Control);
116
      modifiers_.push_back(Qt::Key_Meta);
117
      modifiers_.push_back(Qt::Key_Alt);
118
119
      setText(action->shortcut().toString());
120
    }
121
122
    ShortcutFactory::ShortcutButton::~ShortcutButton()
123
    {
124
      if (hasFocus_)
125
	releaseKeyboard();
126
    }
127
128
    void ShortcutFactory::ShortcutButton::mouseDoubleClickEvent(QMouseEvent* event)
129
    {
130
      if (!hasFocus_ && event->button() == Qt::LeftButton) {
131
	grabKeyboard();
132
	setText("Changing");
133
	hasFocus_ = true;
134
      }
135
    }
136
137
    void ShortcutFactory::ShortcutButton::keyPressEvent(QKeyEvent* event)
138
    {
139
      if (hasFocus_) {
140
	if (modifiers_.indexOf(event->key()) == -1) {
141
	  int seq = event->key() + event->modifiers();
142
143
	  action_->setShortcut(seq);
144
	  setText(action_->shortcut().toString(QKeySequence::NativeText));
145
	  hasFocus_ = false;
146
	  releaseKeyboard();
147
	}
148
      }
149
    }
150
  }
151

3
}