ctkLayoutManager.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <QDebug>
  16. #include <QGridLayout>
  17. #include <QHBoxLayout>
  18. #include <QLayout>
  19. #include <QTabWidget>
  20. #include <QVBoxLayout>
  21. #include <QWidget>
  22. // CTK includes
  23. #include "ctkLayoutManager.h"
  24. #include "ctkLayoutManager_p.h"
  25. //-----------------------------------------------------------------------------
  26. ctkLayoutManagerPrivate::ctkLayoutManagerPrivate(ctkLayoutManager& object)
  27. :q_ptr(&object)
  28. {
  29. this->Viewport = 0;
  30. }
  31. //-----------------------------------------------------------------------------
  32. ctkLayoutManagerPrivate::~ctkLayoutManagerPrivate()
  33. {
  34. }
  35. //-----------------------------------------------------------------------------
  36. void ctkLayoutManagerPrivate::init()
  37. {
  38. //Q_Q(ctkLayoutManager);
  39. }
  40. //-----------------------------------------------------------------------------
  41. void ctkLayoutManagerPrivate::clearLayout(QLayout* layout)
  42. {
  43. if (!layout)
  44. {
  45. return;
  46. }
  47. QLayoutItem * layoutItem = 0;
  48. while ((layoutItem = layout->takeAt(0)) != 0)
  49. {
  50. if (layoutItem->widget())
  51. {
  52. layoutItem->widget()->setVisible(false);
  53. layout->removeWidget(layoutItem->widget());
  54. }
  55. else if (layoutItem->layout())
  56. {
  57. this->clearLayout(layoutItem->layout());
  58. }
  59. }
  60. if (layout->parentWidget() && layout->parentWidget()->layout() == layout)
  61. {
  62. delete layout;
  63. }
  64. }
  65. //-----------------------------------------------------------------------------
  66. // ctkLayoutManager
  67. //-----------------------------------------------------------------------------
  68. ctkLayoutManager::ctkLayoutManager(QObject* parentObject)
  69. : QObject(parentObject)
  70. , d_ptr(new ctkLayoutManagerPrivate(*this))
  71. {
  72. Q_D(ctkLayoutManager);
  73. d->init();
  74. }
  75. //-----------------------------------------------------------------------------
  76. ctkLayoutManager::ctkLayoutManager(QWidget* viewport, QObject* parentObject)
  77. : QObject(parentObject)
  78. , d_ptr(new ctkLayoutManagerPrivate(*this))
  79. {
  80. Q_D(ctkLayoutManager);
  81. d->init();
  82. this->setViewport(viewport);
  83. }
  84. //-----------------------------------------------------------------------------
  85. ctkLayoutManager::ctkLayoutManager(ctkLayoutManagerPrivate* ptr,
  86. QWidget* viewport, QObject* parentObject)
  87. : QObject(parentObject)
  88. , d_ptr(ptr)
  89. {
  90. Q_D(ctkLayoutManager);
  91. d->init();
  92. this->setViewport(viewport);
  93. }
  94. //-----------------------------------------------------------------------------
  95. ctkLayoutManager::~ctkLayoutManager()
  96. {
  97. }
  98. //-----------------------------------------------------------------------------
  99. void ctkLayoutManager::refresh()
  100. {
  101. Q_D(ctkLayoutManager);
  102. if (!d->Viewport)
  103. {
  104. return;
  105. }
  106. // TODO: post an event on the event queue
  107. bool updatesEnabled = d->Viewport->updatesEnabled();
  108. d->Viewport->setUpdatesEnabled(false);
  109. this->clearLayout();
  110. this->setupLayout();
  111. d->Viewport->setUpdatesEnabled(updatesEnabled);
  112. }
  113. //-----------------------------------------------------------------------------
  114. void ctkLayoutManager::clearLayout()
  115. {
  116. Q_D(ctkLayoutManager);
  117. if (!d->Viewport)
  118. {
  119. return;
  120. }
  121. // TODO: post an event on the event queue
  122. d->clearLayout(d->Viewport->layout());
  123. }
  124. //-----------------------------------------------------------------------------
  125. void ctkLayoutManager::setupLayout()
  126. {
  127. Q_D(ctkLayoutManager);
  128. if (!d->Viewport || d->Layout.isNull() ||
  129. d->Layout.documentElement().isNull())
  130. {
  131. return;
  132. }
  133. d->Views.clear();
  134. Q_ASSERT(!d->Viewport->layout());
  135. QLayoutItem* layoutItem = this->processElement(
  136. d->Layout.documentElement());
  137. Q_ASSERT(layoutItem);
  138. QLayout* layout = layoutItem->layout();
  139. if (!layout)
  140. {
  141. QHBoxLayout* hboxLayout = new QHBoxLayout(0);
  142. hboxLayout->addItem(layoutItem);
  143. layout = hboxLayout;
  144. }
  145. // setting the layout to the widget will reparent all the 1 level widgets.
  146. // Unfortunately, it has the side effect of hiding
  147. // (testAttribute(Qt::WA_WState_Hidden)) the widgets that were already having
  148. // a parent (read doc for QWidget::isHidden()).
  149. // we then need to manually display the widgets again. Views is not probably
  150. // not the best list to use to retrieve the widgets to remove the hidden flag
  151. // it seems to fit the bill for the moment so we can keep using it.
  152. d->Viewport->setLayout(layout);
  153. foreach(QWidget* view, d->Views)
  154. {
  155. view->setHidden(false);
  156. }
  157. }
  158. //-----------------------------------------------------------------------------
  159. void ctkLayoutManager::setViewport(QWidget* viewport)
  160. {
  161. Q_D(ctkLayoutManager);
  162. if (viewport == d->Viewport)
  163. {
  164. return;
  165. }
  166. this->clearLayout();
  167. foreach(QWidget* view, d->Views)
  168. {
  169. if (view->parent() == d->Viewport)
  170. {
  171. view->setParent(0);
  172. // reparenting looses the visibility attribute and we want them hidden
  173. view->setVisible(false);
  174. }
  175. }
  176. d->Viewport = viewport;
  177. this->onViewportChanged();
  178. }
  179. //-----------------------------------------------------------------------------
  180. QWidget* ctkLayoutManager::viewport()const
  181. {
  182. Q_D(const ctkLayoutManager);
  183. return d->Viewport;
  184. }
  185. //-----------------------------------------------------------------------------
  186. void ctkLayoutManager::onViewportChanged()
  187. {
  188. this->refresh();
  189. }
  190. //-----------------------------------------------------------------------------
  191. void ctkLayoutManager::setLayout(const QDomDocument& newLayout)
  192. {
  193. Q_D(ctkLayoutManager);
  194. if (newLayout == d->Layout)
  195. {
  196. return;
  197. }
  198. d->Layout = newLayout;
  199. this->refresh();
  200. }
  201. //-----------------------------------------------------------------------------
  202. const QDomDocument ctkLayoutManager::layout()const
  203. {
  204. Q_D(const ctkLayoutManager);
  205. return d->Layout;
  206. }
  207. //-----------------------------------------------------------------------------
  208. QLayoutItem* ctkLayoutManager::processElement(QDomElement element)
  209. {
  210. Q_ASSERT(!element.isNull());
  211. if (element.tagName() == "layout")
  212. {
  213. return this->processLayoutElement(element);
  214. }
  215. else if (element.tagName() == "view")
  216. {
  217. return new QWidgetItem(this->processViewElement(element));
  218. }
  219. qDebug() << element.tagName() << element.text();
  220. Q_ASSERT(element.tagName() != "layout" && element.tagName() != "view");
  221. return 0;
  222. }
  223. //-----------------------------------------------------------------------------
  224. QLayoutItem* ctkLayoutManager::processLayoutElement(QDomElement layoutElement)
  225. {
  226. Q_ASSERT(layoutElement.tagName() == "layout");
  227. QLayoutItem* layoutItem = this->layoutFromXML(layoutElement);
  228. QLayout* layout = layoutItem->layout();
  229. if (layout)
  230. {
  231. layout->setContentsMargins(0,0,0,0);
  232. layout->setSpacing(0);
  233. }
  234. for(QDomNode child = layoutElement.firstChild();
  235. !child.isNull();
  236. child = child.nextSibling())
  237. {
  238. // ignore children that are not QDomElement
  239. if (child.toElement().isNull())
  240. {
  241. continue;
  242. }
  243. this->processItemElement(child.toElement(), layoutItem);
  244. }
  245. return layoutItem;
  246. }
  247. //-----------------------------------------------------------------------------
  248. QLayoutItem* ctkLayoutManager::layoutFromXML(QDomElement layoutElement)
  249. {
  250. Q_ASSERT(layoutElement.tagName() == "layout");
  251. QString type = layoutElement.attribute("type", "horizontal");
  252. if (type == "vertical")
  253. {
  254. return new QVBoxLayout();
  255. }
  256. else if (type == "horizontal")
  257. {
  258. return new QHBoxLayout();
  259. }
  260. else if (type == "grid")
  261. {
  262. return new QGridLayout();
  263. }
  264. else if (type == "tab")
  265. {
  266. return new QWidgetItem(new QTabWidget());
  267. }
  268. return 0;
  269. }
  270. //-----------------------------------------------------------------------------
  271. void ctkLayoutManager::processItemElement(QDomElement itemElement, QLayoutItem* layoutItem)
  272. {
  273. Q_ASSERT(itemElement.tagName() == "item");
  274. Q_ASSERT(itemElement.childNodes().count() == 1);
  275. QLayoutItem* childItem = this->processElement(itemElement.firstChild().toElement());
  276. QLayout* layout = layoutItem->layout();
  277. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(layout);
  278. QLayout* genericLayout = qobject_cast<QLayout*>(layout);
  279. QTabWidget* tabWidget = qobject_cast<QTabWidget*>(layoutItem->widget());
  280. if (gridLayout)
  281. {
  282. int row = itemElement.attribute("row", QString::number(0)).toInt();
  283. int col = itemElement.attribute("column", QString::number(0)).toInt();
  284. int rowSpan = itemElement.attribute("rowspan", QString::number(1)).toInt();
  285. int colSpan = itemElement.attribute("colspan", QString::number(1)).toInt();
  286. gridLayout->addItem(childItem, row, col, rowSpan, colSpan);
  287. }
  288. else if (genericLayout)
  289. {
  290. genericLayout->addItem(childItem);
  291. }
  292. else if (tabWidget)
  293. {
  294. QWidget* childWidget = childItem->widget();
  295. if (!childWidget)
  296. {
  297. childWidget = new QWidget();
  298. childWidget->setLayout(childItem->layout());
  299. }
  300. tabWidget->addTab(childWidget, itemElement.attribute("name"));
  301. }
  302. }
  303. //-----------------------------------------------------------------------------
  304. QWidget* ctkLayoutManager::processViewElement(QDomElement viewElement)
  305. {
  306. Q_D(ctkLayoutManager);
  307. Q_ASSERT(viewElement.tagName() == "view");
  308. QWidget* view = this->viewFromXML(viewElement);
  309. Q_ASSERT(view);
  310. view->setVisible(true);
  311. d->Views.insert(view);
  312. return view;
  313. }
  314. //-----------------------------------------------------------------------------
  315. QWidget* ctkLayoutManager::viewFromXML(QDomElement viewElement)
  316. {
  317. Q_UNUSED(viewElement);
  318. // default, for testing purpose. Must be reimplemented
  319. return new QWidget(0);
  320. }