Fix some display issues with settings button widget. (#5816)

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
BruebachL 2025-04-09 18:08:59 +02:00 committed by GitHub
parent 53e9a91dc6
commit 6661a5d946
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 89 additions and 13 deletions

View file

@ -6,7 +6,7 @@
#include <QScreen>
SettingsButtonWidget::SettingsButtonWidget(QWidget *parent)
: QWidget(parent), button(new QToolButton(this)), popup(new SettingsPopupWidget(this))
: QWidget(parent), button(new QToolButton(this)), popup(new SettingsPopupWidget(nullptr))
{
button->setIcon(QPixmap("theme:icons/cogwheel"));
@ -31,34 +31,30 @@ void SettingsButtonWidget::togglePopup()
if (popup->isVisible()) {
popup->close();
} else {
// Ensure popup size is known before positioning
popup->adjustSize();
QSize popupSize = popup->size();
popup->adjustSizeToFitScreen(); // Ensure proper size
// Get button position
QSize popupSize = popup->size();
QPoint buttonGlobalPos = button->mapToGlobal(QPoint(0, button->height()));
// Get screen geometry
QScreen *screen = QApplication::screenAt(buttonGlobalPos);
QRect screenGeom = screen ? screen->availableGeometry() : QApplication::primaryScreen()->availableGeometry();
int x = buttonGlobalPos.x();
int y = buttonGlobalPos.y();
// Adjust X position if popup overflows the right side of the screen
// Adjust position to stay within screen bounds
if (x + popupSize.width() > screenGeom.right()) {
x = buttonGlobalPos.x() - popupSize.width() + button->width(); // Move left
x = buttonGlobalPos.x() - popupSize.width() + button->width();
}
// Adjust Y position if popup overflows the bottom of the screen
if (y + popupSize.height() > screenGeom.bottom()) {
y = buttonGlobalPos.y() - popupSize.height() - button->height(); // Move up
y = buttonGlobalPos.y() - popupSize.height() - button->height();
}
popup->move(x, y);
popup->show();
popup->setFocus();
button->setChecked(true); // Ensure button is checked when popup is visible
button->setChecked(true);
}
}

View file

@ -3,15 +3,89 @@
#include <QApplication>
#include <QFocusEvent>
#include <QPainter>
#include <QScreen>
#include <QScrollArea>
SettingsPopupWidget::SettingsPopupWidget(QWidget *parent) : QWidget(parent, Qt::Popup | Qt::FramelessWindowHint)
{
// Main layout for the popup itself
layout = new QVBoxLayout(this);
// Container for the content (with or without scroll area)
containerWidget = new QWidget();
containerLayout = new QVBoxLayout(containerWidget); // Store a separate layout
containerWidget->setLayout(containerLayout); // Make sure containerWidget has its layout
// Add the container widget directly to the layout initially
layout->addWidget(containerWidget); // Initially, we add the containerWidget without a scroll area
setLayout(layout);
scrollArea = nullptr; // Initialize scrollArea pointer to null
}
void SettingsPopupWidget::addSettingsWidget(QWidget *toAdd) const
{
layout->addWidget(toAdd);
containerLayout->addWidget(toAdd); // Add to containerWidget's layout
}
void SettingsPopupWidget::adjustSizeToFitScreen()
{
QScreen *screen = QApplication::screenAt(this->pos());
QRect screenGeom = screen ? screen->availableGeometry() : QApplication::primaryScreen()->availableGeometry();
int maxHeight = screenGeom.height() / 2; // Limit height to 50% of screen
// Adjust the container widget's size hint to get the actual size of content
containerWidget->adjustSize();
int contentHeight = containerWidget->sizeHint().height();
// If content height exceeds maxHeight, we need to scroll
if (contentHeight > maxHeight) {
// Create a scroll area and add the container widget to it if not already created
if (!scrollArea) {
scrollArea = new QScrollArea(this);
scrollArea->setWidgetResizable(true);
scrollArea->setFrameShape(QFrame::NoFrame);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // No horizontal scrollbar
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); // Enable vertical scrollbar when needed
}
// Set maximum height for the scroll area and show vertical scrollbar
scrollArea->setMaximumHeight(maxHeight);
// Resize the popup widget without squishing the content
resize(sizeHint().width(), maxHeight); // Ensure content width is kept intact
// Add scrollArea to layout if not already added
if (layout->count() == 1) { // We only have one widget (containerWidget) at the start
layout->addWidget(scrollArea);
}
// Set the scroll area widget
scrollArea->setWidget(containerWidget);
} else {
// If the scroll area exists, remove it
if (scrollArea) {
layout->removeWidget(scrollArea);
delete scrollArea;
scrollArea = nullptr; // Reset the pointer
}
// Set the containerWidget directly without scrollArea and adjust its height
resize(sizeHint().width(), contentHeight); // Resize the widget based on content height
layout->addWidget(containerWidget); // Re-add the containerWidget without scroll area
}
// Ensure layout updates after changes
layout->update(); // Update layout for proper size fitting
updateGeometry(); // Update widget geometry to reflect changes
}
void SettingsPopupWidget::resizeEvent(QResizeEvent *event)
{
// Make sure the scroll area and popup adjust to new size constraints
adjustSizeToFitScreen();
QWidget::resizeEvent(event);
}
void SettingsPopupWidget::focusOutEvent(QFocusEvent *event)
@ -34,4 +108,4 @@ void SettingsPopupWidget::paintEvent(QPaintEvent *event)
painter.setPen(Qt::gray);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
QWidget::paintEvent(event);
}
}

View file

@ -2,6 +2,7 @@
#define SETTINGS_POPUP_WIDGET_H
#include <QLabel>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QWidget>
@ -12,6 +13,7 @@ class SettingsPopupWidget : public QWidget
public:
explicit SettingsPopupWidget(QWidget *parent = nullptr);
void addSettingsWidget(QWidget *toAdd) const;
void adjustSizeToFitScreen();
signals:
void aboutToClose();
@ -20,8 +22,12 @@ protected:
void focusOutEvent(QFocusEvent *event) override;
void closeEvent(QCloseEvent *event) override;
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *event) override;
QVBoxLayout *layout;
QVBoxLayout *containerLayout;
QScrollArea *scrollArea = nullptr;
QWidget *containerWidget;
};
#endif // SETTINGSPOPUP_H