mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-30 18:43:55 -07:00
Fix some display issues with settings button widget. (#5816)
Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
53e9a91dc6
commit
6661a5d946
3 changed files with 89 additions and 13 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
#include <QScreen>
|
#include <QScreen>
|
||||||
|
|
||||||
SettingsButtonWidget::SettingsButtonWidget(QWidget *parent)
|
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"));
|
button->setIcon(QPixmap("theme:icons/cogwheel"));
|
||||||
|
|
@ -31,34 +31,30 @@ void SettingsButtonWidget::togglePopup()
|
||||||
if (popup->isVisible()) {
|
if (popup->isVisible()) {
|
||||||
popup->close();
|
popup->close();
|
||||||
} else {
|
} else {
|
||||||
// Ensure popup size is known before positioning
|
popup->adjustSizeToFitScreen(); // Ensure proper size
|
||||||
popup->adjustSize();
|
|
||||||
QSize popupSize = popup->size();
|
|
||||||
|
|
||||||
// Get button position
|
QSize popupSize = popup->size();
|
||||||
QPoint buttonGlobalPos = button->mapToGlobal(QPoint(0, button->height()));
|
QPoint buttonGlobalPos = button->mapToGlobal(QPoint(0, button->height()));
|
||||||
|
|
||||||
// Get screen geometry
|
|
||||||
QScreen *screen = QApplication::screenAt(buttonGlobalPos);
|
QScreen *screen = QApplication::screenAt(buttonGlobalPos);
|
||||||
QRect screenGeom = screen ? screen->availableGeometry() : QApplication::primaryScreen()->availableGeometry();
|
QRect screenGeom = screen ? screen->availableGeometry() : QApplication::primaryScreen()->availableGeometry();
|
||||||
|
|
||||||
int x = buttonGlobalPos.x();
|
int x = buttonGlobalPos.x();
|
||||||
int y = buttonGlobalPos.y();
|
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()) {
|
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()) {
|
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->move(x, y);
|
||||||
popup->show();
|
popup->show();
|
||||||
popup->setFocus();
|
popup->setFocus();
|
||||||
button->setChecked(true); // Ensure button is checked when popup is visible
|
button->setChecked(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,89 @@
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QFocusEvent>
|
#include <QFocusEvent>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <QScreen>
|
||||||
|
#include <QScrollArea>
|
||||||
|
|
||||||
SettingsPopupWidget::SettingsPopupWidget(QWidget *parent) : QWidget(parent, Qt::Popup | Qt::FramelessWindowHint)
|
SettingsPopupWidget::SettingsPopupWidget(QWidget *parent) : QWidget(parent, Qt::Popup | Qt::FramelessWindowHint)
|
||||||
{
|
{
|
||||||
|
// Main layout for the popup itself
|
||||||
layout = new QVBoxLayout(this);
|
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
|
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)
|
void SettingsPopupWidget::focusOutEvent(QFocusEvent *event)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#define SETTINGS_POPUP_WIDGET_H
|
#define SETTINGS_POPUP_WIDGET_H
|
||||||
|
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
|
#include <QScrollArea>
|
||||||
#include <QVBoxLayout>
|
#include <QVBoxLayout>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
|
|
@ -12,6 +13,7 @@ class SettingsPopupWidget : public QWidget
|
||||||
public:
|
public:
|
||||||
explicit SettingsPopupWidget(QWidget *parent = nullptr);
|
explicit SettingsPopupWidget(QWidget *parent = nullptr);
|
||||||
void addSettingsWidget(QWidget *toAdd) const;
|
void addSettingsWidget(QWidget *toAdd) const;
|
||||||
|
void adjustSizeToFitScreen();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void aboutToClose();
|
void aboutToClose();
|
||||||
|
|
@ -20,8 +22,12 @@ protected:
|
||||||
void focusOutEvent(QFocusEvent *event) override;
|
void focusOutEvent(QFocusEvent *event) override;
|
||||||
void closeEvent(QCloseEvent *event) override;
|
void closeEvent(QCloseEvent *event) override;
|
||||||
void paintEvent(QPaintEvent *event) override;
|
void paintEvent(QPaintEvent *event) override;
|
||||||
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
|
||||||
QVBoxLayout *layout;
|
QVBoxLayout *layout;
|
||||||
|
QVBoxLayout *containerLayout;
|
||||||
|
QScrollArea *scrollArea = nullptr;
|
||||||
|
QWidget *containerWidget;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SETTINGSPOPUP_H
|
#endif // SETTINGSPOPUP_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue