ctkLayoutManagerTest1.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 nestedLayout(
  43. "<layout type=\"tab\">"
  44. " <item>"
  45. " <layout type=\"horizontal\">"
  46. " <item><view/></item>"
  47. " <item>"
  48. " <layout type=\"vertical\">"
  49. " <item><view/></item>"
  50. " <item><view/></item>"
  51. " <item>"
  52. " <layout type=\"grid\">"
  53. " <item row=\"0\" column=\"1\"><view/></item>"
  54. " <item row=\"1\" column=\"0\"><view/></item>"
  55. " </layout>"
  56. " </item>"
  57. " </layout>"
  58. " </item>"
  59. " <item><view/></item>"
  60. " </layout>"
  61. " </item>"
  62. " <item><view name=\"tab2\"/></item>"
  63. " <item><view name=\"tab3\"/></item>"
  64. "</layout>");
  65. /// \ingroup Widgets
  66. //-----------------------------------------------------------------------------
  67. int ctkLayoutManagerTest1(int argc, char * argv [] )
  68. {
  69. QApplication app(argc, argv);
  70. QWidget viewport;
  71. viewport.setWindowTitle("Simple layout");
  72. ctkLayoutFactory layoutManager;
  73. layoutManager.setViewport(&viewport);
  74. if (layoutManager.viewport() != &viewport)
  75. {
  76. std::cerr << __LINE__ << ": ctkLayoutManager::setViewport() failed."
  77. << std::endl;
  78. return EXIT_FAILURE;
  79. }
  80. ctkTemplateLayoutViewFactory<QPushButton>* pButtonInstanciator=
  81. new ctkTemplateLayoutViewFactory<QPushButton>(&viewport);
  82. layoutManager.registerViewFactory(pButtonInstanciator);
  83. QDomDocument simpleLayoutDoc("simplelayout");
  84. bool res = simpleLayoutDoc.setContent(simpleLayout);
  85. Q_ASSERT(res);
  86. QDomDocument vboxLayoutDoc("vboxlayout");
  87. res = vboxLayoutDoc.setContent(vboxLayout);
  88. Q_ASSERT(res);
  89. QDomDocument gridLayoutDoc("gridlayout");
  90. res = gridLayoutDoc.setContent(gridLayout);
  91. Q_ASSERT(res);
  92. QDomDocument tabLayoutDoc("tablayout");
  93. res = tabLayoutDoc.setContent(tabLayout);
  94. Q_ASSERT(res);
  95. QDomDocument nestedLayoutDoc("nestedlayout");
  96. res = nestedLayoutDoc.setContent(nestedLayout);
  97. Q_ASSERT(res);
  98. layoutManager.setLayout(simpleLayoutDoc);
  99. if (layoutManager.layout() != simpleLayoutDoc)
  100. {
  101. std::cerr << __LINE__ << ": ctkLayoutFactory::setLayout() failed."
  102. << std::endl;
  103. return EXIT_FAILURE;
  104. }
  105. viewport.show();
  106. QWidget vbox;
  107. vbox.setWindowTitle("Vertical Box Layout");
  108. ctkLayoutFactory vboxLayoutManager;
  109. vboxLayoutManager.registerViewFactory(pButtonInstanciator);
  110. vboxLayoutManager.setLayout(vboxLayoutDoc);
  111. vboxLayoutManager.setViewport(&vbox);
  112. vbox.show();
  113. QWidget grid;
  114. grid.setWindowTitle("Grid Layout");
  115. ctkLayoutFactory gridLayoutManager;
  116. gridLayoutManager.registerViewFactory(pButtonInstanciator);
  117. gridLayoutManager.setLayout(gridLayoutDoc);
  118. gridLayoutManager.setViewport(&grid);
  119. grid.show();
  120. QWidget tab;
  121. tab.setWindowTitle("Tab Layout");
  122. ctkLayoutFactory tabLayoutManager;
  123. tabLayoutManager.registerViewFactory(pButtonInstanciator);
  124. tabLayoutManager.setLayout(tabLayoutDoc);
  125. tabLayoutManager.setViewport(&tab);
  126. tab.show();
  127. QWidget nested;
  128. nested.setWindowTitle("Nested Layout");
  129. ctkLayoutFactory nestedLayoutManager;
  130. nestedLayoutManager.registerViewFactory(pButtonInstanciator);
  131. nestedLayoutManager.setLayout(nestedLayoutDoc);
  132. nestedLayoutManager.setViewport(&nested);
  133. nested.show();
  134. // TabToGrid
  135. QWidget tabToGrid;
  136. tabToGrid.setWindowTitle("Tab to Grid Layout");
  137. ctkTemplateLayoutViewFactory<ctkSliderWidget>* tabToGridInstanciator =
  138. new ctkTemplateLayoutViewFactory<ctkSliderWidget>(&viewport);
  139. ctkLayoutFactory tabToGridLayoutManager;
  140. tabToGridLayoutManager.registerViewFactory(tabToGridInstanciator);
  141. tabToGridLayoutManager.setLayout(tabLayoutDoc);
  142. tabToGridLayoutManager.setViewport(&tabToGrid);
  143. tabToGrid.show();
  144. QTimer::singleShot(200, &app, SLOT(quit()));
  145. app.exec();
  146. tabToGridLayoutManager.setLayout(gridLayoutDoc);
  147. QTimer::singleShot(200, &app, SLOT(quit()));
  148. app.exec();
  149. if (tabToGridInstanciator->registeredViews().count() != 6 ||
  150. tabToGridInstanciator->registeredViews()[0]->isHidden() ||
  151. tabToGridInstanciator->registeredViews()[1]->isHidden() ||
  152. tabToGridInstanciator->registeredViews()[2]->isHidden() ||
  153. tabToGridInstanciator->registeredViews()[3]->isHidden())
  154. {
  155. std::cout << __LINE__ << " TabToGrid: "
  156. << "ctkLayoutManager::setupLayout() failed to show/hide widgets"
  157. << tabToGridInstanciator->registeredViews().count() << " "
  158. << tabToGridInstanciator->registeredViews()[0]->isHidden() << " "
  159. << tabToGridInstanciator->registeredViews()[1]->isHidden() << " "
  160. << tabToGridInstanciator->registeredViews()[2]->isHidden() << " "
  161. << tabToGridInstanciator->registeredViews()[3]->isHidden() << std::endl;
  162. return EXIT_FAILURE;
  163. }
  164. // TabToSimple
  165. QWidget tabToSimple;
  166. tabToSimple.setWindowTitle("Tab to Simple Layout");
  167. ctkTemplateLayoutViewFactory<ctkSliderWidget>* tabToSimpleInstanciator =
  168. new ctkTemplateLayoutViewFactory<ctkSliderWidget>(&viewport);
  169. ctkLayoutFactory tabToSimpleLayoutManager;
  170. tabToSimpleLayoutManager.registerViewFactory(tabToSimpleInstanciator);
  171. //tabToSimpleLayoutManager.setLayout(gridLayoutDoc);
  172. tabToSimpleLayoutManager.setLayout(tabLayoutDoc);
  173. tabToSimpleLayoutManager.setViewport(&tabToSimple);
  174. tabToSimple.show();
  175. QTimer::singleShot(200, &app, SLOT(quit()));
  176. app.exec();
  177. tabToSimpleLayoutManager.setLayout(simpleLayoutDoc);
  178. QTimer::singleShot(200, &app, SLOT(quit()));
  179. app.exec();
  180. if (tabToSimpleInstanciator->registeredViews().count() != 3 ||
  181. tabToSimpleInstanciator->registeredViews()[0]->isHidden() ||
  182. tabToSimpleInstanciator->registeredViews()[1]->isVisible() ||
  183. tabToSimpleInstanciator->registeredViews()[2]->isVisible())
  184. {
  185. std::cout << __LINE__ << " TabToSimple: "
  186. << "ctkLayoutManager::setupLayout() failed to show/hide widgets"
  187. << tabToSimpleInstanciator->registeredViews().count() << " "
  188. << tabToSimpleInstanciator->registeredViews()[0]->isHidden() << " "
  189. << tabToSimpleInstanciator->registeredViews()[1]->isVisible() << " "
  190. << tabToSimpleInstanciator->registeredViews()[2]->isVisible() << std::endl;
  191. return EXIT_FAILURE;
  192. }
  193. // NestedToTab
  194. QWidget nestedToTab;
  195. nestedToTab.setWindowTitle("Nested to Tab Layout");
  196. ctkTemplateLayoutViewFactory<ctkSliderWidget>* nestedToTabInstanciator =
  197. new ctkTemplateLayoutViewFactory<ctkSliderWidget>(&viewport);
  198. ctkLayoutFactory nestedToTabLayoutManager;
  199. nestedToTabLayoutManager.registerViewFactory(nestedToTabInstanciator);
  200. nestedToTabLayoutManager.setLayout(nestedLayoutDoc);
  201. nestedToTabLayoutManager.setViewport(&nestedToTab);
  202. nestedToTab.show();
  203. QTimer::singleShot(200, &app, SLOT(quit()));
  204. app.exec();
  205. nestedToTabLayoutManager.setLayout(tabLayoutDoc);
  206. QTimer::singleShot(200, &app, SLOT(quit()));
  207. app.exec();
  208. if (nestedToTabInstanciator->registeredViews()[0]->isHidden() ||
  209. nestedToTabInstanciator->registeredViews()[1]->isVisible() ||
  210. nestedToTabInstanciator->registeredViews()[2]->isVisible() ||
  211. nestedToTabInstanciator->registeredViews()[3]->isVisible())
  212. {
  213. std::cout << __LINE__ << " NestedToTab: "
  214. << "ctkLayoutManager::setupLayout() failed to show/hide widgets"
  215. << nestedToTabInstanciator->registeredViews()[0]->isHidden() << " "
  216. << nestedToTabInstanciator->registeredViews()[1]->isVisible() << " "
  217. << nestedToTabInstanciator->registeredViews()[2]->isVisible() << " "
  218. << nestedToTabInstanciator->registeredViews()[3]->isVisible() << std::endl;
  219. return EXIT_FAILURE;
  220. }
  221. if (argc < 2 || QString(argv[1]) != "-I" )
  222. {
  223. QTimer::singleShot(200, &app, SLOT(quit()));
  224. }
  225. return app.exec();
  226. }