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

Minor changes of our scroll area

* In the c'tor we add the layout to the widget before we set the widget as
the scroll area's widget because the API documentation of
QScrollArea::setWidget includes a note that this is required to avoid
problems with the visibility of the widget. Those problems didn't
happen for us, but we better play be the rules.
* In minimumSizeHint the height of the widget's minimum size hint is now
added.
* In sizeHint we use viewportSizeHint() (which uses the actual size of
resizable widgets) instead of the widget's size hint.
* For both size hints the width of the vertical scroll bar is not added
anymore if it is explicitly turned off. The height of the horizontal
scroll bar is added now unless it is explicitly turned off.

I have checked that the current usages of ScrollArea still work as
expected.

GnuPG-bug-id: 5969
parent e97707b4
......@@ -16,41 +16,44 @@
using namespace Kleo;
namespace
ScrollArea::ScrollArea(QWidget *parent)
: QScrollArea{parent}
{
static QSize getMinimumSizeHint(const QWidget *w)
{
return w ? w->minimumSizeHint() : QSize(0, 0);
}
static QSize getSizeHint(const QWidget *w)
{
return w ? w->sizeHint() : QSize(0, 0);
}
}
ScrollArea::ScrollArea(QWidget *parent) : QScrollArea(parent)
{
setWidget(new QWidget);
new QVBoxLayout(widget());
auto w = new QWidget;
w->setObjectName(QLatin1String("scrollarea_widget"));
new QVBoxLayout{w};
setWidget(w);
setWidgetResizable(true);
}
ScrollArea::~ScrollArea()
{
}
ScrollArea::~ScrollArea() = default;
QSize ScrollArea::minimumSizeHint() const
{
return QSize(getMinimumSizeHint(widget()).width() + getSizeHint(verticalScrollBar()).width() + 2 * frameWidth(), 0)
.expandedTo(QScrollArea::minimumSizeHint());
const int fw = frameWidth();
QSize sz{2 * fw, 2 * fw};
sz += widget()->minimumSizeHint();
if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOff) {
sz.setWidth(sz.width() + verticalScrollBar()->sizeHint().width());
}
if (horizontalScrollBarPolicy() != Qt::ScrollBarAlwaysOff) {
sz.setHeight(sz.height() + horizontalScrollBar()->sizeHint().height());
}
return QScrollArea::minimumSizeHint().expandedTo(sz);
}
QSize ScrollArea::sizeHint() const
{
const QSize widgetSizeHint = getSizeHint(widget());
const int fw = frameWidth();
return QScrollArea::sizeHint().expandedTo(widgetSizeHint + QSize(2 * fw, 2 * fw) + QSize(getSizeHint(verticalScrollBar()).width(), 0));
QSize sz{2 * fw, 2 * fw};
sz += viewportSizeHint();
if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOff) {
sz.setWidth(sz.width() + verticalScrollBar()->sizeHint().width());
}
if (horizontalScrollBarPolicy() != Qt::ScrollBarAlwaysOff) {
sz.setHeight(sz.height() + horizontalScrollBar()->sizeHint().height());
}
sz = QScrollArea::sizeHint().expandedTo(sz);
return sz;
}
......@@ -14,15 +14,32 @@
namespace Kleo
{
/**
* This class improves a few aspects of QScrollArea for usage by us, in
* particular, for vertically scrollable widgets.
*/
class ScrollArea : public QScrollArea
{
Q_OBJECT
public:
explicit ScrollArea(QWidget *widget = nullptr);
/**
* Creates a scroll area with a QWidget with QVBoxLayout that is flagged
* as resizable.
*/
explicit ScrollArea(QWidget *parent = nullptr);
~ScrollArea() override;
/**
* Reimplemented to add the minimum size hint of the widget.
*/
QSize minimumSizeHint() const override;
/**
* Reimplemented to remove the caching of the size/size hint of the
* widget and to add the horizontal size hint of the vertical scroll bar
* unless it is explicitly turned off.
*/
QSize sizeHint() const override;
};
......
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