File indexing completed on 2024-12-22 04:12:36

0001 /*
0002  * This file is part of the KDE project
0003  * SPDX-FileCopyrightText: 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "kis_key_input_editor.h"
0009 
0010 #include <QWidgetAction>
0011 #include <QMenu>
0012 #include <QTimer>
0013 
0014 #include "kis_icon_utils.h"
0015 
0016 #include "ui_kis_key_input_editor.h"
0017 
0018 class KisKeyInputEditor::Private
0019 {
0020 public:
0021     Private() { }
0022 
0023     Ui::KisKeyInputEditor *ui {nullptr};
0024 };
0025 
0026 KisKeyInputEditor::KisKeyInputEditor(QWidget *parent)
0027     : QPushButton(parent), d(new Private)
0028 {
0029     QWidget *popup = new QWidget();
0030 
0031     d->ui = new Ui::KisKeyInputEditor;
0032     d->ui->setupUi(popup);
0033 
0034     d->ui->clearKeysButton->setIcon(KisIconUtils::loadIcon("edit-clear"));
0035 
0036     QWidgetAction *action = new QWidgetAction(this);
0037     action->setDefaultWidget(popup);
0038 
0039     QMenu *menu = new QMenu(this);
0040     menu->addAction(action);
0041     setMenu(menu);
0042 
0043     QTimer::singleShot(0, this, SLOT(showMenu()));
0044 
0045     connect(d->ui->keysButton, SIGNAL(dataChanged()), SLOT(updateLabel()));
0046     connect(d->ui->clearKeysButton, SIGNAL(clicked(bool)), d->ui->keysButton, SLOT(clear()));
0047 }
0048 
0049 KisKeyInputEditor::~KisKeyInputEditor()
0050 {
0051     delete d->ui;
0052     delete d;
0053 }
0054 
0055 QList< Qt::Key > KisKeyInputEditor::keys() const
0056 {
0057     return d->ui->keysButton->keys();
0058 }
0059 
0060 void KisKeyInputEditor::setKeys(const QList< Qt::Key > &newKeys)
0061 {
0062     d->ui->keysButton->setKeys(newKeys);
0063     updateLabel();
0064 }
0065 
0066 void KisKeyInputEditor::updateLabel()
0067 {
0068     setText(KisShortcutConfiguration::keysToText(d->ui->keysButton->keys()));
0069 }