FreeCAD

Форк
0
/
FileCardView.cpp 
88 строк · 4.1 Кб
1
// SPDX-License-Identifier: LGPL-2.1-or-later
2
/****************************************************************************
3
 *                                                                          *
4
 *   Copyright (c) 2024 The FreeCAD Project Association AISBL               *
5
 *                                                                          *
6
 *   This file is part of FreeCAD.                                          *
7
 *                                                                          *
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.                        *
12
 *                                                                          *
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.                        *
17
 *                                                                          *
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/>.                                       *
21
 *                                                                          *
22
 ***************************************************************************/
23

24
#include "PreCompiled.h"
25
#include "FileCardView.h"
26

27
#include <App/Application.h>
28
#include "../App/DisplayedFilesModel.h"
29

30
namespace StartGui
31
{
32

33

34
FileCardView::FileCardView(QWidget* parent)
35
    : QListView(parent)
36
{
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);
48
    setSpacing(20);
49
}
50

51
int FileCardView::heightForWidth(int width) const
52
{
53
    auto model = this->model();
54
    auto delegate = this->itemDelegate();
55
    if (!model || !delegate) {
56
        return 0;
57
    }
58
    int numCards = model->rowCount();
59
    auto cardSize = delegate->sizeHint(QStyleOptionViewItem(), model->index(0, 0));
60
    int cardsPerRow = static_cast<int>(width / cardSize.width());
61
    int numRows =
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;
68
}
69

70
QSize FileCardView::sizeHint() const
71
{
72
    auto hGrp = App::GetApplication().GetParameterGroupByPath(
73
        "User parameter:BaseApp/Preferences/Mod/Start");
74
    int cardSpacing = static_cast<int>(hGrp->GetInt("FileCardSpacing", 20));  // NOLINT
75

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};
81
    }
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};
86
}
87

88
}  // namespace StartGui
89

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.