Commit 2c9e073c authored by Robert Knight's avatar Robert Knight
Browse files

BUG: 149626 - Use the KDE shortcut editor widget to edit shortcuts in the Manage Profiles dialog.

(Requires recent kdelibs rv. 792234 for the 'Clear' button to work)

Squashed commit of the following:

commit d60cba88cc4c4027220558cf6fe5487022beb767
Author: Robert Knight <robertknight@gmail.com>
Date:   Mon Mar 31 19:05:00 2008 +0100

    Make the Favorite column toggle respond to double click events as well so that quick clicks in succession are not ignored.

commit 5dd0730a2a838a88d50a91dc7a70ec2ffda23c4b
Author: Robert Knight <robertknight@gmail.com>
Date:   Mon Mar 31 19:02:33 2008 +0100

    Allow more width for the Shortcut column in the Manage Profiles dialog so that the shortcut editor has enough room.

commit 2271b154b1d3c03442827b7553a827ab0b199344
Author: Robert Knight <robertknight@gmail.com>
Date:   Mon Mar 31 17:58:14 2008 +0100

    Do not draw shortcut text whilst a shortcut is being edited.  Move styled background drawing into new class.

commit 4b06713f63d41b0df22fa2e03ab3f7e90be8ab9f
Author: Robert Knight <robertknight@gmail.com>
Date:   Mon Mar 31 17:35:57 2008 +0100

    Prevent profile shortcut being cleared as soon as editing starts - do not connect to the keySequenceChanged() signal until after the existing shortcut has been applied to the editor.  Change edit triggers for table view to start editing when a selected item is clicked.

commit 84ff71e40a1e6b9993c45d8fc17c6aba3a5e25a5
Author: Robert Knight <robertknight@gmail.com>
Date:   Mon Mar 31 17:09:07 2008 +0100

    Use KDE shortcut edit widget to edit shortcuts in Manage Profiles dialog.  Rename ProfileItemDelegate to FavoriteItemDelegate and introduce a new ShortcutItemDelegate for the shortcut column which uses KKeySequenceWidget to edit shortcuts.

