ctkLayoutManager.cpp 12 KB

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