ctkLayoutManager.cpp 14 KB

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