ExpandLayout.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "ExpandLayout.h"
  2. #include <QResizeEvent>
  3. #include <QWidget>
  4. ExpandLayout::ExpandLayout(QWidget *parent) : QLayout(parent) { }
  5. ExpandLayout::~ExpandLayout()
  6. {
  7. removeAllItems();
  8. }
  9. void ExpandLayout::addWidget(QWidget *w)
  10. {
  11. if (m_widgets.contains(w)) {
  12. return;
  13. }
  14. m_widgets.append(w);
  15. w->installEventFilter(this);
  16. }
  17. QSize ExpandLayout::sizeHint() const
  18. {
  19. return minimumSize();
  20. }
  21. void ExpandLayout::addItem(QLayoutItem *item)
  22. {
  23. m_items.append(item);
  24. }
  25. QLayoutItem *ExpandLayout::itemAt(int index) const
  26. {
  27. if (index >= 0 && index < m_items.count()) {
  28. return m_items[index];
  29. }
  30. return nullptr;
  31. }
  32. QLayoutItem *ExpandLayout::takeAt(int index)
  33. {
  34. if (index >= 0 && index < m_items.count()) {
  35. return m_items.takeAt(index);
  36. }
  37. return nullptr;
  38. }
  39. int ExpandLayout::count() const
  40. {
  41. return m_items.count();
  42. }
  43. QSize ExpandLayout::minimumSize() const
  44. {
  45. QSize size;
  46. for (auto w : m_widgets) {
  47. size = size.expandedTo(w->minimumSize());
  48. }
  49. QMargins m = contentsMargins();
  50. size += QSize(m.left() + m.right(), m.top() + m.bottom());
  51. return size;
  52. }
  53. Qt::Orientations ExpandLayout::expandingDirections() const
  54. {
  55. return Qt::Vertical;
  56. }
  57. void ExpandLayout::setGeometry(const QRect &rect)
  58. {
  59. QLayout::setGeometry(rect);
  60. doLayout(rect, true);
  61. }
  62. bool ExpandLayout::hasHeightForWidth() const
  63. {
  64. return true;
  65. }
  66. int ExpandLayout::heightForWidth(int width) const
  67. {
  68. return doLayout(QRect(0, 0, width, 0), false);
  69. }
  70. bool ExpandLayout::eventFilter(QObject *watched, QEvent *event)
  71. {
  72. if (m_widgets.contains((QWidget *)watched)) {
  73. if (event->type() == QEvent::Resize) {
  74. QResizeEvent *re = static_cast<QResizeEvent *>(event);
  75. QSize ds = re->size() - re->oldSize();
  76. if (ds.height() != 0 && ds.width() == 0) {
  77. QWidget *w = this->parentWidget();
  78. w->resize(w->width(), w->height() + ds.height());
  79. }
  80. }
  81. }
  82. return QLayout::eventFilter(watched, event);
  83. }
  84. void ExpandLayout::removeAllItems()
  85. {
  86. QLayoutItem *item;
  87. while ((item = takeAt(0)))
  88. delete item;
  89. }
  90. int ExpandLayout::doLayout(const QRect &rect, bool move) const
  91. {
  92. QMargins margin = contentsMargins();
  93. int x = rect.x() + margin.left();
  94. int y = rect.y() + margin.top() + margin.bottom();
  95. int width = rect.width() - margin.left() - margin.right();
  96. for (int i = 0; i < m_widgets.count(); ++i) {
  97. if (i > 0) {
  98. y += this->spacing();
  99. }
  100. if (move) {
  101. m_widgets.at(i)->setGeometry(QRect(QPoint(x, y), QSize(width, m_widgets.at(i)->height())));
  102. }
  103. y += m_widgets.at(i)->height();
  104. }
  105. return y - rect.y();
  106. }