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

KDEUI

kshortcutseditordelegate.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 
00027 
00028 #include "kshortcutsdialog_p.h"
00029 
00030 #include <QApplication>
00031 #include <QHeaderView>
00032 #include <QLabel>
00033 #include <QPainter>
00034 
00035 #include "kdebug.h"
00036 
00037 
00038 
00039 
00040 KShortcutsEditorDelegate::KShortcutsEditorDelegate(QTreeWidget *parent, bool allowLetterShortcuts)
00041  : KExtendableItemDelegate(parent),
00042    m_allowLetterShortcuts(allowLetterShortcuts),
00043    m_editor(0)
00044 {
00045     Q_ASSERT(qobject_cast<QAbstractItemView *>(parent));
00046 
00047     QPixmap pixmap( 16, 16 );
00048     pixmap.fill( QColor( Qt::transparent ) );
00049     QPainter p( &pixmap );
00050     QStyleOption option;
00051     option.rect = pixmap.rect();
00052 
00053     bool isRtl = QApplication::isRightToLeft();
00054     QApplication::style()->drawPrimitive( isRtl ? QStyle::PE_IndicatorArrowLeft : QStyle::PE_IndicatorArrowRight, &option, &p );
00055     setExtendPixmap( pixmap );
00056 
00057     pixmap.fill( QColor( Qt::transparent ) );
00058     QApplication::style()->drawPrimitive( QStyle::PE_IndicatorArrowDown, &option, &p );
00059     setContractPixmap( pixmap );
00060 
00061     // Listen to activiation signals
00062     // connect(parent, SIGNAL(activated(QModelIndex)), this, SLOT(itemActivated(QModelIndex)));
00063     connect(parent, SIGNAL(clicked(QModelIndex)), this, SLOT(itemActivated(QModelIndex)));
00064 
00065     // Listen to collapse signals
00066     connect(parent, SIGNAL(collapsed(QModelIndex)), this, SLOT(itemCollapsed(QModelIndex)));
00067 }
00068 
00069 
00070 QSize KShortcutsEditorDelegate::sizeHint(const QStyleOptionViewItem &option,
00071                                          const QModelIndex &index) const
00072 {
00073     QSize ret(KExtendableItemDelegate::sizeHint(option, index));
00074     ret.rheight() += 4;
00075     return ret;
00076 }
00077 
00078 
00079 //slot
00080 void KShortcutsEditorDelegate::itemActivated(QModelIndex index)
00081 {
00082     const QAbstractItemModel *model = index.model();
00083     if (!model)
00084         return;
00085     //As per our constructor our parent *is* a QTreeWidget
00086     QTreeWidget *view = static_cast<QTreeWidget *>(parent());
00087 
00088     KShortcutsEditorItem *item = KShortcutsEditorPrivate::itemFromIndex(view, index);
00089     if (!item) {
00090         //that probably was a non-leaf (type() !=ActionItem) item
00091         return;
00092     }
00093 
00094     int column = index.column();
00095     if (column == Name) {
00096         // If user click in the name column activate the (Global|Local)Primary
00097         // column if possible.
00098         if (!view->header()->isSectionHidden(LocalPrimary)) {
00099             column = LocalPrimary;
00100         } else if (!view->header()->isSectionHidden(GlobalPrimary)) {
00101             column = GlobalPrimary;
00102         } else {
00103             // do nothing.
00104         }
00105         index = model->index(index.row(), column, index.parent());
00106         view->selectionModel()->select(index, QItemSelectionModel::SelectCurrent);
00107     }
00108 
00109     // Check if the models wants us to edit the item at index
00110     if (!model->data(index, ShowExtensionIndicatorRole).value<bool>()) {
00111         return;
00112     }
00113 
00114     if (!isExtended(index)) {
00115         //we only want maximum ONE extender open at any time.
00116         if (m_editingIndex.isValid()) {
00117             QModelIndex idx = model->index(m_editingIndex.row(), Name, m_editingIndex.parent());
00118             KShortcutsEditorItem *oldItem = KShortcutsEditorPrivate::itemFromIndex(view, idx);
00119             Q_ASSERT(oldItem); //here we really expect nothing but a real KShortcutsEditorItem
00120 
00121             oldItem->setNameBold(false);
00122             contractItem(m_editingIndex);
00123         }
00124 
00125         m_editingIndex = index;
00126         QWidget *viewport = static_cast<QAbstractItemView*>(parent())->viewport();
00127 
00128         if (column >= LocalPrimary && column <= GlobalAlternate) {
00129             m_editor = new ShortcutEditWidget(viewport,
00130                       model->data(index, DefaultShortcutRole).value<QKeySequence>(),
00131                       model->data(index, ShortcutRole).value<QKeySequence>(),
00132                       m_allowLetterShortcuts);
00133 
00134             connect(m_editor, SIGNAL(keySequenceChanged(const QKeySequence &)),
00135                     this, SLOT(keySequenceChanged(const QKeySequence &)));
00136 
00137         } else if (column == RockerGesture) {
00138             m_editor = new QLabel("A lame placeholder", viewport);
00139 
00140         } else if (column == ShapeGesture) {
00141             m_editor = new QLabel("<i>A towel</i>", viewport);
00142 
00143         } else
00144             return;
00145 
00146         m_editor->installEventFilter(this);
00147         item->setNameBold(true);
00148         extendItem(m_editor, index);
00149 
00150     } else {
00151         //the item is extended, and clicking on it again closes it
00152         item->setNameBold(false);
00153         contractItem(index);
00154         view->selectionModel()->select(index, QItemSelectionModel::Clear);
00155         m_editingIndex = QModelIndex();
00156         m_editor = 0;
00157     }
00158 }
00159 
00160 
00161 //slot
00162 void KShortcutsEditorDelegate::itemCollapsed(QModelIndex index)
00163 {
00164     if (!m_editingIndex.isValid())
00165         return;
00166 
00167     const QAbstractItemModel *model = index.model();
00168     for (int row=0; row<model->rowCount(index);++row) {
00169         QModelIndex rowIndex = model->index(row,0,index);
00170 
00171         for (int col=0; col<index.model()->columnCount(index);++col) {
00172             QModelIndex colIndex = model->index(row,col,index);
00173 
00174             if (colIndex == m_editingIndex) {
00175                 itemActivated(m_editingIndex); //this will *close* the item's editor because it's already open
00176             }
00177 
00178         }
00179 
00180     }
00181 
00182 }
00183 
00184 
00185 //slot
00186 void KShortcutsEditorDelegate::hiddenBySearchLine(QTreeWidgetItem *item, bool hidden)
00187 {
00188     if (!hidden || !item) {
00189         return;
00190     }
00191     QTreeWidget *view = static_cast<QTreeWidget *>(parent());
00192     QTreeWidgetItem *editingItem = KShortcutsEditorPrivate::itemFromIndex(view, m_editingIndex);
00193     if (editingItem == item) {
00194         itemActivated(m_editingIndex); //this will *close* the item's editor because it's already open
00195     }
00196 }
00197 
00198 
00199 //Prevent clicks in the empty part of the editor widget from closing the editor
00200 //because they would propagate to the itemview and be interpreted as a click in
00201 //an item's rect. That in turn would lead to an itemActivated() call, closing
00202 //the current editor.
00203 bool KShortcutsEditorDelegate::eventFilter(QObject *o, QEvent *e)
00204 {
00205     if (o != m_editor)
00206         return false;
00207 
00208     switch (e->type()) {
00209     case QEvent::MouseButtonPress:
00210     case QEvent::MouseButtonRelease:
00211     case QEvent::MouseButtonDblClick:
00212         return true;
00213     default:
00214         return false;
00215     }
00216 }
00217 
00218 
00219 //slot
00220 void KShortcutsEditorDelegate::keySequenceChanged(const QKeySequence &seq)
00221 {
00222     QVariant ret = QVariant::fromValue(seq);
00223     emit shortcutChanged(ret, m_editingIndex);
00224 }
00225 
00226 
00227 //slot
00228 void KShortcutsEditorDelegate::shapeGestureChanged(const KShapeGesture &gest)
00229 {
00230     //this is somewhat verbose because the gesture types are not "built in" to QVariant
00231     QVariant ret = QVariant::fromValue(gest);
00232     emit shortcutChanged(ret, m_editingIndex);
00233 }
00234 
00235 
00236 //slot
00237 void KShortcutsEditorDelegate::rockerGestureChanged(const KRockerGesture &gest)
00238 {
00239     QVariant ret = QVariant::fromValue(gest);
00240     emit shortcutChanged(ret, m_editingIndex);
00241 }
00242 

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