Expand/shrink card view window on double click (#5652)

This commit is contained in:
RickyRister 2025-02-25 15:32:45 -08:00 committed by GitHub
parent 21e22ed5fb
commit 47311b1dfd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 116 additions and 8 deletions

View file

@ -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();
}
}