From 6d60f8e01ce738aea91461c8f8dddd3d48a62041 Mon Sep 17 00:00:00 2001 From: andreidanila1 Date: Fri, 13 Dec 2024 15:17:36 +0200 Subject: [PATCH] core/devicebrowser: Added horizontal scroll bar. Signed-off-by: andreidanila1 --- core/include/core/devicebrowser.h | 2 + core/src/devicebrowser.cpp | 32 +- core/ui/devicebrowser.ui | 466 +++++++++++++++++------------- 3 files changed, 298 insertions(+), 202 deletions(-) diff --git a/core/include/core/devicebrowser.h b/core/include/core/devicebrowser.h index beac1ecc46..5a943bd615 100644 --- a/core/include/core/devicebrowser.h +++ b/core/include/core/devicebrowser.h @@ -64,6 +64,8 @@ private Q_SLOTS: void updateSelectedDeviceIdx(QString); void forwardRequestDeviceWithDirection(); + void onScrollRangeChanged(int min, int max); + private: void initBtns(); DeviceIcon *buildDeviceIcon(Device *d, QWidget *parent = nullptr); diff --git a/core/src/devicebrowser.cpp b/core/src/devicebrowser.cpp index 5ae442c989..55a8791d17 100644 --- a/core/src/devicebrowser.cpp +++ b/core/src/devicebrowser.cpp @@ -31,6 +31,7 @@ #include #include +#include #include Q_LOGGING_CATEGORY(CAT_DEVBROWSER, "DeviceBrowser") @@ -45,13 +46,19 @@ DeviceBrowser::DeviceBrowser(QWidget *parent) this->setFixedHeight(185); auto dbm = ui->wDeviceBrowserMenu; - layout = new QHBoxLayout(dbm); + layout = dynamic_cast(dbm->layout()); + + auto scrollArea = ui->scrollArea; + scrollArea->horizontalScrollBar()->setVisible(false); initBtns(); connect(ui->btnHome, SIGNAL(clicked()), this, SLOT(forwardRequestDeviceWithDirection())); connect(ui->btnAdd, SIGNAL(clicked()), this, SLOT(forwardRequestDeviceWithDirection())); connect(this, SIGNAL(requestDevice(QString, int)), this, SLOT(updateSelectedDeviceIdx(QString))); + + connect(scrollArea->horizontalScrollBar(), &QScrollBar::rangeChanged, this, + &DeviceBrowser::onScrollRangeChanged); } DeviceBrowser::~DeviceBrowser() @@ -96,13 +103,16 @@ void DeviceBrowser::addDevice(QString id, Device *d, int position) { qInfo(CAT_DEVBROWSER) << "adding device " << id; auto w = dynamic_cast(buildDeviceIcon(d, this)); + int spacerIndex = layout->indexOf(ui->hSpacer); w->setProperty(devBrowserId, id); - layout->insertWidget(position, w); bg->addButton(w); - if(position == -1) + if(position == -1) { + layout->insertWidget(spacerIndex, w); list.append(w); - else + } else { + layout->insertWidget(position, w); list.insert(position, w); + } connect(w, &QAbstractButton::clicked, this, &DeviceBrowser::forwardRequestDeviceWithDirection); } @@ -230,6 +240,20 @@ DeviceIcon *DeviceBrowser::buildDeviceIcon(Device *d, QWidget *parent) return devIcon; } +// Used to display the scrollbar when needed and to maintain its size in the scroll area when not needed. +// We chose this approach because for the Qt::ScrollBarAsNeeded policy the size of the scrollball cannot be retained +// with Util::retainWidgetSizeWhenHidden because the resizing of the scrollbar is done dynamically (withoud using the +// show/hide default functions). +void DeviceBrowser::onScrollRangeChanged(int min, int max) +{ + auto scrollArea = ui->scrollArea; + if(max > min) { + scrollArea->horizontalScrollBar()->setVisible(true); + } else { + scrollArea->horizontalScrollBar()->setVisible(false); + } +} + /* auto &&is = ui->wInfoPageStack; auto &&hc = is->getHomepageControls(); diff --git a/core/ui/devicebrowser.ui b/core/ui/devicebrowser.ui index d4de72d3dc..fa61640efb 100644 --- a/core/ui/devicebrowser.ui +++ b/core/ui/devicebrowser.ui @@ -7,11 +7,11 @@ 0 0 464 - 108 + 140 - + 0 0 @@ -42,218 +42,288 @@ 0 - - - - - Qt::Vertical - - - - 0 - 0 - - - - - - - - - 0 - 0 - - - - - 0 - 0 - - - - QFrame::StyledPanel - - - QFrame::Raised - - - - 9 + + + + 0 + 0 + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Orientation::Vertical - - 9 + + + 0 + 0 + - - - - - 0 - 0 - - - - - 40 - 40 - - - - - 40 - 40 - - - - - - - - :/gui/icons/home.svg - - - - - 40 - 40 - - - - true - - - false - - - - - - - - - - - 0 - 0 - - - - - 0 - 0 - - - - QFrame::StyledPanel - - - QFrame::Raised - - - - 9 + + + + + + + 0 + 0 + - - 9 + + + 0 + 0 + - - - - - 0 - 0 - - - - - 40 - 40 - - - - - 40 - 40 - - - - - - - - :/gui/icons/add.svg - - - - - 40 - 40 - - - - true - - - - - - - - - - Qt::Vertical - - - - 0 - 0 - - - - - + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + 9 + + + 9 + + + + + + 0 + 0 + + + + + 40 + 40 + + + + + 40 + 40 + + + + + + + + :/gui/icons/home.svg + + + + + 40 + 40 + + + + true + + + false + + + + + + + + + + + 0 + 0 + + + + + 0 + 0 + + + + QFrame::Shape::StyledPanel + + + QFrame::Shadow::Raised + + + + 9 + + + 9 + + + + + + 0 + 0 + + + + + 40 + 40 + + + + + 40 + 40 + + + + + + + + :/gui/icons/add.svg + + + + + 40 + 40 + + + + true + + + + + + + + + + Qt::Orientation::Vertical + + + + 0 + 0 + + + + + + - + - + 0 0 - - - - - - Qt::Orientation::Horizontal + + Qt::ScrollBarPolicy::ScrollBarAlwaysOff + + + Qt::ScrollBarPolicy::ScrollBarAlwaysOn + + + QAbstractScrollArea::SizeAdjustPolicy::AdjustToContents - - - 40 - 20 - + + true - + + + + 0 + 0 + 392 + 124 + + + + + 0 + 0 + + + + + 0 + + + + + Qt::Orientation::Horizontal + + + + 0 + 0 + + + + + + + - - - 0 + + + + 0 + 0 + - - - - Qt::Vertical - - - - 20 - 40 - - - - - + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Qt::Orientation::Vertical + + + + 0 + 40 + + + + + +