Prechádzať zdrojové kódy

Merge branches 'ctkSettings-reset-button-disabled-by-default', 'improve-ctkSettings-api' and 'ctkSettings-handle-empty-qstringlist'

* ctkSettings-reset-button-disabled-by-default:
  ctkSettingsDialog - Make sure Reset button is disabled by default.

* improve-ctkSettings-api:
  ctkSettingsPanel - Remove "acceptSettings()" and set "applySettings()' as virtual

* ctkSettings-handle-empty-qstringlist:
  Add missing test files
Jean-Christophe Fillion-Robin 14 rokov pred
rodič
commit
a4138ebd66

+ 61 - 0
Libs/Widgets/Testing/Cpp/ctkSettingsDialogTest1.cpp

@@ -21,6 +21,8 @@
 // Qt includes
 #include <QApplication>
 #include <QCheckBox>
+#include <QDialogButtonBox>
+#include <QPushButton>
 #include <QSettings>
 #include <QTimer>
 
@@ -41,8 +43,31 @@ int ctkSettingsDialogTest1(int argc, char * argv [] )
   settings.remove("key 1");
 
   ctkSettingsDialog settingsDialog;
+
+  // Get a reference to the DialogButtonBox
+  QDialogButtonBox * buttonBox = settingsDialog.findChild<QDialogButtonBox*>("SettingsButtonBox");
+  if (!buttonBox)
+    {
+    std::cerr << "Line " << __LINE__ << " - Failed to get a reference to the button box !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Reset button should be disabled by default
+  if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
+    {
+    std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
   settingsDialog.setSettings(&settings);
 
+  // Reset button should be disabled after settings are set
+  if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
+    {
+    std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
   ctkSettingsPanel* panel1 = new ctkSettingsPanel;
   settingsDialog.addPanel("Panel 1", panel1); 
   if (panel1->settings() != &settings)
@@ -73,7 +98,19 @@ int ctkSettingsDialogTest1(int argc, char * argv [] )
     std::cerr << "Saving to settings failed" << std::endl;
     return EXIT_FAILURE;
     }
+  // Reset button should be enabled after settings are modified
+  if (!buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
+    {
+    std::cerr << "Line " << __LINE__ << " - Reset button should be enabled !" << std::endl;
+    return EXIT_FAILURE;
+    }
   settingsDialog.resetSettings();
+  // Reset button should be enabled after settings are reset
+  if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
+    {
+    std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
+    return EXIT_FAILURE;
+    }
   boxVal = settings.value("key 1");
   if (!boxVal.isValid() || boxVal.toBool() != false)
     {
@@ -81,6 +118,30 @@ int ctkSettingsDialogTest1(int argc, char * argv [] )
     return EXIT_FAILURE;
     }
 
+  // Reset button should be enabled after settings are modified
+  box->setChecked(true);
+  boxVal = settings.value("key 1");
+  if (!boxVal.isValid() || boxVal.toBool() != true)
+    {
+    std::cerr << "Saving to settings failed" << std::endl;
+    return EXIT_FAILURE;
+    }
+  if (!buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
+    {
+    std::cerr << "Line " << __LINE__ << " - Reset button should be enabled !" << std::endl;
+    return EXIT_FAILURE;
+    }
+  // .. and reset button should be disabled if new settings object is set
+  QSettings settings2(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
+  settingsDialog.setSettings(&settings2);
+  if (buttonBox->button(QDialogButtonBox::Reset)->isEnabled())
+    {
+    std::cerr << "Line " << __LINE__ << " - Reset button should be disabled !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+
+
   settingsDialog.setCurrentPanel("Panel 4");
 
   settingsDialog.show();

+ 56 - 0
Libs/Widgets/Testing/Cpp/ctkSettingsPanelTest2.cpp

@@ -0,0 +1,56 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QApplication>
+#include <QSettings>
+
+// CTK includes
+#include "ctkSettingsPanel.h"
+#include "ctkSettingsPanelTest2Helper.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkSettingsPanelTest2(int argc, char * argv [] )
+{
+  QApplication app(argc, argv);
+  Q_UNUSED(app);
+
+  {
+    // When QSettings goes out of scope, we are the settings file is up-to-date
+    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
+    settings.clear();
+    settings.setValue("list", QVariant(QStringList()));
+  }
+
+  // Regression: Reading empty QStringList property from settings should be handled properly
+
+  ctkSettingsPanel settingsPanel;
+  ctkSettingsPanelTest2Helper * list = new ctkSettingsPanelTest2Helper(&settingsPanel);
+  settingsPanel.registerProperty("list", list, "list", SIGNAL(listChanged()));
+  QSettings settings2(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
+  settingsPanel.setSettings(&settings2);
+
+  return EXIT_SUCCESS;
+}
+

+ 37 - 0
Libs/Widgets/Testing/Cpp/ctkSettingsPanelTest2Helper.cpp

@@ -0,0 +1,37 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#include "ctkSettingsPanelTest2Helper.h"
+
+// --------------------------------------------------------------------------
+QStringList ctkSettingsPanelTest2Helper::list()const
+{
+  return this->List;
+}
+
+// --------------------------------------------------------------------------
+void ctkSettingsPanelTest2Helper::setList(const QStringList& alist)
+{
+  if (alist != this->List)
+    {
+    this->List = alist;
+    emit this->listChanged();
+    }
+}

+ 44 - 0
Libs/Widgets/Testing/Cpp/ctkSettingsPanelTest2Helper.h

@@ -0,0 +1,44 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkSettingsPanelTest2Helper_h
+#define __ctkSettingsPanelTest2Helper_h
+
+#include <QObject>
+#include <QStringList>
+
+// --------------------------------------------------------------------------
+class ctkSettingsPanelTest2Helper : public QObject
+{
+  Q_OBJECT
+  Q_PROPERTY(QStringList list READ list WRITE setList)
+public:
+  ctkSettingsPanelTest2Helper(QObject * parent = 0) : QObject(parent){}
+  QStringList list()const;
+public slots:
+  void setList(const QStringList& alist);
+signals:
+  void listChanged();
+private:
+  QStringList List;
+};
+    
+#endif
+

+ 5 - 12
Libs/Widgets/ctkSettingsDialog.cpp

@@ -71,6 +71,8 @@ void ctkSettingsDialogPrivate::init()
 
   this->setupUi(q);
 
+  this->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
+
   QObject::connect(this->SettingsTreeWidget,
     SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
     q, SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
@@ -130,6 +132,8 @@ void ctkSettingsDialog::setSettings(QSettings* settings)
 {
   Q_D(ctkSettingsDialog);
 
+  d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
+
   d->Settings = settings;
   foreach(ctkSettingsPanel* panel, d->Panels.values())
     {
@@ -215,7 +219,7 @@ ctkSettingsPanel* ctkSettingsDialog::panel(const QString& label)const
 // --------------------------------------------------------------------------
 void ctkSettingsDialog::accept()
 {
-  this->acceptSettings();
+  this->applySettings();
   this->Superclass::accept();
 }
 
@@ -227,17 +231,6 @@ void ctkSettingsDialog::reject()
 }
 
 // --------------------------------------------------------------------------
-void ctkSettingsDialog::acceptSettings()
-{
-  Q_D(ctkSettingsDialog);
-  foreach(ctkSettingsPanel* panel, d->Panels.values())
-    {
-    panel->acceptSettings();
-    }
-  d->SettingsButtonBox->button(QDialogButtonBox::Reset)->setEnabled(false);
-}
-
-// --------------------------------------------------------------------------
 void ctkSettingsDialog::applySettings()
 {
   Q_D(ctkSettingsDialog);

+ 0 - 1
Libs/Widgets/ctkSettingsDialog.h

@@ -62,7 +62,6 @@ public slots:
   void setCurrentPanel(ctkSettingsPanel* panel);
   void setCurrentPanel(const QString& label);
 
-  void acceptSettings();
   void applySettings();
   void resetSettings();
   void restoreDefaultSettings();

+ 0 - 6
Libs/Widgets/ctkSettingsPanel.cpp

@@ -256,12 +256,6 @@ void ctkSettingsPanel::registerProperty(const QString& key,
 }
 
 // --------------------------------------------------------------------------
-void ctkSettingsPanel::acceptSettings()
-{
-  this->applySettings();
-}
-
-// --------------------------------------------------------------------------
 void ctkSettingsPanel::applySettings()
 {
   Q_D(ctkSettingsPanel);

+ 1 - 4
Libs/Widgets/ctkSettingsPanel.h

@@ -65,13 +65,10 @@ public:
 
 public slots:
 
-  /// By default, it calls applySettings()
-  virtual void acceptSettings();
-
   /// Forget the old property values so next time resetSettings is called it
   /// will set the properties with the same values when applySettings() is
   /// called.
-  void applySettings();
+  virtual void applySettings();
 
   /// Restore all the properties with their values when applySettings() was
   /// called last (or their original values if applySettings was never called).