| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2017 Malte Veerman <[email protected]> * |
| 3 |
* Copyright (c) 2025 Tuomas Nurmi <[email protected]> * |
| 4 |
* * |
| 5 |
* This program is free software; you can redistribute it and/or modify it under * |
| 6 |
* the terms of the GNU General Public License as published by the Free Software * |
| 7 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 8 |
* version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 11 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 12 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 13 |
* * |
| 14 |
* You should have received a copy of the GNU General Public License along with * |
| 15 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 16 |
****************************************************************************************/ |
| 17 |
|
| 18 |
import QtQuick |
| 19 |
import QtQuick.Controls |
| 20 |
import QtQml.Models 2.15 |
| 21 |
import org.kde.amarok.qml 1.0 as AmarokQml |
| 22 |
import org.kde.amarok.albums 1.0 |
| 23 |
import org.kde.kirigami 2.14 as Kirigami |
| 24 |
|
| 25 |
|
| 26 |
AmarokQml.Applet { |
| 27 |
id: applet |
| 28 |
|
| 29 |
TreeView { |
| 30 |
id: treeView |
| 31 |
anchors.fill: parent |
| 32 |
boundsBehavior: Flickable.StopAtBounds |
| 33 |
boundsMovement: Flickable.StopAtBounds |
| 34 |
model: AlbumsEngine.model |
| 35 |
selectionModel: ItemSelectionModel { |
| 36 |
id: selectionModel |
| 37 |
model: AlbumsEngine.model |
| 38 |
} |
| 39 |
TapHandler { |
| 40 |
acceptedButtons: Qt.LeftButton |
| 41 |
onSingleTapped: { |
| 42 |
if (treeView.cellAtPosition(point.position) == Qt.point(-1,-1)) |
| 43 |
selectionModel.clear() |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
delegate: TreeViewDelegate { |
| 48 |
implicitHeight: !!model && !!model["size"] ? model["size"].height : 0 |
| 49 |
implicitWidth: applet.width - 2 * Kirigami.Units.smallSpacing |
| 50 |
|
| 51 |
MouseArea { |
| 52 |
// For some reason unknown to me, TableView.ExtendedSelection fails to work here, so corresponding |
| 53 |
// selection logic has been re-written here. I tried first based on TapHandler, but encountered some |
| 54 |
// seemingly unresolvable issues with first clicked item not getting selected. |
| 55 |
drag.target: dragItem |
| 56 |
drag { |
| 57 |
onActiveChanged: { |
| 58 |
if (drag.active ) |
| 59 |
{ |
| 60 |
if (!parent.selected) |
| 61 |
{ |
| 62 |
treeView.selectionModel.select(treeView.index(row, column), ItemSelectionModel.ClearAndSelect | ItemSelectionModel.Current | ItemSelectionModel.Rows); |
| 63 |
selectionModel.setCurrentIndex(treeView.index(row, column), ItemSelectionModel.NoUpdate) |
| 64 |
} |
| 65 |
dragItem.updateMimedata(); |
| 66 |
dragItem.Drag.active=true; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
acceptedButtons: Qt.LeftButton |
| 71 |
propagateComposedEvents: true |
| 72 |
anchors.fill: parent |
| 73 |
onClicked: (mouse)=> { |
| 74 |
if( mouse && mouse.x >= parent.indicator.x && mouse.x <= parent.indicator.x+parent.indicator.width |
| 75 |
&& mouse.y >= parent.indicator.y && mouse.y <= parent.indicator.y+parent.indicator.height ) |
| 76 |
{ |
| 77 |
treeView.toggleExpanded(row) |
| 78 |
return; |
| 79 |
} |
| 80 |
let treeIndex = treeView.index(row, column); |
| 81 |
switch (mouse.modifiers) { |
| 82 |
case Qt.ControlModifier: |
| 83 |
if (parent.selected) |
| 84 |
selectionModel.clearCurrentIndex() |
| 85 |
else |
| 86 |
selectionModel.setCurrentIndex(treeIndex, ItemSelectionModel.NoUpdate) |
| 87 |
|
| 88 |
selectionModel.select(treeIndex, ItemSelectionModel.Toggle) |
| 89 |
break; |
| 90 |
case Qt.ShiftModifier: |
| 91 |
if(selectionModel.currentIndex.row >= 0 ) |
| 92 |
{ |
| 93 |
for(let i=Math.min(row, selectionModel.currentIndex.row); i<Math.max(row, selectionModel.currentIndex.row); i++) |
| 94 |
{ |
| 95 |
selectionModel.select(treeView.index(i, column), ItemSelectionModel.Select) |
| 96 |
} |
| 97 |
} |
| 98 |
selectionModel.setCurrentIndex(treeIndex, ItemSelectionModel.NoUpdate) |
| 99 |
break; |
| 100 |
default: |
| 101 |
treeView.toggleExpanded(row) |
| 102 |
treeView.selectionModel.select(treeIndex, ItemSelectionModel.ClearAndSelect | ItemSelectionModel.Current | ItemSelectionModel.Rows); |
| 103 |
selectionModel.setCurrentIndex(treeIndex, ItemSelectionModel.NoUpdate) |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
TapHandler { |
| 110 |
acceptedButtons: Qt.RightButton |
| 111 |
onSingleTapped: { |
| 112 |
if (!parent.selected) { |
| 113 |
selectionModel.select(treeView.index(row, column), ItemSelectionModel.ClearAndSelect); |
| 114 |
selectionModel.setCurrentIndex(treeView.index(row, column), ItemSelectionModel.NoUpdate) |
| 115 |
} |
| 116 |
AlbumsEngine.showContextMenu(selectionModel.selectedIndexes, treeView.index(row, column)); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
contentItem: Row { |
| 121 |
spacing: Kirigami.Units.mediumSpacing |
| 122 |
width: parent.width |
| 123 |
|
| 124 |
Loader { |
| 125 |
id: albumCover |
| 126 |
active: !!model && !!model["albumCover"] |
| 127 |
height: parent.height |
| 128 |
width: height |
| 129 |
visible: active |
| 130 |
|
| 131 |
AmarokQml.PixmapItem { |
| 132 |
source: !!model ? model["albumCover"] : undefined |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
Text { |
| 137 |
anchors.verticalCenter: parent.verticalCenter |
| 138 |
color: parent.parent.selected ? applet.palette.highlightedText : applet.palette.text |
| 139 |
text: model.display |
| 140 |
textFormat: Text.StyledText |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
Item { |
| 147 |
id: dragItem |
| 148 |
width: 1; height: 1 |
| 149 |
Drag.hotSpot.x: 1 |
| 150 |
Drag.hotSpot.y: 1 |
| 151 |
Drag.supportedActions: Qt.CopyAction |
| 152 |
function updateMimedata() { |
| 153 |
Drag.mimeData={"text/plain": AlbumsEngine.getSelectedUrlList(selectionModel.selectedIndexes), |
| 154 |
"text/uri-list": AlbumsEngine.getSelectedUrlList(selectionModel.selectedIndexes)} |
| 155 |
} |
| 156 |
|
| 157 |
Drag.dragType: Drag.Automatic |
| 158 |
} |
| 159 |
} |
| 160 |
} |