• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • dialogs
kshortcutseditoritem.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries Copyright (C) 1998 Mark Donohoe <donohoe@kde.org>
2  Copyright (C) 1997 Nicolas Hadacek <hadacek@kde.org>
3  Copyright (C) 1998 Matthias Ettrich <ettrich@kde.org>
4  Copyright (C) 2001 Ellis Whitehead <ellis@kde.org>
5  Copyright (C) 2006 Hamish Rodda <rodda@kde.org>
6  Copyright (C) 2007 Roberto Raggi <roberto@kdevelop.org>
7  Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com>
8  Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Library General Public
12  License as published by the Free Software Foundation; either
13  version 2 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Library General Public License for more details.
19 
20  You should have received a copy of the GNU Library General Public License
21  along with this library; see the file COPYING.LIB. If not, write to
22  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  Boston, MA 02110-1301, USA.
24 */
25 
26 #include "kshortcutsdialog_p.h"
27 
28 #include <kaction.h>
29 #include <kdebug.h>
30 #include <kstringhandler.h>
31 
32 #include <QTreeWidgetItem>
33 
34 KShortcutsEditorItem::KShortcutsEditorItem(QTreeWidgetItem *parent, KAction *action)
35  : QTreeWidgetItem(parent, ActionItem)
36  , m_action(action)
37  , m_isNameBold(false)
38  , m_oldLocalShortcut(0)
39  , m_oldGlobalShortcut(0)
40  , m_oldShapeGesture(0)
41  , m_oldRockerGesture(0)
42 {
43  // Filtering message requested by translators (scripting).
44  m_id = m_action->objectName();
45  m_actionNameInTable = i18nc("@item:intable Action name in shortcuts configuration", "%1", KGlobal::locale()->removeAcceleratorMarker(m_action->text()));
46  if (m_actionNameInTable.isEmpty()) {
47  kWarning() << "Action without text!" << m_action->objectName();
48  m_actionNameInTable = m_id;
49  }
50 }
51 
52 
53 KShortcutsEditorItem::~KShortcutsEditorItem()
54 {
55  delete m_oldLocalShortcut;
56  delete m_oldGlobalShortcut;
57  delete m_oldShapeGesture;
58  delete m_oldRockerGesture;
59 }
60 
61 
62 bool KShortcutsEditorItem::isModified() const
63 {
64  return m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture;
65 }
66 
67 
68 QVariant KShortcutsEditorItem::data(int column, int role) const
69 {
70  switch (role) {
71  case Qt::DisplayRole:
72  switch(column) {
73  case Name:
74  return m_actionNameInTable;
75  case Id:
76  return m_id;
77  case LocalPrimary:
78  case LocalAlternate:
79  case GlobalPrimary:
80  case GlobalAlternate:
81  return keySequence(column);
82  case ShapeGesture:
83  return m_action->shapeGesture().shapeName();
84  case RockerGesture:
85  return m_action->rockerGesture().rockerName();
86  default:
87  break;
88  }
89  break;
90  case Qt::DecorationRole:
91  if (column == Name)
92  return m_action->icon();
93  else
94  return KIcon();
95  break;
96  case Qt::WhatsThisRole:
97  return m_action->whatsThis();
98  case Qt::ToolTipRole:
99  // There is no such thing as a KAction::description(). So we have
100  // nothing to display here.
101  return QVariant();
102  case Qt::FontRole:
103  if (column == Name && m_isNameBold) {
104  QFont modifiedFont = treeWidget()->font();
105  modifiedFont.setBold(true);
106  return modifiedFont;
107  }
108  break;
109  case KExtendableItemDelegate::ShowExtensionIndicatorRole:
110  switch (column) {
111  case Name:
112  return false;
113  case LocalPrimary:
114  case LocalAlternate:
115  if (!m_action->isShortcutConfigurable()) {
116  return false;
117  }
118  return true;
119  case GlobalPrimary:
120  case GlobalAlternate:
121  if (!m_action->isGlobalShortcutEnabled()) {
122  return false;
123  }
124  return true;
125  default:
126  return false;
127  }
128  //the following are custom roles, defined in this source file only
129  case ShortcutRole:
130  switch(column) {
131  case LocalPrimary:
132  case LocalAlternate:
133  case GlobalPrimary:
134  case GlobalAlternate:
135  return keySequence(column);
136  case ShapeGesture: { //scoping for "ret"
137  QVariant ret;
138  ret.setValue(m_action->shapeGesture());
139  return ret; }
140  case RockerGesture: {
141  QVariant ret;
142  ret.setValue(m_action->rockerGesture());
143  return ret; }
144  default:
145  // Column not valid for this role
146  Q_ASSERT(false);
147  return QVariant();
148  }
149 
150  case DefaultShortcutRole:
151  switch(column) {
152  case LocalPrimary:
153  return m_action->shortcut(KAction::DefaultShortcut).primary();
154  case LocalAlternate:
155  return m_action->shortcut(KAction::DefaultShortcut).alternate();
156  case GlobalPrimary:
157  return m_action->globalShortcut(KAction::DefaultShortcut).primary();
158  case GlobalAlternate:
159  return m_action->globalShortcut(KAction::DefaultShortcut).alternate();
160  case ShapeGesture: {
161  QVariant ret;
162  ret.setValue(m_action->shapeGesture(KAction::DefaultShortcut));
163  return ret; }
164  case RockerGesture: {
165  QVariant ret;
166  ret.setValue(m_action->rockerGesture(KAction::DefaultShortcut));
167  return ret; }
168  default:
169  // Column not valid for this role
170  Q_ASSERT(false);
171  return QVariant();
172  }
173  case ObjectRole:
174  return qVariantFromValue((QObject*)m_action);
175 
176  default:
177  break;
178  }
179 
180  return QVariant();
181 }
182 
183 
184 bool KShortcutsEditorItem::operator<(const QTreeWidgetItem &other) const
185 {
186  const int column = treeWidget() ? treeWidget()->sortColumn() : 0;
187  return KStringHandler::naturalCompare(text(column), other.text(column)) < 0;
188 }
189 
190 
191 QKeySequence KShortcutsEditorItem::keySequence(uint column) const
192 {
193  switch (column) {
194  case LocalPrimary:
195  return m_action->shortcut().primary();
196  case LocalAlternate:
197  return m_action->shortcut().alternate();
198  case GlobalPrimary:
199  return m_action->globalShortcut().primary();
200  case GlobalAlternate:
201  return m_action->globalShortcut().alternate();
202  default:
203  return QKeySequence();
204  }
205 }
206 
207 
208 void KShortcutsEditorItem::setKeySequence(uint column, const QKeySequence &seq)
209 {
210  KShortcut ks;
211  if (column == GlobalPrimary || column == GlobalAlternate) {
212  ks = m_action->globalShortcut();
213  if (!m_oldGlobalShortcut)
214  m_oldGlobalShortcut = new KShortcut(ks);
215  } else {
216  ks = m_action->shortcut();
217  if (!m_oldLocalShortcut)
218  m_oldLocalShortcut = new KShortcut(ks);
219  }
220 
221  if (column == LocalAlternate || column == GlobalAlternate)
222  ks.setAlternate(seq);
223  else
224  ks.setPrimary(seq);
225 
226  //avoid also setting the default shortcut - what we are setting here is custom by definition
227  if (column == GlobalPrimary || column == GlobalAlternate) {
228  m_action->setGlobalShortcut(ks, KAction::ActiveShortcut, KAction::NoAutoloading);
229  } else {
230  m_action->setShortcut(ks, KAction::ActiveShortcut);
231  }
232 
233  updateModified();
234 }
235 
236 
237 void KShortcutsEditorItem::setShapeGesture(const KShapeGesture &gst)
238 {
239  if (!m_oldShapeGesture) {
240  m_oldShapeGesture = new KShapeGesture(gst);
241  }
242  m_action->setShapeGesture(gst);
243  updateModified();
244 }
245 
246 
247 void KShortcutsEditorItem::setRockerGesture(const KRockerGesture &gst)
248 {
249  if (!m_oldRockerGesture) {
250  m_oldRockerGesture = new KRockerGesture(gst);
251  }
252  m_action->setRockerGesture(gst);
253  updateModified();
254 }
255 
256 
257 //our definition of modified is "modified since the chooser was shown".
258 void KShortcutsEditorItem::updateModified()
259 {
260  if (m_oldLocalShortcut && *m_oldLocalShortcut == m_action->shortcut()) {
261  delete m_oldLocalShortcut;
262  m_oldLocalShortcut = 0;
263  }
264  if (m_oldGlobalShortcut && *m_oldGlobalShortcut == m_action->globalShortcut()) {
265  delete m_oldGlobalShortcut;
266  m_oldGlobalShortcut = 0;
267  }
268  if (m_oldShapeGesture && *m_oldShapeGesture == m_action->shapeGesture()) {
269  delete m_oldShapeGesture;
270  m_oldShapeGesture = 0;
271  }
272  if (m_oldRockerGesture && *m_oldRockerGesture == m_action->rockerGesture()) {
273  delete m_oldRockerGesture;
274  m_oldRockerGesture = 0;
275  }
276 }
277 
278 
279 bool KShortcutsEditorItem::isModified(uint column) const
280 {
281  switch (column) {
282  case Name:
283  return false;
284  case LocalPrimary:
285  case LocalAlternate:
286  if (!m_oldLocalShortcut)
287  return false;
288  if (column == LocalPrimary)
289  return m_oldLocalShortcut->primary() != m_action->shortcut().primary();
290  else
291  return m_oldLocalShortcut->alternate() != m_action->shortcut().alternate();
292  case GlobalPrimary:
293  case GlobalAlternate:
294  if (!m_oldGlobalShortcut)
295  return false;
296  if (column == GlobalPrimary)
297  return m_oldGlobalShortcut->primary() != m_action->globalShortcut().primary();
298  else
299  return m_oldGlobalShortcut->alternate() != m_action->globalShortcut().alternate();
300  case ShapeGesture:
301  return static_cast<bool>(m_oldShapeGesture);
302  case RockerGesture:
303  return static_cast<bool>(m_oldRockerGesture);
304  default:
305  return false;
306  }
307 }
308 
309 
310 
311 void KShortcutsEditorItem::undo()
312 {
313 #ifndef NDEBUG
314  if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) {
315  kDebug(125) << "Undoing changes for " << data(Name, Qt::DisplayRole).toString();
316  }
317 #endif
318  if (m_oldLocalShortcut) {
319  // We only ever reset the active Shortcut
320  m_action->setShortcut(*m_oldLocalShortcut, KAction::ActiveShortcut);
321  }
322 
323  if (m_oldGlobalShortcut) {
324  m_action->setGlobalShortcut(*m_oldGlobalShortcut, KAction::ActiveShortcut,
325  KAction::NoAutoloading);
326  }
327 
328  if (m_oldShapeGesture) {
329  m_action->setShapeGesture(*m_oldShapeGesture);
330  }
331 
332  if (m_oldRockerGesture) {
333  m_action->setRockerGesture(*m_oldRockerGesture);
334  }
335 
336  updateModified();
337 }
338 
339 
340 void KShortcutsEditorItem::commit()
341 {
342 #ifndef NDEBUG
343  if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) {
344  kDebug(125) << "Committing changes for " << data(Name, Qt::DisplayRole).toString();
345  }
346 #endif
347 
348  delete m_oldLocalShortcut;
349  m_oldLocalShortcut = 0;
350  delete m_oldGlobalShortcut;
351  m_oldGlobalShortcut = 0;
352  delete m_oldShapeGesture;
353  m_oldShapeGesture = 0;
354  delete m_oldRockerGesture;
355  m_oldRockerGesture = 0;
356 }
QVariant
kdebug.h
KStandardAction::undo
KAction * undo(const QObject *recvr, const char *slot, QObject *parent)
Undo the last operation.
Definition: kstandardaction.cpp:284
KStringHandler::naturalCompare
int naturalCompare(const QString &a, const QString &b, Qt::CaseSensitivity caseSensitivity=Qt::CaseSensitive)
KRockerGesture
Definition: kgesture.h:152
KAction::DefaultShortcut
The shortcut is a default shortcut - it becomes active when somebody decides to reset shortcuts to de...
Definition: kaction.h:238
removeAcceleratorMarker
QString removeAcceleratorMarker(const QString &label_)
QObject
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
i18nc
QString i18nc(const char *ctxt, const char *text)
KShortcut
Represents a keyboard shortcut.
Definition: kshortcut.h:57
KIcon
A wrapper around QIcon that provides KDE icon features.
Definition: kicon.h:40
kaction.h
kstringhandler.h
KGlobal::locale
KLocale * locale()
QFont
KAction::ActiveShortcut
The shortcut will immediately become active but may be reset to "default".
Definition: kaction.h:235
KShortcut::setAlternate
void setAlternate(const QKeySequence &keySeq)
Set the alternate key sequence of this shortcut to the given key sequence.
Definition: kshortcut.cpp:189
KShapeGesture
Definition: kgesture.h:37
KAction
Class to encapsulate user-driven action or event.
Definition: kaction.h:216
kWarning
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KShortcut::setPrimary
void setPrimary(const QKeySequence &keySeq)
Set the primary key sequence of this shortcut to the given key sequence.
Definition: kshortcut.cpp:184
KExtendableItemDelegate::ShowExtensionIndicatorRole
Definition: kextendableitemdelegate.h:55
KAction::NoAutoloading
Prevent autoloading of saved global shortcut for action.
Definition: kaction.h:253
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal