1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/****************************************************************************
4
* Copyright (c) 2024 The FreeCAD Project Association AISBL *
6
* This file is part of FreeCAD. *
8
* FreeCAD is free software: you can redistribute it and/or modify it *
9
* under the terms of the GNU Lesser General Public License as *
10
* published by the Free Software Foundation, either version 2.1 of the *
11
* License, or (at your option) any later version. *
13
* FreeCAD is distributed in the hope that it will be useful, but *
14
* WITHOUT ANY WARRANTY; without even the implied warranty of *
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16
* Lesser General Public License for more details. *
18
* You should have received a copy of the GNU Lesser General Public *
19
* License along with FreeCAD. If not, see *
20
* <https://www.gnu.org/licenses/>. *
22
***************************************************************************/
24
#include "PreCompiled.h"
25
#include "FileCardView.h"
27
#include <App/Application.h>
28
#include "../App/DisplayedFilesModel.h"
34
FileCardView::FileCardView(QWidget* parent)
37
QSizePolicy sizePolicy(QSizePolicy::Policy::MinimumExpanding,
38
QSizePolicy::Policy::MinimumExpanding);
39
sizePolicy.setHeightForWidth(true);
40
setSizePolicy(sizePolicy);
41
setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
42
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
43
setViewMode(QListView::ViewMode::IconMode);
44
setFlow(QListView::Flow::LeftToRight);
45
setResizeMode(QListView::ResizeMode::Adjust);
46
setUniformItemSizes(true);
47
setMouseTracking(true);
51
int FileCardView::heightForWidth(int width) const
53
auto model = this->model();
54
auto delegate = this->itemDelegate();
55
if (!model || !delegate) {
58
int numCards = model->rowCount();
59
auto cardSize = delegate->sizeHint(QStyleOptionViewItem(), model->index(0, 0));
60
int cardsPerRow = static_cast<int>(width / cardSize.width());
62
static_cast<int>(ceil(static_cast<double>(numCards) / static_cast<double>(cardsPerRow)));
63
int neededHeight = numRows * cardSize.height();
64
auto hGrp = App::GetApplication().GetParameterGroupByPath(
65
"User parameter:BaseApp/Preferences/Mod/Start");
66
int cardSpacing = static_cast<int>(hGrp->GetInt("FileCardSpacing", 20)); // NOLINT
67
return neededHeight + cardSpacing * (numRows - 1) + 2 * cardSpacing;
70
QSize FileCardView::sizeHint() const
72
auto hGrp = App::GetApplication().GetParameterGroupByPath(
73
"User parameter:BaseApp/Preferences/Mod/Start");
74
int cardSpacing = static_cast<int>(hGrp->GetInt("FileCardSpacing", 20)); // NOLINT
76
auto model = this->model();
77
auto delegate = this->itemDelegate();
78
if (!model || !delegate) {
79
// The model and/or delegate have not been set yet, this was an early startup call
80
return {cardSpacing, cardSpacing};
82
int numCards = model->rowCount();
83
auto cardSize = delegate->sizeHint(QStyleOptionViewItem(), model->index(0, 0));
84
return {(cardSize.width() + cardSpacing) * numCards + cardSpacing,
85
cardSize.height() + 2 * cardSpacing};
88
} // namespace StartGui