summaryrefslogtreecommitdiff
path: root/widgets/CurrentPageHeightStackedWidget.hpp
blob: e205e047d1977a57ce5f70687d69ad75596e17dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// SPDX-License-Identifier: GPL-3.0-or-later
#ifndef CURRENT_PAGE_HEIGHT_STACKED_WIDGET_HPP
#define CURRENT_PAGE_HEIGHT_STACKED_WIDGET_HPP

#include <QStackedWidget>

class CurrentPageHeightStackedWidget final : public QStackedWidget
{
public:
  explicit CurrentPageHeightStackedWidget (QWidget * parent = nullptr)
    : QStackedWidget {parent}
  {
    connect (this, &QStackedWidget::currentChanged, this, [this] {updateGeometry ();});
  }

  QSize sizeHint () const override
  {
    return withCurrentPageHeight (QStackedWidget::sizeHint (), false);
  }

  QSize minimumSizeHint () const override
  {
    return withCurrentPageHeight (QStackedWidget::minimumSizeHint (), true);
  }

private:
  QSize withCurrentPageHeight (QSize hint, bool minimum) const
  {
    if (auto * page = currentWidget ())
      {
        auto const pageHint = minimum ? page->minimumSizeHint () : page->sizeHint ();
        if (pageHint.isValid ()) hint.setHeight (pageHint.height ());
      }
    return hint;
  }
};

#endif