mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-10 16:24:45 -07:00
* move message_log_widget to game * move files * update headers * fix cmakelists * oracle fixes * split implementation out to cpp * fix recursive import * fix main file * format
This commit is contained in:
parent
f484c98152
commit
17dcaf9afa
337 changed files with 728 additions and 721 deletions
|
|
@ -0,0 +1,86 @@
|
|||
#include "settings_button_widget.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QHBoxLayout>
|
||||
#include <QMouseEvent>
|
||||
#include <QScreen>
|
||||
|
||||
SettingsButtonWidget::SettingsButtonWidget(QWidget *parent)
|
||||
: QWidget(parent), button(new QToolButton(this)), popup(new SettingsPopupWidget(nullptr))
|
||||
{
|
||||
|
||||
button->setIcon(QPixmap("theme:icons/cogwheel"));
|
||||
button->setCheckable(true);
|
||||
button->setFixedSize(32, 32);
|
||||
connect(button, &QToolButton::clicked, this, &SettingsButtonWidget::togglePopup);
|
||||
connect(popup, &SettingsPopupWidget::aboutToClose, this, &SettingsButtonWidget::onPopupClosed);
|
||||
|
||||
layout = new QHBoxLayout(this);
|
||||
layout->addWidget(button);
|
||||
layout->setContentsMargins(0, 0, 0, 0);
|
||||
setLayout(layout);
|
||||
}
|
||||
|
||||
void SettingsButtonWidget::addSettingsWidget(QWidget *toAdd) const
|
||||
{
|
||||
popup->addSettingsWidget(toAdd);
|
||||
}
|
||||
|
||||
void SettingsButtonWidget::removeSettingsWidget(QWidget *toRemove) const
|
||||
{
|
||||
popup->removeSettingsWidget(toRemove);
|
||||
}
|
||||
|
||||
void SettingsButtonWidget::setButtonIcon(QPixmap iconMap)
|
||||
{
|
||||
button->setIcon(iconMap);
|
||||
}
|
||||
|
||||
void SettingsButtonWidget::togglePopup()
|
||||
{
|
||||
if (popup->isVisible()) {
|
||||
popup->close();
|
||||
} else {
|
||||
popup->adjustSizeToFitScreen(); // Ensure proper size
|
||||
|
||||
QSize popupSize = popup->size();
|
||||
QPoint buttonGlobalPos = button->mapToGlobal(QPoint(0, button->height()));
|
||||
|
||||
QScreen *screen = QApplication::screenAt(buttonGlobalPos);
|
||||
QRect screenGeom = screen ? screen->availableGeometry() : QApplication::primaryScreen()->availableGeometry();
|
||||
|
||||
int x = buttonGlobalPos.x();
|
||||
int y = buttonGlobalPos.y();
|
||||
|
||||
// Adjust position to stay within screen bounds
|
||||
if (x + popupSize.width() > screenGeom.right()) {
|
||||
x = buttonGlobalPos.x() - popupSize.width() + button->width();
|
||||
}
|
||||
|
||||
if (y + popupSize.height() > screenGeom.bottom()) {
|
||||
y = buttonGlobalPos.y() - popupSize.height() - button->height();
|
||||
}
|
||||
|
||||
popup->move(x, y);
|
||||
popup->show();
|
||||
popup->setFocus();
|
||||
button->setChecked(true);
|
||||
}
|
||||
}
|
||||
|
||||
void SettingsButtonWidget::onPopupClosed() const
|
||||
{
|
||||
button->setChecked(false); // Ensure button unchecks when popup closes
|
||||
}
|
||||
|
||||
void SettingsButtonWidget::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
if (popup->isVisible() && !popup->geometry().contains(event->globalPosition().toPoint())) {
|
||||
#else
|
||||
if (popup->isVisible() && !popup->geometry().contains(event->globalPos())) {
|
||||
#endif
|
||||
popup->close();
|
||||
}
|
||||
QWidget::mousePressEvent(event);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef SETTINGS_BUTTON_WIDGET_H
|
||||
#define SETTINGS_BUTTON_WIDGET_H
|
||||
|
||||
#include "settings_popup_widget.h"
|
||||
|
||||
#include <QToolButton>
|
||||
#include <QWidget>
|
||||
|
||||
class SettingsButtonWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SettingsButtonWidget(QWidget *parent = nullptr);
|
||||
void addSettingsWidget(QWidget *toAdd) const;
|
||||
void removeSettingsWidget(QWidget *toRemove) const;
|
||||
void setButtonIcon(QPixmap iconMap);
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
||||
private slots:
|
||||
void togglePopup();
|
||||
void onPopupClosed() const;
|
||||
|
||||
private:
|
||||
QHBoxLayout *layout;
|
||||
QToolButton *button;
|
||||
|
||||
public:
|
||||
SettingsPopupWidget *popup;
|
||||
};
|
||||
|
||||
#endif // SETTINGS_BUTTON_WIDGET_H
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
#include "settings_popup_widget.h"
|
||||
|
||||
#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
|
||||
{
|
||||
containerLayout->addWidget(toAdd); // Add to containerWidget's layout
|
||||
}
|
||||
|
||||
void SettingsPopupWidget::removeSettingsWidget(QWidget *toRemove) const
|
||||
{
|
||||
containerLayout->removeWidget(toRemove);
|
||||
toRemove->deleteLater();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (!this->isAncestorOf(QApplication::focusWidget())) {
|
||||
close();
|
||||
}
|
||||
QWidget::focusOutEvent(event);
|
||||
}
|
||||
|
||||
void SettingsPopupWidget::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
emit aboutToClose();
|
||||
QWidget::closeEvent(event);
|
||||
}
|
||||
|
||||
void SettingsPopupWidget::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(this);
|
||||
painter.setPen(Qt::gray);
|
||||
painter.drawRect(rect().adjusted(0, 0, -1, -1));
|
||||
QWidget::paintEvent(event);
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef SETTINGS_POPUP_WIDGET_H
|
||||
#define SETTINGS_POPUP_WIDGET_H
|
||||
|
||||
#include <QLabel>
|
||||
#include <QScrollArea>
|
||||
#include <QVBoxLayout>
|
||||
#include <QWidget>
|
||||
|
||||
class SettingsPopupWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SettingsPopupWidget(QWidget *parent = nullptr);
|
||||
void addSettingsWidget(QWidget *toAdd) const;
|
||||
void removeSettingsWidget(QWidget *toRemove) const;
|
||||
void adjustSizeToFitScreen();
|
||||
|
||||
signals:
|
||||
void aboutToClose();
|
||||
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue