From 2513c056d6c06b88fd2ca04f01ead8d02f8ef22d Mon Sep 17 00:00:00 2001 From: Alfredo Monclus Date: Sat, 22 Mar 2025 15:52:54 -0300 Subject: [PATCH] start: fix newfilebuttons sizehint and split it into it's own widget --- src/Mod/Start/Gui/CMakeLists.txt | 2 + src/Mod/Start/Gui/NewFileButton.cpp | 101 ++++++++++++++++++++++++++++ src/Mod/Start/Gui/NewFileButton.h | 64 ++++++++++++++++++ src/Mod/Start/Gui/StartView.cpp | 64 +----------------- 4 files changed, 168 insertions(+), 63 deletions(-) create mode 100644 src/Mod/Start/Gui/NewFileButton.cpp create mode 100644 src/Mod/Start/Gui/NewFileButton.h 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/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)