aboutsummaryrefslogtreecommitdiffstats
path: root/share/qtcreator/qmldesigner/contentLibraryQmlSource/ContentLibraryUserView.qml
blob: 47d3c6ce773ad2025cd039535f4ecca3f7eb1783 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import QtQuick
import Qt.labs.qmlmodels
import HelperWidgets as HelperWidgets
import StudioControls as StudioControls
import StudioTheme as StudioTheme
import ContentLibraryBackend

HelperWidgets.ScrollView {
    id: root

    clip: true
    interactive: !ctxMenu.opened && !ContentLibraryBackend.rootView.isDragging
                 && !HelperWidgets.Controller.contextMenuOpened

    property real cellWidth: 100
    property real cellHeight: 120
    property int numColumns: 4

    property int count: 0
    function assignMaxCount() {
        let c = 0
        for (let i = 0; i < categoryRepeater.count; ++i)
            c = Math.max(c, categoryRepeater.itemAt(i)?.count ?? 0)

        root.count = c
    }

    required property var searchBox

    signal unimport(var bundleItem);

    function closeContextMenu() {
        ctxMenu.close()
    }

    function expandVisibleSections() {
        for (let i = 0; i < categoryRepeater.count; ++i) {
            let cat = categoryRepeater.itemAt(i)
            if (cat.visible && !cat.expanded)
                cat.expandSection()
        }
    }

    Column {
        ContentLibraryMaterialContextMenu {
            id: ctxMenu

            hasModelSelection: ContentLibraryBackend.userModel.hasModelSelection
            importerRunning: ContentLibraryBackend.userModel.importerRunning

            onApplyToSelected: (add) => ContentLibraryBackend.userModel.applyToSelected(ctxMenu.targetMaterial, add)

            onUnimport: root.unimport(ctxMenu.targetMaterial)
            onAddToProject: ContentLibraryBackend.userModel.addToProject(ctxMenu.targetMaterial)
        }

        Repeater {
            id: categoryRepeater

            model: ContentLibraryBackend.userModel

            delegate: HelperWidgets.Section {
                id: section

                width: root.width
                leftPadding: StudioTheme.Values.sectionPadding
                rightPadding: StudioTheme.Values.sectionPadding
                topPadding: StudioTheme.Values.sectionPadding
                bottomPadding: StudioTheme.Values.sectionPadding

                caption: categoryName
                visible: categoryVisible
                expanded: categoryExpanded
                expandOnClick: false
                category: "ContentLib_User"

                onToggleExpand: categoryExpanded = !categoryExpanded
                onExpand: categoryExpanded = true
                onCollapse: categoryExpanded = false

                function expandSection() {
                    categoryExpanded = true
                }

                property alias count: repeater.count

                onCountChanged: root.assignMaxCount()

                property int numVisibleItem: 1 // initially, the tab is invisible so this will be 0

                Grid {
                    width: section.width - section.leftPadding - section.rightPadding
                    spacing: StudioTheme.Values.sectionGridSpacing
                    columns: root.numColumns

                    Repeater {
                        id: repeater
                        model: categoryItems

                        delegate: DelegateChooser {
                            role: "itemType"

                            DelegateChoice {
                                roleValue: "material"
                                ContentLibraryMaterial {
                                    width: root.cellWidth
                                    height: root.cellHeight

                                    importerRunning: ContentLibraryBackend.userModel.importerRunning

                                    onShowContextMenu: ctxMenu.popupMenu(modelData)
                                    onAddToProject: ContentLibraryBackend.userModel.addToProject(modelData)

                                    onVisibleChanged: {
                                        section.numVisibleItem += visible ? 1 : -1
                                    }
                                }
                            }
                            DelegateChoice {
                                roleValue: "texture"
                                delegate: ContentLibraryTexture {
                                    width: root.cellWidth
                                    height: root.cellHeight

                                    // onShowContextMenu: ctxMenu.popupMenu(modelData) // TODO
                                }
                            }
                        }

                        onCountChanged: root.assignMaxCount()
                    }
                }

                Text {
                    text: qsTr("No match found.");
                    color: StudioTheme.Values.themeTextColor
                    font.pixelSize: StudioTheme.Values.baseFontSize
                    leftPadding: 10
                    visible: !searchBox.isEmpty() && section.numVisibleItem === 0
                }
            }
        }

        Text {
            id: infoText
            text: {
                if (!ContentLibraryBackend.effectsModel.bundleExists)
                    qsTr("User bundle couldn't be found.")
                else if (!ContentLibraryBackend.rootView.isQt6Project)
                    qsTr("<b>Content Library</b> is not supported in Qt5 projects.")
                else if (!ContentLibraryBackend.rootView.hasQuick3DImport)
                    qsTr("To use <b>Content Library</b>, first add the QtQuick3D module in the <b>Components</b> view.")
                else if (!ContentLibraryBackend.rootView.hasMaterialLibrary)
                    qsTr("<b>Content Library</b> is disabled inside a non-visual component.")
                else
                    ""
            }
            color: StudioTheme.Values.themeTextColor
            font.pixelSize: StudioTheme.Values.baseFontSize
            topPadding: 10
            leftPadding: 10
            visible: ContentLibraryBackend.effectsModel.isEmpty
        }
    }
}