ctkSettingsPanelTest1.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.commontk.org/LICENSE
  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 "ctkSettingsPanel.h"
  23. // STD includes
  24. #include <cstdlib>
  25. #include <iostream>
  26. namespace
  27. {
  28. //-----------------------------------------------------------------------------
  29. class ctkSettingsPanelForTest : public ctkSettingsPanel
  30. {
  31. public:
  32. QVariant myDefaultPropertyValue(const QString& key) const
  33. {
  34. return this->defaultPropertyValue(key);
  35. }
  36. QVariant myPreviousPropertyValue(const QString& key) const
  37. {
  38. return this->previousPropertyValue(key);
  39. }
  40. QVariant myPropertyValue(const QString& key) const
  41. {
  42. return this->propertyValue(key);
  43. }
  44. };
  45. } // end of anonymous namespace
  46. //-----------------------------------------------------------------------------
  47. int ctkSettingsPanelTest1(int argc, char * argv [] )
  48. {
  49. QApplication app(argc, argv);
  50. QSettings settings(QSettings::IniFormat, QSettings::UserScope, "Common ToolKit", "CTK");
  51. settings.clear();
  52. ctkSettingsPanelForTest settingsPanel;
  53. settingsPanel.setSettings(&settings);
  54. QCheckBox* box = new QCheckBox(&settingsPanel);
  55. settingsPanel.registerProperty("key 1", box, "checked",
  56. SIGNAL(toggled(bool)));
  57. // Check settings value after a property is registered
  58. QVariant boxVal = settings.value("key 1");
  59. if (!boxVal.isValid() || boxVal.toBool() != false)
  60. {
  61. std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
  62. return EXIT_FAILURE;
  63. }
  64. if (settingsPanel.myPreviousPropertyValue("key 1").toBool() != false)
  65. {
  66. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  67. return EXIT_FAILURE;
  68. }
  69. if (settingsPanel.myPropertyValue("key 1").toBool() != false)
  70. {
  71. std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
  72. return EXIT_FAILURE;
  73. }
  74. // Update value using the object/widget API
  75. box->setChecked(true);
  76. // Check settings value after it has been updated using object/widget API
  77. boxVal = settings.value("key 1");
  78. if (!boxVal.isValid() || boxVal.toBool() != true)
  79. {
  80. std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
  81. return EXIT_FAILURE;
  82. }
  83. if (settingsPanel.myPreviousPropertyValue("key 1").toBool() != false)
  84. {
  85. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  86. return EXIT_FAILURE;
  87. }
  88. if (settingsPanel.myPropertyValue("key 1").toBool() != true)
  89. {
  90. std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
  91. return EXIT_FAILURE;
  92. }
  93. QLineEdit* lineEdit = new QLineEdit("default", &settingsPanel);
  94. settingsPanel.registerProperty("key 2", lineEdit, "text",
  95. SIGNAL(textChanged(const QString&)));
  96. // Check value after a property is registered
  97. QVariant lineEditVal = settings.value("key 2");
  98. if (!lineEditVal.isValid() || lineEditVal.toString() != "default")
  99. {
  100. std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
  101. return EXIT_FAILURE;
  102. }
  103. if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "default")
  104. {
  105. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  106. return EXIT_FAILURE;
  107. }
  108. if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
  109. {
  110. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  111. return EXIT_FAILURE;
  112. }
  113. if (settingsPanel.myPropertyValue("key 2").toString() != "default")
  114. {
  115. std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
  116. return EXIT_FAILURE;
  117. }
  118. // Update value using the object/widget API
  119. lineEdit->setText("first edit");
  120. // Check settings value after it has been updated using object/widget API
  121. lineEditVal = settings.value("key 2");
  122. if (!lineEditVal.isValid() || lineEditVal.toString() != "first edit")
  123. {
  124. std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
  125. return EXIT_FAILURE;
  126. }
  127. if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "default")
  128. {
  129. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  130. return EXIT_FAILURE;
  131. }
  132. if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
  133. {
  134. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  135. return EXIT_FAILURE;
  136. }
  137. if (settingsPanel.myPropertyValue("key 2").toString() != "first edit")
  138. {
  139. std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
  140. return EXIT_FAILURE;
  141. }
  142. // Check settings value after applySettings() has been called
  143. settingsPanel.applySettings();
  144. lineEditVal = settings.value("key 2");
  145. if (!lineEditVal.isValid() || lineEditVal.toString() != "first edit")
  146. {
  147. std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
  148. return EXIT_FAILURE;
  149. }
  150. if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "first edit")
  151. {
  152. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  153. return EXIT_FAILURE;
  154. }
  155. if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
  156. {
  157. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  158. return EXIT_FAILURE;
  159. }
  160. if (settingsPanel.myPropertyValue("key 2").toString() != "first edit")
  161. {
  162. std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
  163. return EXIT_FAILURE;
  164. }
  165. // Update value using the object/widget API
  166. lineEdit->setText("second edit");
  167. // Check settings value after it has been updated using object/widget API
  168. lineEditVal = settings.value("key 2");
  169. if (!lineEditVal.isValid() || lineEditVal.toString() != "second edit")
  170. {
  171. std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
  172. return EXIT_FAILURE;
  173. }
  174. if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "first edit")
  175. {
  176. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  177. return EXIT_FAILURE;
  178. }
  179. if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
  180. {
  181. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  182. return EXIT_FAILURE;
  183. }
  184. if (settingsPanel.myPropertyValue("key 2").toString() != "second edit")
  185. {
  186. std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
  187. return EXIT_FAILURE;
  188. }
  189. // Check settings value after applySettings() has been called
  190. settingsPanel.applySettings();
  191. lineEditVal = settings.value("key 2");
  192. if (!lineEditVal.isValid() || lineEditVal.toString() != "second edit")
  193. {
  194. std::cerr << "Line " << __LINE__ << " - Saving to settings failed" << std::endl;
  195. return EXIT_FAILURE;
  196. }
  197. if (settingsPanel.myPreviousPropertyValue("key 2").toString() != "second edit")
  198. {
  199. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  200. return EXIT_FAILURE;
  201. }
  202. if (settingsPanel.myDefaultPropertyValue("key 2").toString() != "default")
  203. {
  204. std::cerr << "Line " << __LINE__ << " - Problem with previousPropertyValue()!" << std::endl;
  205. return EXIT_FAILURE;
  206. }
  207. if (settingsPanel.myPropertyValue("key 2").toString() != "second edit")
  208. {
  209. std::cerr << "Line " << __LINE__ << " - Problem with propertyValue()!" << std::endl;
  210. return EXIT_FAILURE;
  211. }
  212. settingsPanel.show();
  213. if (argc < 2 || QString(argv[1]) != "-I" )
  214. {
  215. QTimer::singleShot(200, &app, SLOT(quit()));
  216. }
  217. return app.exec();
  218. }