BuildmLearn Toolkit  2.0.4
BuildmLearn Toolkit is an easy-to-use program that helps users make mobile apps without any knowledge of application development.
 All Classes Functions Enumerations Groups Pages
dynamicshortcutswidget.cpp
1 /*
2  Copyright (c) 2012, BuildmLearn Contributors listed at http://buildmlearn.org/people/
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  * Neither the name of the BuildmLearn nor the names of its
16  contributors may be used to endorse or promote products derived from
17  this software without specific prior written permission.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 
31 #include "dynamic-shortcuts/dynamicshortcutswidget.h"
32 
33 #include "dynamic-shortcuts/shortcutcatcher.h"
34 #include "dynamic-shortcuts/shortcutbutton.h"
35 #include "definitions/definitions.h"
36 
37 #include <QGridLayout>
38 #include <QAction>
39 #include <QLabel>
40 #include <QSpacerItem>
41 #include <QPalette>
42 
43 
44 DynamicShortcutsWidget::DynamicShortcutsWidget(QWidget *parent) : QWidget(parent) {
45  // Create layout for this control and set is as active.
46  m_layout = new QGridLayout(this);
47  m_layout->setMargin(0);
48 
49  setLayout(m_layout);
50 }
51 
53  delete m_layout;
54 }
55 
57  QList<QKeySequence> all_shortcuts;
58 
59  // Obtain all shortcuts.
60  foreach (const ActionBinding &binding, m_actionBindings) {
61  QKeySequence new_shortcut = binding.second->shortcut();
62 
63  if (!new_shortcut.isEmpty() && all_shortcuts.contains(new_shortcut)) {
64  // Problem, two identical non-empty shortcuts found.
65  return false;
66  }
67  else {
68  all_shortcuts.append(binding.second->shortcut());
69  }
70  }
71 
72  return true;
73 }
74 
76  foreach (const ActionBinding &binding, m_actionBindings) {
77  binding.first->setShortcut(binding.second->shortcut());
78  }
79 }
80 
81 void DynamicShortcutsWidget::populate(const QList<QAction*> actions) {
82  m_actionBindings.clear();
83 
84  int row_id = 0;
85 
86  foreach (QAction *action, actions) {
87  // Create shortcut catcher for this action and set default shortcut.
88  ShortcutCatcher *catcher = new ShortcutCatcher(this);
89  catcher->setDefaultShortcut(action->shortcut());
90 
91  // Store information for re-initialization of shortcuts
92  // of actions when widget gets "confirmed".
93  QPair<QAction*,ShortcutCatcher*> new_binding;
94  new_binding.first = action;
95  new_binding.second = catcher;
96 
97  m_actionBindings << new_binding;
98 
99  // Add new catcher to our control.
100  QLabel *action_label = new QLabel(this);
101  action_label->setText(action->text().remove('&'));
102  action_label->setToolTip(action->toolTip());
103  action_label->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
104 
105  QLabel *action_icon = new QLabel(this);
106  action_icon->setPixmap(action->icon().pixmap(ICON_SIZE_SETTINGS, ICON_SIZE_SETTINGS));
107  action_icon->setToolTip(action->toolTip());
108 
109  m_layout->addWidget(action_icon, row_id, 0);
110  m_layout->addWidget(action_label, row_id, 1);
111  m_layout->addWidget(catcher, row_id, 2);
112 
113  row_id++;
114  }
115 
116  // Make sure that "spacer" is added.
117  m_layout->setRowStretch(row_id, 1);
118  m_layout->setColumnStretch(1, 1);
119 }
DynamicShortcutsWidget(QWidget *parent=0)
Constructor.
void setDefaultShortcut(const QKeySequence &key)
Sets default shortcut for the widget.
virtual ~DynamicShortcutsWidget()
Destructor.
bool areShortcutsUnique()
Checks whether set shortcuts are unique.
void updateShortcuts()
Updates shortcuts of all actions according to changes.
void populate(const QList< QAction * > actions)
Populates this widget with shortcut widgets for given actions.
Represents extra widget for changing single keyboard shortcut.