svn path=/trunk/KDE/kdebase/apps/konsole/; revision=792245
parent e0e9693d
......@@ -30,6 +30,8 @@
#include <QtGui/QShowEvent>
#include <QtGui/QStandardItem>
// KDE
#include <KKeySequenceWidget>
#include <KDebug>
// Konsole
......@@ -50,7 +52,9 @@ ManageProfilesDialog::ManageProfilesDialog(QWidget* parent)
// hide vertical header
_ui->sessionTable->verticalHeader()->hide();
_ui->sessionTable->setItemDelegateForColumn(FavoriteStatusColumn,new ProfileItemDelegate(this));
_ui->sessionTable->setItemDelegateForColumn(FavoriteStatusColumn,new FavoriteItemDelegate(this));
_ui->sessionTable->setItemDelegateForColumn(ShortcutColumn,new ShortcutItemDelegate(this));
_ui->sessionTable->setEditTriggers(_ui->sessionTable->editTriggers() | QAbstractItemView::SelectedClicked);
// update table and listen for changes to the session types
updateTableModel();
......@@ -68,6 +72,11 @@ ManageProfilesDialog::ManageProfilesDialog(QWidget* parent)
_ui->sessionTable->horizontalHeader()->setHighlightSections(false);
_ui->sessionTable->resizeColumnsToContents();
// allow a larger width for the shortcut column to account for the
// increased with needed by the shortcut editor compared with just
// displaying the text of the shortcut
_ui->sessionTable->setColumnWidth(ShortcutColumn,
_ui->sessionTable->columnWidth(ShortcutColumn)+100);
// setup buttons
connect( _ui->newSessionButton , SIGNAL(clicked()) , this , SLOT(newType()) );
......@@ -311,23 +320,28 @@ void ManageProfilesDialog::setShortcutEditorVisible(bool visible)
{
_ui->sessionTable->setColumnHidden(ShortcutColumn,!visible);
}
ProfileItemDelegate::ProfileItemDelegate(QObject* parent)
void StyledBackgroundPainter::drawBackground(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex&)
{
const QStyleOptionViewItemV3* v3option = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option);
const QWidget* widget = v3option ? v3option->widget : 0;
QStyle* style = widget ? widget->style() : QApplication::style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem,&option,painter,widget);
}
FavoriteItemDelegate::FavoriteItemDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
void ProfileItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
void FavoriteItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
// See implementation of QStyledItemDelegate::paint()
QStyleOptionViewItemV4 opt = option;
initStyleOption(&opt,index);
const QStyleOptionViewItemV3* v3option = qstyleoption_cast<const QStyleOptionViewItemV3*>(&option);
const QWidget* widget = v3option ? v3option->widget : 0;
QStyle* style = widget ? widget->style() : QApplication::style();
style->drawPrimitive(QStyle::PE_PanelItemViewItem,&opt,painter,widget);
StyledBackgroundPainter::drawBackground(painter,opt,index);
int margin = (opt.rect.height()-opt.decorationSize.height())/2;
margin++;
......@@ -339,10 +353,11 @@ void ProfileItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& o
icon.paint(painter,opt.rect,Qt::AlignCenter);
}
bool ProfileItemDelegate::editorEvent(QEvent* event,QAbstractItemModel*,
bool FavoriteItemDelegate::editorEvent(QEvent* event,QAbstractItemModel*,
const QStyleOptionViewItem&,const QModelIndex& index)
{
if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::KeyPress )
if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::KeyPress
|| event->type() == QEvent::MouseButtonDblClick )
{
Profile::Ptr profile = index.data(ManageProfilesDialog::ProfileKeyRole).value<Profile::Ptr>();
const bool isFavorite = !SessionManager::instance()->findFavorites().contains(profile);
......@@ -353,5 +368,55 @@ bool ProfileItemDelegate::editorEvent(QEvent* event,QAbstractItemModel*,
return true;
}
ShortcutItemDelegate::ShortcutItemDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
void ShortcutItemDelegate::editorModified(const QKeySequence& keys)
{
kDebug() << keys.toString();
KKeySequenceWidget* editor = qobject_cast<KKeySequenceWidget*>(sender());
Q_ASSERT(editor);
_modifiedEditors.insert(editor);
}
void ShortcutItemDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const
{
_itemsBeingEdited.remove(index);
if (!_modifiedEditors.contains(editor))
return;
QString shortcut = qobject_cast<KKeySequenceWidget*>(editor)->keySequence().toString();
model->setData(index,shortcut,Qt::DisplayRole);
_modifiedEditors.remove(editor);
}
QWidget* ShortcutItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index) const
{
_itemsBeingEdited.insert(index);
KKeySequenceWidget* editor = new KKeySequenceWidget(parent);
editor->setFocusPolicy(Qt::StrongFocus);
editor->setModifierlessAllowed(false);
QString shortcutString = index.data(Qt::DisplayRole).toString();
editor->setKeySequence(QKeySequence::fromString(shortcutString));
connect(editor,SIGNAL(keySequenceChanged(QKeySequence)),this,SLOT(editorModified(QKeySequence)));
editor->captureKeySequence();
return editor;
}
void ShortcutItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (_itemsBeingEdited.contains(index))
StyledBackgroundPainter::drawBackground(painter,option,index);
else
QStyledItemDelegate::paint(painter,option,index);
}
#include "ManageProfilesDialog.moc"
......@@ -22,6 +22,7 @@
// Qt
#include <QtGui/QStyledItemDelegate>
#include <QtCore/QSet>
// KDE
#include <KDialog>
......@@ -51,7 +52,8 @@ class ManageProfilesDialog : public KDialog
{
Q_OBJECT
friend class ProfileItemDelegate;
friend class FavoriteItemDelegate;
friend class ShortcutItemDelegate;
public:
/** Constructs a new profile type with the specified parent. */
......@@ -105,10 +107,17 @@ private:
static const int ShortcutRole = Qt::UserRole + 1;
};
class ProfileItemDelegate : public QStyledItemDelegate
class StyledBackgroundPainter
{
public:
ProfileItemDelegate(QObject* parent = 0);
static void drawBackground(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index);
};
class FavoriteItemDelegate : public QStyledItemDelegate
{
public:
FavoriteItemDelegate(QObject* parent = 0);
virtual bool editorEvent(QEvent* event,QAbstractItemModel* model,
const QStyleOptionViewItem& option,const QModelIndex& index);
......@@ -116,6 +125,27 @@ public:
const QModelIndex& index) const;
};
class ShortcutItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
ShortcutItemDelegate(QObject* parent = 0);
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const;
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
virtual void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const;
private slots:
void editorModified(const QKeySequence& keys);
private:
mutable QSet<QWidget*> _modifiedEditors;
mutable QSet<QModelIndex> _itemsBeingEdited;
};
}
#endif // MANAGEPROFILESDIALOG_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment