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

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 
00031 #include <QTreeWidgetItem>
00032 
00033 KShortcutsEditorItem::KShortcutsEditorItem(QTreeWidgetItem *parent, KAction *action)
00034     : QTreeWidgetItem(parent, ActionItem)
00035     , m_action(action)
00036     , m_isNameBold(false)
00037     , m_oldLocalShortcut(0)
00038     , m_oldGlobalShortcut(0)
00039     , m_oldShapeGesture(0)
00040     , m_oldRockerGesture(0)
00041 {
00042 }
00043 
00044 
00045 KShortcutsEditorItem::~KShortcutsEditorItem()
00046 {
00047     delete m_oldLocalShortcut;
00048     delete m_oldGlobalShortcut;
00049     delete m_oldShapeGesture;
00050     delete m_oldRockerGesture;
00051 }
00052 
00053 
00054 bool KShortcutsEditorItem::isModified() const
00055 {
00056     return m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture;
00057 }
00058 
00059 
00060 QVariant KShortcutsEditorItem::data(int column, int role) const
00061 {
00062     switch (role) {
00063     case Qt::DisplayRole:
00064         switch(column) {
00065         case Name:
00066             return i18nc("@item:intable Action name in shortcuts configuration", "%1", m_action->text().remove('&'));
00067         case LocalPrimary:
00068         case LocalAlternate:
00069         case GlobalPrimary:
00070         case GlobalAlternate:
00071             return keySequence(column);
00072         case ShapeGesture:
00073             return m_action->shapeGesture().shapeName();
00074         case RockerGesture:
00075             return m_action->rockerGesture().rockerName();
00076         default:
00077             break;
00078         }
00079         break;
00080     case Qt::DecorationRole:
00081         if (column == Name)
00082             return m_action->icon();
00083         else
00084             return KIcon();
00085         break;
00086     case Qt::WhatsThisRole:
00087         return m_action->whatsThis();
00088     case Qt::ToolTipRole:
00089         // There is no such thing as a KAction::description(). So we have
00090         // nothing to display here.
00091         return QVariant();
00092     case Qt::FontRole:
00093         if (column == Name && m_isNameBold) {
00094             QFont modifiedFont = treeWidget()->font();
00095             modifiedFont.setBold(true);
00096             return modifiedFont;
00097         }
00098         break;
00099     case KExtendableItemDelegate::ShowExtensionIndicatorRole:
00100         switch (column) {
00101         case Name:
00102             return false;
00103         case LocalPrimary:
00104         case LocalAlternate:
00105             if (!m_action->isShortcutConfigurable()) {
00106                 return false;
00107             }
00108             return true;
00109         case GlobalPrimary:
00110         case GlobalAlternate:
00111             if (!m_action->isGlobalShortcutEnabled()) {
00112                 return false;
00113             }
00114             return true;
00115         default:
00116             return false;
00117         }
00118     //the following are custom roles, defined in this source file only
00119     case ShortcutRole:
00120         switch(column) {
00121         case LocalPrimary:
00122         case LocalAlternate:
00123         case GlobalPrimary:
00124         case GlobalAlternate:
00125             return keySequence(column);
00126         case ShapeGesture: { //scoping for "ret"
00127             QVariant ret;
00128             ret.setValue(m_action->shapeGesture());
00129             return ret; }
00130         case RockerGesture: {
00131             QVariant ret;
00132             ret.setValue(m_action->rockerGesture());
00133             return ret; }
00134         }
00135 
00136     case DefaultShortcutRole:
00137         switch(column) {
00138         case LocalPrimary:
00139             return m_action->shortcut(KAction::DefaultShortcut).primary();
00140         case LocalAlternate:
00141             return m_action->shortcut(KAction::DefaultShortcut).alternate();
00142         case GlobalPrimary:
00143             return m_action->globalShortcut(KAction::DefaultShortcut).primary();
00144         case GlobalAlternate:
00145             return m_action->globalShortcut(KAction::DefaultShortcut).alternate();
00146         case ShapeGesture: {
00147             QVariant ret;
00148             ret.setValue(m_action->shapeGesture(KAction::DefaultShortcut));
00149             return ret; }
00150         case RockerGesture: {
00151             QVariant ret;
00152             ret.setValue(m_action->rockerGesture(KAction::DefaultShortcut));
00153             return ret; }
00154         }
00155 
00156     default:
00157         break;
00158     }
00159 
00160     return QVariant();
00161 }
00162 
00163 
00164 QKeySequence KShortcutsEditorItem::keySequence(uint column) const
00165 {
00166     switch (column) {
00167     case LocalPrimary:
00168         return m_action->shortcut().primary();
00169     case LocalAlternate:
00170         return m_action->shortcut().alternate();
00171     case GlobalPrimary:
00172         return m_action->globalShortcut().primary();
00173     case GlobalAlternate:
00174         return m_action->globalShortcut().alternate();
00175     default:
00176         return QKeySequence();
00177     }
00178 }
00179 
00180 
00181 void KShortcutsEditorItem::setKeySequence(uint column, const QKeySequence &seq)
00182 {
00183     KShortcut ks;
00184     if (column == GlobalPrimary || column == GlobalAlternate) {
00185         ks = m_action->globalShortcut();
00186         if (!m_oldGlobalShortcut)
00187             m_oldGlobalShortcut = new KShortcut(ks);
00188      } else {
00189         ks = m_action->shortcut();
00190         if (!m_oldLocalShortcut)
00191             m_oldLocalShortcut = new KShortcut(ks);
00192     }
00193 
00194     if (column == LocalAlternate || column == GlobalAlternate)
00195         ks.setAlternate(seq);
00196     else
00197         ks.setPrimary(seq);
00198 
00199     //avoid also setting the default shortcut - what we are setting here is custom by definition
00200     if (column == GlobalPrimary || column == GlobalAlternate)
00201         m_action->setGlobalShortcut(ks, KAction::ActiveShortcut, KAction::NoAutoloading);
00202     else
00203         m_action->setShortcut(ks, KAction::ActiveShortcut);
00204 
00205     updateModified();
00206 }
00207 
00208 
00209 void KShortcutsEditorItem::setShapeGesture(const KShapeGesture &gst)
00210 {
00211     if (!m_oldShapeGesture) {
00212         m_oldShapeGesture = new KShapeGesture(gst);
00213     }
00214     m_action->setShapeGesture(gst);
00215     updateModified();
00216 }
00217 
00218 
00219 void KShortcutsEditorItem::setRockerGesture(const KRockerGesture &gst)
00220 {
00221     if (!m_oldRockerGesture) {
00222         m_oldRockerGesture = new KRockerGesture(gst);
00223     }
00224     m_action->setRockerGesture(gst);
00225     updateModified();
00226 }
00227 
00228 
00229 //our definition of modified is "modified since the chooser was shown".
00230 void KShortcutsEditorItem::updateModified()
00231 {
00232     if (m_oldLocalShortcut && *m_oldLocalShortcut == m_action->shortcut()) {
00233         delete m_oldLocalShortcut;
00234         m_oldLocalShortcut = 0;
00235     }
00236     if (m_oldGlobalShortcut && *m_oldGlobalShortcut == m_action->globalShortcut()) {
00237         delete m_oldGlobalShortcut;
00238         m_oldGlobalShortcut = 0;
00239     }
00240     if (m_oldShapeGesture && *m_oldShapeGesture == m_action->shapeGesture()) {
00241         delete m_oldShapeGesture;
00242         m_oldShapeGesture = 0;
00243     }
00244     if (m_oldRockerGesture && *m_oldRockerGesture == m_action->rockerGesture()) {
00245         delete m_oldRockerGesture;
00246         m_oldRockerGesture = 0;
00247     }
00248 }
00249 
00250 
00251 bool KShortcutsEditorItem::isModified(uint column) const
00252 {
00253     switch (column) {
00254     case Name:
00255         return false;
00256     case LocalPrimary:
00257     case LocalAlternate:
00258         if (!m_oldLocalShortcut)
00259             return false;
00260         if (column == LocalPrimary)
00261             return m_oldLocalShortcut->primary() != m_action->shortcut().primary();
00262         else
00263             return m_oldLocalShortcut->alternate() != m_action->shortcut().alternate();
00264     case GlobalPrimary:
00265     case GlobalAlternate:
00266         if (!m_oldGlobalShortcut)
00267             return false;
00268         if (column == GlobalPrimary)
00269             return m_oldGlobalShortcut->primary() != m_action->globalShortcut().primary();
00270         else
00271             return m_oldGlobalShortcut->alternate() != m_action->globalShortcut().alternate();
00272     case ShapeGesture:
00273         return static_cast<bool>(m_oldShapeGesture);
00274     case RockerGesture:
00275         return static_cast<bool>(m_oldRockerGesture);
00276     default:
00277         return false;
00278     }
00279 }
00280 
00281 
00282 
00283 void KShortcutsEditorItem::undo()
00284 {
00285 #ifndef NDEBUG
00286     if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) {
00287         kDebug(125) << "Undoing changes for " << data(Name, Qt::DisplayRole).toString();
00288     }
00289 #endif
00290     if (m_oldLocalShortcut) {
00291         m_action->setShortcut(*m_oldLocalShortcut);
00292     }
00293 
00294     if (m_oldGlobalShortcut) {
00295         m_action->setGlobalShortcut(*m_oldGlobalShortcut, KAction::ActiveShortcut,
00296                                     KAction::NoAutoloading);
00297     }
00298 
00299     if (m_oldShapeGesture) {
00300         m_action->setShapeGesture(*m_oldShapeGesture);
00301     }
00302 
00303     if (m_oldRockerGesture) {
00304         m_action->setRockerGesture(*m_oldRockerGesture);
00305     }
00306 
00307     updateModified();
00308 }
00309 
00310 
00311 void KShortcutsEditorItem::commit()
00312 {
00313 #ifndef NDEBUG
00314     if (m_oldLocalShortcut || m_oldGlobalShortcut || m_oldShapeGesture || m_oldRockerGesture ) {
00315         kDebug(125) << "Committing changes for " << data(Name, Qt::DisplayRole).toString();
00316     }
00317 #endif
00318 
00319     delete m_oldLocalShortcut; 
00320     m_oldLocalShortcut = 0;
00321     delete m_oldGlobalShortcut;
00322     m_oldGlobalShortcut = 0;
00323     delete m_oldShapeGesture;
00324     m_oldShapeGesture = 0;
00325     delete m_oldRockerGesture;
00326     m_oldRockerGesture = 0;
00327 }

KDEUI

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal