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

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 "generalpagewidget.h"
0021 #include "ui_generalpagewidget.h"
0022 
0023 #include "profilemanagement.h"
0024 
0025 // common
0026 #include "dbustabletinterface.h"
0027 #include "globalactions.h"
0028 #include "tabletinfo.h"
0029 
0030 // stdlib
0031 #include <memory>
0032 
0033 // KDE includes
0034 #include <KShortcutsEditor>
0035 
0036 // Qt includes
0037 #include <QDBusInterface>
0038 #include <QDBusReply>
0039 #include <QInputDialog>
0040 #include <QListWidget>
0041 #include <QListWidgetItem>
0042 #include <QStringList>
0043 
0044 using namespace Wacom;
0045 
0046 GeneralPageWidget::GeneralPageWidget(QWidget *parent)
0047     : QWidget(parent)
0048     , ui(new Ui::GeneralPageWidget)
0049 {
0050     ui->setupUi(this);
0051 
0052     // if someone adds another action also add it to kded/tabletdeamon.cpp
0053     _actionCollection = new GlobalActions(true, this);
0054 
0055     _shortcutEditor = new KShortcutsEditor(this, KShortcutsEditor::GlobalAction);
0056     _shortcutEditor->addCollection(_actionCollection, i18n("Wacom Tablet Settings"));
0057 
0058     ui->shortcutGroupBox->layout()->addWidget(_shortcutEditor);
0059 
0060     // setup icons in the profilerotation list box
0061     ui->pbAddToRotationList->setIcon(QIcon::fromTheme(QLatin1String("list-add")));
0062     ui->pbRemoveFromRotationList->setIcon(QIcon::fromTheme(QLatin1String("list-remove")));
0063     ui->pbUp->setIcon(QIcon::fromTheme(QLatin1String("arrow-up")));
0064     ui->pbDown->setIcon(QIcon::fromTheme(QLatin1String("arrow-down")));
0065 
0066     connect(_shortcutEditor, SIGNAL(keyChange()), this, SLOT(profileChanged()));
0067 }
0068 
0069 GeneralPageWidget::~GeneralPageWidget()
0070 {
0071     delete ui;
0072     delete _actionCollection;
0073     delete _shortcutEditor;
0074 }
0075 
0076 void GeneralPageWidget::setTabletId(const QString &tabletId)
0077 {
0078     _tabletId = tabletId;
0079 }
0080 
0081 void GeneralPageWidget::saveToProfile()
0082 {
0083     QList<QListWidgetItem *> items = ui->lwRotationList->findItems(QLatin1String("*"), Qt::MatchWrap | Qt::MatchWildcard);
0084     QStringList newRotationList;
0085     foreach (QListWidgetItem *item, items) {
0086         newRotationList.append(item->text());
0087     }
0088 
0089     DBusTabletInterface::instance().setProfileRotationList(_tabletId, newRotationList);
0090 
0091     _shortcutEditor->save();
0092 }
0093 
0094 void GeneralPageWidget::loadFromProfile()
0095 {
0096 }
0097 
0098 void GeneralPageWidget::profileChanged()
0099 {
0100     emit changed();
0101 }
0102 
0103 void GeneralPageWidget::reloadWidget()
0104 {
0105     // get information via DBus
0106     QDBusReply<QString> deviceName = DBusTabletInterface::instance().getInformation(_tabletId, TabletInfo::TabletName.key());
0107 
0108     // load rotation profile list based on current tablet
0109     QDBusReply<QStringList> rotationList = DBusTabletInterface::instance().getProfileRotationList(_tabletId);
0110     ui->lwRotationList->clear();
0111     ui->lwRotationList->addItems(rotationList);
0112 }
0113 
0114 void GeneralPageWidget::profileUp()
0115 {
0116     QListWidgetItem *curItem = ui->lwRotationList->currentItem();
0117     if (curItem) {
0118         int curRow = ui->lwRotationList->row(curItem);
0119         int nextRow = curRow - 1;
0120         if (nextRow >= 0) {
0121             QListWidgetItem *curItem = ui->lwRotationList->takeItem(curRow);
0122             ui->lwRotationList->insertItem(nextRow, curItem->text());
0123             ui->lwRotationList->setCurrentRow(nextRow);
0124         }
0125         emit changed();
0126     }
0127 }
0128 
0129 void GeneralPageWidget::profileDown()
0130 {
0131     QListWidgetItem *curItem = ui->lwRotationList->currentItem();
0132     if (curItem) {
0133         int curRow = ui->lwRotationList->row(curItem);
0134         curItem = ui->lwRotationList->takeItem(curRow);
0135         ui->lwRotationList->insertItem(curRow + 1, curItem->text());
0136         ui->lwRotationList->setCurrentRow(curRow + 1);
0137 
0138         emit changed();
0139     }
0140 }
0141 
0142 void GeneralPageWidget::profileAdd()
0143 {
0144     bool ok = false;
0145     QString item = QInputDialog::getItem(this,
0146                                          i18n("Profile List"),
0147                                          i18n("Select the Profile you want to add:"),
0148                                          ProfileManagement::instance().availableProfiles(),
0149                                          0,
0150                                          false,
0151                                          &ok);
0152     if (ok && !item.isEmpty()) {
0153         ui->lwRotationList->addItem(item);
0154 
0155         emit changed();
0156     }
0157 }
0158 
0159 void GeneralPageWidget::profileRemove()
0160 {
0161     QListWidgetItem *curItem = ui->lwRotationList->currentItem();
0162     if (curItem) {
0163         ui->lwRotationList->removeItemWidget(curItem);
0164         delete curItem;
0165 
0166         emit changed();
0167     }
0168 }
0169 
0170 #include "moc_generalpagewidget.cpp"