kdelibs/kdeui/dialogs/kshortcutseditor.cpp

254 lines
7.6 KiB
C++
Raw Normal View History

/*
This file is part of the KDE libraries
Copyright (C) 2024 Ivailo Monev <xakepa10@gmail.com>
2014-11-13 01:04:59 +02:00
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
2014-11-13 01:04:59 +02:00
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kshortcutseditor.h"
#include <QHBoxLayout>
2014-11-13 01:04:59 +02:00
#include <QHeaderView>
#include <QTreeWidget>
2014-11-13 01:04:59 +02:00
#include "kaction.h"
#include "kactioncollection.h"
#include "kkeysequencewidget.h"
#include "kaboutdata.h"
#include "kconfiggroup.h"
#include "klocale.h"
#include "kdebug.h"
2014-11-13 01:04:59 +02:00
class KShortcutsEditorPrivate
{
public:
KShortcutsEditorPrivate();
void init(KShortcutsEditor *parent, const KShortcutsEditor::ActionTypes actionType,
const KShortcutsEditor::LetterShortcuts allowLetterShortcuts);
KShortcutsEditor* parent;
KShortcutsEditor::ActionTypes actionTypes;
bool allowLetterShortcuts;
QHBoxLayout* layout;
QTreeWidget* treewidget;
QList<KActionCollection*> actionCollections;
};
2014-11-13 01:04:59 +02:00
KShortcutsEditorPrivate::KShortcutsEditorPrivate()
: 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),
d(new KShortcutsEditorPrivate())
2014-11-13 01:04:59 +02:00
{
d->init(this, actionType, allowLetterShortcuts);
2014-11-13 01:04:59 +02:00
addCollection(collection);
}
KShortcutsEditor::KShortcutsEditor(QWidget *parent, ActionTypes actionType,
LetterShortcuts allowLetterShortcuts)
: QWidget(parent),
d(new KShortcutsEditorPrivate())
2014-11-13 01:04:59 +02:00
{
d->init(this, actionType, allowLetterShortcuts);
2014-11-13 01:04:59 +02:00
}
KShortcutsEditor::~KShortcutsEditor()
{
delete d;
}
bool KShortcutsEditor::isModified() const
{
// TODO: implement
2014-11-13 01:04:59 +02:00
return false;
}
void KShortcutsEditor::clearCollections()
{
d->actionCollections.clear();
d->treewidget->clear();
2014-11-13 01:04:59 +02:00
}
void KShortcutsEditor::addCollection(KActionCollection *collection, const QString &title)
{
if (collection->isEmpty()) {
return;
}
d->actionCollections.append(collection);
QString text = title;
if (text.isEmpty()) {
const KAboutData* aboutdata = collection->componentData().aboutData();
if (aboutdata) {
text = aboutdata->programName();
}
}
if (text.isEmpty()) {
text = collection->objectName();
}
if (text.isEmpty()) {
text = QString::number(quintptr(collection));
}
QTreeWidgetItem* topitem = nullptr;
for (int i = 0; i < d->treewidget->topLevelItemCount(); i++) {
QTreeWidgetItem* treeitem = d->treewidget->topLevelItem(i);
if (treeitem->text(0) == text) {
topitem = treeitem;
break;
}
}
if (!topitem) {
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);
topitem->setExpanded(true);
2014-11-13 01:04:59 +02:00
}
void KShortcutsEditor::importConfiguration(KConfigBase *config)
2014-11-13 01:04:59 +02:00
{
Q_ASSERT(config);
if (!config) {
return;
}
2014-11-13 01:04:59 +02:00
if (d->actionTypes & KShortcutsEditor::LocalAction) {
KConfigGroup group(config, "Shortcuts");
foreach (KActionCollection* collection, d->actionCollections) {
collection->readSettings(&group);
}
}
2014-11-13 01:04:59 +02:00
if (d->actionTypes & KShortcutsEditor::GlobalAction) {
KConfigGroup group(config, "Global Shortcuts");
2014-11-13 01:04:59 +02:00
foreach (KActionCollection* collection, d->actionCollections) {
collection->importGlobalShortcuts(&group);
2014-11-13 01:04:59 +02:00
}
}
}
void KShortcutsEditor::exportConfiguration(KConfigBase *config) const
{
Q_ASSERT(config);
if (!config) {
return;
}
if (d->actionTypes & KShortcutsEditor::LocalAction) {
KConfigGroup group(config, "Shortcuts");
2014-11-13 01:04:59 +02:00
foreach (KActionCollection* collection, d->actionCollections) {
collection->writeSettings(&group, true);
2014-11-13 01:04:59 +02:00
}
}
if (d->actionTypes & KShortcutsEditor::GlobalAction) {
KConfigGroup group(config, "Global Shortcuts");
foreach (KActionCollection* collection, d->actionCollections) {
collection->exportGlobalShortcuts(&group, true);
}
}
2014-11-13 01:04:59 +02:00
}
void KShortcutsEditor::writeConfiguration(KConfigGroup *config) const
2014-11-13 01:04:59 +02:00
{
foreach (KActionCollection* collection, d->actionCollections) {
2014-11-13 01:04:59 +02:00
collection->writeSettings(config);
}
2014-11-13 01:04:59 +02:00
}
void KShortcutsEditor::commit()
{
// TODO: implement
2014-11-13 01:04:59 +02:00
}
void KShortcutsEditor::save()
{
writeConfiguration();
commit();
}
void KShortcutsEditor::undoChanges()
{
// TODO: implement
2014-11-13 01:04:59 +02:00
}
void KShortcutsEditor::allDefault()
{
// TODO: implement
2014-11-13 01:04:59 +02:00
}
#include "moc_kshortcutseditor.cpp"