GridFlowLayout.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "GridFlowLayout.h"
  2. #include <QWidget>
  3. #include <QStyle>
  4. GridFlowLayout::GridFlowLayout(QWidget* parent) :
  5. QLayout{ parent }
  6. {
  7. }
  8. void GridFlowLayout::addItem(QLayoutItem* item)
  9. {
  10. items.append(item);
  11. }
  12. QLayoutItem* GridFlowLayout::itemAt(int index) const
  13. {
  14. return items.value(index);
  15. }
  16. QLayoutItem* GridFlowLayout::takeAt(int index)
  17. {
  18. if (index >= 0 && index < items.size())
  19. return items.takeAt(index);
  20. return nullptr;
  21. }
  22. bool GridFlowLayout::hasHeightForWidth(void) const
  23. {
  24. return true;
  25. }
  26. int GridFlowLayout::heightForWidth(int width) const
  27. {
  28. return 300;
  29. }
  30. int GridFlowLayout::count(void) const
  31. {
  32. return items.size();
  33. }
  34. Qt::Orientations GridFlowLayout::expandingDirections(void) const
  35. {
  36. return 0;
  37. }
  38. QSize GridFlowLayout::minimumSize(void) const
  39. {
  40. return QSize{ 400, 400 };
  41. }
  42. QSize GridFlowLayout::sizeHint(void) const
  43. {
  44. return minimumSize();
  45. }
  46. void GridFlowLayout::setGeometry(const QRect& rect)
  47. {
  48. QLayout::setGeometry(rect);
  49. int mleft, mtop, mright, mbottom;
  50. getContentsMargins(&mleft, &mtop, &mright, &mbottom);
  51. QRect area = rect.adjusted(mleft, mtop, -mright, -mbottom);
  52. QVector<QSize> sizeHints;
  53. for (QLayoutItem* item : qAsConst(items)) {
  54. sizeHints.append(item->sizeHint());
  55. }
  56. int rows;
  57. for (rows = count(); rows > 1; rows--) {
  58. if (checkRows(rows, rect, sizeHints)) {
  59. break;
  60. }
  61. }
  62. rows = 1;
  63. applyLayout(rows, sizeHints, area);
  64. /*
  65. int x = area.x();
  66. int y = area.y();
  67. for (QLayoutItem* item : qAsConst(items)) {
  68. const QWidget* widget = item->widget();
  69. int spaceX = 5;
  70. int spaceY = 5;
  71. int width = area.width();
  72. int height = 200;//area.height();
  73. item->setGeometry(QRect(x, y, width, height));
  74. y += height;
  75. }*/
  76. }
  77. void GridFlowLayout::applyLayout(int rows, const QVector<QSize>& sizeHints,
  78. const QRect& area)
  79. {
  80. calculateRows(rows, sizeHints);
  81. int x = area.x();
  82. for (int i = 0; i < rows; i++) {
  83. int width = widths[i];
  84. int y = area.y();
  85. for (int j = 0; j <= count() / rows; j++) {
  86. int index = i + j * rows;
  87. if (index < count()) {
  88. int height = heights[j];
  89. QLayoutItem* item = items.value(index);
  90. const QWidget* widget = item->widget();
  91. item->setGeometry(QRect(x, y, width, height));
  92. y += height;
  93. }
  94. }
  95. x += width;
  96. }
  97. }
  98. void GridFlowLayout::calculateRows(int rows, const QVector<QSize>& sizeHints)
  99. {
  100. widths.clear();
  101. heights.clear();
  102. for (int i = 0; i < rows; i++) {
  103. int maxWidth = 0;
  104. for(int j = i; j < sizeHints.size(); j += rows) {
  105. maxWidth = std::max(maxWidth, sizeHints[j].width());
  106. }
  107. widths.append(maxWidth);
  108. }
  109. for (int i = 0; i < sizeHints.size(); i += rows) {
  110. int maxHeight = 0;
  111. for(int j = i; j < i + rows && j < sizeHints.size(); j++) {
  112. maxHeight = std::max(maxHeight, sizeHints[j].height());
  113. }
  114. heights.append(maxHeight);
  115. }
  116. }
  117. bool GridFlowLayout::checkRows(int rows, const QRect& rect,
  118. const QVector<QSize>& sizeHints)
  119. {
  120. calculateRows(rows, sizeHints);
  121. int width = 0;
  122. int height = 0;
  123. for (int w : widths) width += w;
  124. for (int h : heights) height += h;
  125. return width <= rect.width() && height <= rect.height();
  126. }