ctkSettingsPanelTest1.cpp 10 KB

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