style: Add braces to all control flow statements (#6887)

* style: Add braces to all control flow statements

  Standardize code style by adding explicit braces to all single-statement
  control flow blocks (if, else, for, while) across the entire codebase.

  Also documents the InsertBraces clang-format option (requires v15+) for
  future automated enforcement.

* InsertBraces-check-enabled
This commit is contained in:
DawnFire42 2026-05-16 13:19:53 -04:00 committed by GitHub
parent 7153f7d4c1
commit aadee34238
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
173 changed files with 2725 additions and 1461 deletions

View file

@ -25,14 +25,16 @@ void CardZone::onCardAdded(CardItem *addedCard)
void CardZone::retranslateUi()
{
for (int i = 0; i < getLogic()->getCards().size(); ++i)
for (int i = 0; i < getLogic()->getCards().size(); ++i) {
getLogic()->getCards()[i]->retranslateUi();
}
}
void CardZone::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * /*event*/)
{
if (doubleClickAction)
if (doubleClickAction) {
doubleClickAction->trigger();
}
}
bool CardZone::showContextMenu(const QPoint &screenPos)
@ -47,12 +49,14 @@ bool CardZone::showContextMenu(const QPoint &screenPos)
void CardZone::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::RightButton) {
if (showContextMenu(event->screenPos()))
if (showContextMenu(event->screenPos())) {
event->accept();
else
} else {
event->ignore();
} else
}
} else {
event->ignore();
}
}
QPointF CardZone::closestGridPoint(const QPointF &point)

View file

