ctkFlowLayout.cpp 13 KB

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