From 00cd6e3a8f2b3d91925ebcf07f0301ee0b6cca8f Mon Sep 17 00:00:00 2001 From: Tasos Sahanidis Date: Fri, 30 Nov 2018 01:01:47 +0200 Subject: [PATCH] Add --profile and --mode to GUI --- src/gui/kbwidget.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- src/gui/kbwidget.h | 2 ++ src/gui/main.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- src/gui/mainwindow.cpp | 6 +++++- src/gui/mainwindow.h | 4 ++++ 5 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/gui/kbwidget.cpp b/src/gui/kbwidget.cpp index 7cbf4427..63b1b050 100644 --- a/src/gui/kbwidget.cpp +++ b/src/gui/kbwidget.cpp @@ -11,6 +11,7 @@ #include "kbprofiledialog.h" #include "ui_kbwidget.h" #include "ui_kblightwidget.h" +#include "mainwindow.h" KbWidget::KbWidget(QWidget *parent, Kb *_device) : QWidget(parent), @@ -29,6 +30,9 @@ KbWidget::KbWidget(QWidget *parent, Kb *_device) : connect(device, SIGNAL(modeRenamed()), this, SLOT(modeChanged())); connect(device, SIGNAL(modeChanged(bool)), this, SLOT(modeChanged(bool))); + connect(static_cast(parent), &MainWindow::switchToProfileCLI, this, &KbWidget::switchToProfile); + connect(static_cast(parent), &MainWindow::switchToModeCLI, this, &KbWidget::switchToMode); + // Remove the Lighting and Performance tabs from non-RGB keyboards if(!device->features.contains("rgb")){ ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->lightTab)); @@ -447,8 +451,7 @@ void KbWidget::on_fwUpdButton_clicked(){ dialog.exec(); } -void KbWidget::on_layoutBox_activated(int index) -{ +void KbWidget::on_layoutBox_activated(int index){ // Can't use currentIndexChanged as it fires when the GUI is first drawn // before the layout has been initialised int idxLayout = ui->layoutBox->itemData(index).toInt(); @@ -460,3 +463,37 @@ void KbWidget::on_layoutBox_activated(int index) CkbSettings::set(layoutSettingsPath.arg(device->usbSerial), KeyMap::getLayout(layout)); device->layout(layout, true); } + +void KbWidget::switchToProfile(QString profile){ + int len = device->profiles().length(); + for(int i = 0; i < len; i++){ + KbProfile* loopProfile = device->profiles().at(i); + if(loopProfile->name() != profile) + continue; + + qDebug() << "Switching" << this->name() << "to" << profile; + device->setCurrentProfile(loopProfile); + + // Also update the dropdown + ui->profileBox->setCurrentIndex(i); + return; + } +} + +void KbWidget::switchToMode(QString mode){ + KbProfile* currentProfile = device->currentProfile(); + int len = currentProfile->modes().length(); + + for(int i = 0; i < len; i++){ + KbMode* loopMode = currentProfile->modes().at(i); + if(loopMode->name() != mode) + continue; + + qDebug() << "Switching" << this->name() << "to mode" << mode << "in" << currentProfile->name(); + device->setCurrentMode(loopMode); + + // Also update the list + ui->modesList->setCurrentRow(i); + return; + } +} diff --git a/src/gui/kbwidget.h b/src/gui/kbwidget.h index ff83ce3d..40fa2cb8 100644 --- a/src/gui/kbwidget.h +++ b/src/gui/kbwidget.h @@ -67,6 +67,8 @@ private slots: void on_tabWidget_currentChanged(int index); void on_fwUpdButton_clicked(); void on_layoutBox_activated(int index); + void switchToProfile(QString profile); + void switchToMode(QString mode); }; #endif // KBWIDGET_H diff --git a/src/gui/main.cpp b/src/gui/main.cpp index 8edbcb7f..e24f5895 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -28,7 +28,9 @@ enum CommandLineParseResults { CommandLineVersionRequested, CommandLineHelpRequested, CommandLineClose, - CommandLineBackground + CommandLineBackground, + CommandLineSwitchToProfile, + CommandLineSwitchToMode, }; /** @@ -57,6 +59,12 @@ CommandLineParseResults parseCommandLine(QCommandLineParser &parser, QString *er "Causes already running instance (if any) to exit."); parser.addOption(closeOption); + const QCommandLineOption switchToProfileOption(QStringList() << "p" << "profile", "Switches to the profile with the specified name on all devices.", "profile-name"); + parser.addOption(switchToProfileOption); + + const QCommandLineOption switchToModeOption(QStringList() << "m" << "mode", "Switches to the mode either in the current profile, or in the one specified by --profile", "mode-name"); + parser.addOption(switchToModeOption); + /* parse arguments */ if (!parser.parse(QCoreApplication::arguments())) { // set error, if there already are some @@ -85,6 +93,14 @@ CommandLineParseResults parseCommandLine(QCommandLineParser &parser, QString *er return CommandLineClose; } + if(parser.isSet(switchToProfileOption)) { + return CommandLineSwitchToProfile; + } + + if(parser.isSet(switchToModeOption)) { + return CommandLineSwitchToMode; + } + /* no explicit argument was passed */ return CommandLineOK; }; @@ -208,6 +224,29 @@ int main(int argc, char *argv[]){ else printf("ckb-next is not running.\n"); return 0; + case CommandLineSwitchToMode: + case CommandLineSwitchToProfile: + { + QString profileName = parser.value("profile").left(30); + QString modeName = parser.value("mode").left(30); + + if(profileName.isEmpty() && modeName.isEmpty()) { + printf("No valid profile or mode specified.\n"); + return 0; + } + + QString switchStr; + + if(!profileName.isEmpty()) + switchStr.append("SwitchToProfile: ").append(profileName).append("\nOption "); + if(!modeName.isEmpty()) + switchStr.append("SwitchToMode: ").append(modeName); + + if (!isRunning(switchStr.toUtf8().constData())) + printf("ckb-next is not running.\n"); + + return 0; + } case CommandLineBackground: // If launched with --background, launch in background background = 1; diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 815caeb3..2603c6dd 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -365,13 +365,17 @@ void MainWindow::timerTick(){ showWindow(); if(line.startsWith("Option ")){ // New ckb option line - QString option = line.split(" ")[1]; + QString option = line.section(' ', 1); if(option == "Open") // Bring to foreground showWindow(); else if(option == "Close") // Quit application qApp->quit(); + else if(option.startsWith("SwitchToProfile")) + emit switchToProfileCLI(option.section(' ', 1)); + else if(option.startsWith("SwitchToMode: ")) + emit switchToModeCLI(option.section(' ', 1)); } } } diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index 5c4c71ce..a244f813 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -74,6 +74,10 @@ public slots: void changeTrayIconToRGB(); void handleTrayScrollEvt(bool up); +signals: + void switchToProfileCLI(QString profile); + void switchToModeCLI(QString mode); + private slots: void addDevice(Kb* device); void removeDevice(Kb* device);