From 6661a5d9464a8f0ea6b5e55579659aa04a6eb855 Mon Sep 17 00:00:00 2001 From: BruebachL <44814898+BruebachL@users.noreply.github.com> Date: Wed, 9 Apr 2025 18:08:59 +0200 Subject: [PATCH] Fix some display issues with settings button widget. (#5816) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Lukas BrĂ¼bach --- .../quick_settings/settings_button_widget.cpp | 18 ++--- .../quick_settings/settings_popup_widget.cpp | 78 ++++++++++++++++++- .../quick_settings/settings_popup_widget.h | 6 ++ 3 files changed, 89 insertions(+), 13 deletions(-) diff --git a/cockatrice/src/client/ui/widgets/quick_settings/settings_button_widget.cpp b/cockatrice/src/client/ui/widgets/quick_settings/settings_button_widget.cpp index 5d3307e52..e4fd0746f 100644 --- a/cockatrice/src/client/ui/widgets/quick_settings/settings_button_widget.cpp +++ b/cockatrice/src/client/ui/widgets/quick_settings/settings_button_widget.cpp @@ -6,7 +6,7 @@ #include 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); } } diff --git a/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.cpp b/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.cpp index 10b2c327c..258f1163e 100644 --- a/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.cpp +++ b/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.cpp @@ -3,15 +3,89 @@ #include #include #include +#include +#include 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); -} \ No newline at end of file +} diff --git a/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.h b/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.h index 291063662..d57c123c4 100644 --- a/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.h +++ b/cockatrice/src/client/ui/widgets/quick_settings/settings_popup_widget.h @@ -2,6 +2,7 @@ #define SETTINGS_POPUP_WIDGET_H #include +#include #include #include @@ -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