summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/QtUtils/WrapInScrollArea.cpp
blob: 0f75706b58c10cc0692d2a1037b8669689531b7e (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinQt/QtUtils/WrapInScrollArea.h"

#include <QFrame>
#include <QLayout>
#include <QPalette>
#include <QScrollArea>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QWidget>

namespace
{

// This scroll area prefers the size of its underlying widget.
class HintingScrollArea final : public QScrollArea
{
public:
  HintingScrollArea()
  {
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    setWidgetResizable(true);

    // Make things not look horrendous on Windows.
    setFrameStyle(QFrame::NoFrame);
  }

  QSize sizeHint() const override
  {
    // Including the scrollbars in our hint can prevent
    //  a window undersized in one direction gaining unnecessary scrolling in both directions.
    const auto scrollbar_padding =
        QSize{verticalScrollBar()->sizeHint().width(), horizontalScrollBar()->sizeHint().height()};

    const auto size_hint = widget()->sizeHint();

    return size_hint + scrollbar_padding;
  }
};

}  // namespace

QWidget* GetWrappedWidget(QWidget* wrapped_widget)
{
  auto* const scroll = new HintingScrollArea;
  scroll->setWidget(wrapped_widget);

  // Workaround for transparency issues on macOS. Not sure if this is still needed.
  scroll->viewport()->setAutoFillBackground(false);
  wrapped_widget->setAutoFillBackground(false);

  return scroll;
}

void WrapInScrollArea(QWidget* parent, QLayout* wrapped_layout)
{
  auto* widget = new QWidget;
  widget->setLayout(wrapped_layout);

  auto* scroll_area = GetWrappedWidget(widget);

  auto* scroll_layout = new QVBoxLayout;
  scroll_layout->addWidget(scroll_area);
  scroll_layout->setContentsMargins(0, 0, 0, 0);

  parent->setLayout(scroll_layout);
}