]> https://gitweb.dealii.org/ - parameter_gui.git/commitdiff
Add preliminary settings dialog
authorRene Gassmoeller <rene.gassmoeller@mailbox.org>
Wed, 5 Apr 2017 22:26:06 +0000 (16:26 -0600)
committerRene Gassmoeller <rene.gassmoeller@mailbox.org>
Thu, 6 Apr 2017 17:29:58 +0000 (11:29 -0600)
Implement change font option

Preliminary hiding feature

Add toolbar

CMakeLists.txt
dealii_parameter_gui.pro
mainwindow.cpp
mainwindow.h
settings_dialog.cpp [new file with mode: 0644]
settings_dialog.h [new file with mode: 0644]

index 1211ed2b224d64670838e62f10e2c11ebac2aa30..5a702c0337367d4d035d5af70864ee208591e5b8 100644 (file)
@@ -30,6 +30,7 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
 QT4_WRAP_CPP(SOURCE_MOC
   browse_lineedit.h
   info_message.h
+  settings_dialog.h
   mainwindow.h
   parameter_delegate.h
   )
@@ -41,6 +42,7 @@ QT4_ADD_RESOURCES(SOURCE_RCC
 ADD_EXECUTABLE(parameter_gui_exe
   browse_lineedit.cpp
   info_message.cpp
+  settings_dialog.cpp
   main.cpp
   mainwindow.cpp
   parameter_delegate.cpp
index 834545d4791f49cb3cca12c3c8fdc0a0a216e39f..4f5f0c18eb9bc66c659d409d031dbe380d6daa0a 100644 (file)
@@ -11,15 +11,19 @@ DESTDIR = ../../lib/bin
 # Input
 HEADERS += browse_lineedit.h \
            info_message.h \
+           settings_dialog.h \
            mainwindow.h \
            parameter_delegate.h \
            xml_parameter_reader.h \
-           xml_parameter_writer.h
+           xml_parameter_writer.h \
+           prm_parameter_writer.h
 SOURCES += browse_lineedit.cpp \
            info_message.cpp \
+           settings_dialog.cpp \
            main.cpp \
            mainwindow.cpp \
            parameter_delegate.cpp \
            xml_parameter_reader.cpp \
-           xml_parameter_writer.cpp
+           xml_parameter_writer.cpp \
+           prm_parameter_writer.cpp
 RESOURCES += application.qrc
index b054d7aa0316be5e4b83fc2301ce6d26fcfa2835..3e2674700f14d1dd071121d078d32a3c9306adca 100644 (file)
@@ -32,8 +32,6 @@ namespace dealii
       QString  settings_file = QDir::currentPath() + "/settings.ini";          // a file for user settings
 
       gui_settings = new QSettings (settings_file, QSettings::IniFormat);      // load settings
-                                                                               // Up to now, we do not read any settings,
-                                                                               // but this can be used in the future for customizing the GUI.
 
       tree_widget = new QTreeWidget;                                           // tree for showing XML tags
 
@@ -80,6 +78,8 @@ namespace dealii
 
       create_actions();                                                                // create window actions as "Open",...
       create_menus();                                                          // and menus
+      create_toolbar();
+
       statusBar()->showMessage(tr("Ready, start editing by double-clicking or hitting F2!"));
       setWindowTitle(tr("[*]parameterGUI"));                                   // set window title
 
@@ -87,6 +87,8 @@ namespace dealii
 
       if (filename.size() > 3)                                                 // if there is a file_name, try to load the file.
         load_file(filename);                                                   // a vliad file has the xml extension, so we require size() > 3
+
+      apply_settings();
     }
 
 
@@ -116,6 +118,10 @@ namespace dealii
           QFont font = item->font(1);
           font.setWeight(QFont::Normal);
           item->setFont(1,font);
+
+          const bool hide_default_items = gui_settings->value("Settings/hideDefault", false).toBool();
+          if (hide_default_items)
+            item->setHidden(true);
         }
       else
         {
@@ -270,6 +276,86 @@ namespace dealii
 
 
 
+    void MainWindow::show_settings ()
+    {
+      settings_dialog = new SettingsDialog(gui_settings,this);
+
+      connect(settings_dialog, SIGNAL(accepted()), this, SLOT(apply_settings()));
+      settings_dialog->exec();
+      disconnect(settings_dialog, SIGNAL(accepted()), this, SLOT(apply_settings()));
+    }
+
+
+
+    void MainWindow::apply_settings ()
+    {
+      update_visible_items();
+      update_font();
+    }
+
+
+
+    void MainWindow::toggle_visible_default_items()
+    {
+      const bool hide_default_values = gui_settings->value("Settings/hideDefault", false).toBool();
+      gui_settings->setValue("Settings/hideDefault", !hide_default_values);
+      update_visible_items();
+    }
+
+
+
+    void MainWindow::update_visible_items()
+    {
+      const bool hide_default_values = gui_settings->value("Settings/hideDefault", false).toBool();
+
+      if (hide_default_values)
+        {
+        for (int i = 0; i < tree_widget->topLevelItemCount(); ++i)
+            hide_default_item(tree_widget->topLevelItem(i));
+        hide_default->setChecked(true);
+        }
+      else
+        {
+          QTreeWidgetItemIterator it(tree_widget,QTreeWidgetItemIterator::Hidden);
+          while (*it)
+            {
+              (*it)->setHidden(false);
+              ++it;
+            }
+          hide_default->setChecked(false);
+          }
+    }
+
+
+
+    bool MainWindow::hide_default_item(QTreeWidgetItem *item)
+    {
+      bool has_default_value = true;
+
+      if (item->childCount() == 0)
+        {
+          if (item->text(5).startsWith("[Double"))
+            has_default_value = item->data(1,Qt::DisplayRole).toReal() == item->data(2,Qt::DisplayRole).toReal();
+          else
+            has_default_value = item->data(1,Qt::DisplayRole).toString() == item->data(2,Qt::DisplayRole).toString();
+        }
+      else
+        {
+          for (int i = 0; i < item->childCount(); ++i)
+            {
+              const bool child_has_default_value = hide_default_item(item->child(i));
+              has_default_value = has_default_value & child_has_default_value;
+            }
+        }
+
+      if (has_default_value)
+        item->setHidden(true);
+
+      return has_default_value;
+    }
+
+
+
     void MainWindow::closeEvent(QCloseEvent *event)
     {
       if (maybe_save())                                                                // reimplement the closeEvent from the QMainWindow class
@@ -317,6 +403,10 @@ namespace dealii
       about_qt_act->setStatusTip(tr("Show the Qt library's About box"));
       connect(about_qt_act, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
 
+      settings_act = new QAction(tr("Settings"), this);
+      settings_act->setStatusTip(tr("Show the Settings Dialog"));
+      connect(settings_act, SIGNAL(triggered()), this, SLOT(show_settings()));
+
       set_to_default_act = new QAction("Set to default",context_menu);
       tree_widget->addAction(set_to_default_act);
       connect(set_to_default_act, SIGNAL(triggered()), this, SLOT(set_to_default()));
@@ -330,6 +420,9 @@ namespace dealii
         file_menu->addAction(open_act);                                                // and add actions
         file_menu->addAction(save_act);
         file_menu->addAction(save_as_act);
+        file_menu->addSeparator();
+        file_menu->addAction(settings_act);
+        file_menu->addSeparator();
         file_menu->addAction(exit_act);
 
         menuBar()->addSeparator();
@@ -341,6 +434,32 @@ namespace dealii
 
 
 
+    void MainWindow::create_toolbar()
+    {
+      QToolBar *toolbar = new QToolBar(tr("Toolbar"),this);
+
+      toolbar->addAction(open_act);                                           // and add actions
+      toolbar->addAction(save_act);
+      toolbar->addAction(save_as_act);
+
+      toolbar->addSeparator();
+
+      hide_default = new QToolButton(toolbar);
+      hide_default->setText(tr("Hide default values"));
+      hide_default->setCheckable(true);
+      connect(hide_default, SIGNAL(clicked()), this, SLOT(toggle_visible_default_items()));
+      QAction *hide_default_act = toolbar->addWidget(hide_default);
+
+      QToolButton *change_font = new QToolButton(toolbar);
+      change_font->setText(tr("Change font"));
+      connect(change_font, SIGNAL(clicked()), this, SLOT(select_font()));
+      QAction *change_font_act = toolbar->addWidget(change_font);
+
+      addToolBar(toolbar);
+    }
+
+
+
     bool MainWindow::maybe_save()
     {
       if (isWindowModified())                                                  // if content was modified
@@ -464,5 +583,32 @@ namespace dealii
       setWindowTitle(tr(win_title.c_str()));                                   // set the window title
       setWindowModified(false);                                                        // and reset window modified
     }
+
+
+
+    void MainWindow::update_font()
+    {
+      QString current_font_string = gui_settings->value("Settings/Font", QFont().toString()).toString();
+      QFont current_font;
+      current_font.fromString(current_font_string);
+      setFont(current_font);
+    }
+
+
+
+    void MainWindow::select_font()
+    {
+      QString current_font_string = gui_settings->value("Settings/Font", QFont().toString()).toString();
+      QFont current_font;
+      current_font.fromString(current_font_string);
+
+      bool ok;
+      QFont new_font = QFontDialog::getFont(
+                      &ok, current_font, this);
+      if (ok) {
+          gui_settings->setValue("Settings/Font", new_font.toString());
+          setFont(new_font);
+      }
+    }
   }
 }
index 6671a738084a62813d7b2aa392a792148a5f525d..1f48dafaa4864fe45d69f7b13e1b39877721ab98 100644 (file)
 #include <QTreeWidget>
 #include <QDialog>
 #include <QSettings>
+#include <QToolBar>
+#include <QToolButton>
 
 #include "info_message.h"
+#include "settings_dialog.h"
 
 
 namespace dealii
@@ -112,6 +115,40 @@ namespace dealii
                                      */
         void item_changed(QTreeWidgetItem *item,
                           int column);
+
+        /**
+         * Show an information dialog, how
+         * parameters can be edited.
+         */
+        void show_settings ();
+
+        /**
+         * Apply the new settings to this window.
+         */
+        void apply_settings ();
+
+        /**
+         * Hide all default items in tree_widget if set in gui_settings.
+         * Otherwise restores all default values.
+         */
+        void update_visible_items();
+
+        /**
+         * Reads the font from gui_settings and applies it.
+         */
+        void update_font();
+
+        /**
+         * Changes whether default items should be displayed in the tree widget
+         * and calls update_visible_items() to apply the changes.
+         */
+        void toggle_visible_default_items();
+
+        /**
+         * Function that displays a font selection dialog, stores the result
+         * in gui_settings, and displays the new font.
+         */
+        void select_font();
       private:
                                     /**
                                      * Show an information dialog, how
@@ -126,6 +163,11 @@ namespace dealii
                                      * This function creates all menus.
                                      */
         void create_menus();
+
+        /**
+         * This function creates the toolbar.
+         */
+        void create_toolbar();
                                     /**
                                      * This function checks, if parameters were changed
                                      * and show a dialog, if changes should be saved.
@@ -146,6 +188,13 @@ namespace dealii
                                      */
         void set_current_file (const QString  &filename);
 
+        /**
+         * Determine if the item and all of its children have the default value,
+         * and hide all default items. Returns true if the item and all of its
+         * children have default values.
+         */
+        bool hide_default_item(QTreeWidgetItem *item);
+
                                     /**
                                      * This is the tree structure in which we store all parameters.
                                      */
@@ -154,6 +203,11 @@ namespace dealii
                                       * This is the documentation text area.
                                       */
         QTextEdit *documentation_text_widget;
+
+        /** A tool button that allows to toggle between showing/hiding parameters
+         * with default values.
+         */
+        QToolButton *hide_default;
                                     /**
                                      * This menu provides all file actions as <tt>open</tt>, <tt>save</tt>, <tt>save as</tt>
                                      * and <tt>exit</tt>
@@ -180,6 +234,10 @@ namespace dealii
                                      * QAction <tt>save as</tt> a file.
                                      */
         QAction * save_as_act;
+                                     /**
+                                      * QAction <tt>save as</tt> a file.
+                                      */
+        QAction * settings_act;
                                     /**
                                      * QAction <tt>exit</tt> the GUI.
                                      */
@@ -205,6 +263,9 @@ namespace dealii
                                      * This dialog shows a short information message after loading a file.
                                      */
         InfoMessage * info_message;
+
+        SettingsDialog * settings_dialog;
+
                                     /**
                                      * An object for storing user settings.
                                      */
diff --git a/settings_dialog.cpp b/settings_dialog.cpp
new file mode 100644 (file)
index 0000000..38333f9
--- /dev/null
@@ -0,0 +1,107 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 by Martin Steigemann and Wolfgang Bangerth
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include <QtGui>
+
+#include "settings_dialog.h"
+
+
+namespace dealii
+{
+  namespace ParameterGui
+  {
+    SettingsDialog::SettingsDialog(QSettings *gui_settings,
+                                   QWidget *parent)
+               : QDialog(parent, 0)
+    {
+      setWindowTitle("Settings");
+
+      settings = gui_settings;
+      loadSettings();
+
+      QFormLayout * grid = new QFormLayout(this);
+
+      // add a choose font button
+      change_font = new QPushButton(this);
+      change_font->setText(QErrorMessage::tr("Change font"));
+      connect(change_font, SIGNAL(clicked()), this, SLOT(selectFont()));
+      grid->addRow("Change Font",change_font);
+
+      // add a checkbox
+      hide_default = new QCheckBox(this);
+      hide_default->setChecked(hide_default_values);
+      connect(hide_default, SIGNAL(stateChanged(int)), this, SLOT(changeHideDefault(int)));
+      grid->addRow("Hide default values",hide_default);
+
+      // add an OK button
+      ok = new QPushButton(this);
+      ok->setText(QErrorMessage::tr("&OK"));
+      connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
+
+      // add a Cancel button
+      cancel = new QPushButton(this);
+      cancel->setText(QErrorMessage::tr("&Cancel"));
+      connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
+      grid->addRow(ok,cancel);
+
+      connect(this, SIGNAL(accepted()), this, SLOT(writeSettings()));
+    }
+
+
+
+    void SettingsDialog::selectFont()
+    {
+      bool ok;
+      QFont new_font = QFontDialog::getFont(
+                      &ok, selected_font, this);
+      if (ok) {
+          selected_font = new_font;
+      }
+    }
+
+
+
+    void SettingsDialog::changeHideDefault(int state)
+    {
+      hide_default_values = state;
+    }
+
+
+
+    void SettingsDialog::loadSettings()
+    {
+      settings->beginGroup("Settings");
+      hide_default_values = settings->value("hideDefault", false).toBool();
+
+      QString stored_font_string = settings->value("Font", QFont().toString()).toString();
+      selected_font.fromString(stored_font_string);
+      settings->endGroup();
+    }
+
+
+
+    void SettingsDialog::writeSettings()
+    {
+      settings->beginGroup("Settings");
+
+      settings->setValue("hideDefault", hide_default_values);
+      settings->setValue("Font", selected_font.toString());
+
+      settings->endGroup();
+    }
+  }
+}
+
diff --git a/settings_dialog.h b/settings_dialog.h
new file mode 100644 (file)
index 0000000..aa7ab9d
--- /dev/null
@@ -0,0 +1,118 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 by Martin Steigemann and Wolfgang Bangerth
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#ifndef SETTINGSDIALOG_H
+#define SETTINGSDIALOG_H
+
+#include <QDialog>
+#include <QSettings>
+#include <QCheckBox>
+
+
+namespace dealii
+{
+/*! @addtogroup ParameterGui
+ *@{
+ */
+  namespace ParameterGui
+  {
+/**
+ * The SettingsDialog class implements a settings dialog for the parameterGUI.
+ * The dialog shows all available settings, and when the user clicks on 'OK'
+ * stores them in the QSettings object handed over in the constructor (which
+ * in turn stores them on disk to allow persistent settings).
+ *
+ * @ingroup ParameterGui
+ * @author Rene Gassmoeller, 2017
+ */
+    class SettingsDialog : public QDialog
+    {
+      Q_OBJECT
+
+      public:
+      /**
+       * Constructor
+       */
+      SettingsDialog (QSettings *settings,
+                      QWidget *parent = 0);
+
+      public slots:
+      /**
+       * Function that displays a font selection dialog and stores the result.
+       */
+      void selectFont();
+
+      /**
+       * Function that stores the checked state of the "Hide default" checkbox.
+       */
+      void changeHideDefault(int state);
+
+      /**
+       * Function that stores the new settings in the settings object
+       * (i.e. on disk).
+       */
+      void writeSettings();
+
+      /**
+       * Function that loads the settings from the settings file.
+       */
+      void loadSettings();
+
+      private:
+      /**
+       * This variable stores if the default values should be hidden. This
+       * might seem duplicative, since it is also stored in <tt>settings</tt>,
+       * but this variable stores the 'current' state of the checkbox,
+       * while <tt>settings</tt> is only updated after clicking OK.
+       */
+      bool hide_default_values;
+
+      /**
+       * The selected font as shown in the Change Font dialog.
+       */
+      QFont selected_font;
+
+      /**
+       * The <tt>Ok</tt> button.
+       */
+      QPushButton * ok;
+
+      /**
+       * The <tt>Cancel</tt> button.
+       */
+      QPushButton * cancel;
+
+      /**
+       * The <tt>Change font</tt> button.
+       */
+      QPushButton * change_font;
+
+      /**
+       * The checkbox<tt>Hide default values</tt>.
+       */
+      QCheckBox * hide_default;
+
+      /**
+       * An object for storing <tt>settings</tt> in a file.
+       */
+      QSettings * settings;
+    };
+  }
+/**@}*/
+}
+
+
+#endif

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.