changeset 20702:b6b16d8c8b57

possibility to set all shortcuts to default values and check overwriting * settings-dialog.cc (settings_dialog): connect new button to new slot; (import_shortcut_set, export_shortcut_set): calling common function shortcut_manager::import_export with an integer for the desired action; (default_shortcut_set): new slot for new button, calling sortcut_manager::import_export with an integer for the desired action * settings-dialog.h: new slot default_shortcut_set * settings-dialog.ui: new button for setting default values to the shortcuts * shortcut-manager.cc (import_shortcuts): take default values when settings pointer is 0; (overwrite_all_shortcuts): new private function checking whether the user wants to overwrite all current shrotcuts; (do_import_export): action import, export, set to default depends on integer parameter; return parameter indicates success of the action; * shortcut-manager.h: enum with actions for import and export, import_export and do_import_export with integer instead of bool parameter, do_import_export returns a boolean
author Torsten <ttl@justmail.de>
date Sat, 14 Nov 2015 13:31:00 +0100
parents ded4efec895f
children 85e5efae848a
files libgui/src/settings-dialog.cc libgui/src/settings-dialog.h libgui/src/settings-dialog.ui libgui/src/shortcut-manager.cc libgui/src/shortcut-manager.h
diffstat 5 files changed, 126 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/settings-dialog.cc	Fri Nov 13 19:31:51 2015 -0800
+++ b/libgui/src/settings-dialog.cc	Sat Nov 14 13:31:00 2015 +0100
@@ -549,6 +549,8 @@
            this, SLOT (import_shortcut_set ()));
   connect (ui->btn_export_shortcut_set, SIGNAL (clicked ()),
            this, SLOT (export_shortcut_set ()));
+  connect (ui->btn_default_shortcut_set, SIGNAL (clicked ()),
+           this, SLOT (default_shortcut_set ()));
 
 
 #ifdef HAVE_QSCINTILLA
@@ -1013,11 +1015,17 @@
 void
 settings_dialog::import_shortcut_set ()
 {
-  shortcut_manager::import_export (true);
+  shortcut_manager::import_export (shortcut_manager::OSC_IMPORT);
 }
 
 void
 settings_dialog::export_shortcut_set ()
 {
-  shortcut_manager::import_export (false);
+  shortcut_manager::import_export (shortcut_manager::OSC_EXPORT);
+}
+
+void
+settings_dialog::default_shortcut_set ()
+{
+  shortcut_manager::import_export (shortcut_manager::OSC_DEFAULT);
 }
\ No newline at end of file
--- a/libgui/src/settings-dialog.h	Fri Nov 13 19:31:51 2015 -0800
+++ b/libgui/src/settings-dialog.h	Sat Nov 14 13:31:00 2015 +0100
@@ -57,6 +57,7 @@
   // slots for import/export-buttons of shortcut sets
   void import_shortcut_set ();
   void export_shortcut_set ();
+  void default_shortcut_set ();
 
 private:
   Ui::settings_dialog * ui;
--- a/libgui/src/settings-dialog.ui	Fri Nov 13 19:31:51 2015 -0800
+++ b/libgui/src/settings-dialog.ui	Sat Nov 14 13:31:00 2015 +0100
@@ -1986,14 +1986,10 @@
                <property name="topMargin">
                 <number>10</number>
                </property>
-               <item row="2" column="0">
-                <layout class="QHBoxLayout" name="horizontalLayout_16">
-                 <property name="topMargin">
-                  <number>0</number>
-                 </property>
-                </layout>
-               </item>
-               <item row="0" column="2">
+               <property name="bottomMargin">
+                <number>10</number>
+               </property>
+               <item row="0" column="3">
                 <spacer name="horizontalSpacer_26">
                  <property name="orientation">
                   <enum>Qt::Horizontal</enum>
@@ -2008,6 +2004,9 @@
                </item>
                <item row="0" column="0">
                 <widget class="QPushButton" name="btn_import_shortcut_set">
+                 <property name="toolTip">
+                  <string>Import shortcut set</string>
+                 </property>
                  <property name="text">
                   <string>Import</string>
                  </property>
@@ -2015,11 +2014,24 @@
                </item>
                <item row="0" column="1">
                 <widget class="QPushButton" name="btn_export_shortcut_set">
+                 <property name="toolTip">
+                  <string>Export current shortcut set</string>
+                 </property>
                  <property name="text">
                   <string>Export</string>
                  </property>
                 </widget>
                </item>
+               <item row="0" column="2">
+                <widget class="QPushButton" name="btn_default_shortcut_set">
+                 <property name="toolTip">
+                  <string>Reset shortcuts to their defaults</string>
+                 </property>
+                 <property name="text">
+                  <string>Default</string>
+                 </property>
+                </widget>
+               </item>
               </layout>
              </item>
              <item>
--- a/libgui/src/shortcut-manager.cc	Fri Nov 13 19:31:51 2015 -0800
+++ b/libgui/src/shortcut-manager.cc	Sat Nov 14 13:31:00 2015 +0100
@@ -602,7 +602,8 @@
   _edit_actual->setText (_label_default->text ());
 }
 
-// import a shortcut set from a given settings file and refresh the tree view
+// import a shortcut set from a given settings file or reset to
+// the defaults (settings = 0) and refresh the tree view
 void
 shortcut_manager::import_shortcuts (QSettings *settings)
 {
@@ -610,9 +611,14 @@
     {
       // update the list of all shortcuts
       shortcut_t sc = _sc.at (i);           // make a copy
-      sc.actual_sc = QKeySequence (         // get new shortcut from settings
-        settings->value ("shortcuts/"+sc.settings_key,sc.actual_sc).
-                        toString ());       // and use the old one as default
+
+      if (settings)
+        sc.actual_sc = QKeySequence (         // get new shortcut from settings
+          settings->value ("shortcuts/"+sc.settings_key,sc.actual_sc).
+                          toString ());       // and use the old one as default
+      else
+        sc.actual_sc = QKeySequence (sc.default_sc); // get default shortcut
+
       _sc.replace (i,sc);                   // replace the old with the new one
 
       // update the tree view
@@ -621,43 +627,90 @@
     }
 }
 
