29 #include "kshortcutsdialog_p.h"
30 #include "kaction_p.h"
32 #include <QApplication>
33 #include <QHeaderView>
37 #include <QTreeWidgetItemIterator>
46 KShortcutsEditorDelegate::KShortcutsEditorDelegate(
QTreeWidget *parent,
bool allowLetterShortcuts)
48 m_allowLetterShortcuts(allowLetterShortcuts),
51 Q_ASSERT(qobject_cast<QAbstractItemView *>(parent));
53 QPixmap pixmap( 16, 16 );
54 pixmap.fill(
QColor( Qt::transparent ) );
55 QPainter p( &pixmap );
57 option.rect = pixmap.rect();
59 bool isRtl = QApplication::isRightToLeft();
60 QApplication::style()->drawPrimitive( isRtl ? QStyle::PE_IndicatorArrowLeft : QStyle::PE_IndicatorArrowRight, &option, &p );
62 setExtendPixmap( pixmap );
64 pixmap.fill(
QColor( Qt::transparent ) );
66 QApplication::style()->drawPrimitive( QStyle::PE_IndicatorArrowDown, &option, &p );
68 setContractPixmap( pixmap );
70 parent->installEventFilter(
this);
74 connect(parent, SIGNAL(clicked(QModelIndex)),
this, SLOT(itemActivated(QModelIndex)));
77 connect(parent, SIGNAL(collapsed(QModelIndex)),
this, SLOT(itemCollapsed(QModelIndex)));
81 void KShortcutsEditorDelegate::stealShortcut(
82 const QKeySequence &seq,
88 QTreeWidgetItemIterator it(view, QTreeWidgetItemIterator::NoChildren);
91 KShortcutsEditorItem* item =
dynamic_cast<KShortcutsEditorItem *
>(*it);
92 if (item && item->data(0, ObjectRole).value<
QObject*>() == action) {
97 if ( cut.
primary().matches(seq) != QKeySequence::NoMatch
98 || seq.matches(cut.
primary()) != QKeySequence::NoMatch) {
99 item->setKeySequence(LocalPrimary, QKeySequence());
102 if ( cut.
alternate().matches(seq) != QKeySequence::NoMatch
103 || seq.matches(cut.
alternate()) != QKeySequence::NoMatch) {
104 item->setKeySequence(LocalAlternate, QKeySequence());
113 QSize KShortcutsEditorDelegate::sizeHint(
const QStyleOptionViewItem &option,
114 const QModelIndex &index)
const
123 void KShortcutsEditorDelegate::itemActivated(QModelIndex index)
128 KShortcutsEditorItem *item = KShortcutsEditorPrivate::itemFromIndex(view, index);
134 int column = index.column();
135 if (column == Name) {
138 if (!view->header()->isSectionHidden(LocalPrimary)) {
139 column = LocalPrimary;
140 }
else if (!view->header()->isSectionHidden(GlobalPrimary)) {
141 column = GlobalPrimary;
145 index = index.sibling(index.row(), column);
146 view->selectionModel()->select(index, QItemSelectionModel::SelectCurrent);
150 if (!index.data(ShowExtensionIndicatorRole).value<
bool>()) {
154 if (!isExtended(index)) {
156 if (m_editingIndex.isValid()) {
157 KShortcutsEditorItem *oldItem = KShortcutsEditorPrivate::itemFromIndex(view,
161 oldItem->setNameBold(
false);
162 contractItem(m_editingIndex);
165 m_editingIndex = index;
166 QWidget *viewport =
static_cast<QAbstractItemView*
>(parent())->viewport();
168 if (column >= LocalPrimary && column <= GlobalAlternate) {
169 ShortcutEditWidget *editor =
new ShortcutEditWidget(viewport,
170 index.data(DefaultShortcutRole).value<QKeySequence>(),
171 index.data(ShortcutRole).value<QKeySequence>(),
172 m_allowLetterShortcuts);
173 if (column==GlobalPrimary) {
176 action, SIGNAL(globalShortcutChanged(QKeySequence)),
177 editor, SLOT(setKeySequence(QKeySequence)));
178 editor->setMultiKeyShortcutsAllowed(
false);
181 editor->setComponentName(kaction->d->componentData.componentName());
187 if (column == GlobalPrimary || column == GlobalAlternate) {
188 editor->setCheckForConflictsAgainst(
194 editor->setCheckActionCollections(m_checkActionCollections);
196 connect(m_editor, SIGNAL(keySequenceChanged(QKeySequence)),
197 this, SLOT(keySequenceChanged(QKeySequence)));
198 connect(m_editor, SIGNAL(stealShortcut(QKeySequence,
KAction*)),
199 this, SLOT(stealShortcut(QKeySequence,
KAction*)));
201 }
else if (column == RockerGesture) {
202 m_editor =
new QLabel(
"A lame placeholder", viewport);
204 }
else if (column == ShapeGesture) {
205 m_editor =
new QLabel(
"<i>A towel</i>", viewport);
210 m_editor->installEventFilter(
this);
211 item->setNameBold(
true);
212 extendItem(m_editor, index);
216 item->setNameBold(
false);
219 m_editingIndex = QModelIndex();
226 void KShortcutsEditorDelegate::itemCollapsed(QModelIndex index)
228 if (!m_editingIndex.isValid()) {
233 for (
int row = 0; row < model->rowCount(index); ++row) {
234 QModelIndex rowIndex = model->index(row, 0, index);
236 for (
int col = 0; col < index.model()->columnCount(index); ++col) {
237 QModelIndex colIndex = model->index(row, col, index);
239 if (colIndex == m_editingIndex) {
240 itemActivated(m_editingIndex);
248 void KShortcutsEditorDelegate::hiddenBySearchLine(QTreeWidgetItem *item,
bool hidden)
250 if (!hidden || !item) {
254 QTreeWidgetItem *editingItem = KShortcutsEditorPrivate::itemFromIndex(view, m_editingIndex);
255 if (editingItem == item) {
256 itemActivated(m_editingIndex);
261 bool KShortcutsEditorDelegate::eventFilter(
QObject *o, QEvent *e)
270 case QEvent::MouseButtonPress:
271 case QEvent::MouseButtonRelease:
272 case QEvent::MouseButtonDblClick:
277 }
else if (o == parent()) {
283 if (e->type() != QEvent::KeyPress) {
286 QKeyEvent *ke =
static_cast<QKeyEvent *
>(e);
289 QModelIndex index = selection->currentIndex();
296 itemActivated(index);
299 index = index.sibling(index.row(), index.column() - 1);
302 index = index.sibling(index.row(), index.column() + 1);
308 if (index.isValid()) {
309 selection->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
312 view->scrollTo(index, QAbstractItemView::PositionAtCenter);
321 void KShortcutsEditorDelegate::keySequenceChanged(
const QKeySequence &seq)
323 QVariant ret = QVariant::fromValue(seq);
324 emit shortcutChanged(ret, m_editingIndex);
328 void KShortcutsEditorDelegate::setCheckActionCollections(
331 m_checkActionCollections = checkActionCollections;
335 void KShortcutsEditorDelegate::shapeGestureChanged(
const KShapeGesture &gest)
338 QVariant ret = QVariant::fromValue(gest);
339 emit shortcutChanged(ret, m_editingIndex);
344 void KShortcutsEditorDelegate::rockerGestureChanged(
const KRockerGesture &gest)
346 QVariant ret = QVariant::fromValue(gest);
347 emit shortcutChanged(ret, m_editingIndex);
KAction * cut(const QObject *recvr, const char *slot, QObject *parent)
Cut selected area and store it in the clipboard.
Represents a keyboard shortcut.
QKeySequence alternate() const
Returns the alternate key sequence of this shortcut.
Class to encapsulate user-driven action or event.
QKeySequence primary() const
Returns the primary key sequence of this shortcut.
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
Re-implemented for internal reasons.