ctkSettingsPanelTest1.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QApplication>
  16. #include <QCheckBox>
  17. #include <QLineEdit>
  18. #include <QSettings>
  19. #include <QTimer>
  20. #include <QVariant>
  21. // CTK includes
  22. #include "ctkBooleanMapper.h"
  23. #include "ctkCoreTestingMacros.h"
  24. #include "ctkSettingsPanel.h"
  25. // STD includes
  26. #include <cstdlib>
  27. #include <iostream>
  28. namespace
  29. {
  30. //-----------------------------------------------------------------------------
  31. class ctkSettingsPanelForTest : public ctkSettingsPanel
  32. {
  33. public:
  34. QVariant myDefaultPropertyValue(const QString& key) const
  35. {
  36. return this->defaultPropertyValue(key);
  37. }
  38. QVariant myPreviousPropertyValue(const QString& key) const
  39. {
  40. return this->previousPropertyValue(key);
  41. }
  42. QVariant myPropertyValue(const QString& key) const
  43. {
  44. return this->propertyValue(key);
  45. }
  46. };
  47. } // end of anonymous namespace
  48. //-----------------------------------------------------------------------------
  49. int ctkSettingsPanelTest1(int argc, char * argv [] )
  50. {
  51. QApplication app(argc, argv);
  52. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
  53. settings.clear();
  54. ctkSettingsPanelForTest settingsPanel;
  55. settingsPanel.setSettings(&settings);
  56. //
  57. // QCheckBox
  58. //
  59. QCheckBox* box = new QCheckBox(&settingsPanel);
  60. settingsPanel.registerProperty("key 1", box, "checked",
  61. SIGNAL(toggled(bool)));
  62. // Check settings value after a property is registered
  63. QVariant boxVal = settings.value("key 1");
  64. CHECK_BOOL(boxVal.isValid(), true);
  65. CHECK_BOOL(boxVal.toBool(), false);
  66. CHECK_BOOL(settingsPanel.myPreviousPropertyValue("key 1").toBool(), false);
  67. CHECK_BOOL(settingsPanel.myPropertyValue("key 1").toBool(), false);
  68. // Update value using the object/widget API
  69. box->setChecked(true);
  70. // Check settings value after it has been updated using object/widget API
  71. boxVal = settings.value("key 1");
  72. CHECK_BOOL(boxVal.isValid(), true);
  73. CHECK_BOOL(boxVal.toBool(), true);
  74. CHECK_BOOL(settingsPanel.myPreviousPropertyValue("key 1").toBool(), false);
  75. CHECK_BOOL(settingsPanel.myPropertyValue("key 1").toBool(), true);
  76. //
  77. // QLineEdit
  78. //
  79. QLineEdit* lineEdit = new QLineEdit("default", &settingsPanel);
  80. settingsPanel.registerProperty("key 2", lineEdit, "text",
  81. SIGNAL(textChanged(QString)));
  82. // Check value after a property is registered
  83. QVariant lineEditVal = settings.value("key 2");
  84. CHECK_BOOL(lineEditVal.isValid(), true);
  85. CHECK_QSTRING(lineEditVal.toString(), QString("default"));
  86. CHECK_QSTRING(settingsPanel.myPreviousPropertyValue("key 2").toString(), QString("default"));
  87. CHECK_QSTRING(settingsPanel.myDefaultPropertyValue("key 2").toString(), QString("default"));
  88. CHECK_QSTRING(settingsPanel.myPropertyValue("key 2").toString(), QString("default"));
  89. // Update value using the object/widget API
  90. lineEdit->setText("first edit");
  91. // Check settings value after it has been updated using object/widget API
  92. lineEditVal = settings.value("key 2");
  93. CHECK_BOOL(lineEditVal.isValid(), true);
  94. CHECK_QSTRING(lineEditVal.toString(), QString("first edit"));
  95. CHECK_QSTRING(settingsPanel.myPreviousPropertyValue("key 2").toString(), QString("default"));
  96. CHECK_QSTRING(settingsPanel.myDefaultPropertyValue("key 2").toString(), QString("default"));
  97. CHECK_QSTRING(settingsPanel.myPropertyValue("key 2").toString(), QString("first edit"));
  98. // Check settings value after applySettings() has been called
  99. settingsPanel.applySettings();
  100. lineEditVal = settings.value("key 2");
  101. CHECK_BOOL(lineEditVal.isValid(), true);
  102. CHECK_QSTRING(lineEditVal.toString(), QString("first edit"));
  103. CHECK_QSTRING(settingsPanel.myPreviousPropertyValue("key 2").toString(), QString("first edit"));
  104. CHECK_QSTRING(settingsPanel.myDefaultPropertyValue("key 2").toString(), QString("default"));
  105. CHECK_QSTRING(settingsPanel.myPropertyValue("key 2").toString(), QString("first edit"));
  106. // Update value using the object/widget API
  107. lineEdit->setText("second edit");
  108. // Check settings value after it has been updated using object/widget API
  109. lineEditVal = settings.value("key 2");
  110. CHECK_BOOL(lineEditVal.isValid(), true);
  111. CHECK_QSTRING(lineEditVal.toString(), QString("second edit"));
  112. CHECK_QSTRING(settingsPanel.myPreviousPropertyValue("key 2").toString(), QString("first edit"));
  113. CHECK_QSTRING(settingsPanel.myDefaultPropertyValue("key 2").toString(), QString("default"));
  114. CHECK_QSTRING(settingsPanel.myPropertyValue("key 2").toString(), QString("second edit"));
  115. // Check settings value after applySettings() has been called
  116. settingsPanel.applySettings();
  117. lineEditVal = settings.value("key 2");
  118. CHECK_BOOL(lineEditVal.isValid(), true);
  119. CHECK_QSTRING(lineEditVal.toString(), QString("second edit"));
  120. CHECK_QSTRING(settingsPanel.myPreviousPropertyValue("key 2").toString(), QString("second edit"));
  121. CHECK_QSTRING(settingsPanel.myDefaultPropertyValue("key 2").toString(), QString("default"));
  122. CHECK_QSTRING(settingsPanel.myPropertyValue("key 2").toString(), QString("second edit"));
  123. //
  124. // QCheckBox + ctkBooleanMapper
  125. //
  126. box->setChecked(false);
  127. settingsPanel.registerProperty("key complement",
  128. new ctkBooleanMapper(box, "checked", SIGNAL(toggled(bool))),
  129. "complement",
  130. SIGNAL(complementChanged(bool)));
  131. // Check settings value after a property is registered
  132. boxVal = settings.value("key complement");
  133. CHECK_BOOL(boxVal.isValid(), true);
  134. CHECK_BOOL(boxVal.toBool(), true);
  135. CHECK_BOOL(settingsPanel.myPreviousPropertyValue("key complement").toBool(), true);
  136. CHECK_BOOL(settingsPanel.myPropertyValue("key complement").toBool(), true);
  137. // Update value using the object/widget API
  138. box->setChecked(true);
  139. // Check settings value after it has been updated using object/widget API
  140. boxVal = settings.value("key complement");
  141. CHECK_BOOL(boxVal.isValid(), true);
  142. CHECK_BOOL(boxVal.toBool(), false);
  143. CHECK_BOOL(settingsPanel.myPreviousPropertyValue("key complement").toBool(), true);
  144. CHECK_BOOL(settingsPanel.myPropertyValue("key complement").toBool(), false);
  145. settingsPanel.show();
  146. if (argc < 2 || QString(argv[1]) != "-I" )
  147. {
  148. QTimer::singleShot(200, &app, SLOT(quit()));
  149. }
  150. return app.exec();
  151. }