ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
settingswidget.cpp
Go to the documentation of this file.
1 #include <QTimer>
2 #include "animscript.h"
3 #include "autorun.h"
4 #include "ckbsettings.h"
5 #include "kb.h"
6 #include "layoutdialog.h"
7 #include "settingswidget.h"
8 #include "ui_settingswidget.h"
9 
10 extern QString devpath;
11 
12 // Modifier keys (OS-dependent)
13 static QStringList modKeys, modNames;
14 
16  QWidget(parent),
17  ui(new Ui::SettingsWidget)
18 {
19  ui->setupUi(this);
20  CkbSettings settings("Program");
21 
22  // Read keyboard layout
23  ui->layoutBox->addItems(KeyMap::layoutNames());
24  KeyMap::Layout layout = KeyMap::getLayout(settings.value("KbdLayout").toString());
25  if(layout == KeyMap::NO_LAYOUT){
26  // If the layout hasn't been set yet, show a dialog to let the user choose it
27  layout = KeyMap::locale();
28  QTimer::singleShot(1000, this, SLOT(showLayoutDialog())); // Run the function after a delay as the dialog may not appear correctly otherwise
29  settings.setValue("KbdLayout", KeyMap::getLayout(layout));
30  }
31  Kb::layout(layout);
32  ui->layoutBox->setCurrentIndex((int)layout);
33 
34  // Load modifier remap
36  if(modKeys.isEmpty()){
37  modKeys << "caps" << "lshift" << "lctrl" << "lalt" << "lwin";
38 #ifdef Q_OS_MACX
39  modNames << "Caps Lock" << "Shift" << "Control (⌃)" << "Option (⌥)" << "Command (⌘)";
40 #else
41  modNames << "Caps Lock" << "Shift" << "Control" << "Alt" << "Super";
42 #endif
43  }
44  foreach(const QString& name, modNames){
45  ui->capsBox->addItem(name);
46  ui->shiftBox->addItem(name);
47  ui->ctrlBox->addItem(name);
48  ui->altBox->addItem(name);
49  ui->winBox->addItem(name);
50  }
51 
52  int idx = modKeys.indexOf(KbBind::globalRemap("caps"));
53  if(idx >= 0)
54  ui->capsBox->setCurrentIndex(idx);
55  idx = modKeys.indexOf(KbBind::globalRemap("lshift"));
56  if(idx >= 0)
57  ui->shiftBox->setCurrentIndex(idx);
58  idx = modKeys.indexOf(KbBind::globalRemap("lctrl"));
59  if(idx >= 0)
60  ui->ctrlBox->setCurrentIndex(idx);
61  idx = modKeys.indexOf(KbBind::globalRemap("lwin"));
62  if(idx >= 0)
63  ui->winBox->setCurrentIndex(idx);
64  idx = modKeys.indexOf(KbBind::globalRemap("lalt"));
65  if(idx >= 0)
66  ui->altBox->setCurrentIndex(idx);
67 #ifdef Q_OS_MACX
68  if(settings.value("osxCmdSwap").toBool()){
69  // ckb <0.0.41
70  settings.remove("osxCmdSwap");
71  ui->ctrlBox->setCurrentIndex(modKeys.indexOf("lwin"));
72  ui->winBox->setCurrentIndex(modKeys.indexOf("lctrl"));
74  }
75 #endif
76 
77  // Read auto update settings
78  ui->autoFWBox->setChecked(!settings.value("DisableAutoFWCheck").toBool());
79 
80  // Read auto run settings
81  if(!AutoRun::available())
82  ui->loginItemBox->hide();
83  else {
84  if(!AutoRun::once())
85  // If this is the first time running the app, enable auto run by default
87  ui->loginItemBox->setChecked(AutoRun::isEnabled());
88  }
89 
90  // Prepare extra settings
91  extra = new ExtraSettingsWidget(this);
92 }
93 
95  delete ui;
96 }
97 
99  LayoutDialog dialog(this);
100  dialog.exec();
101  // Set selected layout
102  ui->layoutBox->setCurrentIndex((int)dialog.selected());
103  on_layoutBox_activated((int)dialog.selected()); // Call activated() signal manually to trigger save
104 }
105 
107  extra->pollUpdates();
108 }
109 
110 void SettingsWidget::setStatus(const QString& text){
111  ui->devicesLabel->setText(text);
112 }
113 
114 void SettingsWidget::setVersion(const QString& version){
115  ui->versionLabel->setText(version);
116 }
117 
118 static inline QString right(const QString& left){
119  if(left[0] != 'l')
120  return left;
121  return "r" + left.mid(1);
122 }
123 
125  QHash<QString, QString> newMods;
126  newMods["caps"] = modKeys[ui->capsBox->currentIndex()];
127  newMods["lshift"] = modKeys[ui->shiftBox->currentIndex()];
128  newMods["rshift"] = right(modKeys[ui->shiftBox->currentIndex()]);
129  newMods["lctrl"] = modKeys[ui->ctrlBox->currentIndex()];
130  newMods["rctrl"] = right(modKeys[ui->ctrlBox->currentIndex()]);
131  newMods["lalt"] = modKeys[ui->altBox->currentIndex()];
132  newMods["ralt"] = right(modKeys[ui->altBox->currentIndex()]);
133  newMods["lwin"] = modKeys[ui->winBox->currentIndex()];
134  newMods["rwin"] = right(modKeys[ui->winBox->currentIndex()]);
135  KbBind::setGlobalRemap(newMods);
137 }
138 
140  qApp->quit();
141 }
142 
144  updateModifiers();
145 }
146 
148  updateModifiers();
149 }
150 
152  updateModifiers();
153 }
154 
156  updateModifiers();
157 }
158 
160  updateModifiers();
161 }
162 
164  CkbSettings::set("Program/DisableAutoFWCheck", !checked);
165 }
166 
168  if(checked)
169  AutoRun::enable();
170  else
172 }
173 
175  KeyMap::Layout layout = (KeyMap::Layout)index;
176  CkbSettings::set("Program/KbdLayout", KeyMap::getLayout(layout));
177  Kb::layout(layout);
178 }
179 
181  extra->exec();
182 }
Layout
Definition: keymap.h:70
static KeyMap::Layout layout()
Definition: kb.h:30
static bool available()
Definition: autorun.cpp:20
void setValue(const QString &key, const QVariant &value)
ExtraSettingsWidget * extra
static void set(const QString &key, const QVariant &value)
void on_extraButton_clicked()
static QString globalRemap(const QString &key)
Definition: kbbind.cpp:98
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
void on_pushButton_clicked()
static void setGlobalRemap(const QHash< QString, QString > keyToActual)
Definition: kbbind.cpp:104
static QStringList layoutNames()
Definition: keymap.cpp:757
static void disable()
Definition: autorun.cpp:53
KeyMap::Layout selected() const
void setStatus(const QString &text)
Ui::SettingsWidget * ui
static Layout getLayout(const QString &name)
Definition: keymap.cpp:677
void on_altBox_activated(int index)
void on_capsBox_activated(int index)
void on_autoFWBox_clicked(bool checked)
void on_ctrlBox_activated(int index)
double left
Definition: main.c:51
static QString right(const QString &left)
void setVersion(const QString &version)
void setupUi(QWidget *SettingsWidget)
static QStringList modNames
QCheckBox * loginItemBox
void remove(const QString &key)
static void saveGlobalRemap()
Definition: kbbind.cpp:124
void on_loginItemBox_clicked(bool checked)
static bool isEnabled()
Definition: autorun.cpp:33
static void enable()
Definition: autorun.cpp:42
static Layout locale()
Definition: keymap.cpp:641
void on_layoutBox_activated(int index)
QString devpath
Device path base ("/dev/input/ckb" or "/var/run/ckb")
Definition: kbmanager.cpp:4
void on_shiftBox_activated(int index)
void on_winBox_activated(int index)
static bool once()
Definition: autorun.cpp:29
static void loadGlobalRemap()
Definition: kbbind.cpp:116
static QStringList modKeys
SettingsWidget(QWidget *parent=0)