Debian Codegraph @

rc/qml/Utility/SelectionState.qml

raw
1 /*
2 * Copyright (C) 2012-2015 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Charles Lindsay <[email protected]>
18 */
19
20 import QtQuick 2.9
21 import Gallery 1.0
22
23 // Holds all the state for selection between the Organic views, and some other
24 // places.
25 Item {
26 id: organicSelectionState
27
28 /// Is true if the selection mode is active
29 property bool inSelectionMode: false
30 /// If false, none of the selection functions has an effect
31 property bool allowSelectionModeChange: true
32 /// It true, only one item can be selected
33 property bool singleSelect: false
34 /// If true, a mix of videos and pictures is selected.
35 /// Should be ignored if only one item is selected.
36 property bool isMixed: false
37 /// The type of all the selected media.
38 /// Should be ignored if isMixed is true.
39 property var mediaType
40
41 /*!
42 */
43 property var model: EventOverviewModel {
44 monitored: true
45 }
46
47 Connections {
48 target: model
49 onSelectionChanged: {
50 var data = model.selectedMediasQML;
51 var photos = 0;
52 var videos = 0;
53 var medias = 0;
54
55 for (var i in data) {
56 if (data[i].hasOwnProperty('type')) {
57 medias++;
58 (data[i].type === MediaSource.Photo) ? photos++ : videos++;
59 }
60 }
61
62 organicSelectionState.mediaType = (photos > videos) ? MediaSource.Photo : MediaSource.Video;
63 organicSelectionState.isMixed = (photos > 0 && videos > 0);
64 organicSelectionState.selectedMediaCount = medias;
65
66 organicSelectionState.selectedPhotosCount = photos;
67 organicSelectionState.selectedVideosCount = videos;
68 }
69 }
70
71 // readonly
72 /// The number of currently selected items
73 property int selectedCount: model.selectedCount
74
75 // readonly
76 /// The number of currently selected media items
77 property int selectedMediaCount: 0
78
79 // readonly
80 /// The number of currently selected photos and videos items
81 property int selectedPhotosCount: 0
82 property int selectedVideosCount: 0
83
84 //internal
85 // HACK: this is used as a spurious extra QML condition in our isSelected
86 // check so we can cause the function to be reevaluated whenever the
87 // selection changes. I couldn't see any easier way to trigger the model's
88 // isSelected function to get reevaluated in QML whenever its internal
89 // selection state changes.
90 property int refresh: 1
91
92 /*!
93 */
94 function isSelected(item) {
95 return refresh > 0 && model.isSelected(item);
96 }
97
98 /*!
99 */
100 function toggleSelection(item) {
101 if (tryEnterSelectionMode()) {
102 if (singleSelect) {
103 var select = !isSelected(item);
104 unselectAll();
105 if (select)
106 model.toggleSelection(item);
107 }
108 else
109 model.toggleSelection(item);
110 }
111 }
112
113 /*!
114 */
115 function selectAll() {
116 if (tryEnterSelectionMode() && !singleSelect)
117 model.selectAll();
118 }
119
120 /*!
121 */
122 function unselectAll() {
123 model.unselectAll();
124 }
125
126 /*!
127 */
128 function tryEnterSelectionMode() {
129 if (allowSelectionModeChange)
130 inSelectionMode = true;
131 return inSelectionMode;
132 }
133
134 /*!
135 */
136 function leaveSelectionMode() {
137 if (allowSelectionModeChange) {
138 inSelectionMode = false;
139 model.unselectAll();
140 }
141 }
142
143 Connections {
144 target: model
145 onSelectedCountChanged: ++refresh
146 }
147 }