Skip to content

Commit ecce573

Browse files
committed
core/devicebrowser: Added horizontal scroll bar.
Signed-off-by: andreidanila1 <[email protected]>
1 parent d70c3e1 commit ecce573

File tree

3 files changed

+298
-202
lines changed

3 files changed

+298
-202
lines changed

Diff for: core/include/core/devicebrowser.h

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ private Q_SLOTS:
6464
void updateSelectedDeviceIdx(QString);
6565
void forwardRequestDeviceWithDirection();
6666

67+
void onScrollRangeChanged(int min, int max);
68+
6769
private:
6870
void initBtns();
6971
DeviceIcon *buildDeviceIcon(Device *d, QWidget *parent = nullptr);

Diff for: core/src/devicebrowser.cpp

+28-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include <QDebug>
3333
#include <QLoggingCategory>
34+
#include <qscrollbar.h>
3435
#include <style.h>
3536

3637
Q_LOGGING_CATEGORY(CAT_DEVBROWSER, "DeviceBrowser")
@@ -45,13 +46,19 @@ DeviceBrowser::DeviceBrowser(QWidget *parent)
4546
this->setFixedHeight(185);
4647

4748
auto dbm = ui->wDeviceBrowserMenu;
48-
layout = new QHBoxLayout(dbm);
49+
layout = dynamic_cast<QHBoxLayout *>(dbm->layout());
50+
51+
auto scrollArea = ui->scrollArea;
52+
scrollArea->horizontalScrollBar()->setVisible(false);
4953

5054
initBtns();
5155

5256
connect(ui->btnHome, SIGNAL(clicked()), this, SLOT(forwardRequestDeviceWithDirection()));
5357
connect(ui->btnAdd, SIGNAL(clicked()), this, SLOT(forwardRequestDeviceWithDirection()));
5458
connect(this, SIGNAL(requestDevice(QString, int)), this, SLOT(updateSelectedDeviceIdx(QString)));
59+
60+
connect(scrollArea->horizontalScrollBar(), &QScrollBar::rangeChanged, this,
61+
&DeviceBrowser::onScrollRangeChanged);
5562
}
5663

5764
DeviceBrowser::~DeviceBrowser()
@@ -96,13 +103,16 @@ void DeviceBrowser::addDevice(QString id, Device *d, int position)
96103
{
97104
qInfo(CAT_DEVBROWSER) << "adding device " << id;
98105
auto w = dynamic_cast<QAbstractButton *>(buildDeviceIcon(d, this));
106+
int spacerIndex = layout->indexOf(ui->hSpacer);
99107
w->setProperty(devBrowserId, id);
100-
layout->insertWidget(position, w);
101108
bg->addButton(w);
102-
if(position == -1)
109+
if(position == -1) {
110+
layout->insertWidget(spacerIndex, w);
103111
list.append(w);
104-
else
112+
} else {
113+
layout->insertWidget(position, w);
105114
list.insert(position, w);
115+
}
106116

107117
connect(w, &QAbstractButton::clicked, this, &DeviceBrowser::forwardRequestDeviceWithDirection);
108118
}
@@ -230,6 +240,20 @@ DeviceIcon *DeviceBrowser::buildDeviceIcon(Device *d, QWidget *parent)
230240
return devIcon;
231241
}
232242

243+
// Used to display the scrollbar when needed and to maintain its size in the scroll area when not needed.
244+
// We chose this approach because for the Qt::ScrollBarAsNeeded policy the size of the scrollball cannot be retained
245+
// with Util::retainWidgetSizeWhenHidden because the resizing of the scrollbar is done dynamically (withoud using the
246+
// show/hide default functions).
247+
void DeviceBrowser::onScrollRangeChanged(int min, int max)
248+
{
249+
auto scrollArea = ui->scrollArea;
250+
if(max > min) {
251+
scrollArea->horizontalScrollBar()->setVisible(true);
252+
} else {
253+
scrollArea->horizontalScrollBar()->setVisible(false);
254+
}
255+
}
256+
233257
/*
234258
auto &&is = ui->wInfoPageStack;
235259
auto &&hc = is->getHomepageControls();

0 commit comments

Comments
 (0)