# HG changeset patch # User Torsten Lilge # Date 1713100457 -7200 # Node ID 531d4ae37c60c8fbfbb7c9370048d72e8e4f09c3 # Parent 507b5fec6bb2c374c472663a817527e355c24890 allow unbinding GUI shortcuts (bug #65580) * gui-settings.cc (sc_value): if shortcut is not contained in the settings, take the default shortcut, otherwise take contents of settings, even if empty (no shortcut selected for the action); (set_shortcut): use sc_value and set the return value as shortcut for the action, even if empty (shortcut): do not treat empty shortcut as special case * shortcuts-tree-widget.cc (shortcut_edit_dialog): add a clear button, set the current contents of the actual column into the line edit instead of the settings value, use lambda expression instead of separate trivial slot set_default_shortcut; (shortcuts_tree_widget): do not use settings group as now sc_value is used becuase we also want to show active shortcuts that are identical to the default value; (export_shortcuts): only write the shortcut into the settings file if it differs from the default value or if a full export into a user file is desired; (write_settings): call export_shortcuts with full flag false * shortcuts-tree-widget.h: remove slot set_default_shortcut, export-shortcuts with boolean argument if full export or not diff -r 507b5fec6bb2 -r 531d4ae37c60 libgui/src/gui-settings.cc --- a/libgui/src/gui-settings.cc Sun Apr 14 12:20:03 2024 +0200 +++ b/libgui/src/gui-settings.cc Sun Apr 14 15:14:17 2024 +0200 @@ -192,12 +192,18 @@ QString gui_settings::sc_value (const sc_pref& scpref) const { - QKeySequence key_seq = sc_def_value (scpref); + QString full_settings_key = sc_group + "/" + scpref.settings_key (); + + if (contains (full_settings_key)) + { + QKeySequence key_seq = sc_def_value (scpref); - // Get the value from the settings where the key sequences are stored - // as strings - return value (sc_group + "/" + scpref.settings_key (), - key_seq.toString ()).toString (); + // Get the value from the settings where the key sequences are stored + // as strings + return value (full_settings_key, key_seq.toString ()).toString (); + } + else + return scpref.def_text (); } QKeySequence @@ -216,25 +222,15 @@ return; } - QString shortcut = sc_value (scpref); - - if (! shortcut.isEmpty ()) + QString shortcut = sc_value (scpref); action->setShortcut (QKeySequence (shortcut)); - else - qDebug () << "Key: " << scpref.settings_key () - << " not found in settings"; } void gui_settings::shortcut (QShortcut *sc, const sc_pref& scpref) { QString shortcut = sc_value (scpref); - - if (! shortcut.isEmpty ()) - sc->setKey (QKeySequence (shortcut)); - else - qDebug () << "Key: " << scpref.settings_key () - << " not found in settings"; + sc->setKey (QKeySequence (shortcut)); } void diff -r 507b5fec6bb2 -r 531d4ae37c60 libgui/src/shortcuts-tree-widget.cc --- a/libgui/src/shortcuts-tree-widget.cc Sun Apr 14 12:20:03 2024 +0200 +++ b/libgui/src/shortcuts-tree-widget.cc Sun Apr 14 15:14:17 2024 +0200 @@ -221,12 +221,11 @@ grid->addWidget (def, 1, 0); grid->addWidget (label_default, 1, 1); + QPushButton *clear_text = new QPushButton (tr ("Clear")); QPushButton *set_default = new QPushButton (tr ("Set to default")); - connect (set_default, &QPushButton::clicked, - this, &shortcut_edit_dialog::set_default_shortcut); - - grid->addWidget (set_default, 0, 2); + grid->addWidget (clear_text, 0, 2); + grid->addWidget (set_default, 0, 3); box->addLayout (grid); box->addSpacing (18); @@ -260,12 +259,16 @@ const sc_pref scpref = all_shortcut_preferences::value (m_settings_key); - QString actual_text = settings.sc_value (scpref); + m_default_text = scpref.def_text (); + label_default->setText (m_default_text); - m_default_text = scpref.def_text (); + QString actual_text = shortcut_item->actual_text (); + m_edit_actual->setText (actual_text); - m_edit_actual->setText (actual_text); - label_default->setText (m_default_text); + connect (clear_text, &QPushButton::clicked, + [this] () { m_edit_actual->setText (QString ()); }); + connect (set_default, &QPushButton::clicked, + [this] () { m_edit_actual->setText (m_default_text); }); m_edit_actual->setFocus (); @@ -344,13 +347,6 @@ m_shortcut_item->set_actual_text (actual_text); } -void -shortcut_edit_dialog::set_default_shortcut () -{ - // Just remove user-set value so that the default will be used. - m_edit_actual->setText (""); -} - shortcuts_tree_widget::shortcuts_tree_widget (QWidget *parent) : QTreeWidget (parent) { @@ -450,13 +446,12 @@ connect (this, &QTreeWidget::itemDoubleClicked, this, &shortcuts_tree_widget::edit_selection); - const QList shortcut_settings_keys + QList shortcut_settings_keys = all_shortcut_preferences::keys (); + shortcut_settings_keys.sort (); gui_settings settings; - settings.beginGroup (sc_group); - for (const auto& settings_key : shortcut_settings_keys) { QTreeWidgetItem *section = level_hash[settings_key.section (':', 0, 0)]; @@ -482,17 +477,13 @@ section = main_zoom; } - // We don't want to apply default value here. - QString actual_text = settings.value (settings_key).toString (); - const sc_pref scpref = all_shortcut_preferences::value (settings_key); // Inserts itself in the tree widget in SECTION. The parent // object will delete it. + QString actual_text = settings.sc_value (scpref); new tree_widget_shortcut_item (section, scpref, actual_text); } - - settings.endGroup (); } void @@ -625,7 +616,7 @@ // Export all shortcuts from the tree view to the settings object. void -shortcuts_tree_widget::export_shortcuts (gui_settings& settings) +shortcuts_tree_widget::export_shortcuts (gui_settings& settings, bool full) { settings.beginGroup (sc_group); @@ -643,10 +634,19 @@ QString settings_key = shortcut_item->settings_key (); QString sc_text = shortcut_item->actual_text (); - if (sc_text.isEmpty ()) - sc_text = shortcut_item->default_text (); + if (full || sc_text != shortcut_item->default_text ()) + { + // Only write the shortcut to the settings file if it is + // different to its default value or if an export into a + // custom file is desired by the user. + settings.setValue (settings_key, sc_text); + } else - settings.setValue (settings_key, sc_text); + { + // Remove the key otherwise, the default value will be + // chosen for non existing shortcut keys. + settings.remove (settings_key); + } QString section = get_shortcut_section (settings_key); @@ -723,7 +723,7 @@ { gui_settings settings; - export_shortcuts (settings); + export_shortcuts (settings, false); // false: omit values identical to the default } OCTAVE_END_NAMESPACE(octave) diff -r 507b5fec6bb2 -r 531d4ae37c60 libgui/src/shortcuts-tree-widget.h --- a/libgui/src/shortcuts-tree-widget.h Sun Apr 14 12:20:03 2024 +0200 +++ b/libgui/src/shortcuts-tree-widget.h Sun Apr 14 15:14:17 2024 +0200 @@ -105,8 +105,6 @@ void finished (int result); - void set_default_shortcut (); - signals: void set_shortcut (const QString& settings_key, @@ -132,7 +130,7 @@ void import_shortcuts (gui_settings& settings); - void export_shortcuts (gui_settings& settings); + void export_shortcuts (gui_settings& settings, bool full = true); void set_default_shortcuts ();