00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00062
00063 connect(parent, SIGNAL(clicked(QModelIndex)), this, SLOT(itemActivated(QModelIndex)));
00064
00065
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
00080 void KShortcutsEditorDelegate::itemActivated(QModelIndex index)
00081 {
00082 const QAbstractItemModel *model = index.model();
00083 if (!model)
00084 return;
00085
00086 QTreeWidget *view = static_cast<QTreeWidget *>(parent());
00087
00088 KShortcutsEditorItem *item = KShortcutsEditorPrivate::itemFromIndex(view, index);
00089 if (!item) {
00090
00091 return;
00092 }
00093
00094 int column = index.column();
00095 if (column == Name) {
00096
00097
00098 if (!view->header()->isSectionHidden(LocalPrimary)) {
00099 column = LocalPrimary;
00100 } else if (!view->header()->isSectionHidden(GlobalPrimary)) {
00101 column = GlobalPrimary;
00102 } else {
00103
00104 }
00105 index = model->index(index.row(), column, index.parent());
00106 view->selectionModel()->select(index, QItemSelectionModel::SelectCurrent);
00107 }
00108
00109
00110 if (!model->data(index, ShowExtensionIndicatorRole).value<bool>()) {
00111 return;
00112 }
00113
00114 if (!isExtended(index)) {
00115
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);
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
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
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);
00176 }
00177
00178 }
00179
00180 }
00181
00182 }
00183
00184
00185
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);
00195 }
00196 }
00197
00198
00199
00200
00201
00202
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
00220 void KShortcutsEditorDelegate::keySequenceChanged(const QKeySequence &seq)
00221 {
00222 QVariant ret = QVariant::fromValue(seq);
00223 emit shortcutChanged(ret, m_editingIndex);
00224 }
00225
00226
00227
00228 void KShortcutsEditorDelegate::shapeGestureChanged(const KShapeGesture &gest)
00229 {
00230
00231 QVariant ret = QVariant::fromValue(gest);
00232 emit shortcutChanged(ret, m_editingIndex);
00233 }
00234
00235
00236
00237 void KShortcutsEditorDelegate::rockerGestureChanged(const KRockerGesture &gest)
00238 {
00239 QVariant ret = QVariant::fromValue(gest);
00240 emit shortcutChanged(ret, m_editingIndex);
00241 }
00242