From 47311b1dfd53dc18e65f364e373713646b65c99a Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Tue, 25 Feb 2025 15:32:45 -0800 Subject: [PATCH] Expand/shrink card view window on double click (#5652) --- cockatrice/src/dialogs/dlg_settings.cpp | 39 ++++++++++++- cockatrice/src/dialogs/dlg_settings.h | 5 ++ .../src/game/zones/view_zone_widget.cpp | 57 +++++++++++++++++-- cockatrice/src/game/zones/view_zone_widget.h | 4 +- cockatrice/src/settings/cache_settings.cpp | 7 +++ cockatrice/src/settings/cache_settings.h | 6 ++ dbconverter/src/mocks.cpp | 3 + tests/carddatabase/mocks.cpp | 3 + 8 files changed, 116 insertions(+), 8 deletions(-) diff --git a/cockatrice/src/dialogs/dlg_settings.cpp b/cockatrice/src/dialogs/dlg_settings.cpp index cfb84bfdb..e80598e5a 100644 --- a/cockatrice/src/dialogs/dlg_settings.cpp +++ b/cockatrice/src/dialogs/dlg_settings.cpp @@ -401,8 +401,13 @@ AppearanceSettingsPage::AppearanceSettingsPage() cardViewInitialRowsMaxBox.setRange(1, 999); cardViewInitialRowsMaxBox.setValue(SettingsCache::instance().getCardViewInitialRowsMax()); - connect(&cardViewInitialRowsMaxBox, qOverload(&QSpinBox::valueChanged), &SettingsCache::instance(), - &SettingsCache::setCardViewInitialRowsMax); + connect(&cardViewInitialRowsMaxBox, qOverload(&QSpinBox::valueChanged), this, + &AppearanceSettingsPage::cardViewInitialRowsMaxChanged); + + cardViewExpandedRowsMaxBox.setRange(1, 999); + cardViewExpandedRowsMaxBox.setValue(SettingsCache::instance().getCardViewExpandedRowsMax()); + connect(&cardViewExpandedRowsMaxBox, qOverload(&QSpinBox::valueChanged), this, + &AppearanceSettingsPage::cardViewExpandedRowsMaxChanged); auto *cardsGrid = new QGridLayout; cardsGrid->addWidget(&displayCardNamesCheckBox, 0, 0, 1, 2); @@ -414,6 +419,8 @@ AppearanceSettingsPage::AppearanceSettingsPage() cardsGrid->addWidget(&verticalCardOverlapPercentBox, 5, 1, 1, 1); cardsGrid->addWidget(&cardViewInitialRowsMaxLabel, 6, 0); cardsGrid->addWidget(&cardViewInitialRowsMaxBox, 6, 1); + cardsGrid->addWidget(&cardViewExpandedRowsMaxLabel, 7, 0); + cardsGrid->addWidget(&cardViewExpandedRowsMaxBox, 7, 1); cardsGroupBox = new QGroupBox; cardsGroupBox->setLayout(cardsGrid); @@ -500,6 +507,32 @@ void AppearanceSettingsPage::showShortcutsChanged(QT_STATE_CHANGED_T value) qApp->setAttribute(Qt::AA_DontShowShortcutsInContextMenus, value == 0); // 0 = unchecked } +/** + * Updates the settings for cardViewInitialRowsMax. + * Forces expanded rows max to always be >= initial rows max + * @param value The new value + */ +void AppearanceSettingsPage::cardViewInitialRowsMaxChanged(int value) +{ + SettingsCache::instance().setCardViewInitialRowsMax(value); + if (cardViewExpandedRowsMaxBox.value() < value) { + cardViewExpandedRowsMaxBox.setValue(value); + } +} + +/** + * Updates the settings for cardViewExpandedRowsMax. + * Forces initial rows max to always be <= expanded rows max + * @param value The new value + */ +void AppearanceSettingsPage::cardViewExpandedRowsMaxChanged(int value) +{ + SettingsCache::instance().setCardViewExpandedRowsMax(value); + if (cardViewInitialRowsMaxBox.value() > value) { + cardViewInitialRowsMaxBox.setValue(value); + } +} + void AppearanceSettingsPage::retranslateUi() { themeGroupBox->setTitle(tr("Theme settings")); @@ -526,6 +559,8 @@ void AppearanceSettingsPage::retranslateUi() tr("Minimum overlap percentage of cards on the stack and in vertical hand")); cardViewInitialRowsMaxLabel.setText(tr("Maximum initial height for card view window:")); cardViewInitialRowsMaxBox.setSuffix(tr(" rows")); + cardViewExpandedRowsMaxLabel.setText(tr("Maximum expanded height for card view window:")); + cardViewExpandedRowsMaxBox.setSuffix(tr(" rows")); handGroupBox->setTitle(tr("Hand layout")); horizontalHandCheckBox.setText(tr("Display hand horizontally (wastes space)")); diff --git a/cockatrice/src/dialogs/dlg_settings.h b/cockatrice/src/dialogs/dlg_settings.h index 5b0f58078..4438e3f6c 100644 --- a/cockatrice/src/dialogs/dlg_settings.h +++ b/cockatrice/src/dialogs/dlg_settings.h @@ -91,6 +91,9 @@ private slots: void openThemeLocation(); void showShortcutsChanged(QT_STATE_CHANGED_T enabled); + void cardViewInitialRowsMaxChanged(int value); + void cardViewExpandedRowsMaxChanged(int value); + private: QLabel themeLabel; QComboBox themeBox; @@ -110,6 +113,8 @@ private: QSpinBox verticalCardOverlapPercentBox; QLabel cardViewInitialRowsMaxLabel; QSpinBox cardViewInitialRowsMaxBox; + QLabel cardViewExpandedRowsMaxLabel; + QSpinBox cardViewExpandedRowsMaxBox; QCheckBox horizontalHandCheckBox; QCheckBox leftJustifiedHandCheckBox; QCheckBox invertVerticalCoordinateCheckBox; diff --git a/cockatrice/src/game/zones/view_zone_widget.cpp b/cockatrice/src/game/zones/view_zone_widget.cpp index 531ca06df..7dd21b9d5 100644 --- a/cockatrice/src/game/zones/view_zone_widget.cpp +++ b/cockatrice/src/game/zones/view_zone_widget.cpp @@ -132,7 +132,7 @@ ZoneViewWidget::ZoneViewWidget(Player *_player, setLayout(vbox); - connect(zone, &ZoneViewZone::optimumRectChanged, this, &ZoneViewWidget::resizeToZoneContents); + connect(zone, &ZoneViewZone::optimumRectChanged, this, [this] { resizeToZoneContents(); }); connect(zone, &ZoneViewZone::closed, this, &ZoneViewWidget::zoneDeleted); zone->initializeCards(cardList); @@ -258,14 +258,25 @@ void ZoneViewWidget::resizeScrollbar(const qreal newZoneHeight) scrollBar->setMaximum(newMax); } +/** + * Maps a height that is given as number of rows of cards to the actual height, given in pixels. + * + * @param rows Rows of cards + * @return The height in pixels + */ +static qreal rowsToHeight(int rows) +{ + const qreal cardsHeight = (rows + 1) * (CARD_HEIGHT / 3); + return cardsHeight + 5; // +5 padding to make the cutoff look nicer +} + /** * Calculates the max initial height from the settings. * The max initial height setting is given as number of rows, so we need to map it to a height. **/ static qreal calcMaxInitialHeight() { - const qreal cardsHeight = (SettingsCache::instance().getCardViewInitialRowsMax() + 1) * (CARD_HEIGHT / 3); - return cardsHeight + 5; // +5 padding to make the cutoff look nicer + return rowsToHeight(SettingsCache::instance().getCardViewInitialRowsMax()); } /** @@ -282,7 +293,7 @@ static qreal determineNewZoneHeight(qreal oldZoneHeight) return calcMaxInitialHeight(); } -void ZoneViewWidget::resizeToZoneContents() +void ZoneViewWidget::resizeToZoneContents(bool forceInitialHeight) { QRectF zoneRect = zone->getOptimumRect(); qreal totalZoneHeight = zoneRect.height(); @@ -293,7 +304,7 @@ void ZoneViewWidget::resizeToZoneContents() QSizeF maxSize(width, zoneRect.height() + extraHeight + 10); qreal currentZoneHeight = rect().height() - extraHeight - 10; - qreal newZoneHeight = determineNewZoneHeight(currentZoneHeight); + qreal newZoneHeight = forceInitialHeight ? calcMaxInitialHeight() : determineNewZoneHeight(currentZoneHeight); QSizeF initialSize(width, newZoneHeight + extraHeight + 10); @@ -335,3 +346,39 @@ void ZoneViewWidget::initStyleOption(QStyleOption *option) const if (titleBar) titleBar->icon = QPixmap("theme:cockatrice"); } + +/** + * Expands/shrinks the window, depending on the current height as well as the configured initial and expanded max + * heights. + */ +void ZoneViewWidget::expandWindow() +{ + qreal maxInitialHeight = calcMaxInitialHeight(); + qreal maxExpandedHeight = rowsToHeight(SettingsCache::instance().getCardViewExpandedRowsMax()); + qreal height = rect().height() - extraHeight - 10; + qreal maxHeight = maximumHeight() - extraHeight - 10; + + // reset window to initial max height if... + bool doResetSize = + // current height is less than that + (height < maxInitialHeight) || + // current height is at expanded max height + (height == maxExpandedHeight) || + // current height is at actual max height, and actual max height is less than expanded max height + (height == maxHeight && height > maxInitialHeight && height < maxExpandedHeight); + + if (doResetSize) { + resizeToZoneContents(true); + } else { + // expand/shrink window to expanded max height if current height is anywhere at or between initial max height + // and actual max height + resize(maximumSize().boundedTo({maximumWidth(), maxExpandedHeight + extraHeight + 10})); + } +} + +void ZoneViewWidget::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) +{ + if (event->pos().y() <= 0) { + expandWindow(); + } +} \ No newline at end of file diff --git a/cockatrice/src/game/zones/view_zone_widget.h b/cockatrice/src/game/zones/view_zone_widget.h index af86fe624..177bcb92a 100644 --- a/cockatrice/src/game/zones/view_zone_widget.h +++ b/cockatrice/src/game/zones/view_zone_widget.h @@ -63,11 +63,12 @@ private slots: void processGroupBy(int value); void processSortBy(int value); void processSetPileView(QT_STATE_CHANGED_T value); - void resizeToZoneContents(); + void resizeToZoneContents(bool forceInitialHeight = false); void handleScrollBarChange(int value); void zoneDeleted(); void moveEvent(QGraphicsSceneMoveEvent * /* event */) override; void resizeEvent(QGraphicsSceneResizeEvent * /* event */) override; + void expandWindow(); public: ZoneViewWidget(Player *_player, @@ -90,6 +91,7 @@ public: protected: void closeEvent(QCloseEvent *event) override; void initStyleOption(QStyleOption *option) const override; + void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override; }; #endif diff --git a/cockatrice/src/settings/cache_settings.cpp b/cockatrice/src/settings/cache_settings.cpp index 58e16f24b..accea7bc7 100644 --- a/cockatrice/src/settings/cache_settings.cpp +++ b/cockatrice/src/settings/cache_settings.cpp @@ -249,6 +249,7 @@ SettingsCache::SettingsCache() knownMissingFeatures = settings->value("interface/knownmissingfeatures", "").toString(); useTearOffMenus = settings->value("interface/usetearoffmenus", true).toBool(); cardViewInitialRowsMax = settings->value("interface/cardViewInitialRowsMax", 14).toInt(); + cardViewExpandedRowsMax = settings->value("interface/cardViewExpandedRowsMax", 20).toInt(); closeEmptyCardView = settings->value("interface/closeEmptyCardView", true).toBool(); showShortcuts = settings->value("menu/showshortcuts", true).toBool(); @@ -344,6 +345,12 @@ void SettingsCache::setCardViewInitialRowsMax(int _cardViewInitialRowsMax) settings->setValue("interface/cardViewInitialRowsMax", cardViewInitialRowsMax); } +void SettingsCache::setCardViewExpandedRowsMax(int value) +{ + cardViewExpandedRowsMax = value; + settings->setValue("interface/cardViewExpandedRowsMax", cardViewExpandedRowsMax); +} + void SettingsCache::setCloseEmptyCardView(QT_STATE_CHANGED_T value) { closeEmptyCardView = value; diff --git a/cockatrice/src/settings/cache_settings.h b/cockatrice/src/settings/cache_settings.h index bb23394e0..d6f187020 100644 --- a/cockatrice/src/settings/cache_settings.h +++ b/cockatrice/src/settings/cache_settings.h @@ -165,6 +165,7 @@ private: QString knownMissingFeatures; bool useTearOffMenus; int cardViewInitialRowsMax; + int cardViewExpandedRowsMax; bool closeEmptyCardView; int pixmapCacheSize; int networkCacheSize; @@ -642,6 +643,7 @@ public: void setKnownMissingFeatures(const QString &_knownMissingFeatures); void setUseTearOffMenus(bool _useTearOffMenus); void setCardViewInitialRowsMax(int _cardViewInitialRowsMax); + void setCardViewExpandedRowsMax(int value); void setCloseEmptyCardView(QT_STATE_CHANGED_T value); QString getClientID() { @@ -663,6 +665,10 @@ public: { return cardViewInitialRowsMax; } + int getCardViewExpandedRowsMax() const + { + return cardViewExpandedRowsMax; + } bool getCloseEmptyCardView() const { return closeEmptyCardView; diff --git a/dbconverter/src/mocks.cpp b/dbconverter/src/mocks.cpp index 9a76edf84..9ce12d7f7 100644 --- a/dbconverter/src/mocks.cpp +++ b/dbconverter/src/mocks.cpp @@ -58,6 +58,9 @@ void SettingsCache::setUseTearOffMenus(bool /* _useTearOffMenus */) void SettingsCache::setCardViewInitialRowsMax(int /* _cardViewInitialRowsMax */) { } +void SettingsCache::setCardViewExpandedRowsMax(int /* value */) +{ +} void SettingsCache::setCloseEmptyCardView(const QT_STATE_CHANGED_T /* value */) { } diff --git a/tests/carddatabase/mocks.cpp b/tests/carddatabase/mocks.cpp index 0383a81d2..557655160 100644 --- a/tests/carddatabase/mocks.cpp +++ b/tests/carddatabase/mocks.cpp @@ -62,6 +62,9 @@ void SettingsCache::setUseTearOffMenus(bool /* _useTearOffMenus */) void SettingsCache::setCardViewInitialRowsMax(int /* _cardViewInitialRowsMax */) { } +void SettingsCache::setCardViewExpandedRowsMax(int /* value */) +{ +} void SettingsCache::setCloseEmptyCardView(QT_STATE_CHANGED_T /* value */) { }