Mantid
Loading...
Searching...
No Matches
WidgetScrollbarDecorator.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//----------------------------------
8// Includes
9//----------------------------------
11
12#include <QMainWindow>
13
14using namespace MantidQt::API;
15
16//-----------------------------------------------------------------------------
22WidgetScrollbarDecorator::WidgetScrollbarDecorator(QWidget *target) : m_target(target), m_enabled(false) {
23 // Off-screen widget to hold layout/widgets when scrolling disabled
24 m_offscreen = new QWidget(nullptr);
25
26 // This layout replaces dialog's main layout when scrolling enabled
27 m_layout = new QVBoxLayout(m_offscreen);
28 m_layout->setSpacing(0);
29 m_layout->setMargin(0);
30
31 // QScrollArea provides scrolling functionality
32 m_scrollarea = new QScrollArea(m_offscreen);
33 m_scrollarea->setFrameStyle(QFrame::NoFrame);
34 m_layout->addWidget(m_scrollarea);
35
36 // Viewport represents the inside of QScrollArea
37 // It will take over parentship of the layout and widgets of the target
38 m_viewport = new QWidget(m_scrollarea);
39 m_scrollarea->setWidget(m_viewport);
40 m_scrollarea->setWidgetResizable(true);
41
42 // With QMainWindows we must work on the centralWidget instead
43 auto mainwindow = dynamic_cast<QMainWindow *>(m_target);
44 if (mainwindow)
45 m_target = mainwindow->centralWidget();
46}
47
48//-----------------------------------------------------------------------------
51 // Must be deleted manually since it has no parent
52 delete m_offscreen;
53}
54
55//-----------------------------------------------------------------------------
62
63//-----------------------------------------------------------------------------
88 // Only enable if scrollbars were previously disabled
89 if (enable && !enabled()) {
90 m_viewport->setLayout(m_target->layout());
91 m_target->setLayout(m_layout);
92 }
93
94 // Only disable if scrollbars were previously enabled
95 if (!enable && enabled()) {
96 m_offscreen->setLayout(m_target->layout());
97 m_target->setLayout(m_viewport->layout());
98 }
99
100 m_enabled = enable;
101}
102
103//-----------------------------------------------------------------------------
116void WidgetScrollbarDecorator::setThresholdWidth(int width) { m_viewport->setMinimumWidth(width); }
117
118//-----------------------------------------------------------------------------
132
133//-----------------------------------------------------------------------------
147void WidgetScrollbarDecorator::setThresholdSize(int width, int height) { m_viewport->setMinimumSize(width, height); }
double height
Definition: GetAllEi.cpp:155
void setThresholdSize(int width, int height)
Set the size, in pixels, at which scrollbars should appear.
QWidget * m_target
The target widget that is given scrollbars.
QWidget * m_offscreen
Used to hold above widgets when disabled.
QVBoxLayout * m_layout
Main layout used when scrollable.
WidgetScrollbarDecorator(QWidget *target)
Constructor.
QWidget * m_viewport
Single widget inside QScrollArea.
bool enabled() const
Check whether the target is currently scrollable.
bool m_enabled
Whether the target is currently scrollable.
void setEnabled(bool enable)
Enable or disable scrollable behaviour.
QScrollArea * m_scrollarea
Used to provide scrolling functionality.
void setThresholdHeight(int height)
Set the height, in pixels, at which scrollbars should appear.
void setThresholdWidth(int width)
Set the width, in pixels, at which scrollbars should appear.