kdeui: semi-working KShortcutsEditor

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-24 13:41:29 +03:00
parent 378011afe8
commit f623641b73
5 changed files with 136 additions and 53 deletions

View file

@ -77,8 +77,10 @@ public:
}; };
KShortcutsDialog::KShortcutsDialog( KShortcutsEditor::ActionTypes types, KShortcutsEditor::LetterShortcuts allowLetterShortcuts, QWidget *parent ) KShortcutsDialog::KShortcutsDialog(KShortcutsEditor::ActionTypes types,
: KDialog( parent ), d(new KShortcutsDialogPrivate(this)) KShortcutsEditor::LetterShortcuts allowLetterShortcuts, QWidget *parent)
: KDialog(parent),
d(new KShortcutsDialogPrivate(this))
{ {
setCaption(i18n("Configure Shortcuts")); setCaption(i18n("Configure Shortcuts"));
setButtons(Reset|Ok|Cancel); setButtons(Reset|Ok|Cancel);
@ -135,7 +137,8 @@ QSize KShortcutsDialog::sizeHint() const
return QSize(600, 480); return QSize(600, 480);
} }
int KShortcutsDialog::configure(KActionCollection *collection, KShortcutsEditor::LetterShortcuts allowLetterShortcuts, int KShortcutsDialog::configure(KActionCollection *collection,
KShortcutsEditor::LetterShortcuts allowLetterShortcuts,
QWidget *parent, bool saveSettings) QWidget *parent, bool saveSettings)
{ {
kDebug(125) << "KShortcutsDialog::configureKeys( KActionCollection*, " << saveSettings << " )"; kDebug(125) << "KShortcutsDialog::configureKeys( KActionCollection*, " << saveSettings << " )";

View file

@ -77,7 +77,7 @@ public:
*/ */
explicit KShortcutsDialog(KShortcutsEditor::ActionTypes types = KShortcutsEditor::AllActions, explicit KShortcutsDialog(KShortcutsEditor::ActionTypes types = KShortcutsEditor::AllActions,
KShortcutsEditor::LetterShortcuts allowLetterShortcuts = KShortcutsEditor::LetterShortcutsAllowed, KShortcutsEditor::LetterShortcuts allowLetterShortcuts = KShortcutsEditor::LetterShortcutsAllowed,
QWidget *parent = 0); QWidget *parent = nullptr);
/** /**
* Destructor. Deletes all resources used by a KShortcutsDialog object. * Destructor. Deletes all resources used by a KShortcutsDialog object.
@ -120,8 +120,9 @@ public:
* *
* @return Accept if the dialog was closed with OK, Reject otherwise. * @return Accept if the dialog was closed with OK, Reject otherwise.
*/ */
static int configure(KActionCollection *collection, KShortcutsEditor::LetterShortcuts allowLetterShortcuts = static int configure(KActionCollection *collection,
KShortcutsEditor::LetterShortcutsAllowed, QWidget *parent = 0, bool bSaveSettings = true); KShortcutsEditor::LetterShortcuts allowLetterShortcuts = KShortcutsEditor::LetterShortcutsAllowed,
QWidget *parent = nullptr, bool bSaveSettings = true);
Q_SIGNALS: Q_SIGNALS:
/** /**

View file

@ -25,48 +25,85 @@
#include "kshortcutseditor.h" #include "kshortcutseditor.h"
#include <QHBoxLayout>
#include <QHeaderView> #include <QHeaderView>
#include <QList> #include <QTreeWidget>
#include <QObject>
#include <QTimer>
#include <QTextDocument>
#include <QTextTable>
#include <QTextCursor>
#include <QTextFormat>
#include <QPrinter>
#include <QPrintDialog>
#include "kaction.h" #include "kaction.h"
#include "kactioncollection.h" #include "kactioncollection.h"
#include "kactioncategory.h" #include "kkeysequencewidget.h"
#include "kdebug.h"
#include "kdeprintdialog.h"
#include "kglobalaccel.h"
#include "kmessagebox.h"
#include "kaboutdata.h"
#include "kconfiggroup.h" #include "kconfiggroup.h"
#include "klocale.h"
#include "kdebug.h"
class KShortcutsEditorPrivate class KShortcutsEditorPrivate
{ {
public: public:
KShortcutsEditorPrivate();
void init(KShortcutsEditor *parent, const KShortcutsEditor::ActionTypes actionType,
const KShortcutsEditor::LetterShortcuts allowLetterShortcuts);
KShortcutsEditor* parent;
KShortcutsEditor::ActionTypes actionTypes; KShortcutsEditor::ActionTypes actionTypes;
bool allowLetterShortcuts;
QHBoxLayout* layout;
QTreeWidget* treewidget;
QList<KActionCollection*> actionCollections; QList<KActionCollection*> actionCollections;
}; };
KShortcutsEditor::KShortcutsEditor(KActionCollection *collection, QWidget *parent, ActionTypes actionType, KShortcutsEditorPrivate::KShortcutsEditorPrivate()
LetterShortcuts allowLetterShortcuts ) : layout(nullptr),
treewidget(nullptr)
{
}
void KShortcutsEditorPrivate::init(KShortcutsEditor *_parent,
const KShortcutsEditor::ActionTypes actionType,
const KShortcutsEditor::LetterShortcuts _allowLetterShortcuts)
{
parent = _parent;
actionTypes = actionType;
allowLetterShortcuts = (_allowLetterShortcuts == KShortcutsEditor::LetterShortcutsAllowed);
layout = new QHBoxLayout(parent);
parent->setLayout(layout);
treewidget = new QTreeWidget(parent);
treewidget->setColumnCount(3);
QStringList treeheaders = QStringList()
<< i18n("Collection")
<< i18n("Local")
<< i18n("Global");
treewidget->setHeaderLabels(treeheaders);
treewidget->setRootIsDecorated(true);
QHeaderView* treeheader = treewidget->header();
treeheader->setMovable(false);
treeheader->setStretchLastSection(false);
treeheader->setResizeMode(0, QHeaderView::Stretch);
treeheader->setResizeMode(1, QHeaderView::Stretch);
treeheader->setResizeMode(2, QHeaderView::Stretch);
treeheader->setSectionHidden(1, !(actionType & KShortcutsEditor::LocalAction));
treeheader->setSectionHidden(2, !(actionType & KShortcutsEditor::GlobalAction));
layout->addWidget(treewidget);
}
KShortcutsEditor::KShortcutsEditor(KActionCollection *collection, QWidget *parent,
ActionTypes actionType, LetterShortcuts allowLetterShortcuts)
: QWidget(parent), : QWidget(parent),
d(new KShortcutsEditorPrivate()) d(new KShortcutsEditorPrivate())
{ {
d->actionTypes = actionType; d->init(this, actionType, allowLetterShortcuts);
addCollection(collection); addCollection(collection);
} }
KShortcutsEditor::KShortcutsEditor(QWidget *parent, ActionTypes actionType, LetterShortcuts allowLetterShortcuts) KShortcutsEditor::KShortcutsEditor(QWidget *parent, ActionTypes actionType,
LetterShortcuts allowLetterShortcuts)
: QWidget(parent), : QWidget(parent),
d(new KShortcutsEditorPrivate()) d(new KShortcutsEditorPrivate())
{ {
d->actionTypes = actionType; d->init(this, actionType, allowLetterShortcuts);
} }
KShortcutsEditor::~KShortcutsEditor() KShortcutsEditor::~KShortcutsEditor()
@ -76,32 +113,78 @@ KShortcutsEditor::~KShortcutsEditor()
bool KShortcutsEditor::isModified() const bool KShortcutsEditor::isModified() const
{ {
// TODO: implement
return false; return false;
} }
void KShortcutsEditor::clearCollections() void KShortcutsEditor::clearCollections()
{ {
d->actionCollections.clear(); d->actionCollections.clear();
d->treewidget->clear();
} }
void KShortcutsEditor::addCollection(KActionCollection *collection, const QString &title) void KShortcutsEditor::addCollection(KActionCollection *collection, const QString &title)
{ {
// KXmlGui add action collections unconditionally. If some plugin doesn't
// provide actions we don't want to create empty subgroups.
if (collection->isEmpty()) { if (collection->isEmpty()) {
return; return;
} }
d->actionCollections.append(collection); d->actionCollections.append(collection);
}
void KShortcutsEditor::clearConfiguration() QString text = title;
{ if (text.isEmpty()) {
text = collection->objectName();
}
if (text.isEmpty()) {
text = QString::number(quintptr(collection));
}
QTreeWidgetItem* topitem = new QTreeWidgetItem();
topitem->setText(0, text);
int rowcounter = 0;
foreach (QAction *action, collection->actions()) {
QTreeWidgetItem* actionitem = new QTreeWidgetItem(topitem);
actionitem->setIcon(0, action->icon());
actionitem->setText(0, action->iconText());
if (d->actionTypes & KShortcutsEditor::LocalAction) {
KKeySequenceWidget* localkswidget = new KKeySequenceWidget(d->treewidget);
localkswidget->setKeySequence(action->shortcut());
localkswidget->setModifierlessAllowed(d->allowLetterShortcuts);
d->treewidget->setItemWidget(actionitem, 1, localkswidget);
}
if (d->actionTypes & KShortcutsEditor::GlobalAction) {
KAction* kaction = qobject_cast<KAction*>(action);
if (kaction) {
KKeySequenceWidget* globalkswidget = new KKeySequenceWidget(d->treewidget);
globalkswidget->setKeySequence(kaction->globalShortcut());
globalkswidget->setModifierlessAllowed(d->allowLetterShortcuts);
d->treewidget->setItemWidget(actionitem, 2, globalkswidget);
} else {
kWarning() << "action is not KAction" << action;
}
}
rowcounter++;
}
d->treewidget->addTopLevelItem(topitem);
} }
void KShortcutsEditor::importConfiguration(KConfigBase *config) void KShortcutsEditor::importConfiguration(KConfigBase *config)
{ {
Q_ASSERT(config);
if (!config) {
return;
}
if (d->actionTypes & KShortcutsEditor::LocalAction) {
KConfigGroup group(config, "Shortcuts");
foreach (KActionCollection* collection, d->actionCollections) {
collection->readSettings(&group);
}
}
if (d->actionTypes & KShortcutsEditor::GlobalAction) {
KConfigGroup group(config, "Global Shortcuts");
foreach (KActionCollection* collection, d->actionCollections) {
collection->importGlobalShortcuts(&group);
}
}
} }
void KShortcutsEditor::exportConfiguration(KConfigBase *config) const void KShortcutsEditor::exportConfiguration(KConfigBase *config) const
@ -111,18 +194,18 @@ void KShortcutsEditor::exportConfiguration(KConfigBase *config) const
return; return;
} }
if (d->actionTypes & KShortcutsEditor::LocalAction) {
KConfigGroup group(config, "Shortcuts");
foreach (KActionCollection* collection, d->actionCollections) {
collection->writeSettings(&group, true);
}
}
if (d->actionTypes & KShortcutsEditor::GlobalAction) { if (d->actionTypes & KShortcutsEditor::GlobalAction) {
KConfigGroup group(config, "Global Shortcuts"); KConfigGroup group(config, "Global Shortcuts");
foreach (KActionCollection* collection, d->actionCollections) { foreach (KActionCollection* collection, d->actionCollections) {
collection->exportGlobalShortcuts(&group, true); collection->exportGlobalShortcuts(&group, true);
} }
} }
if (d->actionTypes & ~KShortcutsEditor::GlobalAction) {
KConfigGroup group(config, "Shortcuts");
foreach (KActionCollection* collection, d->actionCollections) {
collection->writeSettings(&group, true);
}
}
} }
void KShortcutsEditor::writeConfiguration(KConfigGroup *config) const void KShortcutsEditor::writeConfiguration(KConfigGroup *config) const
@ -134,6 +217,7 @@ void KShortcutsEditor::writeConfiguration(KConfigGroup *config) const
void KShortcutsEditor::commit() void KShortcutsEditor::commit()
{ {
// TODO: implement
} }
void KShortcutsEditor::save() void KShortcutsEditor::save()
@ -144,11 +228,13 @@ void KShortcutsEditor::save()
void KShortcutsEditor::undoChanges() void KShortcutsEditor::undoChanges()
{ {
// TODO: implement
} }
void KShortcutsEditor::allDefault() void KShortcutsEditor::allDefault()
{ {
// TODO: implement
} }
#include "moc_kshortcutseditor.cpp" #include "moc_kshortcutseditor.cpp"

View file

@ -27,13 +27,11 @@
#include <kdeui_export.h> #include <kdeui_export.h>
#include <QtGui/QWidget> #include <QWidget>
class KActionCollection; class KActionCollection;
class KConfig;
class KConfigBase; class KConfigBase;
class KConfigGroup; class KConfigGroup;
class KGlobalAccel;
class KShortcutsEditorPrivate; class KShortcutsEditorPrivate;
// KShortcutsEditor expects that the list of existing shortcuts is already // KShortcutsEditor expects that the list of existing shortcuts is already
@ -63,9 +61,9 @@ class KDEUI_EXPORT KShortcutsEditor : public QWidget
public: public:
enum ActionType { enum ActionType {
/// Actions which are triggered by any keypress in a widget /// Actions which are triggered by any keypress in a widget
LocalAction = 0, LocalAction = 1,
/// Actions which are triggered by any keypress in the windowing system /// Actions which are triggered by any keypress in the windowing system
GlobalAction = 1, GlobalAction = 2,
/// All actions /// All actions
AllActions = (LocalAction | GlobalAction) AllActions = (LocalAction | GlobalAction)
}; };
@ -104,7 +102,7 @@ public:
* @param allowLetterShortcuts set to LetterShortcutsDisallowed if unmodified alphanumeric * @param allowLetterShortcuts set to LetterShortcutsDisallowed if unmodified alphanumeric
* keys ('A', '1', etc.) are not permissible shortcuts. * keys ('A', '1', etc.) are not permissible shortcuts.
*/ */
explicit KShortcutsEditor(QWidget* parent, ActionTypes actionTypes = AllActions, explicit KShortcutsEditor(QWidget *parent, ActionTypes actionTypes = AllActions,
LetterShortcuts allowLetterShortcuts = LetterShortcutsAllowed); LetterShortcuts allowLetterShortcuts = LetterShortcutsAllowed);
/// Destructor /// Destructor
@ -151,11 +149,6 @@ public:
*/ */
void commit(); void commit();
/**
* Removes all configured shortcuts.
*/
void clearConfiguration();
/** /**
* Write the current settings to the \p config object. * Write the current settings to the \p config object.
* *
@ -170,7 +163,7 @@ public:
* applications config object * applications config object
* *
*/ */
void writeConfiguration(KConfigGroup* config = 0 ) const; void writeConfiguration(KConfigGroup* config = nullptr) const;
/** /**
* Export the current setting to configuration @p config. * Export the current setting to configuration @p config.

View file

@ -685,7 +685,7 @@ bool KKeySequenceButton::event(QEvent* e)
void KKeySequenceButton::keyPressEvent(QKeyEvent *e) void KKeySequenceButton::keyPressEvent(QKeyEvent *e)
{ {
int keyQt = e->key(); int keyQt = e->key();
if (keyQt == -1) { if (keyQt <= 0) {
// Qt sometimes returns garbage keycodes, I observed -1, if it doesn't know a key. // Qt sometimes returns garbage keycodes, I observed -1, if it doesn't know a key.
// We cannot do anything useful with those (several keys have -1, indistinguishable) // We cannot do anything useful with those (several keys have -1, indistinguishable)
// and QKeySequence.toString() will also yield a garbage string. // and QKeySequence.toString() will also yield a garbage string.
@ -771,7 +771,7 @@ void KKeySequenceButton::keyPressEvent(QKeyEvent *e)
void KKeySequenceButton::keyReleaseEvent(QKeyEvent *e) void KKeySequenceButton::keyReleaseEvent(QKeyEvent *e)
{ {
if (e->key() == -1) { if (e->key() <= 0) {
// ignore garbage, see keyPressEvent() // ignore garbage, see keyPressEvent()
return; return;
} }