File indexing completed on 2025-01-26 05:09:30

0001 /*
0002  * This file is part of the KDE wacomtablet project. For copyright
0003  * information and license terms see the AUTHORS and COPYING files
0004  * in the top-level directory of this distribution.
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 #include "buttonactionselectorwidget.h"
0021 #include "ui_buttonactionselectorwidget.h"
0022 
0023 #include "buttonactionselectiondialog.h"
0024 
0025 #include <QIcon>
0026 
0027 using namespace Wacom;
0028 
0029 namespace Wacom
0030 {
0031 class ButtonActionSelectorWidgetPrivate
0032 {
0033 public:
0034     ButtonActionSelectorWidgetPrivate()
0035         : ui(new Ui::ButtonActionSelectorWidget)
0036     {
0037     }
0038     ~ButtonActionSelectorWidgetPrivate()
0039     {
0040         delete ui;
0041     }
0042 
0043     ButtonShortcut shortcut;
0044     Ui::ButtonActionSelectorWidget *ui;
0045 };
0046 }
0047 
0048 ButtonActionSelectorWidget::ButtonActionSelectorWidget(QWidget *parent)
0049     : QWidget(parent)
0050     , d_ptr(new ButtonActionSelectorWidgetPrivate)
0051 {
0052     setupUi();
0053 }
0054 
0055 ButtonActionSelectorWidget::~ButtonActionSelectorWidget()
0056 {
0057     delete this->d_ptr;
0058 }
0059 
0060 const ButtonShortcut &ButtonActionSelectorWidget::getShortcut() const
0061 {
0062     Q_D(const ButtonActionSelectorWidget);
0063     return d->shortcut;
0064 }
0065 
0066 void ButtonActionSelectorWidget::setShortcut(const ButtonShortcut &shortcut)
0067 {
0068     Q_D(ButtonActionSelectorWidget);
0069     d->shortcut = shortcut;
0070 
0071     updateSelectorButton(shortcut);
0072     updateActionName(shortcut);
0073 }
0074 
0075 void ButtonActionSelectorWidget::onButtonActionSelectorClicked()
0076 {
0077     Q_D(ButtonActionSelectorWidget);
0078 
0079     ButtonActionSelectionDialog selectorDialog;
0080 
0081     selectorDialog.setShortcut(d->shortcut);
0082     selectorDialog.exec();
0083 
0084     ButtonShortcut selectedShortcut(selectorDialog.getShortcut());
0085 
0086     if (d->shortcut != selectedShortcut) {
0087         setShortcut(selectedShortcut);
0088         emit buttonActionChanged(d->shortcut);
0089     }
0090 }
0091 
0092 void ButtonActionSelectorWidget::onLineEditSelectionChanged()
0093 {
0094     Q_D(ButtonActionSelectorWidget);
0095     d->ui->actionNameDisplayWidget->deselect();
0096 }
0097 
0098 void ButtonActionSelectorWidget::setupUi()
0099 {
0100     Q_D(ButtonActionSelectorWidget);
0101 
0102     d->ui->setupUi(this);
0103 
0104     connect(d->ui->actionSelectionButton, SIGNAL(clicked(bool)), this, SLOT(onButtonActionSelectorClicked()));
0105     connect(d->ui->actionNameDisplayWidget, SIGNAL(selectionChanged()), this, SLOT(onLineEditSelectionChanged()));
0106     connect(d->ui->actionNameDisplayWidget, SIGNAL(mousePressed()), this, SLOT(onButtonActionSelectorClicked()));
0107 
0108     setShortcut(ButtonShortcut());
0109 }
0110 
0111 void ButtonActionSelectorWidget::updateActionName(const ButtonShortcut &shortcut)
0112 {
0113     Q_D(ButtonActionSelectorWidget);
0114 
0115     QString displayName = shortcut.toDisplayString();
0116 
0117     d->ui->actionNameDisplayWidget->blockSignals(true);
0118     d->ui->actionNameDisplayWidget->setText(displayName);
0119     d->ui->actionNameDisplayWidget->blockSignals(false);
0120 }
0121 
0122 void ButtonActionSelectorWidget::updateSelectorButton(const ButtonShortcut &shortcut)
0123 {
0124     Q_D(ButtonActionSelectorWidget);
0125 
0126     if (shortcut.isButton()) {
0127         // shortcut is a mouse button click
0128         d->ui->actionSelectionButton->setIcon(QIcon::fromTheme(QLatin1String("input-mouse")));
0129 
0130     } else if (shortcut.isModifier() || shortcut.isKeystroke()) {
0131         // shortcut is a keyboard action
0132         d->ui->actionSelectionButton->setIcon(QIcon::fromTheme(QLatin1String("input-keyboard")));
0133 
0134     } else {
0135         // shortcut not set or unknown
0136         d->ui->actionSelectionButton->setIcon(QIcon::fromTheme(QLatin1String("configure")));
0137     }
0138 }
0139 
0140 #include "moc_buttonactionselectorwidget.cpp"