ctkLayoutManager.cpp 12 KB

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