diff --git a/src/Mod/Start/Gui/CMakeLists.txt b/src/Mod/Start/Gui/CMakeLists.txt index 30d21b550b..9b4097545e 100644 --- a/src/Mod/Start/Gui/CMakeLists.txt +++ b/src/Mod/Start/Gui/CMakeLists.txt @@ -59,6 +59,8 @@ SET(StartGui_SRCS GeneralSettingsWidget.h Manipulator.cpp Manipulator.h + NewFileButton.cpp + NewFileButton.h PreCompiled.cpp PreCompiled.h StartView.cpp diff --git a/src/Mod/Start/Gui/FlowLayout.cpp b/src/Mod/Start/Gui/FlowLayout.cpp index 441bfb9fc4..f12b45d003 100644 --- a/src/Mod/Start/Gui/FlowLayout.cpp +++ b/src/Mod/Start/Gui/FlowLayout.cpp @@ -151,12 +151,17 @@ int FlowLayout::doLayout(const QRect& rect, bool testOnly) const int bottom {}; getContentsMargins(&left, &top, &right, &bottom); QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); + int x = effectiveRect.x(); int y = effectiveRect.y(); int lineHeight = 0; + QVector currentRow; + for (auto item : std::as_const(itemList)) { QWidget* wid = item->widget(); + QSize itemSizeHint = item->sizeHint(); + int spaceX = horizontalSpacing(); if (spaceX == -1) { spaceX = wid->style()->layoutSpacing(QSizePolicy::PushButton, @@ -171,20 +176,40 @@ int FlowLayout::doLayout(const QRect& rect, bool testOnly) const Qt::Vertical); } - int nextX = x + item->sizeHint().width() + spaceX; - if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { + int nextX = x + itemSizeHint.width() + spaceX; + + // Step 1: Wrap if necessary + if (nextX - spaceX > effectiveRect.right() && !currentRow.isEmpty()) { + // Apply row height to all items in the current row + for (auto rowItem : currentRow) { + if (!testOnly) { + rowItem->setGeometry(QRect(QPoint(rowItem->geometry().x(), y), + QSize(rowItem->sizeHint().width(), lineHeight))); + } + } + + // Move to next row + y += lineHeight + spaceY; x = effectiveRect.x(); - y = y + lineHeight + spaceY; - nextX = x + item->sizeHint().width() + spaceX; - lineHeight = 0; + currentRow.clear(); } + currentRow.append(item); + lineHeight = std::max(lineHeight, itemSizeHint.height()); + if (!testOnly) { - item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); + item->setGeometry(QRect(QPoint(x, y), QSize(itemSizeHint.width(), lineHeight))); } - x = nextX; - lineHeight = std::max(lineHeight, item->sizeHint().height()); + x += itemSizeHint.width() + spaceX; + } + + // Step 2: Apply the last row's height + for (auto rowItem : currentRow) { + if (!testOnly) { + rowItem->setGeometry(QRect(QPoint(rowItem->geometry().x(), y), + QSize(rowItem->sizeHint().width(), lineHeight))); + } } return y + lineHeight - rect.y() + bottom; diff --git a/src/Mod/Start/Gui/NewFileButton.cpp b/src/Mod/Start/Gui/NewFileButton.cpp new file mode 100644 index 0000000000..22081f0434 --- /dev/null +++ b/src/Mod/Start/Gui/NewFileButton.cpp @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/**************************************************************************** + * * + * Copyright (c) 2025 Alfredo Monclus * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + ***************************************************************************/ + +#include "PreCompiled.h" + +#ifndef _PreComp_ +#include +#include +#include +#include +#include +#include +#endif + +#include "NewFileButton.h" +#include + +namespace StartGui +{ + +NewFileButton::NewFileButton(const NewButton& newButton) + : mainLayout(new QHBoxLayout(this)) + , textLayout(new QVBoxLayout()) + , headingLabel(new QLabel()) + , descriptionLabel(new QLabel()) +{ + setObjectName(QStringLiteral("newFileButton")); + auto hGrp = App::GetApplication().GetParameterGroupByPath( + "User parameter:BaseApp/Preferences/Mod/Start"); + + constexpr int defaultWidth = 180; // #newFileButton width in QSS + labelWidth = int(hGrp->GetInt("FileCardLabelWith", defaultWidth)); + + constexpr int defaultSize = 48; + iconSize = int(hGrp->GetInt("NewFileIconSize", defaultSize)); + + auto iconLabel = new QLabel(this); + QIcon baseIcon(newButton.iconPath); + iconLabel->setPixmap(baseIcon.pixmap(iconSize, iconSize)); + + textLayout->addWidget(headingLabel); + textLayout->addWidget(descriptionLabel); + textLayout->setSpacing(0); + textLayout->setContentsMargins(0, 0, 0, 0); + + headingLabel->setText(newButton.heading); + QFont font = headingLabel->font(); + font.setWeight(QFont::Bold); + headingLabel->setFont(font); + + descriptionLabel->setText(newButton.description); + descriptionLabel->setWordWrap(true); + descriptionLabel->setFixedWidth(labelWidth); + descriptionLabel->setAlignment(Qt::AlignTop); + + mainLayout->setAlignment(Qt::AlignVCenter); + mainLayout->addWidget(iconLabel); + mainLayout->addLayout(textLayout); + mainLayout->addStretch(); + QFontMetrics qfm(font); + int margin = qfm.height() / 2; + mainLayout->setSpacing(margin); + mainLayout->setContentsMargins(margin, margin, 2 * margin, margin); + setLayout(mainLayout); + setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); +} + +QSize NewFileButton::minimumSizeHint() const +{ + int minWidth = labelWidth + iconSize + mainLayout->contentsMargins().left() + + mainLayout->contentsMargins().right() + mainLayout->spacing(); + + int textHeight = headingLabel->sizeHint().height() + descriptionLabel->sizeHint().height(); + + int minHeight = std::max(iconSize, textHeight) + mainLayout->contentsMargins().top() + + mainLayout->contentsMargins().bottom(); + + return {minWidth, minHeight}; +} + +} // namespace StartGui diff --git a/src/Mod/Start/Gui/NewFileButton.h b/src/Mod/Start/Gui/NewFileButton.h new file mode 100644 index 0000000000..452e78923a --- /dev/null +++ b/src/Mod/Start/Gui/NewFileButton.h @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/**************************************************************************** + * * + * Copyright (c) 2025 Alfredo Monclus * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + ***************************************************************************/ + +#ifndef FREECAD_NEWFILEBUTTON_H +#define FREECAD_NEWFILEBUTTON_H + +#include +#include +#include +#include +#include + +#include + +namespace StartGui +{ + +struct NewButton +{ + QString heading; + QString description; + QString iconPath; +}; + +class NewFileButton: public QPushButton +{ +public: + explicit NewFileButton(const NewButton& newButton); + +private: + int iconSize; + int labelWidth; + QHBoxLayout* mainLayout; + QVBoxLayout* textLayout; + QLabel* headingLabel; + QLabel* descriptionLabel; + +protected: + QSize minimumSizeHint() const override; +}; + +} // namespace StartGui + +#endif // FREECAD_NEWFILEBUTTON_H diff --git a/src/Mod/Start/Gui/StartView.cpp b/src/Mod/Start/Gui/StartView.cpp index d74200a02d..dc45b87849 100644 --- a/src/Mod/Start/Gui/StartView.cpp +++ b/src/Mod/Start/Gui/StartView.cpp @@ -42,6 +42,7 @@ #include "FileCardView.h" #include "FirstStartWidget.h" #include "FlowLayout.h" +#include "NewFileButton.h" #include #include #include @@ -59,69 +60,6 @@ using namespace StartGui; TYPESYSTEM_SOURCE_ABSTRACT(StartGui::StartView, Gui::MDIView) // NOLINT -namespace -{ - -struct NewButton -{ - QString heading; - QString description; - QString iconPath; -}; - -class NewFileButton: public QPushButton -{ - -public: - explicit NewFileButton(const NewButton& newButton) - { - auto hGrp = App::GetApplication().GetParameterGroupByPath( - "User parameter:BaseApp/Preferences/Mod/Start"); - const auto cardSpacing = static_cast(hGrp->GetInt("FileCardSpacing", 25)); // NOLINT - const auto newFileIconSize = - static_cast(hGrp->GetInt("NewFileIconSize", 48)); // NOLINT - const auto cardLabelWith = - static_cast(hGrp->GetInt("FileCardLabelWith", 180)); // NOLINT - - auto mainLayout = gsl::owner(new QHBoxLayout(this)); - mainLayout->setAlignment(Qt::AlignVCenter); - auto iconLabel = gsl::owner(new QLabel(this)); - mainLayout->addWidget(iconLabel); - QIcon baseIcon(newButton.iconPath); - iconLabel->setPixmap(baseIcon.pixmap(newFileIconSize, newFileIconSize)); - iconLabel->setPixmap(baseIcon.pixmap(newFileIconSize, newFileIconSize)); - - auto textLayout = gsl::owner(new QVBoxLayout); - auto textLabelLine1 = gsl::owner(new QLabel(this)); - textLabelLine1->setText(newButton.heading); - QFont font = textLabelLine1->font(); - font.setWeight(QFont::Bold); - textLabelLine1->setFont(font); - auto textLabelLine2 = gsl::owner(new QLabel(this)); - textLabelLine2->setText(newButton.description); - textLabelLine2->setWordWrap(true); - textLayout->addWidget(textLabelLine1); - textLayout->addWidget(textLabelLine2); - textLayout->setSpacing(0); - mainLayout->addItem(textLayout); - - mainLayout->addStretch(); - - textLabelLine1->adjustSize(); - textLabelLine2->adjustSize(); - int textHeight = - textLabelLine1->height() + textLabelLine2->height() + textLayout->spacing(); - - int minWidth = newFileIconSize + cardLabelWith + cardSpacing; - int minHeight = std::max(newFileIconSize, textHeight) + cardSpacing; - - this->setMinimumHeight(minHeight); - this->setMinimumWidth(minWidth); - } -}; - -} // namespace - StartView::StartView(QWidget* parent) : Gui::MDIView(nullptr, parent)