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

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
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
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