+// ask the user whether to save the current shortcut set;
+// returns true to proceed with import action, false to abort it
+bool
+shortcut_manager::overwrite_all_shortcuts ()
+{
+  QMessageBox msg_box;
+  msg_box.setWindowTitle(tr ("Overwriting Shortcuts"));
+  msg_box.setIcon (QMessageBox::Warning);
+  msg_box.setText(tr ("You are about to overwrite all shortcuts.\n"
+     "Would you like to save the current shortcut set or cancel the action?"));
+  msg_box.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
+  QPushButton *discard = msg_box.addButton (tr ("Don't save"),
+                                            QMessageBox::DestructiveRole);
+  msg_box.setDefaultButton(QMessageBox::Save);
+
+  int ret = msg_box.exec ();
+
+  if (msg_box.clickedButton () == discard)
+    return true;  // do not save and go ahead
+
+  if (ret == QMessageBox::Save)
+    {
+      if (do_import_export (OSC_EXPORT))
+        return true;  // go ahead
+    }
+
+  return false; // abort the import
+}
+
 // import or export of shortcut sets,
-// called from settings dialog when related buttons are clicked
-void
-shortcut_manager::do_import_export (bool import)
+// called from settings dialog when related buttons are clicked;
+// returns true on success, false otherwise
+bool
+shortcut_manager::do_import_export (int action)
 {
-  QString file;
+  // ask to save the current shortcuts, maybe abort import
+  if (action == OSC_DEFAULT || action == OSC_IMPORT)
+    {
+      if (! overwrite_all_shortcuts ())
+        return false;
+    }
 
   // get the file name to read or write the shortcuts,
   // the default extension is .osc (octave shortcuts)
-  if (import)
+  if (action != OSC_DEFAULT)
     {
-      file = QFileDialog::getOpenFileName (this,
-              tr ("Import shortcuts from file ..."), QString (),
-              tr ("Octave Shortcut Files (*.osc);;All Files (*)"),
-              0,QFileDialog::DontUseNativeDialog);
+      QString file;
+
+      if (action == OSC_IMPORT)
+        file  = QFileDialog::getOpenFileName (this,
+                    tr ("Import shortcuts from file ..."), QString (),
+                    tr ("Octave Shortcut Files (*.osc);;All Files (*)"),
+                    0,QFileDialog::DontUseNativeDialog);
+      else if (action == OSC_EXPORT)
+        file = QFileDialog::getSaveFileName (this,
+                    tr ("Export shortcuts into file ..."), QString (),
+                    tr ("Octave Shortcut Files (*.osc);;All Files (*)"),
+                    0,QFileDialog::DontUseNativeDialog);
+
+      if (file.isEmpty ())
+          return false;
+
+      QSettings *osc_settings = new QSettings (file, QSettings::IniFormat);
+
+      if (! osc_settings)
+        {
+          qWarning () << tr ("Failed to open %1 as octave shortcut file")
+                         .arg (file);
+          return false;
+        }
+      else
+        {
+          if (action == OSC_IMPORT)
+            import_shortcuts (osc_settings);   // import (special action)
+          else if (action == OSC_EXPORT)
+            do_write_shortcuts (osc_settings, false); // export, (save settings)
+        }
     }
   else
     {
-      file = QFileDialog::getSaveFileName (this,
-              tr ("Export shortcuts into file ..."), QString (),
-              tr ("Octave Shortcut Files (*.osc);;All Files (*)"),
-              0,QFileDialog::DontUseNativeDialog);
+      import_shortcuts (0);
     }
 
-  // create a settings object related to this file
-  QSettings *osc_settings = new QSettings (file, QSettings::IniFormat);
-  if (osc_settings)
-    {
-      // the settings object was successfully created: carry on
-      if (import)
-        import_shortcuts (osc_settings);   // import (special action)
-      else
-        do_write_shortcuts (osc_settings, false); // export, (saving settings)
-    }
-  else
-    qWarning () << tr ("Failed to open %1 as octave shortcut file"). arg (file);
-
+  return true;
 }
 
 
--- a/libgui/src/shortcut-manager.h	Fri Nov 13 19:31:51 2015 -0800
+++ b/libgui/src/shortcut-manager.h	Sat Nov 14 13:31:00 2015 +0100
@@ -54,6 +54,14 @@
   Q_OBJECT
 
 public:
+
+  enum
+  {
+    OSC_IMPORT  = 0,
+    OSC_EXPORT  = 1,
+    OSC_DEFAULT = 2
+  };
+
   shortcut_manager ();
   ~shortcut_manager ();
 
@@ -81,10 +89,10 @@
       instance->do_fill_treewidget (tree_view);
   }
 
-  static void import_export (bool import)
+  static void import_export (int action)
   {
     if (instance_ok ())
-      instance->do_import_export (import);
+      instance->do_import_export (action);
   }
 
 public slots:
@@ -116,9 +124,10 @@
   void do_write_shortcuts (QSettings *settings, bool closing);
   void do_set_shortcut (QAction *action, const QString& key);
   void do_fill_treewidget (QTreeWidget *tree_view);
-  void do_import_export (bool import);
+  bool do_import_export (int action);
   void shortcut_dialog (int);
   void import_shortcuts (QSettings *settings);
+  bool overwrite_all_shortcuts (void);
 
   class shortcut_t
   {