changeset 27724:19e80f22aa47

provide structure and settings value method for shortcut key sequences * QTerminal.cc (notice_settings): fix comparison of copy shortcut with the interrupt shortcut ctrl+c for dis-/enabling an extra action for the interrupt, more detailed comments on this * gui-preferences-sc.h: use new structure for defining shortcut preferences * gui-preferences.h: define new structure sc_pref for shortcut preferences with key name and default values as unsigned int (combinations of Qt::Key) and as QKeySequence::StandardKey togehter with three constructors for the different ways to initialize the structure * gui-settings.cc (sc_value): new method for reading the value of a key sequence from the preferences file testing the possible given default values with different type * gui-settings.h: new method sc_value
author Torsten Lilge <ttl-octave@mailbox.org>
date Wed, 20 Nov 2019 07:33:24 +0100
parents a0143104b224
children 6388a240de87
files libgui/qterminal/libqterminal/QTerminal.cc libgui/src/gui-preferences-sc.h libgui/src/gui-preferences.h libgui/src/gui-settings.cc libgui/src/gui-settings.h
diffstat 5 files changed, 82 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/qterminal/libqterminal/QTerminal.cc	Tue Nov 19 22:53:09 2019 +0100
+++ b/libgui/qterminal/libqterminal/QTerminal.cc	Wed Nov 20 07:33:24 2019 +0100
@@ -35,9 +35,9 @@
 #include <QApplication>
 #include <QAction>
 
+#include "gui-preferences-global.h"
 #include "gui-preferences-cs.h"
 #include "gui-preferences-sc.h"
-#include "gui-preferences-global.h"
 #include "octave-qobject.h"
 #include "resource-manager.h"
 
@@ -268,13 +268,17 @@
 
   setScrollBufferSize (settings->value (cs_hist_buffer).toInt ());
 
-  // If the Copy shortcut is Ctrl-C, then set up to allow Ctrl-C to also
-  // be processed as the interrupt character in the command window.
+  // If the Copy shortcut is Ctrl+C, then the Copy action also emits
+  // a signal for interrupting the current code executed by the worker.
+  // If the Copy shortcut is not Ctrl+C, an extra interrupt action is
+  // set up for emitting the interrupt signal.
 
-  QString sc = settings->value (sc_main_edit_copy).toString ();
+  QString sc = settings->sc_value (sc_main_edit_copy).toString ();
 
-  //  dis- or enable extra interrupt action
-  bool extra_ir_action = (QKeySequence (sc) != QKeySequence::Copy);
+  //  Dis- or enable extra interrupt action depending on the Copy shortcut
+  bool extra_ir_action
+      = (sc != QKeySequence (Qt::ControlModifier | Qt::Key_C).toString ());
+
   _interrupt_action->setEnabled (extra_ir_action);
   has_extra_interrupt (extra_ir_action);
 
--- a/libgui/src/gui-preferences-sc.h	Tue Nov 19 22:53:09 2019 +0100
+++ b/libgui/src/gui-preferences-sc.h	Wed Nov 20 07:33:24 2019 +0100
@@ -25,10 +25,23 @@
 
 #include "gui-preferences.h"
 
-// Console preferences
+// Define shortcuts
 
-const gui_pref
-sc_main_edit_copy ("shortcuts/main_edit:copy", QVariant (QKeySequence::Copy));
+// The shortcut's default values are given as QKeySequence for being able
+// to use platform independent standard keys (QKeySequence::StandardKey).
+// However, converting key sequences into QVariants does not seem to
+// revertable. In addition the related string (which is saved in the
+// preferences file) can not be determined during compile time since the
+// result depends on the platform (at least in case of standard key sequences
+// like, e.g., QKeySequence::Copy)
+// Therefore, these prefs for key sequences require a separate constant
+// definition and value method for the settings class.
+
+
+
+const sc_pref sc_main_edit_copy ("shortcuts/main_edit:copy", Qt::CTRL + Qt::Key_C);
+
+// Other normal, shortcut related options
 
 const gui_pref
 sc_main_ctrld ("shortcuts/main_ctrld", QVariant (false));
--- a/libgui/src/gui-preferences.h	Tue Nov 19 22:53:09 2019 +0100
+++ b/libgui/src/gui-preferences.h	Wed Nov 20 07:33:24 2019 +0100
@@ -49,4 +49,36 @@
   const QVariant def;  // the default value
 };
 
+// The version for shortcuts, where the default value is stored as a
+// combination of Qt:Keys (resutling in an unsigend int, when added)
+// or as one of the predefined standard key sequences.
+
+struct sc_pref
+{
+  sc_pref (const QString& key_arg, Qt::Key def_arg)
+    : key (key_arg), def (static_cast<unsigned int> (def_arg)),
+      def_std (QKeySequence::UnknownKey)
+  { }
+
+  sc_pref (const QString& key_arg, unsigned int def_arg)
+    : key (key_arg), def (def_arg), def_std (QKeySequence::UnknownKey)
+  { }
+
+  sc_pref (const QString& key_arg, QKeySequence::StandardKey def_std_arg)
+    : key (key_arg), def (0), def_std (def_std_arg)
+  { }
+
+  // No copying!
+
+  sc_pref (const sc_pref&) = delete;
+
+  sc_pref& operator = (const sc_pref&) = delete;
+
+  ~sc_pref (void) = default;
+
+  const QString key;                        // the key name
+  const unsigned int def;                   // the default as key
+  const QKeySequence::StandardKey def_std;  // the default as standard key
+};
+
 #endif
--- a/libgui/src/gui-settings.cc	Tue Nov 19 22:53:09 2019 +0100
+++ b/libgui/src/gui-settings.cc	Wed Nov 20 07:33:24 2019 +0100
@@ -24,9 +24,30 @@
 #  include "config.h"
 #endif
 
+#include <QSettings>
+
 #include "gui-settings.h"
 
 namespace octave
 {
+
+  QVariant gui_settings::sc_value (const sc_pref& pref) const
+  {
+    QKeySequence key_seq = QKeySequence ();
+
+    // Check, which of the elements for the default value in the sc_pref
+    // structure has a valid value and take this as default. If both
+    // elements are not valid, leave the key sequence empty
+    if (pref.def)
+      key_seq = QKeySequence (pref.def);
+    else if (pref.def_std != QKeySequence::UnknownKey)
+      key_seq = QKeySequence (pref.def_std);
+
+    // Get the value from the settings where the key sequences are stored
+    // as strings
+    return value (pref.key, key_seq.toString ());
+  }
+
   // Additional gui_settings functions will be added later.
+
 }
--- a/libgui/src/gui-settings.h	Tue Nov 19 22:53:09 2019 +0100
+++ b/libgui/src/gui-settings.h	Wed Nov 20 07:33:24 2019 +0100
@@ -61,6 +61,9 @@
     {
       return value (pref.key, pref.def);
     }
+
+    QVariant sc_value (const sc_pref& pref) const;
+
   };
 }