31
31
32
32
#include < QDebug>
33
33
#include < QLoggingCategory>
34
+ #include < qscrollbar.h>
34
35
#include < style.h>
35
36
36
37
Q_LOGGING_CATEGORY (CAT_DEVBROWSER, " DeviceBrowser" )
@@ -45,13 +46,19 @@ DeviceBrowser::DeviceBrowser(QWidget *parent)
45
46
this ->setFixedHeight (185 );
46
47
47
48
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 );
49
53
50
54
initBtns ();
51
55
52
56
connect (ui->btnHome , SIGNAL (clicked ()), this , SLOT (forwardRequestDeviceWithDirection ()));
53
57
connect (ui->btnAdd , SIGNAL (clicked ()), this , SLOT (forwardRequestDeviceWithDirection ()));
54
58
connect (this , SIGNAL (requestDevice (QString, int )), this , SLOT (updateSelectedDeviceIdx (QString)));
59
+
60
+ connect (scrollArea->horizontalScrollBar (), &QScrollBar::rangeChanged, this ,
61
+ &DeviceBrowser::onScrollRangeChanged);
55
62
}
56
63
57
64
DeviceBrowser::~DeviceBrowser ()
@@ -96,13 +103,16 @@ void DeviceBrowser::addDevice(QString id, Device *d, int position)
96
103
{
97
104
qInfo (CAT_DEVBROWSER) << " adding device " << id;
98
105
auto w = dynamic_cast <QAbstractButton *>(buildDeviceIcon (d, this ));
106
+ int spacerIndex = layout->indexOf (ui->hSpacer );
99
107
w->setProperty (devBrowserId, id);
100
- layout->insertWidget (position, w);
101
108
bg->addButton (w);
102
- if (position == -1 )
109
+ if (position == -1 ) {
110
+ layout->insertWidget (spacerIndex, w);
103
111
list.append (w);
104
- else
112
+ } else {
113
+ layout->insertWidget (position, w);
105
114
list.insert (position, w);
115
+ }
106
116
107
117
connect (w, &QAbstractButton::clicked, this , &DeviceBrowser::forwardRequestDeviceWithDirection);
108
118
}
@@ -230,6 +240,20 @@ DeviceIcon *DeviceBrowser::buildDeviceIcon(Device *d, QWidget *parent)
230
240
return devIcon;
231
241
}
232
242
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
+
233
257
/*
234
258
auto &&is = ui->wInfoPageStack;
235
259
auto &&hc = is->getHomepageControls();
0 commit comments