ctkFlowLayout.cpp 12 KB

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