ctkFlowLayout.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 <QStyle>
  17. #include <QWidget>
  18. // CTK includes
  19. #include "ctkFlowLayout.h"
  20. #include "ctkLogger.h"
  21. // STD includes
  22. #include <cmath>
  23. static ctkLogger logger("org.commontk.libs.widgets.ctkFlowLayout");
  24. //-----------------------------------------------------------------------------
  25. class ctkFlowLayoutPrivate
  26. {
  27. Q_DECLARE_PUBLIC(ctkFlowLayout);
  28. protected:
  29. ctkFlowLayout* const q_ptr;
  30. public:
  31. ctkFlowLayoutPrivate(ctkFlowLayout& object);
  32. void init();
  33. void deleteAll();
  34. int doLayout(const QRect &rect, bool testOnly) const;
  35. int smartSpacing(QStyle::PixelMetric pm) const;
  36. QSize maxSizeHint(int* visibleItemsCount = 0)const;
  37. QList<QLayoutItem *> ItemList;
  38. Qt::Orientation Orientation;
  39. int HorizontalSpacing;
  40. int VerticalSpacing;
  41. bool AlignItems;
  42. Qt::Orientations PreferredDirections;
  43. };
  44. // --------------------------------------------------------------------------
  45. ctkFlowLayoutPrivate::ctkFlowLayoutPrivate(ctkFlowLayout& object)
  46. :q_ptr(&object)
  47. {
  48. this->HorizontalSpacing = -1;
  49. this->VerticalSpacing = -1;
  50. this->Orientation = Qt::Horizontal;
  51. this->PreferredDirections = Qt::Horizontal | Qt::Vertical;
  52. this->AlignItems = true;
  53. }
  54. // --------------------------------------------------------------------------
  55. void ctkFlowLayoutPrivate::init()
  56. {
  57. }
  58. // --------------------------------------------------------------------------
  59. void ctkFlowLayoutPrivate::deleteAll()
  60. {
  61. Q_Q(ctkFlowLayout);
  62. foreach(QLayoutItem* item, this->ItemList)
  63. {
  64. delete item;
  65. }
  66. this->ItemList.clear();
  67. q->invalidate();
  68. }
  69. // --------------------------------------------------------------------------
  70. QSize ctkFlowLayoutPrivate::maxSizeHint(int *visibleItemsCount)const
  71. {
  72. if (visibleItemsCount)
  73. {
  74. *visibleItemsCount = 0;
  75. }
  76. QSize maxItemSize;
  77. foreach (QLayoutItem* item, this->ItemList)
  78. {
  79. QWidget *wid = item->widget();
  80. if (wid && !wid->isVisibleTo(wid->parentWidget()))
  81. {// don't take into account hidden items
  82. continue;
  83. }
  84. maxItemSize.rwidth() = qMax(item->sizeHint().width(), maxItemSize.width());
  85. maxItemSize.rheight() = qMax(item->sizeHint().height(), maxItemSize.height());
  86. if (visibleItemsCount)
  87. {
  88. ++*visibleItemsCount;
  89. }
  90. }
  91. return maxItemSize;
  92. }
  93. // --------------------------------------------------------------------------
  94. int ctkFlowLayoutPrivate::doLayout(const QRect& rect, bool testOnly)const
  95. {
  96. Q_Q(const ctkFlowLayout);
  97. int left, top, right, bottom;
  98. q->getContentsMargins(&left, &top, &right, &bottom);
  99. QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
  100. QPoint pos = QPoint(effectiveRect.x(), effectiveRect.y());
  101. int length = 0;
  102. int max = this->Orientation == Qt::Horizontal ?
  103. effectiveRect.right() + 1 : effectiveRect.bottom() + 1;
  104. int maxX = left + right;
  105. int maxY = top + bottom;
  106. QSize maxItemSize = this->AlignItems ? this->maxSizeHint() : QSize();
  107. int spaceX = q->horizontalSpacing();
  108. int spaceY = q->verticalSpacing();
  109. int space = this->Orientation == Qt::Horizontal ? spaceX : spaceY;
  110. QLayoutItem* previousItem = NULL;
  111. foreach (QLayoutItem* item, this->ItemList)
  112. {
  113. QWidget *wid = item->widget();
  114. if (wid && wid->isHidden())
  115. {
  116. continue;
  117. }
  118. QPoint next = pos;
  119. QSize itemSize = this->AlignItems ? maxItemSize : item->sizeHint();
  120. if (this->Orientation == Qt::Horizontal)
  121. {
  122. next += QPoint(itemSize.width() + spaceX, 0);
  123. }
  124. else
  125. {
  126. next += QPoint(0, itemSize.height() + spaceY);
  127. }
  128. if (this->Orientation == Qt::Horizontal &&
  129. (next.x() - space > max) && length > 0)
  130. {
  131. // If justified alignment is requested then expand the last item in the row
  132. // to fill the available space. If the width of items were highly varying then
  133. // expanding width of all items proportionally could provide visually more
  134. // apealing results, but expanding only the last item was much simpler to implement,
  135. // and works very well most of the cases.
  136. if (!testOnly && q->alignment() == Qt::AlignJustify && previousItem)
  137. {
  138. QRect geometry = previousItem->geometry();
  139. geometry.adjust(0, 0, max + space - pos.x(), 0);
  140. previousItem->setGeometry(geometry);
  141. maxX = qMax(maxX, geometry.right() + right);
  142. }
  143. pos = QPoint(effectiveRect.x(), pos.y() + length + space);
  144. next = pos + QPoint(itemSize.width() + space, 0);
  145. length = 0;
  146. }
  147. else if (this->Orientation == Qt::Vertical &&
  148. (next.y() - space > max) && length > 0)
  149. {
  150. pos = QPoint( pos.x() + length + space, effectiveRect.y());
  151. next = pos + QPoint(0, itemSize.height() + space);
  152. length = 0;
  153. }
  154. if (!testOnly)
  155. {
  156. item->setGeometry(QRect(pos, item->sizeHint()));
  157. }
  158. maxX = qMax( maxX , pos.x() + item->sizeHint().width() + right);
  159. maxY = qMax( maxY , pos.y() + item->sizeHint().height() + bottom);
  160. pos = next;
  161. length = qMax(length, this->Orientation == Qt::Horizontal ?
  162. itemSize.height() : itemSize.width());
  163. previousItem = item;
  164. }
  165. return this->Orientation == Qt::Horizontal ? maxY : maxX;
  166. }
  167. //-----------------------------------------------------------------------------
  168. int ctkFlowLayoutPrivate::smartSpacing(QStyle::PixelMetric pm) const
  169. {
  170. Q_Q(const ctkFlowLayout);
  171. QObject* parentObject = q->parent();
  172. if (!parentObject)
  173. {
  174. return -1;
  175. }
  176. else if (parentObject->isWidgetType())
  177. {
  178. QWidget* parentWidget = qobject_cast<QWidget *>(parentObject);
  179. return parentWidget->style()->pixelMetric(pm, 0, parentWidget);
  180. }
  181. else
  182. {
  183. return static_cast<QLayout *>(parentObject)->spacing();
  184. }
  185. }
  186. // --------------------------------------------------------------------------
  187. ctkFlowLayout::ctkFlowLayout(Qt::Orientation orientation, QWidget *parentWidget)
  188. : Superclass(parentWidget)
  189. , d_ptr(new ctkFlowLayoutPrivate(*this))
  190. {
  191. Q_D(ctkFlowLayout);
  192. d->init();
  193. this->setOrientation(orientation);
  194. }
  195. // --------------------------------------------------------------------------
  196. ctkFlowLayout::ctkFlowLayout(QWidget *parentWidget)
  197. : Superclass(parentWidget)
  198. , d_ptr(new ctkFlowLayoutPrivate(*this))
  199. {
  200. Q_D(ctkFlowLayout);
  201. d->init();
  202. }
  203. // --------------------------------------------------------------------------
  204. ctkFlowLayout::ctkFlowLayout()
  205. : d_ptr(new ctkFlowLayoutPrivate(*this))
  206. {
  207. Q_D(ctkFlowLayout);
  208. d->init();
  209. }
  210. // --------------------------------------------------------------------------
  211. ctkFlowLayout::~ctkFlowLayout()
  212. {
  213. Q_D(ctkFlowLayout);
  214. d->deleteAll();
  215. }
  216. // --------------------------------------------------------------------------
  217. void ctkFlowLayout::setOrientation(Qt::Orientation orientation)
  218. {
  219. Q_D(ctkFlowLayout);
  220. d->Orientation = orientation;
  221. this->invalidate();
  222. }
  223. // --------------------------------------------------------------------------
  224. void ctkFlowLayout::setPreferredExpandingDirections(Qt::Orientations directions)
  225. {
  226. Q_D(ctkFlowLayout);
  227. d->PreferredDirections = directions;
  228. }
  229. // --------------------------------------------------------------------------
  230. Qt::Orientations ctkFlowLayout::preferredExpandingDirections()const
  231. {
  232. Q_D(const ctkFlowLayout);
  233. return d->PreferredDirections;
  234. }
  235. // --------------------------------------------------------------------------
  236. Qt::Orientation ctkFlowLayout::orientation() const
  237. {
  238. Q_D(const ctkFlowLayout);
  239. return d->Orientation;
  240. }
  241. // --------------------------------------------------------------------------
  242. void ctkFlowLayout::setHorizontalSpacing(int spacing)
  243. {
  244. Q_D(ctkFlowLayout);
  245. d->HorizontalSpacing = spacing;
  246. this->invalidate();
  247. }
  248. // --------------------------------------------------------------------------
  249. int ctkFlowLayout::horizontalSpacing() const
  250. {
  251. Q_D(const ctkFlowLayout);
  252. if (d->HorizontalSpacing < 0)
  253. {
  254. return d->smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
  255. }
  256. return d->HorizontalSpacing;
  257. }
  258. // --------------------------------------------------------------------------
  259. void ctkFlowLayout::setVerticalSpacing(int spacing)
  260. {
  261. Q_D(ctkFlowLayout);
  262. d->VerticalSpacing = spacing;
  263. this->invalidate();
  264. }
  265. // --------------------------------------------------------------------------
  266. int ctkFlowLayout::verticalSpacing() const
  267. {
  268. Q_D(const ctkFlowLayout);
  269. if (d->VerticalSpacing < 0)
  270. {
  271. return d->smartSpacing(QStyle::PM_LayoutVerticalSpacing);
  272. }
  273. return d->VerticalSpacing;
  274. }
  275. // --------------------------------------------------------------------------
  276. void ctkFlowLayout::setAlignItems(bool align)
  277. {
  278. Q_D(ctkFlowLayout);
  279. d->AlignItems = align;
  280. this->invalidate();
  281. }
  282. // --------------------------------------------------------------------------
  283. bool ctkFlowLayout::alignItems() const
  284. {
  285. Q_D(const ctkFlowLayout);
  286. return d->AlignItems;
  287. }
  288. // --------------------------------------------------------------------------
  289. void ctkFlowLayout::addItem(QLayoutItem *item)
  290. {
  291. Q_D(ctkFlowLayout);
  292. d->ItemList << item;
  293. this->invalidate();
  294. }
  295. // --------------------------------------------------------------------------
  296. Qt::Orientations ctkFlowLayout::expandingDirections() const
  297. {
  298. return Qt::Vertical | Qt::Horizontal;
  299. }
  300. // --------------------------------------------------------------------------
  301. bool ctkFlowLayout::hasWidthForHeight() const
  302. {
  303. Q_D(const ctkFlowLayout);
  304. return d->Orientation == Qt::Vertical;
  305. }
  306. // --------------------------------------------------------------------------
  307. int ctkFlowLayout::widthForHeight(int height) const
  308. {
  309. Q_D(const ctkFlowLayout);
  310. QRect rect(0, 0, 0, height);
  311. int width = d->doLayout(rect, true);
  312. return width;
  313. }
  314. // --------------------------------------------------------------------------
  315. bool ctkFlowLayout::hasHeightForWidth() const
  316. {
  317. Q_D(const ctkFlowLayout);
  318. return d->Orientation == Qt::Horizontal;
  319. }
  320. // --------------------------------------------------------------------------
  321. int ctkFlowLayout::heightForWidth(int width) const
  322. {
  323. Q_D(const ctkFlowLayout);
  324. QRect rect(0, 0, width, 0);
  325. /// here we see the limitations of the vertical layout, it should be
  326. /// widthForHeight in this case.
  327. if (d->AlignItems && d->Orientation == Qt::Vertical)
  328. {
  329. int itemCount;
  330. QSize itemSize = d->maxSizeHint(&itemCount);
  331. QMargins margins = this->contentsMargins();
  332. int realWidth = width - margins.left() - margins.right();
  333. int itemCountPerRow = (realWidth + this->horizontalSpacing())
  334. / (itemSize.width() + this->horizontalSpacing());
  335. int rowCount = std::ceil( static_cast<float>(itemCount) / itemCountPerRow);
  336. rect.setHeight(rowCount * itemSize.height() +
  337. (rowCount -1) * this->verticalSpacing() +
  338. margins.top() + margins.bottom());
  339. }
  340. int height = d->doLayout(rect, true);
  341. return height;
  342. }
  343. // --------------------------------------------------------------------------
  344. int ctkFlowLayout::count() const
  345. {
  346. Q_D(const ctkFlowLayout);
  347. return d->ItemList.count();
  348. }
  349. // --------------------------------------------------------------------------
  350. QLayoutItem *ctkFlowLayout::itemAt(int index) const
  351. {
  352. Q_D(const ctkFlowLayout);
  353. if (index < 0 || index >= this->count())
  354. {
  355. return 0;
  356. }
  357. return d->ItemList[index];
  358. }
  359. // --------------------------------------------------------------------------
  360. QSize ctkFlowLayout::minimumSize() const
  361. {
  362. Q_D(const ctkFlowLayout);
  363. QSize size;
  364. foreach(QLayoutItem* item, d->ItemList)
  365. {
  366. QWidget* widget = item->widget();
  367. if (widget && !widget->isVisibleTo(widget->parentWidget()))
  368. {
  369. continue;
  370. }
  371. size = size.expandedTo(item->minimumSize());
  372. }
  373. int left, top, right, bottom;
  374. this->getContentsMargins(&left, &top, &right, &bottom);
  375. size += QSize(left+right, top+bottom);
  376. return size;
  377. }
  378. // --------------------------------------------------------------------------
  379. void ctkFlowLayout::setGeometry(const QRect &rect)
  380. {
  381. Q_D(ctkFlowLayout);
  382. this->QLayout::setGeometry(rect);
  383. d->doLayout(rect, false);
  384. }
  385. // --------------------------------------------------------------------------
  386. QSize ctkFlowLayout::sizeHint() const
  387. {
  388. Q_D(const ctkFlowLayout);
  389. QSize size = QSize(0,0);
  390. int countX = 0;
  391. int countY = 0;
  392. QSize maxSizeHint = d->AlignItems ? d->maxSizeHint() : QSize();
  393. // Add items
  394. foreach (QLayoutItem* item, d->ItemList)
  395. {
  396. QWidget* widget = item->widget();
  397. if (widget && !widget->isVisibleTo(widget->parentWidget()))
  398. {
  399. continue;
  400. }
  401. QSize itemSize = d->AlignItems ? maxSizeHint : item->sizeHint();
  402. Qt::Orientation grow;
  403. if (d->PreferredDirections & Qt::Horizontal &&
  404. !(d->PreferredDirections & Qt::Vertical))
  405. {
  406. grow = Qt::Horizontal;
  407. }
  408. else if (d->PreferredDirections & Qt::Vertical &&
  409. !(d->PreferredDirections & Qt::Horizontal))
  410. {
  411. grow = Qt::Vertical;
  412. }
  413. else
  414. {
  415. grow = countY >= countX ? Qt::Horizontal : Qt::Vertical;
  416. }
  417. if (grow == Qt::Horizontal)
  418. {
  419. size.rwidth() += itemSize.width();
  420. size.rheight() = qMax(itemSize.height(), size.height());
  421. ++countX;
  422. }
  423. else
  424. {
  425. size.rwidth() = qMax(itemSize.width(), size.width());
  426. size.rheight() += itemSize.height();
  427. ++countY;
  428. }
  429. }
  430. // Add spacing
  431. size += QSize((countX-1) * this->horizontalSpacing(),
  432. (countY-1) * this->verticalSpacing());
  433. // Add margins
  434. int left, top, right, bottom;
  435. this->getContentsMargins(&left, &top, &right, &bottom);
  436. size += QSize(left+right, top+bottom);
  437. return size;
  438. }
  439. // --------------------------------------------------------------------------
  440. QLayoutItem *ctkFlowLayout::takeAt(int index)
  441. {
  442. Q_D(ctkFlowLayout);
  443. if (index < 0 || index >= this->count())
  444. {
  445. return 0;
  446. }
  447. QLayoutItem* item = d->ItemList.takeAt(index);
  448. this->invalidate();
  449. return item;
  450. }
  451. // --------------------------------------------------------------------------
  452. ctkFlowLayout* ctkFlowLayout::replaceLayout(QWidget* widget)
  453. {
  454. QLayout* oldLayout = widget->layout();
  455. ctkFlowLayout* flowLayout = new ctkFlowLayout;
  456. bool isVerticalLayout = qobject_cast<QVBoxLayout*>(oldLayout) != 0;
  457. flowLayout->setPreferredExpandingDirections(
  458. isVerticalLayout ? Qt::Vertical : Qt::Horizontal);
  459. flowLayout->setAlignItems(false);
  460. int margins[4];
  461. oldLayout->getContentsMargins(&margins[0],&margins[1],&margins[2],&margins[3]);
  462. QLayoutItem* item = 0;
  463. while((item = oldLayout->takeAt(0)))
  464. {
  465. if (item->widget())
  466. {
  467. flowLayout->addWidget(item->widget());
  468. }
  469. }
  470. // setLayout() will take care or reparenting layouts and widgets
  471. delete oldLayout;
  472. flowLayout->setContentsMargins(0,0,0,0);
  473. widget->setLayout(flowLayout);
  474. return flowLayout;
  475. }