12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 * GNU General Public License for more details. |
13 * GNU General Public License for more details. |
14 * |
14 * |
15 * You should have received a copy of the GNU General Public License |
15 * You should have received a copy of the GNU General Public License |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 * ===================================================================== |
|
18 * |
|
19 * configDialog.cxx: Settings dialog and everything related to it. |
|
20 * Actual configuration core is in config.cxx. |
|
21 */ |
17 */ |
22 |
18 |
23 #include <QGridLayout> |
19 #include <QGridLayout> |
24 #include <QFileDialog> |
20 #include <QFileDialog> |
25 #include <QColorDialog> |
21 #include <QColorDialog> |
45 #ifdef _WIN32 |
41 #ifdef _WIN32 |
46 "Applications (*.exe)(*.exe);;" |
42 "Applications (*.exe)(*.exe);;" |
47 #endif |
43 #endif |
48 "All files (*.*)(*.*)"; |
44 "All files (*.*)(*.*)"; |
49 |
45 |
50 ShortcutListItem::ShortcutListItem (QListWidget* view, int type) : |
|
51 QListWidgetItem (view, type) {} |
|
52 |
|
53 QAction* ShortcutListItem::action() const |
|
54 { |
|
55 return m_action; |
|
56 } |
|
57 |
|
58 void ShortcutListItem::setAction (QAction* action) |
|
59 { |
|
60 m_action = action; |
|
61 } |
|
62 |
|
63 QKeySequence ShortcutListItem::sequence() const |
|
64 { |
|
65 return m_sequence; |
|
66 } |
|
67 |
|
68 void ShortcutListItem::setSequence (const QKeySequence& sequence) |
|
69 { |
|
70 m_sequence = sequence; |
|
71 } |
|
72 |
|
73 ConfigDialog::ConfigDialog (QWidget* parent, ConfigDialog::Tab defaulttab, Qt::WindowFlags f) : |
46 ConfigDialog::ConfigDialog (QWidget* parent, ConfigDialog::Tab defaulttab, Qt::WindowFlags f) : |
74 QDialog (parent, f), |
47 QDialog (parent, f), |
75 HierarchyElement (parent), |
48 HierarchyElement (parent), |
76 ui (*new Ui_ConfigDialog), |
49 ui (*new Ui_ConfigDialog), |
77 librariesModel {new LibrariesModel {this->libraries, this}}, |
50 librariesModel {new LibrariesModel {this->libraries, this}}, |
78 libraries {config::libraries()} |
51 libraries {config::libraries()}, |
|
52 shortcuts {m_window}, |
|
53 shortcutsDelegate {this} |
79 { |
54 { |
80 ui.setupUi (this); |
55 ui.setupUi (this); |
81 ui.librariesView->setModel(this->librariesModel); |
56 ui.librariesView->setModel(this->librariesModel); |
|
57 ui.shortcutsList->setModel(&shortcuts); |
|
58 ui.shortcutsList->setItemDelegateForColumn(ShortcutsModel::KeySequenceColumn, &shortcutsDelegate); |
82 |
59 |
83 // Set defaults |
60 // Set defaults |
84 applyToWidgetOptions([&](QWidget* widget, QString confname) |
61 applyToWidgetOptions([&](QWidget* widget, QString confname) |
85 { |
62 { |
86 QVariant value = ::settingsObject().value(confname, config::defaults().value(confname)); |
63 QVariant value = ::settingsObject().value(confname, config::defaults().value(confname)); |
120 { |
97 { |
121 print ("Unknown widget of type %1\n", widget->metaObject()->className()); |
98 print ("Unknown widget of type %1\n", widget->metaObject()->className()); |
122 } |
99 } |
123 }); |
100 }); |
124 |
101 |
125 m_window->applyToActions ([&](QAction* act) |
|
126 { |
|
127 addShortcut (act); |
|
128 }); |
|
129 |
|
130 ui.shortcutsList->setSortingEnabled (true); |
|
131 ui.shortcutsList->sortItems(); |
|
132 initExtProgs(); |
102 initExtProgs(); |
133 selectPage (defaulttab); |
103 selectPage (defaulttab); |
134 connect (ui.shortcut_set, SIGNAL (clicked()), this, SLOT (slot_setShortcut())); |
|
135 connect (ui.shortcut_reset, SIGNAL (clicked()), this, SLOT (slot_resetShortcut())); |
|
136 connect (ui.shortcut_clear, SIGNAL (clicked()), this, SLOT (slot_clearShortcut())); |
|
137 connect (ui.findDownloadPath, SIGNAL (clicked (bool)), this, SLOT (slot_findDownloadFolder())); |
104 connect (ui.findDownloadPath, SIGNAL (clicked (bool)), this, SLOT (slot_findDownloadFolder())); |
138 connect (ui.buttonBox, SIGNAL (clicked (QAbstractButton*)), |
105 connect (ui.buttonBox, SIGNAL (clicked (QAbstractButton*)), |
139 this, SLOT (buttonClicked (QAbstractButton*))); |
106 this, SLOT (buttonClicked (QAbstractButton*))); |
140 connect (ui.m_pages, SIGNAL (currentChanged (int)), this, SLOT (selectPage (int))); |
107 connect (ui.m_pages, SIGNAL (currentChanged (int)), this, SLOT (selectPage (int))); |
141 connect (ui.m_pagelist, SIGNAL (currentRowChanged (int)), this, SLOT (selectPage (int))); |
108 connect (ui.m_pagelist, SIGNAL (currentRowChanged (int)), this, SLOT (selectPage (int))); |
190 |
157 |
191 void ConfigDialog::selectPage (int row) |
158 void ConfigDialog::selectPage (int row) |
192 { |
159 { |
193 ui.m_pagelist->setCurrentRow (row); |
160 ui.m_pagelist->setCurrentRow (row); |
194 ui.m_pages->setCurrentIndex (row); |
161 ui.m_pages->setCurrentIndex (row); |
195 } |
|
196 |
|
197 // |
|
198 // Adds a shortcut entry to the list of shortcuts. |
|
199 // |
|
200 void ConfigDialog::addShortcut (QAction* act) |
|
201 { |
|
202 ShortcutListItem* item = new ShortcutListItem; |
|
203 item->setIcon (act->icon()); |
|
204 item->setAction (act); |
|
205 item->setSequence (act->shortcut()); |
|
206 setShortcutText (item); |
|
207 |
|
208 // If the action doesn't have a valid icon, use an empty one |
|
209 // so that the list is kept aligned. |
|
210 if (act->icon().isNull()) |
|
211 item->setIcon (MainWindow::getIcon ("empty")); |
|
212 |
|
213 ui.shortcutsList->insertItem (ui.shortcutsList->count(), item); |
|
214 } |
162 } |
215 |
163 |
216 // |
164 // |
217 // Initializes the stuff in the ext programs tab |
165 // Initializes the stuff in the ext programs tab |
218 // |
166 // |
308 |
256 |
309 settingsObject().setValue(confname, value); |
257 settingsObject().setValue(confname, value); |
310 }); |
258 }); |
311 |
259 |
312 ui.colorToolbarEditor->saveChanges(); |
260 ui.colorToolbarEditor->saveChanges(); |
|
261 shortcuts.saveChanges(); |
313 config::setLibraries(this->libraries); |
262 config::setLibraries(this->libraries); |
314 |
263 |
315 // Ext program settings |
264 // Ext program settings |
316 for (int i = 0; i < NumExternalPrograms; ++i) |
265 for (int i = 0; i < NumExternalPrograms; ++i) |
317 { |
266 { |
322 |
271 |
323 if (widgets.wineBox) |
272 if (widgets.wineBox) |
324 toolset->setWineSetting (program, widgets.wineBox->isChecked()); |
273 toolset->setWineSetting (program, widgets.wineBox->isChecked()); |
325 } |
274 } |
326 |
275 |
327 // Apply shortcuts |
|
328 for (int i = 0; i < ui.shortcutsList->count(); ++i) |
|
329 { |
|
330 auto item = static_cast<ShortcutListItem*> (ui.shortcutsList->item (i)); |
|
331 item->action()->setShortcut (item->sequence()); |
|
332 } |
|
333 |
|
334 settingsObject().sync(); |
276 settingsObject().sync(); |
335 emit settingsChanged(); |
277 emit settingsChanged(); |
336 } |
278 } |
337 |
279 |
338 // |
280 // |
386 { |
328 { |
387 button->setIcon (MainWindow::getIcon ("colorselect")); |
329 button->setIcon (MainWindow::getIcon ("colorselect")); |
388 button->setAutoFillBackground (true); |
330 button->setAutoFillBackground (true); |
389 button->setStyleSheet (format ("background-color: %1", value)); |
331 button->setStyleSheet (format ("background-color: %1", value)); |
390 m_buttonColors[button] = QColor (value); |
332 m_buttonColors[button] = QColor (value); |
391 } |
|
392 |
|
393 // |
|
394 // Finds the given list widget item in the list of widget items given. |
|
395 // |
|
396 int ConfigDialog::getItemRow (QListWidgetItem* item, QVector<QListWidgetItem*>& haystack) |
|
397 { |
|
398 int i = 0; |
|
399 |
|
400 for (QListWidgetItem* it : haystack) |
|
401 { |
|
402 if (it == item) |
|
403 return i; |
|
404 |
|
405 ++i; |
|
406 } |
|
407 |
|
408 return -1; |
|
409 } |
|
410 |
|
411 // |
|
412 // Get the list of shortcuts selected |
|
413 // |
|
414 QVector<ShortcutListItem*> ConfigDialog::getShortcutSelection() |
|
415 { |
|
416 QVector<ShortcutListItem*> out; |
|
417 |
|
418 for (QListWidgetItem* entry : ui.shortcutsList->selectedItems()) |
|
419 out << static_cast<ShortcutListItem*> (entry); |
|
420 |
|
421 return out; |
|
422 } |
|
423 |
|
424 // |
|
425 // Edit the shortcut of a given action. |
|
426 // |
|
427 void ConfigDialog::slot_setShortcut() |
|
428 { |
|
429 QVector<ShortcutListItem*> sel = getShortcutSelection(); |
|
430 |
|
431 if (countof(sel) < 1) |
|
432 return; |
|
433 |
|
434 ShortcutListItem* item = sel[0]; |
|
435 |
|
436 if (KeySequenceDialog::staticDialog (item, this)) |
|
437 setShortcutText (item); |
|
438 } |
|
439 |
|
440 // |
|
441 // Reset a shortcut to defaults |
|
442 // |
|
443 void ConfigDialog::slot_resetShortcut() |
|
444 { |
|
445 QVector<ShortcutListItem*> sel = getShortcutSelection(); |
|
446 |
|
447 for (ShortcutListItem* item : sel) |
|
448 { |
|
449 item->setSequence (m_window->defaultShortcut (item->action())); |
|
450 setShortcutText (item); |
|
451 } |
|
452 } |
|
453 |
|
454 // |
|
455 // Remove the shortcut of an action. |
|
456 // |
|
457 void ConfigDialog::slot_clearShortcut() |
|
458 { |
|
459 QVector<ShortcutListItem*> sel = getShortcutSelection(); |
|
460 |
|
461 for (ShortcutListItem* item : sel) |
|
462 { |
|
463 item->setSequence (QKeySequence()); |
|
464 setShortcutText (item); |
|
465 } |
|
466 } |
333 } |
467 |
334 |
468 // |
335 // |
469 // Set the path of an external program |
336 // Set the path of an external program |
470 // |
337 // |
504 QString dpath = QFileDialog::getExistingDirectory(); |
371 QString dpath = QFileDialog::getExistingDirectory(); |
505 |
372 |
506 if (not dpath.isEmpty()) |
373 if (not dpath.isEmpty()) |
507 ui.configDownloadFilePath->setText (dpath); |
374 ui.configDownloadFilePath->setText (dpath); |
508 } |
375 } |
509 |
|
510 // |
|
511 // |
|
512 // Updates the text string for a given shortcut list item |
|
513 // |
|
514 void ConfigDialog::setShortcutText (ShortcutListItem* item) |
|
515 { |
|
516 QAction* act = item->action(); |
|
517 QString label = act->iconText(); |
|
518 QString keybind = item->sequence().toString(); |
|
519 item->setText (format ("%1 (%2)", label, keybind)); |
|
520 } |
|
521 |
|
522 // |
|
523 // |
|
524 KeySequenceDialog::KeySequenceDialog (QKeySequence seq, QWidget* parent, Qt::WindowFlags f) : |
|
525 QDialog (parent, f), seq (seq) |
|
526 { |
|
527 lb_output = new QLabel; |
|
528 |
|
529 bbx_buttons = new QDialogButtonBox (QDialogButtonBox::Ok | QDialogButtonBox::Cancel); \ |
|
530 connect (bbx_buttons, SIGNAL (accepted()), this, SLOT (accept())); \ |
|
531 connect (bbx_buttons, SIGNAL (rejected()), this, SLOT (reject())); \ |
|
532 |
|
533 setWhatsThis (tr ("Into this dialog you can input a key sequence for use as a " |
|
534 "shortcut in LDForge. Use OK to confirm the new shortcut and Cancel to " |
|
535 "dismiss.")); |
|
536 |
|
537 QVBoxLayout* layout = new QVBoxLayout; |
|
538 layout->addWidget (lb_output); |
|
539 layout->addWidget (bbx_buttons); |
|
540 setLayout (layout); |
|
541 |
|
542 updateOutput(); |
|
543 } |
|
544 |
|
545 // |
|
546 // |
|
547 bool KeySequenceDialog::staticDialog (ShortcutListItem* item, QWidget* parent) |
|
548 { |
|
549 KeySequenceDialog dlg (item->sequence(), parent); |
|
550 |
|
551 if (dlg.exec() == QDialog::Rejected) |
|
552 return false; |
|
553 |
|
554 item->setSequence (dlg.seq); |
|
555 return true; |
|
556 } |
|
557 |
|
558 // |
|
559 // |
|
560 void KeySequenceDialog::updateOutput() |
|
561 { |
|
562 QString shortcut = seq.toString(); |
|
563 |
|
564 if (seq == QKeySequence()) |
|
565 shortcut = "<empty>"; |
|
566 |
|
567 QString text = format ("<center><b>%1</b></center>", shortcut); |
|
568 lb_output->setText (text); |
|
569 } |
|
570 |
|
571 // |
|
572 // |
|
573 void KeySequenceDialog::keyPressEvent (QKeyEvent* ev) |
|
574 { |
|
575 seq = ev->key() + ev->modifiers(); |
|
576 updateOutput(); |
|
577 } |
|