qml/TableViewDelegate: Syntax cleanup
[vlc.git] / modules / gui / qt / widgets / qml / TableViewDelegate.qml
blobffe73d30bcb5444badc872b3affc4ebb7447a15c
1 /*****************************************************************************
2  * Copyright (C) 2021 VLC authors and VideoLAN
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 as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * ( at your option ) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
17  *****************************************************************************/
19 import QtQuick         2.11
20 import QtQuick.Layouts 1.3
22 import "qrc:///widgets/" as Widgets
23 import "qrc:///style/"
25 Rectangle {
26     id: delegate
28     //---------------------------------------------------------------------------------------------
29     // Properties
30     //---------------------------------------------------------------------------------------------
32     property var rowModel: model
34     property bool selected: selectionDelegateModel.isSelected(root.model.index(index, 0))
36     readonly property bool highlighted: (selected || hoverArea.containsMouse || activeFocus)
38     readonly property int _index: index
40     property int _modifiersOnLastPress: Qt.NoModifier
42     readonly property color foregroundColor: (highlighted) ? VLCStyle.colors.bgHoverText
43                                                            : VLCStyle.colors.text
45     //---------------------------------------------------------------------------------------------
46     // Settings
47     //---------------------------------------------------------------------------------------------
49     width: view.width
51     height: root.rowHeight
53     color: (highlighted) ? VLCStyle.colors.bgHover
54                          : "transparent"
56     //---------------------------------------------------------------------------------------------
57     // Connections
58     //---------------------------------------------------------------------------------------------
60     Connections {
61         target: selectionDelegateModel
63         onSelectionChanged: {
64             delegate.selected = selectionDelegateModel.isSelected(root.model.index(index, 0));
65         }
66     }
68     //---------------------------------------------------------------------------------------------
69     // Childs
70     //---------------------------------------------------------------------------------------------
72     MouseArea {
73         id: hoverArea
75         //-----------------------------------------------------------------------------------------
76         // Settings
78         anchors.fill: parent
80         hoverEnabled: true
82         Keys.onMenuPressed: root.contextMenuButtonClicked(contextButton,rowModel)
84         acceptedButtons: Qt.RightButton | Qt.LeftButton
86         drag.target: root.dragItem
88         drag.axis: Drag.XAndYAxis
90         //-----------------------------------------------------------------------------------------
91         // Events
93         onPressed: _modifiersOnLastPress = mouse.modifiers
95         onClicked: {
96             if (mouse.button === Qt.LeftButton
97                 ||
98                 selectionDelegateModel.isSelected(root.model.index(index, 0)) == false) {
100                 selectionDelegateModel.updateSelection(mouse.modifiers, view.currentIndex, index);
102                 view.currentIndex = index;
104                 delegate.forceActiveFocus();
105             }
107             if (mouse.button === Qt.RightButton)
108                 root.rightClick(delegate, rowModel, hoverArea.mapToGlobal(mouse.x, mouse.y));
109         }
111         onPositionChanged: {
112             if (drag.active == false)
113                 return;
115             var pos = drag.target.parent.mapFromItem(hoverArea, mouseX, mouseY);
117             // FIXME: Shouldn't this be specified in VLCStyle ?
118             var delta = VLCStyle.dp(12);
120             drag.target.x = pos.x + delta;
121             drag.target.y = pos.y + delta;
122         }
124         onDoubleClicked: {
125             if (mouse.button === Qt.LeftButton)
126                 root.itemDoubleClicked(delegate._index, rowModel)
127         }
129         drag.onActiveChanged: {
130             // NOTE: Perform the "click" action because the click action is only executed on mouse
131             //       release (we are in the pressed state) but we will need the updated list on drop.
132             if (drag.active
133                 &&
134                 selectionDelegateModel.isSelected(root.model.index(index, 0)) == false) {
136                 selectionDelegateModel.updateSelection(_modifiersOnLastPress, view.currentIndex,
137                                                        index);
138             } else if (root.dragItem) {
139                 root.dragItem.Drag.drop();
140             }
142             root.dragItem.Drag.active = drag.active;
143         }
145         //-----------------------------------------------------------------------------------------
146         // Childs
148         Row {
149             id: content
151             anchors.top   : parent.top
152             anchors.bottom: parent.bottom
154             anchors.leftMargin  : VLCStyle.margin_xxxsmall
155             anchors.rightMargin : VLCStyle.margin_xxxsmall
156             anchors.topMargin   : VLCStyle.margin_xxsmall
157             anchors.bottomMargin: VLCStyle.margin_xxsmall
159             anchors.horizontalCenter: parent.horizontalCenter
161             anchors.horizontalCenterOffset: Math.round(-(root._contextButtonHorizontalSpace) / 2)
163             spacing: root.horizontalSpacing
165             Repeater {
166                 model: sortModel
168                 Item {
169                     height: parent.height
171                     width: (modelData.width) ? modelData.width
172                                              : 1
174                     Layout.alignment: Qt.AlignVCenter
176                     SmoothedAnimation on width {
177                         duration: 256
179                         easing.type: Easing.OutCubic
180                     }
182                     Loader{
183                         property var rowModel: delegate.rowModel
184                         property var colModel: modelData
186                         readonly property int index: delegate._index
188                         readonly property bool currentlyFocused: delegate.activeFocus
190                         readonly property bool containsMouse: hoverArea.containsMouse
192                         readonly property color foregroundColor: delegate.foregroundColor
194                         anchors.fill: parent
196                         sourceComponent: (colModel.colDelegate) ? colModel.colDelegate
197                                                                 : root.colDelegate
198                     }
199                 }
200             }
201         }
203         Widgets.ContextButton {
204             anchors.left: content.right
206             anchors.leftMargin: VLCStyle.margin_xxsmall
208             anchors.verticalCenter: content.verticalCenter
210             color: delegate.foregroundColor
212             backgroundColor: (hovered || activeFocus)
213                              ? VLCStyle.colors.getBgColor(delegate.selected, hovered, activeFocus)
214                              : "transparent"
216             visible: hoverArea.containsMouse
218             onClicked: root.contextMenuButtonClicked(this, delegate.rowModel)
219         }
220     }