ctkLayoutManagerTest1.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 includse
  15. #include <QApplication>
  16. #include <QDebug>
  17. #include <QPushButton>
  18. #include <QTimer>
  19. // CTK includes
  20. #include "ctkSliderWidget.h"
  21. #include "ctkLayoutFactory.h"
  22. #include "ctkLayoutViewFactory.h"
  23. // STD includes
  24. #include <iostream>
  25. QString simpleLayout("<layout><item><view/></item></layout>");
  26. QString vboxLayout("<layout type=\"vertical\"><item><view/></item><item><view/></item></layout>");
  27. QString gridLayout(
  28. "<layout type=\"grid\">"
  29. " <item><view/></item>"
  30. " <item column=\"1\"><view/></item>"
  31. " <item row=\"1\"><view/></item>"
  32. " <item row=\"1\" column=\"1\"><view/></item>"
  33. " <item row=\"2\" colspan=\"2\"><view/></item>"
  34. " <item row=\"3\" rowspan=\"2\"><view/></item>"
  35. "</layout>");
  36. QString tabLayout(
  37. "<layout type=\"tab\">"
  38. " <item><view name=\"tab1\"/></item>"
  39. " <item><view name=\"tab2\"/></item>"
  40. " <item><view name=\"tab3\"/></item>"
  41. "</layout>");
  42. QString tabMultipleLayout(
  43. "<layout type=\"tab\">"
  44. " <item multiple=\"true\"><view name=\"tab1\"/></item>"
  45. "</layout>");
  46. QString nestedLayout(
  47. "<layout type=\"tab\">"
  48. " <item>"
  49. " <layout type=\"horizontal\">"
  50. " <item><view/></item>"
  51. " <item>"
  52. " <layout type=\"vertical\">"
  53. " <item><view/></item>"
  54. " <item><view/></item>"
  55. " <item>"
  56. " <layout type=\"grid\">"
  57. " <item row=\"0\" column=\"1\"><view/></item>"
  58. " <item row=\"1\" column=\"0\"><view/></item>"
  59. " </layout>"
  60. " </item>"
  61. " </layout>"
  62. " </item>"
  63. " <item><view/></item>"
  64. " </layout>"
  65. " </item>"
  66. " <item><view name=\"tab2\"/></item>"
  67. " <item><view name=\"tab3\"/></item>"
  68. "</layout>");
  69. /// \ingroup Widgets
  70. //-----------------------------------------------------------------------------
  71. int ctkLayoutManagerTest1(int argc, char * argv [] )
  72. {
  73. QApplication app(argc, argv);
  74. QWidget viewport;
  75. viewport.setWindowTitle("Simple layout");
  76. ctkLayoutFactory layoutManager;
  77. layoutManager.setViewport(&viewport);
  78. if (layoutManager.viewport() != &viewport)
  79. {
  80. std::cerr << __LINE__ << ": ctkLayoutManager::setViewport() failed."
  81. << std::endl;
  82. return EXIT_FAILURE;
  83. }
  84. ctkTemplateLayoutViewFactory<QPushButton>* pButtonInstanciator=
  85. new ctkTemplateLayoutViewFactory<QPushButton>(&viewport);
  86. layoutManager.registerViewFactory(pButtonInstanciator);
  87. QDomDocument simpleLayoutDoc("simplelayout");
  88. bool res = simpleLayoutDoc.setContent(simpleLayout);
  89. Q_ASSERT(res);
  90. QDomDocument vboxLayoutDoc("vboxlayout");
  91. res = vboxLayoutDoc.setContent(vboxLayout);
  92. Q_ASSERT(res);
  93. QDomDocument gridLayoutDoc("gridlayout");
  94. res = gridLayoutDoc.setContent(gridLayout);
  95. Q_ASSERT(res);
  96. QDomDocument tabLayoutDoc("tablayout");
  97. res = tabLayoutDoc.setContent(tabLayout);
  98. Q_ASSERT(res);
  99. QDomDocument tabMultipleLayoutDoc("tabMultiplelayout");
  100. res = tabMultipleLayoutDoc.setContent(tabMultipleLayout);
  101. Q_ASSERT(res);
  102. QDomDocument nestedLayoutDoc("nestedlayout");
  103. res = nestedLayoutDoc.setContent(nestedLayout);
  104. Q_ASSERT(res);
  105. Q_UNUSED(res);
  106. layoutManager.setLayout(simpleLayoutDoc);
  107. if (layoutManager.layout() != simpleLayoutDoc)
  108. {
  109. std::cerr << __LINE__ << ": ctkLayoutFactory::setLayout() failed."
  110. << std::endl;
  111. return EXIT_FAILURE;
  112. }
  113. viewport.show();
  114. QWidget vbox;
  115. vbox.setWindowTitle("Vertical Box Layout");
  116. ctkLayoutFactory vboxLayoutManager;
  117. vboxLayoutManager.registerViewFactory(pButtonInstanciator);
  118. vboxLayoutManager.setLayout(vboxLayoutDoc);
  119. vboxLayoutManager.setViewport(&vbox);
  120. vbox.show();
  121. QWidget grid;
  122. grid.setWindowTitle("Grid Layout");
  123. ctkLayoutFactory gridLayoutManager;
  124. gridLayoutManager.registerViewFactory(pButtonInstanciator);
  125. gridLayoutManager.setLayout(gridLayoutDoc);
  126. gridLayoutManager.setViewport(&grid);
  127. grid.show();
  128. QWidget tab;
  129. tab.setWindowTitle("Tab Layout");
  130. ctkLayoutFactory tabLayoutManager;
  131. tabLayoutManager.registerViewFactory(pButtonInstanciator);
  132. tabLayoutManager.setLayout(tabLayoutDoc);
  133. tabLayoutManager.setViewport(&tab);
  134. tab.show();
  135. QWidget nested;
  136. nested.setWindowTitle("Nested Layout");
  137. ctkLayoutFactory nestedLayoutManager;
  138. nestedLayoutManager.registerViewFactory(pButtonInstanciator);
  139. nestedLayoutManager.setLayout(nestedLayoutDoc);
  140. nestedLayoutManager.setViewport(&nested);
  141. nested.show();
  142. // TabToGrid
  143. QWidget tabToGrid;
  144. tabToGrid.setWindowTitle("Tab to Grid Layout");
  145. ctkTemplateLayoutViewFactory<ctkSliderWidget>* tabToGridInstanciator =
  146. new ctkTemplateLayoutViewFactory<ctkSliderWidget>(&viewport);
  147. ctkLayoutFactory tabToGridLayoutManager;
  148. tabToGridLayoutManager.registerViewFactory(tabToGridInstanciator);
  149. tabToGridLayoutManager.setLayout(tabLayoutDoc);
  150. tabToGridLayoutManager.setViewport(&tabToGrid);
  151. tabToGrid.show();
  152. QTimer::singleShot(200, &app, SLOT(quit()));
  153. app.exec();
  154. tabToGridLayoutManager.setLayout(gridLayoutDoc);
  155. QTimer::singleShot(200, &app, SLOT(quit()));
  156. app.exec();
  157. if (tabToGridInstanciator->registeredViews().count() != 6 ||
  158. tabToGridInstanciator->registeredViews()[0]->isHidden() ||
  159. tabToGridInstanciator->registeredViews()[1]->isHidden() ||
  160. tabToGridInstanciator->registeredViews()[2]->isHidden() ||
  161. tabToGridInstanciator->registeredViews()[3]->isHidden())
  162. {
  163. std::cout << __LINE__ << " TabToGrid: "
  164. << "ctkLayoutManager::setupLayout() failed to show/hide widgets"
  165. << tabToGridInstanciator->registeredViews().count() << " "
  166. << tabToGridInstanciator->registeredViews()[0]->isHidden() << " "
  167. << tabToGridInstanciator->registeredViews()[1]->isHidden() << " "
  168. << tabToGridInstanciator->registeredViews()[2]->isHidden() << " "
  169. << tabToGridInstanciator->registeredViews()[3]->isHidden() << std::endl;
  170. return EXIT_FAILURE;
  171. }
  172. // TabToSimple
  173. QWidget tabToSimple;
  174. tabToSimple.setWindowTitle("Tab to Simple Layout");
  175. ctkTemplateLayoutViewFactory<ctkSliderWidget>* tabToSimpleInstanciator =
  176. new ctkTemplateLayoutViewFactory<ctkSliderWidget>(&viewport);
  177. ctkLayoutFactory tabToSimpleLayoutManager;
  178. tabToSimpleLayoutManager.registerViewFactory(tabToSimpleInstanciator);
  179. //tabToSimpleLayoutManager.setLayout(gridLayoutDoc);
  180. tabToSimpleLayoutManager.setLayout(tabLayoutDoc);
  181. tabToSimpleLayoutManager.setViewport(&tabToSimple);
  182. tabToSimple.show();
  183. QTimer::singleShot(200, &app, SLOT(quit()));
  184. app.exec();
  185. tabToSimpleLayoutManager.setLayout(simpleLayoutDoc);
  186. QTimer::singleShot(200, &app, SLOT(quit()));
  187. app.exec();
  188. if (tabToSimpleInstanciator->registeredViews().count() != 3 ||
  189. tabToSimpleInstanciator->registeredViews()[0]->isHidden() ||
  190. tabToSimpleInstanciator->registeredViews()[1]->isVisible() ||
  191. tabToSimpleInstanciator->registeredViews()[2]->isVisible())
  192. {
  193. std::cout << __LINE__ << " TabToSimple: "
  194. << "ctkLayoutManager::setupLayout() failed to show/hide widgets"
  195. << tabToSimpleInstanciator->registeredViews().count() << " "
  196. << tabToSimpleInstanciator->registeredViews()[0]->isHidden() << " "
  197. << tabToSimpleInstanciator->registeredViews()[1]->isVisible() << " "
  198. << tabToSimpleInstanciator->registeredViews()[2]->isVisible() << std::endl;
  199. return EXIT_FAILURE;
  200. }
  201. // NestedToTab
  202. QWidget nestedToTab;
  203. nestedToTab.setWindowTitle("Nested to Tab Layout");
  204. ctkTemplateLayoutViewFactory<ctkSliderWidget>* nestedToTabInstanciator =
  205. new ctkTemplateLayoutViewFactory<ctkSliderWidget>(&viewport);
  206. ctkLayoutFactory nestedToTabLayoutManager;
  207. nestedToTabLayoutManager.registerViewFactory(nestedToTabInstanciator);
  208. nestedToTabLayoutManager.setLayout(nestedLayoutDoc);
  209. nestedToTabLayoutManager.setViewport(&nestedToTab);
  210. nestedToTab.show();
  211. QTimer::singleShot(200, &app, SLOT(quit()));
  212. app.exec();
  213. nestedToTabLayoutManager.setLayout(tabLayoutDoc);
  214. QTimer::singleShot(200, &app, SLOT(quit()));
  215. app.exec();
  216. if (nestedToTabInstanciator->registeredViews()[0]->isHidden() ||
  217. nestedToTabInstanciator->registeredViews()[1]->isVisible() ||
  218. nestedToTabInstanciator->registeredViews()[2]->isVisible() ||
  219. nestedToTabInstanciator->registeredViews()[3]->isVisible())
  220. {
  221. std::cout << __LINE__ << " NestedToTab: "
  222. << "ctkLayoutManager::setupLayout() failed to show/hide widgets"
  223. << nestedToTabInstanciator->registeredViews()[0]->isHidden() << " "
  224. << nestedToTabInstanciator->registeredViews()[1]->isVisible() << " "
  225. << nestedToTabInstanciator->registeredViews()[2]->isVisible() << " "
  226. << nestedToTabInstanciator->registeredViews()[3]->isVisible() << std::endl;
  227. return EXIT_FAILURE;
  228. }
  229. // Switch back to nested layout
  230. nestedToTabLayoutManager.setLayout(nestedLayoutDoc);
  231. QTimer::singleShot(200, &app, SLOT(quit()));
  232. app.exec();
  233. // Test that multiple="true" makes all cached views shown,
  234. // even if cached views are disabled.
  235. nestedToTabInstanciator->setUseCachedViews(false);
  236. nestedToTabLayoutManager.setLayout(tabMultipleLayoutDoc);
  237. if (nestedToTabInstanciator->registeredViews().count() != 2 * 4 ||
  238. nestedToTabInstanciator->registeredViews()[0]->isHidden() ||
  239. !nestedToTabInstanciator->registeredViews()[1]->isHidden() ||
  240. !nestedToTabInstanciator->registeredViews()[2]->isHidden())
  241. {
  242. std::cout << __LINE__ << " tabMultiple: "
  243. << "ctkLayoutManager::setupLayout() failed to show/hide widgets "
  244. << nestedToTabInstanciator->registeredViews().count();
  245. for (int i = 0; i < nestedToTabInstanciator->registeredViews().count(); i++)
  246. {
  247. std::cout << " " << nestedToTabInstanciator->registeredViews()[i]->isHidden();
  248. }
  249. std::cout << std::endl;
  250. return EXIT_FAILURE;
  251. }
  252. if (argc < 2 || QString(argv[1]) != "-I" )
  253. {
  254. QTimer::singleShot(200, &app, SLOT(quit()));
  255. }
  256. return app.exec();
  257. }