ctkFlowLayout.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.commontk.org/LICENSE
  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 maxY = top + bottom;
  105. QSize maxItemSize = this->AlignItems ? this->maxSizeHint() : QSize();
  106. int spaceX = q->horizontalSpacing();
  107. int spaceY = q->verticalSpacing();
  108. int space = this->Orientation == Qt::Horizontal ? spaceX : spaceY;
  109. foreach (QLayoutItem* item, this->ItemList)
  110. {
  111. QWidget *wid = item->widget();
  112. if (wid && wid->isHidden())
  113. {
  114. continue;
  115. }
  116. QPoint next = pos;
  117. QSize itemSize = this->AlignItems ? maxItemSize : item->sizeHint();
  118. if (this->Orientation == Qt::Horizontal)
  119. {
  120. next += QPoint(itemSize.width() + spaceX, 0);
  121. }
  122. else
  123. {
  124. next += QPoint(0, itemSize.height() + spaceY);
  125. }
  126. if (this->Orientation == Qt::Horizontal &&
  127. (next.x() - space > max) && length > 0)
  128. {
  129. pos = QPoint(effectiveRect.x(), pos.y() + length + space);
  130. next = pos + QPoint(itemSize.width() + space, 0);
  131. length = 0;
  132. }
  133. else if (this->Orientation == Qt::Vertical &&
  134. (next.y() - space > max) && length > 0)
  135. {
  136. pos = QPoint( pos.x() + length + space, effectiveRect.y());
  137. next = pos + QPoint(0, itemSize.height() + space);
  138. length = 0;
  139. }
  140. if (!testOnly)
  141. {
  142. item->setGeometry(QRect(pos, item->sizeHint()));
  143. }
  144. maxY = qMax( maxY , pos.y() + item->sizeHint().height() + bottom);
  145. pos = next;
  146. length = qMax(length, this->Orientation == Qt::Horizontal ?
  147. itemSize.height() : itemSize.width());
  148. }
  149. return maxY;
  150. }
  151. //-----------------------------------------------------------------------------
  152. int ctkFlowLayoutPrivate::smartSpacing(QStyle::PixelMetric pm) const
  153. {
  154. Q_Q(const ctkFlowLayout);
  155. QObject* parentObject = q->parent();
  156. if (!parentObject)
  157. {
  158. return -1;
  159. }
  160. else if (parentObject->isWidgetType())
  161. {
  162. QWidget* parentWidget = qobject_cast<QWidget *>(parentObject);
  163. return parentWidget->style()->pixelMetric(pm, 0, parentWidget);
  164. }
  165. else
  166. {
  167. return static_cast<QLayout *>(parentObject)->spacing();
  168. }
  169. }
  170. // --------------------------------------------------------------------------
  171. ctkFlowLayout::ctkFlowLayout(Qt::Orientation orientation, QWidget *parentWidget)
  172. : Superclass(parentWidget)
  173. , d_ptr(new ctkFlowLayoutPrivate(*this))
  174. {
  175. Q_D(ctkFlowLayout);
  176. d->init();
  177. this->setOrientation(orientation);
  178. }
  179. // --------------------------------------------------------------------------
  180. ctkFlowLayout::ctkFlowLayout(QWidget *parentWidget)
  181. : Superclass(parentWidget)
  182. , d_ptr(new ctkFlowLayoutPrivate(*this))
  183. {
  184. Q_D(ctkFlowLayout);
  185. d->init();
  186. }
  187. // --------------------------------------------------------------------------
  188. ctkFlowLayout::ctkFlowLayout()
  189. : d_ptr(new ctkFlowLayoutPrivate(*this))
  190. {
  191. Q_D(ctkFlowLayout);
  192. d->init();
  193. }
  194. // --------------------------------------------------------------------------
  195. ctkFlowLayout::~ctkFlowLayout()
  196. {
  197. Q_D(ctkFlowLayout);
  198. d->deleteAll();
  199. }
  200. // --------------------------------------------------------------------------
  201. void ctkFlowLayout::setOrientation(Qt::Orientation orientation)
  202. {
  203. Q_D(ctkFlowLayout);
  204. d->Orientation = orientation;
  205. this->invalidate();
  206. }
  207. // --------------------------------------------------------------------------
  208. void ctkFlowLayout::setPreferredExpandingDirections(Qt::Orientations directions)
  209. {
  210. Q_D(ctkFlowLayout);
  211. d->PreferredDirections = directions;
  212. }
  213. // --------------------------------------------------------------------------
  214. Qt::Orientations ctkFlowLayout::preferredExpandingDirections()const
  215. {
  216. Q_D(const ctkFlowLayout);
  217. return d->PreferredDirections;
  218. }
  219. // --------------------------------------------------------------------------
  220. Qt::Orientation ctkFlowLayout::orientation() const
  221. {
  222. Q_D(const ctkFlowLayout);
  223. return d->Orientation;
  224. }
  225. // --------------------------------------------------------------------------
  226. void ctkFlowLayout::setHorizontalSpacing(int spacing)
  227. {
  228. Q_D(ctkFlowLayout);
  229. d->HorizontalSpacing = spacing;
  230. this->invalidate();
  231. }
  232. // --------------------------------------------------------------------------
  233. int ctkFlowLayout::horizontalSpacing() const
  234. {
  235. Q_D(const ctkFlowLayout);
  236. if (d->HorizontalSpacing < 0)
  237. {
  238. return d->smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
  239. }
  240. return d->HorizontalSpacing;
  241. }
  242. // --------------------------------------------------------------------------
  243. void ctkFlowLayout::setVerticalSpacing(int spacing)
  244. {
  245. Q_D(ctkFlowLayout);
  246. d->VerticalSpacing = spacing;
  247. this->invalidate();
  248. }
  249. // --------------------------------------------------------------------------
  250. int ctkFlowLayout::verticalSpacing() const
  251. {
  252. Q_D(const ctkFlowLayout);
  253. if (d->VerticalSpacing < 0)
  254. {
  255. return d->smartSpacing(QStyle::PM_LayoutVerticalSpacing);
  256. }
  257. return d->VerticalSpacing;
  258. }
  259. // --------------------------------------------------------------------------
  260. void ctkFlowLayout::setAlignItems(bool align)
  261. {
  262. Q_D(ctkFlowLayout);
  263. d->AlignItems = align;
  264. this->invalidate();
  265. }
  266. // --------------------------------------------------------------------------
  267. bool ctkFlowLayout::alignItems() const
  268. {
  269. Q_D(const ctkFlowLayout);
  270. return d->AlignItems;
  271. }
  272. // --------------------------------------------------------------------------
  273. void ctkFlowLayout::addItem(QLayoutItem *item)
  274. {
  275. Q_D(ctkFlowLayout);
  276. d->ItemList << item;
  277. this->invalidate();
  278. }
  279. // --------------------------------------------------------------------------
  280. Qt::Orientations ctkFlowLayout::expandingDirections() const
  281. {
  282. return Qt::Vertical | Qt::Horizontal;
  283. }
  284. // --------------------------------------------------------------------------
  285. bool ctkFlowLayout::hasHeightForWidth() const
  286. {
  287. return true;
  288. }
  289. // --------------------------------------------------------------------------
  290. int ctkFlowLayout::heightForWidth(int width) const
  291. {
  292. Q_D(const ctkFlowLayout);
  293. QRect rect(0, 0, width, 0);
  294. /// here we see the limitations of the vertical layout, it should be
  295. /// widthForHeight in this case.
  296. if (d->AlignItems && d->Orientation == Qt::Vertical)
  297. {
  298. int itemCount;
  299. QSize itemSize = d->maxSizeHint(&itemCount);
  300. QMargins margins = this->contentsMargins();
  301. int realWidth = width - margins.left() - margins.right();
  302. int itemCountPerRow = (realWidth + this->horizontalSpacing())
  303. / (itemSize.width() + this->horizontalSpacing());
  304. int rowCount = std::ceil( static_cast<float>(itemCount) / itemCountPerRow);
  305. rect.setHeight(rowCount * itemSize.height() +
  306. (rowCount -1) * this->verticalSpacing() +
  307. margins.top() + margins.bottom());
  308. }
  309. int height = d->doLayout(rect, true);
  310. return height;
  311. }
  312. // --------------------------------------------------------------------------
  313. int ctkFlowLayout::count() const
  314. {
  315. Q_D(const ctkFlowLayout);
  316. return d->ItemList.count();
  317. }
  318. // --------------------------------------------------------------------------
  319. QLayoutItem *ctkFlowLayout::itemAt(int index) const
  320. {
  321. Q_D(const ctkFlowLayout);
  322. if (index < 0 || index >= this->count())
  323. {
  324. return 0;
  325. }
  326. return d->ItemList[index];
  327. }
  328. // --------------------------------------------------------------------------
  329. QSize ctkFlowLayout::minimumSize() const
  330. {
  331. Q_D(const ctkFlowLayout);
  332. QSize size;
  333. foreach(QLayoutItem* item, d->ItemList)
  334. {
  335. QWidget* widget = item->widget();
  336. if (widget && !widget->isVisibleTo(widget->parentWidget()))
  337. {
  338. continue;
  339. }
  340. size = size.expandedTo(item->minimumSize());
  341. }
  342. int left, top, right, bottom;
  343. this->getContentsMargins(&left, &top, &right, &bottom);
  344. size += QSize(left+right, top+bottom);
  345. return size;
  346. }
  347. // --------------------------------------------------------------------------
  348. void ctkFlowLayout::setGeometry(const QRect &rect)
  349. {
  350. Q_D(ctkFlowLayout);
  351. this->QLayout::setGeometry(rect);
  352. d->doLayout(rect, false);
  353. }
  354. // --------------------------------------------------------------------------
  355. QSize ctkFlowLayout::sizeHint() const
  356. {
  357. Q_D(const ctkFlowLayout);
  358. QSize size = QSize(0,0);
  359. int countX = 0;
  360. int countY = 0;
  361. QSize maxSizeHint = d->AlignItems ? d->maxSizeHint() : QSize();
  362. // Add items
  363. foreach (QLayoutItem* item, d->ItemList)
  364. {
  365. QWidget* widget = item->widget();
  366. if (widget && !widget->isVisibleTo(widget->parentWidget()))
  367. {
  368. continue;
  369. }
  370. QSize itemSize = d->AlignItems ? maxSizeHint : item->sizeHint();
  371. Qt::Orientation grow;
  372. if (d->PreferredDirections & Qt::Horizontal &&
  373. !(d->PreferredDirections & Qt::Vertical))
  374. {
  375. grow = Qt::Horizontal;
  376. }
  377. else if (d->PreferredDirections & Qt::Vertical &&
  378. !(d->PreferredDirections & Qt::Horizontal))
  379. {
  380. grow = Qt::Vertical;
  381. }
  382. else
  383. {
  384. grow = countY >= countX ? Qt::Horizontal : Qt::Vertical;
  385. }
  386. if (grow == Qt::Horizontal)
  387. {
  388. size.rwidth() += itemSize.width();
  389. size.rheight() = qMax(itemSize.height(), size.height());
  390. ++countX;
  391. }
  392. else
  393. {
  394. size.rwidth() = qMax(itemSize.width(), size.width());
  395. size.rheight() += itemSize.height();
  396. ++countY;
  397. }
  398. }
  399. // Add spacing
  400. size += QSize((countX-1) * this->horizontalSpacing(),
  401. (countY-1) * this->verticalSpacing());
  402. // Add margins
  403. int left, top, right, bottom;
  404. this->getContentsMargins(&left, &top, &right, &bottom);
  405. size += QSize(left+right, top+bottom);
  406. return size;
  407. }
  408. // --------------------------------------------------------------------------
  409. QLayoutItem *ctkFlowLayout::takeAt(int index)
  410. {
  411. Q_D(ctkFlowLayout);
  412. if (index < 0 || index >= this->count())
  413. {
  414. return 0;
  415. }
  416. QLayoutItem* item = d->ItemList.takeAt(index);
  417. this->invalidate();
  418. return item;
  419. }