Libkleo

adjustingscrollarea.cpp
1/* -*- mode: c++; c-basic-offset:4 -*-
2 ui/adjustingscrollarea.cpp
3
4 This file is part of Kleopatra, the KDE keymanager
5 SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
6 SPDX-FileCopyrightText: 2022 g10 Code GmbH
7 SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
8
9 SPDX-License-Identifier: GPL-2.0-or-later
10*/
11
12#include "adjustingscrollarea.h"
13
14#include <QApplication>
15#include <QResizeEvent>
16#include <QScreen>
17#include <QScrollBar>
18#include <QVBoxLayout>
19
20using namespace Kleo;
21
23 : QScrollArea{parent}
24{
25 auto w = new QWidget;
26 w->setObjectName(QLatin1StringView("scrollarea_widget"));
27 new QVBoxLayout{w};
28 setWidget(w);
30 w->installEventFilter(this);
31
32 connect(qApp, &QApplication::focusChanged, this, [this](QWidget *old, QWidget *now) {
33 Q_UNUSED(old);
35 });
36}
37
38AdjustingScrollArea::~AdjustingScrollArea()
39{
41}
42
44{
45 const int fw = frameWidth();
46 QSize sz{2 * fw, 2 * fw};
47 sz += {widget()->minimumSizeHint().width(), 0};
49 sz.setWidth(sz.width() + verticalScrollBar()->sizeHint().width());
50 }
52 sz.setHeight(sz.height() + horizontalScrollBar()->sizeHint().height());
53 }
54 return QScrollArea::minimumSizeHint().expandedTo(sz);
55}
56
58{
59 const int fw = frameWidth();
60 QSize sz{2 * fw, 2 * fw};
61 sz += viewportSizeHint();
63 sz.setWidth(sz.width() + verticalScrollBar()->sizeHint().width());
64 }
66 sz.setHeight(sz.height() + horizontalScrollBar()->sizeHint().height());
67 }
69 return sz;
70}
71
72void AdjustingScrollArea::adjustSizeOfWindowBy(const QSize &extent)
73{
74 if (auto w = window()) {
75 const auto currentSize = w->size();
76 // we limit the automatic size adjustment to 2/3 of the screen's size
77 const auto maxWindowSize = screen()->geometry().size() * 2 / 3;
78 const auto newWindowSize = currentSize.expandedTo((currentSize + extent).boundedTo(maxWindowSize));
79 if (newWindowSize != currentSize) {
80 w->resize(newWindowSize);
81 }
82 }
83}
84
85bool AdjustingScrollArea::eventFilter(QObject *obj, QEvent *ev)
86{
87 if (ev->type() == QEvent::Resize && obj == widget() && sizeAdjustPolicy() == AdjustToContents) {
88 const auto *const event = static_cast<QResizeEvent *>(ev);
89 if (event->size().height() > event->oldSize().height()) {
90 const auto currentViewportHeight = viewport()->height();
91 const auto wantedViewportHeight = event->size().height();
92 const auto wantedAdditionalHeight = wantedViewportHeight - currentViewportHeight;
93 if (wantedAdditionalHeight > 0) {
94 adjustSizeOfWindowBy(QSize{0, wantedAdditionalHeight});
95 }
96 }
97 }
98 return QScrollArea::eventFilter(obj, ev);
99}
100
101#include "moc_adjustingscrollarea.cpp"
QSize sizeHint() const override
Reimplemented to remove the caching of the size/size hint of the widget and to add the horizontal siz...
AdjustingScrollArea(QWidget *parent=nullptr)
Creates a scroll area with a QWidget with QVBoxLayout that is flagged as resizable.
QSize minimumSizeHint() const override
Reimplemented to add the minimum size hint of the widget.
QScrollBar * horizontalScrollBar() const const
QScrollBar * verticalScrollBar() const const
QWidget * viewport() const const
void focusChanged(QWidget *old, QWidget *now)
Type type() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void removeEventFilter(QObject *obj)
void setObjectName(QAnyStringView name)
void ensureWidgetVisible(QWidget *childWidget, int xmargin, int ymargin)
virtual bool event(QEvent *e) override
virtual bool eventFilter(QObject *o, QEvent *e) override
void setWidget(QWidget *widget)
virtual QSize sizeHint() const const override
virtual QSize viewportSizeHint() const const override
QWidget * widget() const const
void setWidgetResizable(bool resizable)
QSize expandedTo(const QSize &otherSize) const const
ScrollBarAlwaysOff
QWidget(QWidget *parent, Qt::WindowFlags f)
QScreen * screen() const const
QWidget * window() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:44:55 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.