1 ////////////////////////////////////////
3 ////////////////////////////////////////
7 JrugrConfig::JrugrConfig (QWidget
*parent
) : QDialog(parent
) {
8 xkb_conf
.setupUi(this);
9 // initialize the binding between list and stackedWidget
10 xkb_conf
.listWidget
->setCurrentRow(0);
11 QSettings
jrugr(QDir::homePath()+"/.config/jrugr.cfg", QSettings::IniFormat
, this);
12 jrugr
.beginGroup("Style");
13 theme
= jrugr
.value("path").toString();
14 ico_path
= theme
+"/language/";
15 jrugr
.endGroup(); //Style
16 qDebug() << "Theme " << theme
;
17 qDebug() << "Icons " << ico_path
;
18 xkbConf
= X11tools::loadXKBconf();
20 connect(xkb_conf
.buttonBox
, SIGNAL(rejected()), this, SLOT(close()));
21 connect(xkb_conf
.buttonBox
, SIGNAL(accepted()), SLOT(apply()));
22 connect(xkb_conf
.radioButton
, SIGNAL(clicked(bool)), SLOT(statSelect(bool)));
23 connect(xkb_conf
.radioButton_2
, SIGNAL(clicked(bool)), SLOT(statSelect(bool)));
24 connect(xkb_conf
.radioButton_3
, SIGNAL(clicked(bool)), SLOT(statSelect(bool)));
26 xkb_conf
.btnAdd
->setEnabled(false);
27 xkb_conf
.btnAdd
->setIcon(QIcon::fromTheme(QString("arrow-right"), QIcon(theme
+"/add.png")));
29 xkb_conf
.btnRemove
->setEnabled(false);
30 xkb_conf
.btnRemove
->setIcon(QIcon::fromTheme(QString("arrow-left"), QIcon(theme
+"/rem.png")));
32 xkb_conf
.btnUp
->setEnabled(false);
33 xkb_conf
.btnUp
->setIcon(QIcon::fromTheme(QString("arrow-up"), QIcon(theme
+"/up.png")));
35 xkb_conf
.btnDown
->setEnabled(false);
36 xkb_conf
.btnDown
->setIcon(QIcon::fromTheme(QString("arrow-down"), QIcon(theme
+"/down.png")));
38 if (!setStat()) return;
40 xkbConf
->lockKeys
= true;
44 JrugrConfig::~JrugrConfig () {
49 void JrugrConfig::apply () {
50 X11tools::saveXKBconf(xkbConf
);
52 if (xkb_conf
.stackedWidget
->currentIndex() == 0 && xkbConf
->status
!= ONLY_INDICATION
) {
53 QStringList parm
= xkb_conf
.editCmdLine
->text().split(" ");
57 qDebug() << "parm:" << parm
;
58 qpres
= 0;//QProcess::execute("setxkbmap", parm);
59 qDebug() << "Set XKB result" << qpres
;
60 } else if (xkb_conf
.stackedWidget
->currentIndex() == 2 && xkbConf
->status
!= ONLY_INDICATION
) {
61 QStringList parm
= xkb_conf
.editCmdLineOpt
->text().split(" ");
65 qDebug() << "parm:" << parm
;
66 qpres
= 0;//QProcess::execute("setxkbmap", parm);
67 qDebug() << "Set XKB result" << qpres
;
75 void JrugrConfig::statSelect (bool check
) {
78 if (xkb_conf
.radioButton
->isChecked()) xkbConf
->status
= 2;
79 if (xkb_conf
.radioButton_2
->isChecked()) xkbConf
->status
= 1;
80 if (xkb_conf
.radioButton_3
->isChecked()) xkbConf
->status
= 0;
81 if (!setStat()) return;
86 void JrugrConfig::initXKBTab () {
87 connect(xkb_conf
.btnAdd
, SIGNAL(clicked()), SLOT(addLayout()));
88 connect(xkb_conf
.btnRemove
, SIGNAL(clicked()), SLOT(delLayout()));
91 QHashIterator
<QString
, QString
> i(curRule
->models
);
92 while (i
.hasNext()) { i
.next(); listModels
<< i
.value(); }
93 // set avaleble language
94 srcLayoutModel
= new SrcLayoutModel(curRule
, ico_path
, this);
96 dstLayoutModel
= new DstLayoutModel(curRule
, xkbConf
, ico_path
, this);
97 xkbOptionsModel
= new XkbOptionsModel(curRule
, xkbConf
, this);
98 xkb_conf
.comboModel
->addItems(listModels
);
99 xkb_conf
.comboModel
->setCurrentIndex(listModels
.indexOf(curRule
->models
.value(xkbConf
->model
)));
100 xkb_conf
.srcTableView
->setModel(srcLayoutModel
);
101 xkb_conf
.srcTableView
->resizeColumnToContents(0);
102 xkb_conf
.dstTableView
->setModel(dstLayoutModel
);
103 xkb_conf
.dstTableView
->resizeColumnToContents(0);
104 xkb_conf
.xkbOptionsTreeView
->setModel(xkbOptionsModel
);
105 xkb_conf
.xkbOptionsTreeView
->header()->hide();
106 xkb_conf
.xkbOptionsTreeView
->expandAll();
108 for (int f
= 0; f
< xkbConf
->options
.count(); ++f
) {
109 if (xkbConf
->options
[f
].contains("grp:")) xkb_conf
.btnXkbShortcut
->setText(tr("Defined"));
110 if (xkbConf
->options
[f
].contains("lv3:")) xkb_conf
.btnXkbShortcut3d
->setText(tr("Defined"));
113 switch (xkbConf
->switching
) {
114 case GLOBAL_LAYOUT
: xkb_conf
.rdBtnSwitchGlobal
->setChecked(true); break;
115 case APP_LAYOUT
: xkb_conf
.rdBtnSwitchPerApp
->setChecked(true); break;
116 case WIN_LAYOUT
: xkb_conf
.rdBtnSwitchPerWin
->setChecked(true); break;
119 connect(xkb_conf
.srcTableView
, SIGNAL(clicked (const QModelIndex
&)), SLOT(srcClick(QModelIndex
)));
120 connect(xkb_conf
.dstTableView
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)), SLOT(dstClick()));
121 connect(xkb_conf
.comboModel
, SIGNAL(currentIndexChanged (int)) , SLOT(comboModelCh(int)));
122 connect(xkb_conf
.comboVariant
, SIGNAL(currentIndexChanged (int)), SLOT(comboVariantCh(int)));
123 connect(xkb_conf
.btnUp
, SIGNAL(clicked()), SLOT(layoutUp()));
124 connect(xkb_conf
.btnDown
, SIGNAL(clicked()), SLOT(layoutDown()));
125 connect(xkbOptionsModel
, SIGNAL(dataChanged(const QModelIndex
&, const QModelIndex
&)), SLOT(xkbOptionsChanged(const QModelIndex
&, const QModelIndex
&)));
126 connect( xkb_conf
.btnXkbShortcut
, SIGNAL(clicked()), SLOT(xkbShortcutPressed()));
127 connect(xkb_conf
.btnXkbShortcut3d
, SIGNAL(clicked()), SLOT(xkbShortcut3dPressed()));
130 updateOptionsCommand();
134 bool JrugrConfig::setStat () {
135 switch (xkbConf
->status
) {
137 xkb_conf
.grpIndicatorOptions
->setEnabled(false);
138 xkb_conf
.grpLayouts
->setEnabled(false);
139 xkb_conf
.page_2
->setEnabled(false);
140 xkb_conf
.page_3
->setEnabled(false);
141 xkb_conf
.radioButton
->setChecked(false);
142 xkb_conf
.radioButton_2
->setChecked(false);
143 xkb_conf
.radioButton_3
->setChecked(true);
145 case ONLY_INDICATION
:
146 xkb_conf
.grpIndicatorOptions
->setEnabled(true);
147 xkb_conf
.grpLayouts
->setEnabled(false);
148 xkb_conf
.page_2
->setEnabled(false);
149 xkb_conf
.page_3
->setEnabled(false);
150 xkb_conf
.radioButton
->setChecked(false);
151 xkb_conf
.radioButton_2
->setChecked(true);
152 xkb_conf
.radioButton_3
->setChecked(false);
153 xkb_conf
.chkShowFlag
->setChecked(xkbConf
->showFlag
);
154 xkb_conf
.chkShowSingle
->setChecked(xkbConf
->showSingle
);
155 connect(xkb_conf
.chkShowFlag
, SIGNAL(clicked()), SLOT(setFlagUse()));
156 connect(xkb_conf
.chkShowSingle
, SIGNAL(clicked()), SLOT(setSinglShow()));
159 xkb_conf
.grpIndicatorOptions
->setEnabled(true);
160 xkb_conf
.grpLayouts
->setEnabled(true);
161 xkb_conf
.page_2
->setEnabled(true);//not work yet
162 xkb_conf
.page_3
->setEnabled(true);
163 xkb_conf
.radioButton
->setChecked(true);
164 xkb_conf
.radioButton_2
->setChecked(false);
165 xkb_conf
.radioButton_3
->setChecked(false);
166 xkb_conf
.chkShowFlag
->setChecked(xkbConf
->showFlag
);
167 xkb_conf
.chkShowSingle
->setChecked(xkbConf
->showSingle
);
168 connect(xkb_conf
.chkShowFlag
, SIGNAL(clicked()), SLOT(setFlagUse()));
169 connect(xkb_conf
.chkShowSingle
, SIGNAL(clicked()), SLOT(setSinglShow()));
176 void JrugrConfig::setCmdLine () {
177 QString cmdLine
= "setxkbmap -model "+xkbConf
->model
;
179 cmdLine
+= " -layout ";
180 for (int f
= 0;f
< xkbConf
->layouts
.size(); ++f
) {
181 cmdLine
+= xkbConf
->layouts
[f
].layout
;
182 if (f
< xkbConf
->layouts
.size()-1) cmdLine
+= ',';
185 cmdLine
+= " -variant ";
186 for (int f
= 0; f
< xkbConf
->layouts
.size(); ++f
) {
187 cmdLine
+= xkbConf
->layouts
[f
].variant
;
188 if (f
< xkbConf
->layouts
.size()-1) cmdLine
+= ',';
190 xkb_conf
.editCmdLine
->setText(cmdLine
);
194 void JrugrConfig::addLayout () {
196 QItemSelectionModel
*selectionModel
= xkb_conf
.srcTableView
->selectionModel();
198 if (selectionModel
== NULL
|| !selectionModel
->hasSelection() || xkbConf
->layouts
.size() >= 4) return;
199 QModelIndexList selected
= selectionModel
->selectedRows();
200 qDebug() << "selected : " << selected
;
201 QString layout
= srcLayoutModel
->getLayoutAt(selected
[0].row());
202 if (layout
.isEmpty() || xkbConf
->layouts
.contains(layout
)) return;
204 xkbConf
->layouts
.append(lu
);
205 dstLayoutModel
->reset();
206 xkb_conf
.dstTableView
->update();
211 void JrugrConfig::delLayout () {
212 QItemSelectionModel
*selectionModel
= xkb_conf
.dstTableView
->selectionModel();
213 if (selectionModel
== NULL
|| !selectionModel
->hasSelection()|| xkbConf
->layouts
.size() == 0) return;
214 QModelIndexList selected
= selectionModel
->selectedRows();
215 xkbConf
->layouts
.removeAt(selected
[0].row());
216 dstLayoutModel
->reset();
217 xkb_conf
.dstTableView
->update();
222 void JrugrConfig::srcClick (QModelIndex index
) {
224 xkb_conf
.btnAdd
->setEnabled(true);
228 void JrugrConfig::dstClick () {
229 int row
= getSelectedDstLayout();
230 xkb_conf
.btnRemove
->setEnabled(row
!=-1);
231 int el_count
= xkb_conf
.comboVariant
->count();
232 for (int f
= el_count
; f
> 0; --f
) xkb_conf
.comboVariant
->removeItem(f
);
233 //xkb_conf.comboVariant->clear(); some sheet seg fail in use
234 xkb_conf
.comboVariant
->setEnabled(row
!=-1);
235 xkb_conf
.btnUp
->setEnabled(row
!=-1);
236 xkb_conf
.btnDown
->setEnabled(row
!=-1);
237 if (row
== -1) return;
238 QString layout
= xkbConf
->layouts
[row
].layout
;
240 variants
= X11tools::getVariants(layout
, X11tools::findX11Dir());
241 qDebug() << "JrugrConfig:addItem";
242 if (xkb_conf
.comboVariant
->count() == 0) {
243 xkb_conf
.comboVariant
->addItem(tr("Default"), "Default");
244 xkb_conf
.comboVariant
->setItemData(0, tr("Default"), Qt::ToolTipRole
);
245 xkb_conf
.comboVariant
->setItemText(0, tr("Default"));
247 if (variants
.count() > 0) {
248 for (int f
= 0; f
< variants
.count(); ++f
) {
249 xkb_conf
.comboVariant
->addItem(variants
[f
].description
, variants
[f
].name
);
250 xkb_conf
.comboVariant
->setItemData(xkb_conf
.comboVariant
->count()-1, variants
[f
].description
, Qt::ToolTipRole
);
252 QString variant
= xkbConf
->layouts
[row
].variant
;
253 if (variant
!= NULL
&& !variant
.isEmpty()) {
254 int idx
= xkb_conf
.comboVariant
->findData(variant
);
255 xkb_conf
.comboVariant
->setCurrentIndex(idx
);
257 xkb_conf
.comboVariant
->setCurrentIndex(0);
263 bool JrugrConfig::loadRules () {
264 QString x11dir
= X11tools::findX11Dir();
265 if (x11dir
.isNull() || x11dir
.isEmpty()) return false;
266 QString rulesFile
= X11tools::findXkbRulesFile(x11dir
, QX11Info::display());
267 if (rulesFile
.isNull() || rulesFile
.isEmpty()) return false;
268 curRule
= X11tools::loadRules(rulesFile
, false);
269 if (curRule
== NULL
) return false;
274 void JrugrConfig::comboModelCh (int index
) {
275 xkbConf
->model
= curRule
->models
.key(xkb_conf
.comboModel
->itemText(index
));
280 void JrugrConfig::comboVariantCh (int index
) {
281 if (index
== 0) xkbConf
->layouts
[getSelectedDstLayout()].variant
= "";
282 else if (index
< variants
.count()) xkbConf
->layouts
[getSelectedDstLayout()].variant
= variants
.at(index
-1).name
; // it sometimes crashed here with "index out of bounds"
287 int JrugrConfig::getSelectedDstLayout () {
288 QItemSelectionModel
*selectionModel
= xkb_conf
.dstTableView
->selectionModel();
289 if (selectionModel
== NULL
|| !selectionModel
->hasSelection()) return -1;
290 QModelIndexList selected
= selectionModel
->selectedRows();
291 int row
= selected
.count() > 0 ? selected
[0].row() : -1;
296 void JrugrConfig::setFlagUse () {
297 xkbConf
->showFlag
= xkb_conf
.chkShowFlag
->isChecked();
301 void JrugrConfig::setSinglShow () {
302 xkbConf
->showSingle
= xkb_conf
.chkShowSingle
->isChecked();
306 void JrugrConfig::layoutUp () {
307 int row
= getSelectedDstLayout();
309 LayoutUnit lu
= xkbConf
->layouts
[row
-1];
310 xkbConf
->layouts
[row
-1] = xkbConf
->layouts
[row
];
311 xkbConf
->layouts
[row
] = lu
;
312 dstLayoutModel
->reset();
313 xkb_conf
.dstTableView
->update();
319 void JrugrConfig::layoutDown () {
320 int row
= getSelectedDstLayout();
321 if (row
< xkbConf
->layouts
.count()-1 && row
> -1) {
322 LayoutUnit lu
= xkbConf
->layouts
[row
+1];
323 xkbConf
->layouts
[row
+1] = xkbConf
->layouts
[row
];
324 xkbConf
->layouts
[row
] = lu
;
325 dstLayoutModel
->reset();
331 void JrugrConfig::updateOptionsCommand () {
332 QString cmd
= "setxkbmap";
333 if (xkb_conf
.checkResetOld
->isChecked()) cmd
+= " -option";
334 if (!xkbConf
->options
.empty()) {
336 cmd
+= xkbConf
->options
.join(",");
338 xkb_conf
.editCmdLineOpt
->setText(cmd
);
342 void JrugrConfig::xkbOptionsChanged (const QModelIndex
&topLeft
, const QModelIndex
&bottomRight
) {
344 Q_UNUSED(bottomRight
)
345 updateOptionsCommand();
349 void JrugrConfig::closeEvent (QCloseEvent
*event
) {
350 xkbConf
->lockKeys
= false;
356 void JrugrConfig::xkbShortcutPressed () {
357 xkb_conf
.stackedWidget
->setCurrentIndex(2);
358 xkbOptionsModel
->gotoGroup("grp", xkb_conf
.xkbOptionsTreeView
);
362 void JrugrConfig::xkbShortcut3dPressed () {
363 xkb_conf
.stackedWidget
->setCurrentIndex(2);
364 xkbOptionsModel
->gotoGroup("lv3", xkb_conf
.xkbOptionsTreeView
);
368 void JrugrConfig::statSwitching (bool check
) {
370 if (xkb_conf
.rdBtnSwitchGlobal
->isChecked()) xkbConf
->switching
= 2;
371 if (xkb_conf
.rdBtnSwitchPerApp
->isChecked()) xkbConf
->switching
= 1;
372 if (xkb_conf
.rdBtnSwitchPerWin
->isChecked()) xkbConf
->switching
= 0;
376 void JrugrConfig::getHotKeys (XEvent
*event
) {
377 XKeyEvent
*keys
= (XKeyEvent
*)event
;
379 switch (XKeycodeToKeysym(QX11Info::display(), keys
->keycode
, 0)) {
396 key
= QString(XKeysymToString(XKeycodeToKeysym(QX11Info::display(), keys
->keycode
, 0)));
397 if (key
.count() < 2) key
= key
.toUpper();
399 if (!mods
.isEmpty()) hot_keys
=mods
+"+"+key
;
400 else if (key
.count() > 1) hot_keys
= key
;
404 void JrugrConfig::clearHotKeys () {
405 qDebug() << "JrugrConfig:: Current mods " << mods
;
406 if (mods
.isEmpty()) return;
407 qDebug() << "JrugrConfig:: Current key " << key
;
408 if (!key
.isEmpty()) return;