ctkLayoutManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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::clearWidget(QWidget* widget, QLayout* parentLayout)
  44. {
  45. if (!this->LayoutWidgets.contains(widget))
  46. {
  47. widget->setVisible(false);
  48. if (parentLayout)
  49. {
  50. parentLayout->removeWidget(widget);
  51. }
  52. widget->setParent(this->Viewport);
  53. }
  54. else
  55. {
  56. if (widget->layout())
  57. {
  58. this->clearLayout(widget->layout());
  59. }
  60. else if (qobject_cast<QTabWidget*>(widget))
  61. {
  62. QTabWidget* tabWidget = qobject_cast<QTabWidget*>(widget);
  63. while (tabWidget->count())
  64. {
  65. QWidget* page = tabWidget->widget(0);
  66. this->clearWidget(page);
  67. }
  68. }
  69. else if (qobject_cast<QSplitter*>(widget))
  70. {
  71. QSplitter* splitterWidget = qobject_cast<QSplitter*>(widget);
  72. // Hide the splitter before removing pages. Removing pages
  73. // would resize the other children if the splitter is visible.
  74. // We don't want to resize the children as we are trying to clear the
  75. // layout, it would set intermediate sizes to widgets.
  76. // See QSplitter::childEvent
  77. splitterWidget->setVisible(false);
  78. while (splitterWidget->count())
  79. {
  80. QWidget* page = splitterWidget->widget(0);
  81. this->clearWidget(page);
  82. }
  83. }
  84. this->LayoutWidgets.remove(widget);
  85. if (parentLayout)
  86. {
  87. parentLayout->removeWidget(widget);
  88. }
  89. delete widget;
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. void ctkLayoutManagerPrivate::clearLayout(QLayout* layout)
  94. {
  95. if (!layout)
  96. {
  97. return;
  98. }
  99. layout->setEnabled(false);
  100. QLayoutItem * layoutItem = 0;
  101. while ((layoutItem = layout->itemAt(0)) != 0)
  102. {
  103. if (layoutItem->widget())
  104. {
  105. this->clearWidget(layoutItem->widget(), layout);
  106. }
  107. else if (layoutItem->layout())
  108. {
  109. /// Warning, this might delete the layouts of "custom" widgets, not just
  110. /// the layouts generated by ctkLayoutManager
  111. this->clearLayout(layoutItem->layout());
  112. layout->removeItem(layoutItem);
  113. delete layoutItem;
  114. }
  115. }
  116. if (layout->parentWidget() && layout->parentWidget()->layout() == layout)
  117. {
  118. delete layout;
  119. }
  120. }
  121. //-----------------------------------------------------------------------------
  122. // ctkLayoutManager
  123. //-----------------------------------------------------------------------------
  124. ctkLayoutManager::ctkLayoutManager(QObject* parentObject)
  125. : QObject(parentObject)
  126. , d_ptr(new ctkLayoutManagerPrivate(*this))
  127. {
  128. Q_D(ctkLayoutManager);
  129. d->init();
  130. }
  131. //-----------------------------------------------------------------------------
  132. ctkLayoutManager::ctkLayoutManager(QWidget* viewport, QObject* parentObject)
  133. : QObject(parentObject)
  134. , d_ptr(new ctkLayoutManagerPrivate(*this))
  135. {
  136. Q_D(ctkLayoutManager);
  137. d->init();
  138. this->setViewport(viewport);
  139. }
  140. //-----------------------------------------------------------------------------
  141. ctkLayoutManager::ctkLayoutManager(ctkLayoutManagerPrivate* ptr,
  142. QWidget* viewport, QObject* parentObject)
  143. : QObject(parentObject)
  144. , d_ptr(ptr)
  145. {
  146. Q_D(ctkLayoutManager);
  147. d->init();
  148. this->setViewport(viewport);
  149. }
  150. //-----------------------------------------------------------------------------
  151. ctkLayoutManager::~ctkLayoutManager()
  152. {
  153. }
  154. //-----------------------------------------------------------------------------
  155. int ctkLayoutManager::spacing()const
  156. {
  157. Q_D(const ctkLayoutManager);
  158. return d->Spacing;
  159. }
  160. //-----------------------------------------------------------------------------
  161. void ctkLayoutManager::setSpacing(int spacing)
  162. {
  163. Q_D(ctkLayoutManager);
  164. d->Spacing = spacing;
  165. this->refresh();
  166. }
  167. //-----------------------------------------------------------------------------
  168. void ctkLayoutManager::refresh()
  169. {
  170. Q_D(ctkLayoutManager);
  171. if (!d->Viewport)
  172. {
  173. return;
  174. }
  175. // TODO: post an event on the event queue
  176. bool updatesEnabled = d->Viewport->updatesEnabled();
  177. d->Viewport->setUpdatesEnabled(false);
  178. this->clearLayout();
  179. this->setupLayout();
  180. d->Viewport->setUpdatesEnabled(updatesEnabled);
  181. }
  182. //-----------------------------------------------------------------------------
  183. void ctkLayoutManager::clearLayout()
  184. {
  185. Q_D(ctkLayoutManager);
  186. if (!d->Viewport)
  187. {
  188. return;
  189. }
  190. // TODO: post an event on the event queue
  191. d->clearLayout(d->Viewport->layout());
  192. Q_ASSERT(d->LayoutWidgets.size() == 0);
  193. }
  194. //-----------------------------------------------------------------------------
  195. void ctkLayoutManager::setupLayout()
  196. {
  197. Q_D(ctkLayoutManager);
  198. if (!d->Viewport || d->Layout.isNull() ||
  199. d->Layout.documentElement().isNull())
  200. {
  201. return;
  202. }
  203. d->Views.clear();
  204. d->LayoutWidgets.clear();
  205. Q_ASSERT(!d->Viewport->layout());
  206. QLayoutItem* layoutItem = this->processElement(
  207. d->Layout.documentElement());
  208. Q_ASSERT(layoutItem);
  209. QLayout* layout = layoutItem->layout();
  210. if (!layout)
  211. {
  212. QHBoxLayout* hboxLayout = new QHBoxLayout(0);
  213. hboxLayout->setContentsMargins(0, 0, 0, 0);
  214. hboxLayout->addItem(layoutItem);
  215. layout = hboxLayout;
  216. }
  217. d->Viewport->setLayout(layout);
  218. }
  219. //-----------------------------------------------------------------------------
  220. void ctkLayoutManager::setViewport(QWidget* viewport)
  221. {
  222. Q_D(ctkLayoutManager);
  223. if (viewport == d->Viewport)
  224. {
  225. return;
  226. }
  227. this->clearLayout();
  228. foreach(QWidget* view, d->Views)
  229. {
  230. if (view->parent() == d->Viewport)
  231. {
  232. view->setParent(0);
  233. // reparenting looses the visibility attribute and we want them hidden
  234. view->setVisible(false);
  235. }
  236. }
  237. d->Viewport = viewport;
  238. this->onViewportChanged();
  239. }
  240. //-----------------------------------------------------------------------------
  241. QWidget* ctkLayoutManager::viewport()const
  242. {
  243. Q_D(const ctkLayoutManager);
  244. return d->Viewport;
  245. }
  246. //-----------------------------------------------------------------------------
  247. void ctkLayoutManager::onViewportChanged()
  248. {
  249. this->refresh();
  250. }
  251. //-----------------------------------------------------------------------------
  252. void ctkLayoutManager::setLayout(const QDomDocument& newLayout)
  253. {
  254. Q_D(ctkLayoutManager);
  255. if (newLayout == d->Layout)
  256. {
  257. return;
  258. }
  259. d->Layout = newLayout;
  260. this->refresh();
  261. }
  262. //-----------------------------------------------------------------------------
  263. const QDomDocument ctkLayoutManager::layout()const
  264. {
  265. Q_D(const ctkLayoutManager);
  266. return d->Layout;
  267. }
  268. //-----------------------------------------------------------------------------
  269. QLayoutItem* ctkLayoutManager::processElement(QDomElement element)
  270. {
  271. Q_ASSERT(!element.isNull());
  272. if (element.tagName() == "layout")
  273. {
  274. return this->processLayoutElement(element);
  275. }
  276. else if (element.tagName() == "view")
  277. {
  278. return this->widgetItemFromXML(element);
  279. }
  280. Q_ASSERT(element.tagName() != "layout" && element.tagName() != "view");
  281. return 0;
  282. }
  283. //-----------------------------------------------------------------------------
  284. QLayoutItem* ctkLayoutManager::processLayoutElement(QDomElement layoutElement)
  285. {
  286. Q_D(ctkLayoutManager);
  287. Q_ASSERT(layoutElement.tagName() == "layout");
  288. QLayoutItem* layoutItem = this->layoutFromXML(layoutElement);
  289. QLayout* layout = layoutItem->layout();
  290. QWidget* widget = layoutItem->widget();
  291. if (layout)
  292. {
  293. layout->setContentsMargins(0,0,0,0);
  294. layout->setSpacing(d->Spacing);
  295. }
  296. else if (widget)
  297. {
  298. // mark the widget as ctkLayoutManager own widget so that it can be
  299. // deleted when the layout is cleared.
  300. d->LayoutWidgets << widget;
  301. }
  302. for(QDomNode child = layoutElement.firstChild();
  303. !child.isNull();
  304. child = child.nextSibling())
  305. {
  306. // ignore children that are not QDomElement
  307. if (child.toElement().isNull())
  308. {
  309. continue;
  310. }
  311. this->processItemElement(child.toElement(), layoutItem);
  312. }
  313. return layoutItem;
  314. }
  315. //-----------------------------------------------------------------------------
  316. QLayoutItem* ctkLayoutManager::layoutFromXML(QDomElement layoutElement)
  317. {
  318. Q_ASSERT(layoutElement.tagName() == "layout");
  319. QString type = layoutElement.attribute("type", "horizontal");
  320. bool split = layoutElement.attribute("split", "false") == "true";
  321. if (type == "vertical")
  322. {
  323. if (split)
  324. {
  325. return new QWidgetItem(new QSplitter(Qt::Vertical));
  326. }
  327. return new QVBoxLayout();
  328. }
  329. else if (type == "horizontal")
  330. {
  331. if (split)
  332. {
  333. return new QWidgetItem(new QSplitter(Qt::Horizontal));
  334. }
  335. return new QHBoxLayout();
  336. }
  337. else if (type == "grid")
  338. {
  339. return new QGridLayout();
  340. }
  341. else if (type == "tab")
  342. {
  343. return new QWidgetItem(new QTabWidget());
  344. }
  345. return 0;
  346. }
  347. //-----------------------------------------------------------------------------
  348. void ctkLayoutManager::processItemElement(QDomElement itemElement, QLayoutItem* layoutItem)
  349. {
  350. Q_ASSERT(itemElement.tagName() == "item");
  351. Q_ASSERT(itemElement.childNodes().count() == 1);
  352. bool multiple = itemElement.attribute("multiple", "false") == "true";
  353. QList<QLayoutItem*> childrenItem;
  354. if (multiple)
  355. {
  356. childrenItem = this->widgetItemsFromXML(itemElement.firstChild().toElement());
  357. }
  358. else
  359. {
  360. childrenItem << this->processElement(itemElement.firstChild().toElement());
  361. }
  362. foreach(QLayoutItem* item, childrenItem)
  363. {
  364. this->addChildItemToLayout(itemElement, item, layoutItem);
  365. }
  366. }
  367. //-----------------------------------------------------------------------------
  368. void ctkLayoutManager::addChildItemToLayout(QDomElement itemElement, QLayoutItem* childItem, QLayoutItem* layoutItem)
  369. {
  370. Q_D(ctkLayoutManager);
  371. Q_ASSERT(childItem);
  372. QString itemName = itemElement.attribute("name");
  373. if (itemName.isEmpty() && childItem->widget())
  374. {
  375. itemName = childItem->widget()->windowTitle();
  376. }
  377. QLayout* layout = layoutItem->layout();
  378. QGridLayout* gridLayout = qobject_cast<QGridLayout*>(layout);
  379. QLayout* genericLayout = qobject_cast<QLayout*>(layout);
  380. QTabWidget* tabWidget = qobject_cast<QTabWidget*>(layoutItem->widget());
  381. QSplitter* splitter = qobject_cast<QSplitter*>(layoutItem->widget());
  382. if (gridLayout)
  383. {
  384. int row = itemElement.attribute("row", QString::number(0)).toInt();
  385. int col = itemElement.attribute("column", QString::number(0)).toInt();
  386. int rowSpan = itemElement.attribute("rowspan", QString::number(1)).toInt();
  387. int colSpan = itemElement.attribute("colspan", QString::number(1)).toInt();
  388. gridLayout->addItem(childItem, row, col, rowSpan, colSpan);
  389. }
  390. else if (genericLayout)
  391. {
  392. genericLayout->addItem(childItem);
  393. }
  394. else if (tabWidget || splitter)
  395. {
  396. QWidget* childWidget = childItem->widget();
  397. if (!childWidget)
  398. {
  399. childWidget = new QWidget();
  400. d->LayoutWidgets << childWidget;
  401. childWidget->setLayout(childItem->layout());
  402. }
  403. if (tabWidget)
  404. {
  405. tabWidget->addTab(childWidget, itemName);
  406. }
  407. else
  408. {
  409. splitter->addWidget(childWidget);
  410. }
  411. }
  412. }
  413. //-----------------------------------------------------------------------------
  414. QWidgetItem* ctkLayoutManager::widgetItemFromXML(QDomElement viewElement)
  415. {
  416. Q_ASSERT(viewElement.tagName() == "view");
  417. QWidget* view = this->viewFromXML(viewElement);
  418. this->setupView(viewElement, view);
  419. return new QWidgetItem(view);
  420. }
  421. //-----------------------------------------------------------------------------
  422. void ctkLayoutManager::setupView(QDomElement viewElement, QWidget* view)
  423. {
  424. Q_UNUSED(viewElement);
  425. Q_D(ctkLayoutManager);
  426. Q_ASSERT(view);
  427. view->setVisible(true);
  428. d->Views.insert(view);
  429. }
  430. //-----------------------------------------------------------------------------
  431. QList<QLayoutItem*> ctkLayoutManager::widgetItemsFromXML(QDomElement viewElement)
  432. {
  433. Q_ASSERT(viewElement.tagName() == "view");
  434. QList<QLayoutItem*> res;
  435. QList<QWidget*> views = this->viewsFromXML(viewElement);
  436. Q_ASSERT(views.count());
  437. foreach(QWidget* view, views)
  438. {
  439. this->setupView(viewElement, view);
  440. res << new QWidgetItem(view);
  441. }
  442. return res;
  443. }
  444. //-----------------------------------------------------------------------------
  445. QWidget* ctkLayoutManager::viewFromXML(QDomElement viewElement)
  446. {
  447. Q_UNUSED(viewElement);
  448. // default, for testing purpose. Must be reimplemented
  449. return new QWidget(0);
  450. }
  451. //-----------------------------------------------------------------------------
  452. QList<QWidget*> ctkLayoutManager::viewsFromXML(QDomElement viewElement)
  453. {
  454. QList<QWidget*> res;
  455. res << this->viewFromXML(viewElement);
  456. return res;
  457. }