KDEUI
kshortcutseditoritem.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries Copyright (C) 1998 Mark Donohoe <donohoe@kde.org> 00002 Copyright (C) 1997 Nicolas Hadacek <hadacek@kde.org> 00003 Copyright (C) 1998 Matthias Ettrich <ettrich@kde.org> 00004 Copyright (C) 2001 Ellis Whitehead <ellis@kde.org> 00005 Copyright (C) 2006 Hamish Rodda <rodda@kde.org> 00006 Copyright (C) 2007 Roberto Raggi <roberto@kdevelop.org> 00007 Copyright (C) 2007 Andreas Hartmetz <ahartmetz@gmail.com> 00008 Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz> 00009 00010 This library is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU Library General Public 00012 License as published by the Free Software Foundation; either 00013 version 2 of the License, or (at your option) any later version. 00014 00015 This library is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 Library General Public License for more details. 00019 00020 You should have received a copy of the GNU Library General Public License 00021 along with this library; see the file COPYING.LIB. If not, write to 00022 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00023 Boston, MA 02110-1301, USA. 00024 */ 00025 00026 #include "kshortcutsdialog_p.h" 00027 00028 #include <kaction.h> 00029 #include <kdebug.h> 00030 #include <kstringhandler.h> 00031 00032 #include <QTreeWidgetItem> 00033 00034 KShortcutsEditorItem::KShortcutsEditorItem(QTreeWidgetItem *parent, KAction *action) 00035 : QTreeWidgetItem(parent, ActionItem) 00036 , m_action(action) 00037 , m_isNameBold(false) 00038 , m_oldLocalShortcut(0) 00039 , m_oldGlobalShortcut(0) 00040 , m_oldShapeGesture(0) 00041 , m_oldRockerGesture(0) 00042 { 00043 // Filtering message requested by translators (scripting). 00044 m_id = m_action->objectName(); 00045 m_actionNameInTable = i18nc("@item:intable Action name in shortcuts configuration", "%1", KGlobal::locale()->removeAcceleratorMarker(m_action->text())); 00046 if (m_actionNameInTable.isEmpty()) { 00047 kWarning() << "Action without text!" << m_action->objectName(); 00048 m_actionNameInTable = m_id; 00049 } 00050 } 00051 00052 00053 KShortcutsEditorItem::~KShortcutsEditorItem() 00054 { 00055 delete m_oldLocalShortcut; 00056 delete m_oldGlobalShortcut; 00057 delete m_oldShapeGesture; 00058 delete m_oldRockerGesture; 00059 } 00060 00061 00062 bool KShortcutsEditorItem::isModified() const 00063 { 00064 return m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture; 00065 } 00066 00067 00068 QVariant KShortcutsEditorItem::data(int column, int role) const 00069 { 00070 switch (role) { 00071 case Qt::DisplayRole: 00072 switch(column) { 00073 case Name: 00074 return m_actionNameInTable; 00075 case Id: 00076 return m_id; 00077 case LocalPrimary: 00078 case LocalAlternate: 00079 case GlobalPrimary: 00080 case GlobalAlternate: 00081 return keySequence(column); 00082 case ShapeGesture: 00083 return m_action->shapeGesture().shapeName(); 00084 case RockerGesture: 00085 return m_action->rockerGesture().rockerName(); 00086 default: 00087 break; 00088 } 00089 break; 00090 case Qt::DecorationRole: 00091 if (column == Name) 00092 return m_action->icon(); 00093 else 00094 return KIcon(); 00095 break; 00096 case Qt::WhatsThisRole: 00097 return m_action->whatsThis(); 00098 case Qt::ToolTipRole: 00099 // There is no such thing as a KAction::description(). So we have 00100 // nothing to display here. 00101 return QVariant(); 00102 case Qt::FontRole: 00103 if (column == Name && m_isNameBold) { 00104 QFont modifiedFont = treeWidget()->font(); 00105 modifiedFont.setBold(true); 00106 return modifiedFont; 00107 } 00108 break; 00109 case KExtendableItemDelegate::ShowExtensionIndicatorRole: 00110 switch (column) { 00111 case Name: 00112 return false; 00113 case LocalPrimary: 00114 case LocalAlternate: 00115 if (!m_action->isShortcutConfigurable()) { 00116 return false; 00117 } 00118 return true; 00119 case GlobalPrimary: 00120 case GlobalAlternate: 00121 if (!m_action->isGlobalShortcutEnabled()) { 00122 return false; 00123 } 00124 return true; 00125 default: 00126 return false; 00127 } 00128 //the following are custom roles, defined in this source file only 00129 case ShortcutRole: 00130 switch(column) { 00131 case LocalPrimary: 00132 case LocalAlternate: 00133 case GlobalPrimary: 00134 case GlobalAlternate: 00135 return keySequence(column); 00136 case ShapeGesture: { //scoping for "ret" 00137 QVariant ret; 00138 ret.setValue(m_action->shapeGesture()); 00139 return ret; } 00140 case RockerGesture: { 00141 QVariant ret; 00142 ret.setValue(m_action->rockerGesture()); 00143 return ret; } 00144 default: 00145 // Column not valid for this role 00146 Q_ASSERT(false); 00147 return QVariant(); 00148 } 00149 00150 case DefaultShortcutRole: 00151 switch(column) { 00152 case LocalPrimary: 00153 return m_action->shortcut(KAction::DefaultShortcut).primary(); 00154 case LocalAlternate: 00155 return m_action->shortcut(KAction::DefaultShortcut).alternate(); 00156 case GlobalPrimary: 00157 return m_action->globalShortcut(KAction::DefaultShortcut).primary(); 00158 case GlobalAlternate: 00159 return m_action->globalShortcut(KAction::DefaultShortcut).alternate(); 00160 case ShapeGesture: { 00161 QVariant ret; 00162 ret.setValue(m_action->shapeGesture(KAction::DefaultShortcut)); 00163 return ret; } 00164 case RockerGesture: { 00165 QVariant ret; 00166 ret.setValue(m_action->rockerGesture(KAction::DefaultShortcut)); 00167 return ret; } 00168 default: 00169 // Column not valid for this role 00170 Q_ASSERT(false); 00171 return QVariant(); 00172 } 00173 case ObjectRole: 00174 return qVariantFromValue((QObject*)m_action); 00175 00176 default: 00177 break; 00178 } 00179 00180 return QVariant(); 00181 } 00182 00183 00184 bool KShortcutsEditorItem::operator<(const QTreeWidgetItem &other) const 00185 { 00186 const int column = treeWidget() ? treeWidget()->sortColumn() : 0; 00187 return KStringHandler::naturalCompare(text(column), other.text(column)) < 0; 00188 } 00189 00190 00191 QKeySequence KShortcutsEditorItem::keySequence(uint column) const 00192 { 00193 switch (column) { 00194 case LocalPrimary: 00195 return m_action->shortcut().primary(); 00196 case LocalAlternate: 00197 return m_action->shortcut().alternate(); 00198 case GlobalPrimary: 00199 return m_action->globalShortcut().primary(); 00200 case GlobalAlternate: 00201 return m_action->globalShortcut().alternate(); 00202 default: 00203 return QKeySequence(); 00204 } 00205 } 00206 00207 00208 void KShortcutsEditorItem::setKeySequence(uint column, const QKeySequence &seq) 00209 { 00210 KShortcut ks; 00211 if (column == GlobalPrimary || column == GlobalAlternate) { 00212 ks = m_action->globalShortcut(); 00213 if (!m_oldGlobalShortcut) 00214 m_oldGlobalShortcut = new KShortcut(ks); 00215 } else { 00216 ks = m_action->shortcut(); 00217 if (!m_oldLocalShortcut) 00218 m_oldLocalShortcut = new KShortcut(ks); 00219 } 00220 00221 if (column == LocalAlternate || column == GlobalAlternate) 00222 ks.setAlternate(seq); 00223 else 00224 ks.setPrimary(seq); 00225 00226 //avoid also setting the default shortcut - what we are setting here is custom by definition 00227 if (column == GlobalPrimary || column == GlobalAlternate) { 00228 m_action->setGlobalShortcut(ks, KAction::ActiveShortcut, KAction::NoAutoloading); 00229 } else { 00230 m_action->setShortcut(ks, KAction::ActiveShortcut); 00231 } 00232 00233 updateModified(); 00234 } 00235 00236 00237 void KShortcutsEditorItem::setShapeGesture(const KShapeGesture &gst) 00238 { 00239 if (!m_oldShapeGesture) { 00240 m_oldShapeGesture = new KShapeGesture(gst); 00241 } 00242 m_action->setShapeGesture(gst); 00243 updateModified(); 00244 } 00245 00246 00247 void KShortcutsEditorItem::setRockerGesture(const KRockerGesture &gst) 00248 { 00249 if (!m_oldRockerGesture) { 00250 m_oldRockerGesture = new KRockerGesture(gst); 00251 } 00252 m_action->setRockerGesture(gst); 00253 updateModified(); 00254 } 00255 00256 00257 //our definition of modified is "modified since the chooser was shown". 00258 void KShortcutsEditorItem::updateModified() 00259 { 00260 if (m_oldLocalShortcut && *m_oldLocalShortcut == m_action->shortcut()) { 00261 delete m_oldLocalShortcut; 00262 m_oldLocalShortcut = 0; 00263 } 00264 if (m_oldGlobalShortcut && *m_oldGlobalShortcut == m_action->globalShortcut()) { 00265 delete m_oldGlobalShortcut; 00266 m_oldGlobalShortcut = 0; 00267 } 00268 if (m_oldShapeGesture && *m_oldShapeGesture == m_action->shapeGesture()) { 00269 delete m_oldShapeGesture; 00270 m_oldShapeGesture = 0; 00271 } 00272 if (m_oldRockerGesture && *m_oldRockerGesture == m_action->rockerGesture()) { 00273 delete m_oldRockerGesture; 00274 m_oldRockerGesture = 0; 00275 } 00276 } 00277 00278 00279 bool KShortcutsEditorItem::isModified(uint column) const 00280 { 00281 switch (column) { 00282 case Name: 00283 return false; 00284 case LocalPrimary: 00285 case LocalAlternate: 00286 if (!m_oldLocalShortcut) 00287 return false; 00288 if (column == LocalPrimary) 00289 return m_oldLocalShortcut->primary() != m_action->shortcut().primary(); 00290 else 00291 return m_oldLocalShortcut->alternate() != m_action->shortcut().alternate(); 00292 case GlobalPrimary: 00293 case GlobalAlternate: 00294 if (!m_oldGlobalShortcut) 00295 return false; 00296 if (column == GlobalPrimary) 00297 return m_oldGlobalShortcut->primary() != m_action->globalShortcut().primary(); 00298 else 00299 return m_oldGlobalShortcut->alternate() != m_action->globalShortcut().alternate(); 00300 case ShapeGesture: 00301 return static_cast<bool>(m_oldShapeGesture); 00302 case RockerGesture: 00303 return static_cast<bool>(m_oldRockerGesture); 00304 default: 00305 return false; 00306 } 00307 } 00308 00309 00310 00311 void KShortcutsEditorItem::undo() 00312 { 00313 #ifndef NDEBUG 00314 if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) { 00315 kDebug(125) << "Undoing changes for " << data(Name, Qt::DisplayRole).toString(); 00316 } 00317 #endif 00318 if (m_oldLocalShortcut) { 00319 // We only ever reset the active Shortcut 00320 m_action->setShortcut(*m_oldLocalShortcut, KAction::ActiveShortcut); 00321 } 00322 00323 if (m_oldGlobalShortcut) { 00324 m_action->setGlobalShortcut(*m_oldGlobalShortcut, KAction::ActiveShortcut, 00325 KAction::NoAutoloading); 00326 } 00327 00328 if (m_oldShapeGesture) { 00329 m_action->setShapeGesture(*m_oldShapeGesture); 00330 } 00331 00332 if (m_oldRockerGesture) { 00333 m_action->setRockerGesture(*m_oldRockerGesture); 00334 } 00335 00336 updateModified(); 00337 } 00338 00339 00340 void KShortcutsEditorItem::commit() 00341 { 00342 #ifndef NDEBUG 00343 if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) { 00344 kDebug(125) << "Committing changes for " << data(Name, Qt::DisplayRole).toString(); 00345 } 00346 #endif 00347 00348 delete m_oldLocalShortcut; 00349 m_oldLocalShortcut = 0; 00350 delete m_oldGlobalShortcut; 00351 m_oldGlobalShortcut = 0; 00352 delete m_oldShapeGesture; 00353 m_oldShapeGesture = 0; 00354 delete m_oldRockerGesture; 00355 m_oldRockerGesture = 0; 00356 }
KDE 4.6 API Reference