mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-09 11:33:25 -08:00
Runtime inversion is not a theming system: it rotates hue, breaks duotone art and mishandles alpha, and a QPixmap snapshot cannot follow a palette change. Redraw the top-level UI glyph set on a 24x24 grid with `currentColor` as the paint token, drop Inkscape/Sodipodi cruft, and delete the icons whose metaphor was duplicated (cogwheel/gear -> settings, scale_balanced -> scales, update.png -> reload). Load the glyphs through `loadThemedIcon` so the engine resolves the token from the palette on every request, and let `themePixmap` use the same resolution for one-shot pixmaps. `loadColorAdjustedPixmap` is gone; every caller now keeps a QIcon, which `SettingsButtonWidget::setButtonIcon` accepts instead of a QPixmap. Content art (mana, counters, countries) keeps its authored colors. Add a render test covering aspect ratio, palette flips, icon modes and high-DPI pixmaps.
129 lines
3.8 KiB
C++
129 lines
3.8 KiB
C++
#include "settings_button_widget.h"
|
|
|
|
#include "../../theme_icon.h"
|
|
#include "settings_popup_widget.h"
|
|
|
|
#include <QApplication>
|
|
#include <QHBoxLayout>
|
|
#include <QIcon>
|
|
#include <QMouseEvent>
|
|
#include <QPoint>
|
|
#include <QPointF>
|
|
#include <QRect>
|
|
#include <QScreen>
|
|
#include <QSize>
|
|
#include <QSizePolicy>
|
|
#include <QStringLiteral>
|
|
#include <QToolButton>
|
|
#include <libcockatrice/utility/qt_utils.h>
|
|
#include <qnamespace.h>
|
|
|
|
SettingsButtonWidget::SettingsButtonWidget(QWidget *parent)
|
|
: QWidget(parent), button(new QToolButton(this)), popup(new SettingsPopupWidget(nullptr))
|
|
{
|
|
button->setIcon(loadThemedIcon(QStringLiteral("icons/settings")));
|
|
button->setCheckable(true);
|
|
button->setFixedSize(32, 32);
|
|
connect(button, &QToolButton::clicked, this, &SettingsButtonWidget::togglePopup);
|
|
connect(popup, &SettingsPopupWidget::aboutToClose, this, &SettingsButtonWidget::onPopupClosed);
|
|
|
|
layout = QtUtils::makeLayout<QHBoxLayout>(this);
|
|
layout->addWidget(button);
|
|
setLayout(layout);
|
|
}
|
|
|
|
SettingsButtonWidget::~SettingsButtonWidget()
|
|
{
|
|
// We don't parent the popup because it might lead to better behavior on certain window managers.
|
|
// So we have to manually delete it
|
|
popup->deleteLater();
|
|
}
|
|
|
|
void SettingsButtonWidget::addSettingsWidget(QWidget *toAdd) const
|
|
{
|
|
popup->addSettingsWidget(toAdd);
|
|
}
|
|
|
|
void SettingsButtonWidget::removeSettingsWidget(QWidget *toRemove) const
|
|
{
|
|
popup->removeSettingsWidget(toRemove);
|
|
}
|
|
|
|
void SettingsButtonWidget::setButtonIcon(const QIcon &icon)
|
|
{
|
|
button->setIcon(icon);
|
|
}
|
|
|
|
void SettingsButtonWidget::setButtonText(const QString &text)
|
|
{
|
|
buttonText = text;
|
|
|
|
button->setMinimumSize(QSize(0, 0));
|
|
button->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
|
|
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
button->setText(text);
|
|
button->setFixedHeight(32);
|
|
button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
|
|
|
button->setMinimumWidth(32); // icon-only fallback minimum
|
|
}
|
|
|
|
void SettingsButtonWidget::setCompact(bool _compact)
|
|
{
|
|
compact = _compact;
|
|
if (compact) {
|
|
button->setToolButtonStyle(Qt::ToolButtonIconOnly);
|
|
button->setFixedWidth(32);
|
|
} else {
|
|
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
button->setText(buttonText);
|
|
button->setFixedWidth(QWIDGETSIZE_MAX); // release fixed width
|
|
button->setMinimumWidth(32);
|
|
button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
|
|
}
|
|
}
|
|
|
|
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 (popup->isVisible() && !popup->geometry().contains(event->globalPosition().toPoint())) {
|
|
popup->close();
|
|
}
|
|
QWidget::mousePressEvent(event);
|
|
}
|