Skip to content

Commit

Permalink
core/devicebrowser: Added horizontal scroll bar.
Browse files Browse the repository at this point in the history
Signed-off-by: andreidanila1 <[email protected]>
  • Loading branch information
andreidanila1 committed Dec 13, 2024
1 parent 2734022 commit 22617de
Show file tree
Hide file tree
Showing 3 changed files with 298 additions and 202 deletions.
2 changes: 2 additions & 0 deletions core/include/core/devicebrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 28 additions & 4 deletions core/src/devicebrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <QDebug>
#include <QLoggingCategory>
#include <qscrollbar.h>
#include <style.h>

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

auto dbm = ui->wDeviceBrowserMenu;
layout = new QHBoxLayout(dbm);
layout = dynamic_cast<QHBoxLayout *>(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()
Expand Down Expand Up @@ -96,13 +103,16 @@ void DeviceBrowser::addDevice(QString id, Device *d, int position)
{
qInfo(CAT_DEVBROWSER) << "adding device " << id;
auto w = dynamic_cast<QAbstractButton *>(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);
}
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 22617de

Please sign in to comment.