@ -34,9 +34,11 @@ void HandZone::handleDropEvent(const QList<CardDragItem *> &dragItems,
QPoint point = dropPoint + scenePos().toPoint();
int x = -1;
if (SettingsCache::instance().getHorizontalHand()) {
for (x = 0; x < getLogic()->getCards().size(); x++)
if (point.x() < static_cast<CardItem *>(getLogic()->getCards().at(x))->scenePos().x())
for (x = 0; x < getLogic()->getCards().size(); x++) {
if (point.x() < static_cast<CardItem *>(getLogic()->getCards().at(x))->scenePos().x()) {
break;
}
}
} else {
x = calcDropIndexFromY(dropPoint.y());
}
@ -49,18 +51,20 @@ void HandZone::handleDropEvent(const QList<CardDragItem *> &dragItems,
cmd.set_x(x);
cmd.set_y(-1);
for (int i = 0; i < dragItems.size(); ++i)
for (int i = 0; i < dragItems.size(); ++i) {
cmd.mutable_cards_to_move()->add_card()->set_card_id(dragItems[i]->getId());
}
getLogic()->getPlayer()->getPlayerActions()->sendGameCommand(cmd);
}
QRectF HandZone::boundingRect() const
{
if (SettingsCache::instance().getHorizontalHand())
if (SettingsCache::instance().getHorizontalHand()) {
return QRectF(0, 0, width, CardDimensions::HEIGHT_F + 10);
else
} else {
return QRectF(0, 0, CardDimensions::WIDTH_F * 1.5, zoneHeight);
}
}
void HandZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*option*/, QWidget * /*widget*/)
@ -90,9 +94,9 @@ void HandZone::reorganizeCards()
CardItem *c = getLogic()->getCards().at(i);
// If the total width of the cards is smaller than the available width,
// the cards do not need to overlap and are displayed in the center of the area.
if (cardWidth * cardCount > totalWidth)
if (cardWidth * cardCount > totalWidth) {
c->setPos(xPadding + ((qreal)i) * (totalWidth - cardWidth) / (cardCount - 1), 5);
else {
} else {
qreal xPosition = leftJustified ? xPadding + ((qreal)i) * cardWidth
: xPadding + ((qreal)i) * cardWidth +
(totalWidth - cardCount * cardWidth) / 2;

View file

@ -55,8 +55,9 @@ void CardZoneLogic::addCard(CardItem *card, const bool reorganize, const int x,
emit cardAdded(card);
addCardImpl(card, x, y);
if (reorganize)
if (reorganize) {
emit reorganizeCards();
}
emit cardCountChanged();
}
@ -66,16 +67,19 @@ CardItem *CardZoneLogic::takeCard(int position, int cardId, bool toNewZone)
if (position == -1) {
// position == -1 means either that the zone is indexed by card id
// or that it doesn't matter which card you take.
for (int i = 0; i < cards.size(); ++i)
for (int i = 0; i < cards.size(); ++i) {
if (cards[i]->getId() == cardId) {
position = i;
break;
}
if (position == -1)
}
if (position == -1) {
position = 0;
}
}
if (position >= cards.size())
if (position >= cards.size()) {
return nullptr;
}
for (auto *view : views) {
qobject_cast<ZoneViewZoneLogic *>(view->getLogic())->removeCard(position, toNewZone);
@ -142,8 +146,9 @@ void CardZoneLogic::moveAllToZone()
cmd.set_target_zone(targetZone.toStdString());
cmd.set_x(targetX);
for (int i = 0; i < cards.size(); ++i)
for (int i = 0; i < cards.size(); ++i) {
cmd.mutable_cards_to_move()->add_card()->set_card_id(cards[i]->getId());
}
player->getPlayerActions()->sendGameCommand(cmd);
}
@ -175,9 +180,9 @@ void CardZoneLogic::clearContents()
QString CardZoneLogic::getTranslatedName(bool theirOwn, GrammaticalCase gc) const
{
QString ownerName = player->getPlayerInfo()->getName();
if (name == ZoneNames::HAND)
if (name == ZoneNames::HAND) {
return (theirOwn ? tr("their hand", "nominative") : tr("%1's hand", "nominative").arg(ownerName));
else if (name == ZoneNames::DECK)
} else if (name == ZoneNames::DECK) {
switch (gc) {
case CaseLookAtZone:
return (theirOwn ? tr("their library", "look at zone")
@ -193,11 +198,11 @@ QString CardZoneLogic::getTranslatedName(bool theirOwn, GrammaticalCase gc) cons
default:
return (theirOwn ? tr("their library", "nominative") : tr("%1's library", "nominative").arg(ownerName));
}
else if (name == ZoneNames::GRAVE)
} else if (name == ZoneNames::GRAVE) {
return (theirOwn ? tr("their graveyard", "nominative") : tr("%1's graveyard", "nominative").arg(ownerName));
else if (name == ZoneNames::EXILE)
} else if (name == ZoneNames::EXILE) {
return (theirOwn ? tr("their exile", "nominative") : tr("%1's exile", "nominative").arg(ownerName));
else if (name == ZoneNames::SIDEBOARD)
} else if (name == ZoneNames::SIDEBOARD) {
switch (gc) {
case CaseLookAtZone:
return (theirOwn ? tr("their sideboard", "look at zone")
@ -208,7 +213,7 @@ QString CardZoneLogic::getTranslatedName(bool theirOwn, GrammaticalCase gc) cons
default:
break;
}
else {
} else {
return (theirOwn ? tr("their custom zone '%1'", "nominative").arg(name)
: tr("%1's custom zone '%2'", "nominative").arg(ownerName).arg(name));
}

View file

@ -25,8 +25,9 @@ void PileZoneLogic::addCardImpl(CardItem *card, int x, int /*y*/)
card->setCardRef({});
card->setId(-1);
// If we obscure a previously revealed card, its name has to be forgotten
if (cards.size() > x + 1)
if (cards.size() > x + 1) {
cards.at(x + 1)->setCardRef({});
}
}
card->setVisible(false);
card->resetState();

View file

@ -29,7 +29,8 @@ CardItem *TableZoneLogic::takeCard(int position, int cardId, bool toNewZone)
{
CardItem *result = CardZoneLogic::takeCard(position, cardId);
if (toNewZone)
if (toNewZone) {
emit contentSizeChanged();
}
return result;
}

View file

@ -48,9 +48,10 @@ void PileZone::paint(QPainter *painter, const QStyleOptionGraphicsItem * /*optio
{
painter->drawPath(shape());
if (!getLogic()->getCards().isEmpty())
if (!getLogic()->getCards().isEmpty()) {
getLogic()->getCards().at(0)->paintPicture(painter, getLogic()->getCards().at(0)->getTranslatedSize(painter),
90);
}
painter->translate(CardDimensions::WIDTH_HALF_F, CardDimensions::HEIGHT_HALF_F);
painter->rotate(-90);
@ -87,24 +88,28 @@ void PileZone::reorganizeCards()
void PileZone::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
CardZone::mousePressEvent(event);
if (event->isAccepted())
if (event->isAccepted()) {
return;
}
if (event->button() == Qt::LeftButton) {
setCursor(Qt::ClosedHandCursor);
event->accept();
} else
} else {
event->ignore();
}
}
void PileZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if ((event->screenPos() - event->buttonDownScreenPos(Qt::LeftButton)).manhattanLength() <
QApplication::startDragDistance())
QApplication::startDragDistance()) {
return;
}
if (getLogic()->getCards().isEmpty())
if (getLogic()->getCards().isEmpty()) {
return;
}
bool forceFaceDown = event->modifiers().testFlag(Qt::ShiftModifier);
bool bottomCard = event->modifiers().testFlag(Qt::ControlModifier);
@ -123,7 +128,8 @@ void PileZone::mouseReleaseEvent(QGraphicsSceneMouseEvent * /*event*/)
void PileZone::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
if (!getLogic()->getCards().isEmpty())
if (!getLogic()->getCards().isEmpty()) {
getLogic()->getCards()[0]->processHoverEvent();
}
QGraphicsItem::hoverEnterEvent(event);
}

View file

@ -69,8 +69,9 @@ SelectZone *SelectZone::findOwningSelectZone(const QGraphicsItem *card)
SelectZone::StackLayoutParams SelectZone::buildStackParams(qreal minOffset) const
{
const auto &cards = getLogic()->getCards();
if (cards.isEmpty())
if (cards.isEmpty()) {
return {0, boundingRect().height(), 0.0, 0.0, minOffset};
}
const auto cardCount = static_cast<int>(cards.size());
const qreal cardHeight = cards.at(0)->boundingRect().height();
const qreal offset = stackingOffset(cardHeight);
@ -93,8 +94,9 @@ int SelectZone::calcDropIndexFromY(qreal dropY, qreal minOffset) const
void SelectZone::restoreStaleEscapedCards()
{
if (!cardClipContainer)
if (!cardClipContainer) {
return;
}
for (auto *card : getLogic()->getCards()) {
// A card parented to the zone (instead of the clip container) should
// only occur while it is actively hovered. If hover cleanup was
@ -108,10 +110,12 @@ void SelectZone::restoreStaleEscapedCards()
void SelectZone::layoutCardsVertically(const StackLayoutParams &params)
{
const auto &cards = getLogic()->getCards();
if (cards.isEmpty() || params.cardCount <= 0)
if (cards.isEmpty() || params.cardCount <= 0) {
return;
if (params.cardCount > cards.size())
}
if (params.cardCount > cards.size()) {
return;
}
constexpr qreal xspace = 5;
const qreal cardWidth = cards.at(0)->boundingRect().width();
@ -163,8 +167,9 @@ void SelectZone::onCardAdded(CardItem *addedCard)
void SelectZone::setupClipContainer(std::optional<qreal> zValue)
{
if (cardClipContainer)
if (cardClipContainer) {
return;
}
setFlag(QGraphicsItem::ItemClipsChildrenToShape, false);
@ -209,15 +214,19 @@ void SelectZone::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if (event->buttons().testFlag(Qt::LeftButton)) {
QPointF pos = event->pos();
if (pos.x() < 0)
if (pos.x() < 0) {
pos.setX(0);
}
QRectF br = boundingRect();
if (pos.x() > br.width())
if (pos.x() > br.width()) {
pos.setX(br.width());
if (pos.y() < 0)
}
if (pos.y() < 0) {
pos.setY(0);
if (pos.y() > br.height())
}
if (pos.y() > br.height()) {
pos.setY(br.height());
}
QRectF selectionRect = QRectF(selectionOrigin, pos).normalized();
for (auto card : getLogic()->getCards()) {
@ -253,8 +262,9 @@ void SelectZone::mousePressEvent(QGraphicsSceneMouseEvent *event)
selectionOrigin = event->pos();
static_cast<GameScene *>(scene())->startRubberBand(event->scenePos());
event->accept();
} else
} else {
CardZone::mousePressEvent(event);
}
}
void SelectZone::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)

View file

@ -108,8 +108,9 @@ void TableZone::paintLandDivider(QPainter *painter)
// Place the line 2 grid heights down then back it off just enough to allow
// some space between a 3-card stack and the land area.
qreal separatorY = MARGIN_TOP + 2 * (CardDimensions::HEIGHT + PADDING_Y) - STACKED_CARD_OFFSET_Y / 2;
if (isInverted())
if (isInverted()) {
separatorY = height - separatorY;
}
painter->setPen(QColor(255, 255, 255, 40));
painter->drawLine(QPointF(0, separatorY), QPointF(width, separatorY));
}
@ -157,8 +158,9 @@ void TableZone::reorganizeCards()
for (int i = 0; i < getLogic()->getCards().size(); ++i) {
QPoint gridPoint = getLogic()->getCards()[i]->getGridPos();
if (gridPoint.x() == -1)
if (gridPoint.x() == -1) {
continue;
}
QPointF mapPoint = mapFromGrid(gridPoint);
qreal x = mapPoint.x();
@ -167,8 +169,9 @@ void TableZone::reorganizeCards()
int numberAttachedCards = getLogic()->getCards()[i]->getAttachedCards().size();
qreal actualX = x + numberAttachedCards * STACKED_CARD_OFFSET_X;
qreal actualY = y;
if (numberAttachedCards)
if (numberAttachedCards) {
actualY += 15;
}
getLogic()->getCards()[i]->setPos(actualX, actualY);
getLogic()->getCards()[i]->setRealZValue(ZValues::tableCardZValue(actualX, actualY));
@ -227,16 +230,19 @@ void TableZone::resizeToContents()
int xMax = 0;
// Find rightmost card position, which includes the left margin amount.
for (int i = 0; i < getLogic()->getCards().size(); ++i)
if (getLogic()->getCards()[i]->pos().x() > xMax)
for (int i = 0; i < getLogic()->getCards().size(); ++i) {
if (getLogic()->getCards()[i]->pos().x() > xMax) {
xMax = (int)getLogic()->getCards()[i]->pos().x();
}
}
// Minimum width is the rightmost card position plus enough room for
// another card with padding, then margin.
currentMinimumWidth = xMax + (2 * CardDimensions::WIDTH) + PADDING_X + MARGIN_RIGHT;
if (currentMinimumWidth < MIN_WIDTH)
if (currentMinimumWidth < MIN_WIDTH) {
currentMinimumWidth = MIN_WIDTH;
}
if (currentMinimumWidth != width) {
prepareGeometryChange();
@ -247,9 +253,11 @@ void TableZone::resizeToContents()
CardItem *TableZone::getCardFromGrid(const QPoint &gridPoint) const
{
for (int i = 0; i < getLogic()->getCards().size(); i++)
if (getLogic()->getCards().at(i)->getGridPoint() == gridPoint)
for (int i = 0; i < getLogic()->getCards().size(); i++) {
if (getLogic()->getCards().at(i)->getGridPoint() == gridPoint) {
return getLogic()->getCards().at(i);
}
}
return 0;
}
@ -266,8 +274,9 @@ void TableZone::computeCardStackWidths()
QMap<int, int> cardStackCount;
for (int i = 0; i < getLogic()->getCards().size(); ++i) {
const QPoint &gridPoint = getLogic()->getCards()[i]->getGridPos();
if (gridPoint.x() == -1)
if (gridPoint.x() == -1) {
continue;
}
const int key = getCardStackMapKey(gridPoint.x() / 3, gridPoint.y());
cardStackCount.insert(key, cardStackCount.value(key, 0) + 1);
@ -277,16 +286,18 @@ void TableZone::computeCardStackWidths()
cardStackWidth.clear();
for (int i = 0; i < getLogic()->getCards().size(); ++i) {
const QPoint &gridPoint = getLogic()->getCards()[i]->getGridPos();
if (gridPoint.x() == -1)
if (gridPoint.x() == -1) {
continue;
}
const int key = getCardStackMapKey(gridPoint.x() / 3, gridPoint.y());
const int stackCount = cardStackCount.value(key, 0);
if (stackCount == 1)
if (stackCount == 1) {
cardStackWidth.insert(key, CardDimensions::WIDTH + getLogic()->getCards()[i]->getAttachedCards().size() *
STACKED_CARD_OFFSET_X);
else
} else {
cardStackWidth.insert(key, CardDimensions::WIDTH + (stackCount - 1) * STACKED_CARD_OFFSET_X);
}
}
}
@ -303,15 +314,17 @@ QPointF TableZone::mapFromGrid(QPoint gridPoint) const
x += cardStackWidth.value(key, CardDimensions::WIDTH) + PADDING_X;
}
if (isInverted())
if (isInverted()) {
gridPoint.setY(TABLEROWS - 1 - gridPoint.y());
}
// Start with margin plus stacked card offset
y = MARGIN_TOP + (gridPoint.x() % 3) * STACKED_CARD_OFFSET_Y;
// Add in card size and padding for each row
for (int i = 0; i < gridPoint.y(); ++i)
for (int i = 0; i < gridPoint.y(); ++i) {
y += CardDimensions::HEIGHT + PADDING_Y;
}
return QPointF(x, y);
}
@ -330,8 +343,9 @@ QPoint TableZone::mapToGrid(const QPointF &mapPoint) const
gridPointY = clampValidTableRow(gridPointY);
if (isInverted())
if (isInverted()) {
gridPointY = TABLEROWS - 1 - gridPointY;
}
// Calculating the x-coordinate of the grid space requires adding up the
// widths of each card stack along the row.
@ -367,19 +381,23 @@ QPointF TableZone::closestGridPoint(const QPointF &point)
{
QPoint gridPoint = mapToGrid(point);
gridPoint.setX((gridPoint.x() / 3) * 3);
if (getCardFromGrid(gridPoint))
if (getCardFromGrid(gridPoint)) {
gridPoint.setX(gridPoint.x() + 1);
if (getCardFromGrid(gridPoint))
}
if (getCardFromGrid(gridPoint)) {
gridPoint.setX(gridPoint.x() + 1);
}
return mapFromGrid(gridPoint);
}
int TableZone::clampValidTableRow(const int row)
{
if (row < 0)
if (row < 0) {
return 0;
if (row >= TABLEROWS)
}
if (row >= TABLEROWS) {
return TABLEROWS - 1;
}
return row;
}

View file

@ -203,9 +203,9 @@ ZoneViewZone::GridSize ZoneViewZone::positionCardsForDisplay(CardList &cards, Ca
QString columnProp = extractor(c);
if (i) { // if not the first card
if (columnProp == lastColumnProp)
if (columnProp == lastColumnProp) {
row++; // add below current card
else { // if no match then move card to next column
} else { // if no match then move card to next column
col++;
row = 0;
}
@ -233,8 +233,9 @@ ZoneViewZone::GridSize ZoneViewZone::positionCardsForDisplay(CardList &cards, Ca
cols = qCeil((double)cardCount / minRows);
}
if (cols < 2)
if (cols < 2) {
cols = 2;
}
qCDebug(ViewZoneLog) << "reorganizeCards: rows=" << rows << "cols=" << cols;

View file

@ -252,8 +252,9 @@ void ZoneViewWidget::retranslateUi()
void ZoneViewWidget::stopWindowDrag()
{
if (!draggingWindow)
if (!draggingWindow) {
return;
}
draggingWindow = false;
ungrabMouse();
@ -312,13 +313,15 @@ QGraphicsView *ZoneViewWidget::findDragView(QWidget *eventWidget) const
{
QWidget *current = eventWidget;
while (current) {
if (auto *view = qobject_cast<QGraphicsView *>(current))
if (auto *view = qobject_cast<QGraphicsView *>(current)) {
return view;
}
current = current->parentWidget();
}
if (scene() && !scene()->views().isEmpty())
if (scene() && !scene()->views().isEmpty()) {
return scene()->views().constFirst();
}
return nullptr;
}
@ -346,8 +349,9 @@ bool ZoneViewWidget::windowFrameEvent(QEvent *event)
}
auto *me = dynamic_cast<QGraphicsSceneMouseEvent *>(event);
if (!me)
if (!me) {
return QGraphicsWidget::windowFrameEvent(event);
}
switch (event->type()) {
case QEvent::GraphicsSceneMousePress:
@ -506,8 +510,9 @@ void ZoneViewWidget::resizeToZoneContents(bool forceInitialHeight)
zone->setGeometry(QRectF(0, -scrollBar->value(), zoneContainer->size().width(), totalZoneHeight));
if (layout())
if (layout()) {
layout()->invalidate();
}
}
void ZoneViewWidget::handleScrollBarChange(int value)
@ -521,8 +526,9 @@ void ZoneViewWidget::closeEvent(QCloseEvent *event)
disconnect(zone, &ZoneViewZone::closed, this, 0);
// manually call zone->close in order to remove it from the origZones views
zone->close();
if (shuffleCheckBox.isChecked())
if (shuffleCheckBox.isChecked()) {
player->getPlayerActions()->sendGameCommand(Command_Shuffle());
}
zoneDeleted();
event->accept();
}
@ -536,8 +542,9 @@ void ZoneViewWidget::zoneDeleted()
void ZoneViewWidget::initStyleOption(QStyleOption *option) const
{
QStyleOptionTitleBar *titleBar = qstyleoption_cast<QStyleOptionTitleBar *>(option);
if (titleBar)
if (titleBar) {
titleBar->icon = QPixmap("theme:cockatrice");
}
}
/**