Commit 7ce8511a authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Make the automatic size adjustment work as expected

Adjusting the minimum height of the scroll area doesn't work correctly
because the main layout fails to calculate the correct minimal height
of the window. (It uses the minimumSizeHint of the info label which is
1 line of text although the info label shows 2 lines of text. This
causes the widget after the scroll area to be paint over the lower bit
of the scroll area.) Setting the minimum height of the scroll area also
had the disadvantage that it made it impossible for the user to reduce
the height of the window.

Instead we now simply resize the window as needed. Also rather crude, but
effective.

We limit the automatic size adjustment to 2/3 of the screen's size (i.e.
the same upper limit as QWidget::adjustSize()) instead of allowing the
user of ScrollArea to set an own limit for the height of the scroll area.

GnuPG-bug-id: 5969
parent 3f9caabd
Pipeline #175687 passed with stage
in 1 minute and 53 seconds
......@@ -42,21 +42,6 @@ ScrollArea::~ScrollArea()
widget()->removeEventFilter(this);
}
void ScrollArea::setMaximumAutoAdjustHeight(int maxHeight)
{
mMaximumAutoAdjustHeight = maxHeight;
}
int ScrollArea::maximumAutoAdjustHeight() const
{
if (mMaximumAutoAdjustHeight < 0) {
// if no height is set then use 2/3 of the desktop's height, i.e.
// the same as Qt uses for top-level widgets
return screen()->availableGeometry().height() * 2 / 3;
}
return mMaximumAutoAdjustHeight;
}
QSize ScrollArea::minimumSizeHint() const
{
const int fw = frameWidth();
......@@ -86,15 +71,26 @@ QSize ScrollArea::sizeHint() const
return sz;
}
void ScrollArea::adjustSizeOfWindowBy(const QSize &extent)
{
if (auto w = window()) {
// we limit the automatic size adjustment to 2/3 of the screen's size
const auto maxWindowSize = screen()->geometry().size() * 2 / 3;
const auto newWindowSize = (w->size() + extent).boundedTo(maxWindowSize);
w->resize(newWindowSize);
}
}
bool ScrollArea::eventFilter(QObject *obj, QEvent *ev)
{
if (ev->type() == QEvent::Resize && obj == widget() && sizeAdjustPolicy() == AdjustToContents) {
const auto *const event = static_cast<QResizeEvent*>(ev);
if (event->size().height() > event->oldSize().height()) {
const auto currentViewportHeight = viewport()->height();
const auto wantedViewportHeight = std::min(event->size().height(), maximumAutoAdjustHeight());
if (currentViewportHeight < wantedViewportHeight) {
setMinimumHeight(height() - currentViewportHeight + wantedViewportHeight);
const auto wantedViewportHeight = event->size().height();
const auto wantedAdditionalHeight = wantedViewportHeight - currentViewportHeight;
if (wantedAdditionalHeight > 0) {
adjustSizeOfWindowBy(QSize{0, wantedAdditionalHeight});
}
}
}
......
......@@ -36,19 +36,6 @@ public:
explicit ScrollArea(QWidget *parent = nullptr);
~ScrollArea() override;
/**
* Sets the maximum height that the scroll area should automatically resize
* to to \p maxHeight. By default, or if \p maxHeight is negative, the
* scroll area will resize to at most 2/3 of the desktop's height.
*/
void setMaximumAutoAdjustHeight(int maxHeight);
/**
* Returns the maximum height that the scroll area will automatically resize
* to.
*/
int maximumAutoAdjustHeight() const;
/**
* Reimplemented to add the minimum size hint of the widget.
*/
......@@ -62,10 +49,8 @@ public:
QSize sizeHint() const override;
private:
void adjustSizeOfWindowBy(const QSize &extent);
bool eventFilter(QObject *obj, QEvent *ev) override;
private:
int mMaximumAutoAdjustHeight = -1;
};
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment