| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/QtUtils/WrapInScrollArea.h" |
| 5 | |
| 6 | #include <QFrame> |
| 7 | #include <QLayout> |
| 8 | #include <QPalette> |
| 9 | #include <QScrollArea> |
| 10 | #include <QScrollBar> |
| 11 | #include <QVBoxLayout> |
| 12 | #include <QWidget> |
| 13 | |
| 14 | namespace |
| 15 | { |
| 16 | |
| 17 | // This scroll area prefers the size of its underlying widget. |
| 18 | class HintingScrollArea final : public QScrollArea |
| 19 | { |
| 20 | public: |
| 21 | HintingScrollArea() |
| 22 | { |
| 23 | setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 24 | setWidgetResizable(true); |
| 25 | |
| 26 | // Make things not look horrendous on Windows. |
| 27 | setFrameStyle(QFrame::NoFrame); |
| 28 | } |
| 29 | |
| 30 | QSize sizeHint() const override |
| 31 | { |
| 32 | // Including the scrollbars in our hint can prevent |
| 33 | // a window undersized in one direction gaining unnecessary scrolling in both directions. |
| 34 | const auto scrollbar_padding = |
| 35 | QSize{verticalScrollBar()->sizeHint().width(), horizontalScrollBar()->sizeHint().height()}; |
| 36 | |
| 37 | const auto size_hint = widget()->sizeHint(); |
| 38 | |
| 39 | return size_hint + scrollbar_padding; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | } // namespace |
| 44 | |
| 45 | QWidget* GetWrappedWidget(QWidget* wrapped_widget) |
| 46 | { |
| 47 | auto* const scroll = new HintingScrollArea; |
| 48 | scroll->setWidget(wrapped_widget); |
| 49 | |
| 50 | // Workaround for transparency issues on macOS. Not sure if this is still needed. |
| 51 | scroll->viewport()->setAutoFillBackground(false); |
| 52 | wrapped_widget->setAutoFillBackground(false); |
| 53 | |
| 54 | return scroll; |
| 55 | } |
| 56 | |
| 57 | void WrapInScrollArea(QWidget* parent, QLayout* wrapped_layout) |
| 58 | { |
| 59 | auto* widget = new QWidget; |
| 60 | widget->setLayout(wrapped_layout); |
| 61 | |
| 62 | auto* scroll_area = GetWrappedWidget(widget); |
| 63 | |
| 64 | auto* scroll_layout = new QVBoxLayout; |
| 65 | scroll_layout->addWidget(scroll_area); |
| 66 | scroll_layout->setContentsMargins(0, 0, 0, 0); |
| 67 | |
| 68 | parent->setLayout(scroll_layout); |
| 69 | } |