ctkLayoutManagerTest1.cpp 8.9 KB

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