Browse Source

Merge branch 'improve-ctkSettings-api'

* improve-ctkSettings-api:
  ctkSettingsPanel - Add defaultPropertyValue(), previousPropertyValue() and propertyValue() methods
Jean-Christophe Fillion-Robin 14 years ago
parent
commit
05b9a6e242

+ 180 - 3
Libs/Widgets/Testing/Cpp/ctkSettingsPanelTest1.cpp

@@ -21,8 +21,10 @@
 // Qt includes
 #include <QApplication>
 #include <QCheckBox>
+#include <QLineEdit>
 #include <QSettings>
 #include <QTimer>
+#include <QVariant>
 
 // CTK includes
 #include "ctkSettingsPanel.h"
@@ -31,6 +33,28 @@
 #include <cstdlib>
 #include <iostream>
 
+namespace
+{
+//-----------------------------------------------------------------------------
+class ctkSettingsPanelForTest : public ctkSettingsPanel
+{
+public:
+  QVariant myDefaultPropertyValue(const QString& key) const
+    {
+    return this->defaultPropertyValue(key);
+    }
+  QVariant myPreviousPropertyValue(const QString& key) const
+    {
+    return this->previousPropertyValue(key);
+    }
+  QVariant myPropertyValue(const QString& key) const
+    {
+    return this->propertyValue(key);
+    }
+};
+
+} // end of anonymous namespace
+
 //-----------------------------------------------------------------------------
 int ctkSettingsPanelTest1(int argc, char * argv [] )
 {
@@ -39,7 +63,7 @@ int ctkSettingsPanelTest1(int argc, char * argv [] )
   QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
   settings.clear();
 
-  ctkSettingsPanel settingsPanel;
+  ctkSettingsPanelForTest settingsPanel;
   settingsPanel.setSettings(&settings);
 
   QCheckBox* box = new QCheckBox(&settingsPanel);
@@ -47,19 +71,172 @@ int ctkSettingsPanelTest1(int argc, char * argv [] )
   settingsPanel.registerProperty("key 1", box, "checked",
                                   SIGNAL(toggled(bool)));
   
+  // Check settings value after a property is registered
   QVariant boxVal = settings.value("key 1");
   if (!boxVal.isValid() || boxVal.toBool() != false)
     {
-    std::cerr << "Saving to settings failed" << std::endl;
+    std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPreviousPropertyValue("key 1").toBool() != false)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPropertyValue("key 1").toBool() != false)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
     return EXIT_FAILURE;
     }
+  // Update value using the object/widget API
   box->setChecked(true);
+
+  // Check settings value after it has been updated using object/widget API
   boxVal = settings.value("key 1");
   if (!boxVal.isValid() || boxVal.toBool() != true)
     {
-    std::cerr << "Saving to settings failed" << std::endl;
+    std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPreviousPropertyValue("key 1").toBool() != false)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPropertyValue("key 1").toBool() != true)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+
+  QLineEdit* lineEdit = new QLineEdit("default", &settingsPanel);
+  settingsPanel.registerProperty("key 2", lineEdit, "text",
+                                  SIGNAL(textChanged(const QString&)));
+
+  // Check value after a property is registered
+  QVariant lineEditVal = settings.value("key 2");
+  if (!lineEditVal.isValid() || lineEditVal.toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Update value using the object/widget API
+  lineEdit->setText("first edit");
+
+  // Check settings value after it has been updated using object/widget API
+  lineEditVal = settings.value("key 2");
+  if (!lineEditVal.isValid() || lineEditVal.toString() != "first edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
     return EXIT_FAILURE;
     }
+  if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPropertyValue("key 2").toString() != "first edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Check settings value after applySettings() has been called
+  settingsPanel.applySettings();
+  lineEditVal = settings.value("key 2");
+  if (!lineEditVal.isValid() || lineEditVal.toString() != "first edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "first edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPropertyValue("key 2").toString() != "first edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Update value using the object/widget API
+  lineEdit->setText("second edit");
+
+  // Check settings value after it has been updated using object/widget API
+  lineEditVal = settings.value("key 2");
+  if (!lineEditVal.isValid() || lineEditVal.toString() != "second edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "first edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPropertyValue("key 2").toString() != "second edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Check settings value after applySettings() has been called
+  settingsPanel.applySettings();
+  lineEditVal = settings.value("key 2");
+  if (!lineEditVal.isValid() || lineEditVal.toString() != "second edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "second edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (settingsPanel.myPropertyValue("key 2").toString() != "second edit")
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
+    return EXIT_FAILURE;
+    }
+
 
   settingsPanel.show();
       

+ 33 - 0
Libs/Widgets/ctkSettingsPanel.cpp

@@ -256,6 +256,39 @@ void ctkSettingsPanel::registerProperty(const QString& key,
 }
 
 // --------------------------------------------------------------------------
+QVariant ctkSettingsPanel::defaultPropertyValue(const QString& key) const
+{
+  Q_D(const ctkSettingsPanel);
+  if (!d->Properties.contains(key))
+    {
+    return QVariant();
+    }
+  return d->Properties.value(key).DefaultValue;
+}
+
+// --------------------------------------------------------------------------
+QVariant ctkSettingsPanel::previousPropertyValue(const QString& key) const
+{
+  Q_D(const ctkSettingsPanel);
+  if (!d->Properties.contains(key))
+    {
+    return QVariant();
+    }
+  return d->Properties.value(key).PreviousValue;
+}
+
+// --------------------------------------------------------------------------
+QVariant ctkSettingsPanel::propertyValue(const QString& key) const
+{
+  Q_D(const ctkSettingsPanel);
+  if (!d->Properties.contains(key))
+    {
+    return QVariant();
+    }
+  return d->Properties.value(key).value();
+}
+
+// --------------------------------------------------------------------------
 void ctkSettingsPanel::applySettings()
 {
   Q_D(ctkSettingsPanel);

+ 14 - 1
Libs/Widgets/ctkSettingsPanel.h

@@ -46,7 +46,7 @@ public:
   QSettings* settings()const;
   void setSettings(QSettings* settings);
 
-  /// Add an entrie into the settings uniquely defined by the key name and the
+  /// Add an entry into the settings uniquely defined by the \a key name and the
   /// current value of the property.
   /// The property is then synchronized with the settings by observing the signal
   /// notification. Anytime the property is modified (the signal \a signal is
@@ -82,6 +82,19 @@ signals:
   /// Fired anytime a property is modified.
   void settingChanged(const QString& key, const QVariant& value);
 
+protected:
+  /// Return the default value of a property identified by its settings \a key
+  /// \sa registerProperty();
+  QVariant defaultPropertyValue(const QString& key) const;
+
+  /// Return the previous value of a property identified by its settings \a key
+  /// \sa registerProperty();
+  QVariant previousPropertyValue(const QString& key) const;
+
+  /// Return the value of a property identified by its settings \a key
+  /// \sa registerProperty();
+  QVariant propertyValue(const QString& key) const;
+
 protected slots:
   void updateSetting(const QString& key);