Merge pull request #20216 from alfrix/start_fixes_5

Start: fix newbuttons vertical sizing
This commit is contained in:
Kacper Donat
2025-03-23 17:10:19 +01:00
committed by GitHub
5 changed files with 201 additions and 71 deletions

View File

@@ -59,6 +59,8 @@ SET(StartGui_SRCS
GeneralSettingsWidget.h
Manipulator.cpp
Manipulator.h
NewFileButton.cpp
NewFileButton.h
PreCompiled.cpp
PreCompiled.h
StartView.cpp

View File

@@ -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<QLayoutItem*> 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;

View File

@@ -0,0 +1,101 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2025 Alfredo Monclus <alfredomonclus@gmail.com> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
#include <QLabel>
#include <QFont>
#include <QIcon>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>
#endif
#include "NewFileButton.h"
#include <algorithm>
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

View File

@@ -0,0 +1,64 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* *
* Copyright (c) 2025 Alfredo Monclus <alfredomonclus@gmail.com> *
* *
* 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 *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#ifndef FREECAD_NEWFILEBUTTON_H
#define FREECAD_NEWFILEBUTTON_H
#include <QLabel>
#include <QString>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <App/Application.h>
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

View File

@@ -42,6 +42,7 @@
#include "FileCardView.h"
#include "FirstStartWidget.h"
#include "FlowLayout.h"
#include "NewFileButton.h"
#include <App/DocumentObject.h>
#include <App/Application.h>
#include <Base/Interpreter.h>
@@ -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<int>(hGrp->GetInt("FileCardSpacing", 25)); // NOLINT
const auto newFileIconSize =
static_cast<int>(hGrp->GetInt("NewFileIconSize", 48)); // NOLINT
const auto cardLabelWith =
static_cast<int>(hGrp->GetInt("FileCardLabelWith", 180)); // NOLINT
auto mainLayout = gsl::owner<QHBoxLayout*>(new QHBoxLayout(this));
mainLayout->setAlignment(Qt::AlignVCenter);
auto iconLabel = gsl::owner<QLabel*>(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<QVBoxLayout*>(new QVBoxLayout);
auto textLabelLine1 = gsl::owner<QLabel*>(new QLabel(this));
textLabelLine1->setText(newButton.heading);
QFont font = textLabelLine1->font();
font.setWeight(QFont::Bold);
textLabelLine1->setFont(font);
auto textLabelLine2 = gsl::owner<QLabel*>(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)