FolderView.qml 55.3 KB
Newer Older
1
2
3
4
5
/*
    SPDX-FileCopyrightText: 2014-2015 Eike Hein <hein@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
6

7
import QtQuick 2.12
8
import QtQuick.Layouts 1.1
Konrad Materka's avatar
Konrad Materka committed
9
import QtQml 2.15
10

Konrad Materka's avatar
Konrad Materka committed
11
import org.kde.plasma.plasmoid 2.0
12
13
14
15
16
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.kquickcontrolsaddons 2.0

17
import org.kde.private.desktopcontainment.folder 0.1 as Folder
18
import "code/FolderTools.js" as FolderTools
19

20
FocusScope {
21
22
    id: main

Eike Hein's avatar
Eike Hein committed
23
24
    signal pressed

25
26
27
    property QtObject model: dir
    property Item rubberBand: null

Marco Martin's avatar
Marco Martin committed
28
    property alias view: gridView
29
    property alias isRootView: gridView.isRootView
30
    property alias currentIndex: gridView.currentIndex
31
    property alias url: dir.url
32
    property alias status: dir.status
33
    property alias positions: positioner.positions
Eike Hein's avatar
Eike Hein committed
34
    property alias errorString: dir.errorString
35
    property alias dragging: dir.dragging
36
37
    property alias locked: dir.locked
    property alias sortMode: dir.sortMode
38
39
40
41
42
43
44
    property alias filterMode: dir.filterMode
    property alias filterPattern: dir.filterPattern
    property alias filterMimeTypes: dir.filterMimeTypes
    property alias flow: gridView.flow
    property alias layoutDirection: gridView.layoutDirection
    property alias cellWidth: gridView.cellWidth
    property alias cellHeight: gridView.cellHeight
45
    property alias overflowing: gridView.overflowing
46
47
48
49
    property alias scrollLeft: gridView.scrollLeft
    property alias scrollRight: gridView.scrollRight
    property alias scrollUp: gridView.scrollUp
    property alias scrollDown: gridView.scrollDown
50
    property alias hoveredItem: gridView.hoveredItem
51
    property var history: []
52
53
    property var lastPosition: null
    property bool goingBack: false
54
    property Item backButton: null
55
    property var dialog: null
56
    property Item editor: null
57

58
59
    property int previouslySelectedItemIndex: -1

Eike Hein's avatar
Eike Hein committed
60
61
62
63
    function positionViewAtBeginning() {
        gridView.positionViewAtBeginning();
    }

64
    function rename() {
65
        if (gridView.currentIndex != -1) {
66
67
68
69
70
            var renameAction = folderView.model.action("rename");
            if (renameAction && !renameAction.enabled) {
                return;
            }

71
72
73
74
            if (!editor) {
                editor = editorComponent.createObject(listener);
            }

75
76
77
78
            editor.targetItem = gridView.currentItem;
        }
    }

79
    function cancelRename() {
80
81
82
        if (editor) {
            editor.targetItem = null;
        }
83
84
    }

Eike Hein's avatar
Eike Hein committed
85
86
87
    function linkHere(sourceUrl) {
        dir.linkHere(sourceUrl);
    }
88

89
90
91
    function handleDragMove(x, y) {
        var child = childAt(x, y);

Laurent Montel's avatar
Laurent Montel committed
92
        if (child !== null && child === backButton) {
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
            hoveredItem = null;
            backButton.handleDragMove();
        } else {
            if (backButton && backButton.containsDrag) {
                backButton.endDragMove();
            }

            var pos = mapToItem(gridView.contentItem, x, y);
            var item = gridView.itemAt(pos.x, pos.y);

            if (item && item.isDir) {
                hoveredItem = item;
            } else {
                hoveredItem = null;
            }
        }
    }

    function endDragMove() {
        if (backButton && backButton.active) {
            backButton.endDragMove();
        } else if (hoveredItem && !hoveredItem.popupDialog) {
            hoveredItem = null;
        }
    }

119
120
    function dropItemAt(pos) {
        var item = gridView.itemAt(pos.x, pos.y);
121
122
123
124
125
126

        if (item) {
            if (item.blank) {
                return -1;
            }

127
128
            var hOffset = Math.abs(Math.min(gridView.contentX, gridView.originX));
            var hPos = mapToItem(item.hoverArea, pos.x + hOffset, pos.y);
129
130
131
132
133
134
135
136
137
138
139

            if ((hPos.x < 0 || hPos.y < 0 || hPos.x > item.hoverArea.width || hPos.y > item.hoverArea.height)) {
                return -1;
            } else {
                return positioner.map(item.index);
            }
        }

        return -1;
    }

140
141
142
143
144
145
    function drop(target, event, pos) {
        var dropPos = mapToItem(gridView.contentItem, pos.x, pos.y);
        var dropIndex = gridView.indexAt(dropPos.x, dropPos.y);
        var dragPos = mapToItem(gridView.contentItem, listener.dragX, listener.dragY);
        var dragIndex = gridView.indexAt(dragPos.x, dragPos.y);

Laurent Montel's avatar
Laurent Montel committed
146
        if (listener.dragX == -1 || dragIndex !== dropIndex) {
147
            dir.drop(target, event, dropItemAt(dropPos), root.isContainment && !plasmoid.immutable);
148
149
150
        }
    }

151
152
    Connections {
        target: dir
Konrad Materka's avatar
Konrad Materka committed
153
        function onPopupMenuAboutToShow(dropJob, mimeData, x, y) {
154
            if (root.isContainment && !plasmoid.immutable) {
155
156
                plasmoid.processMimeData(mimeData, x, y, dropJob);
            }
157
158
        }
    }
159

160
161
    Connections {
        target: plasmoid
Konrad Materka's avatar
Konrad Materka committed
162
        function onExpandedChanged() {
163
164
165
166
167
168
            if (plasmoid.expanded && dir.status === Folder.FolderModel.Ready && !gridView.model) {
                gridView.model = positioner;
            }
        }
    }

169
170
171
172
    Binding {
        target: plasmoid
        property: "busy"
        value: !gridView.model && dir.status === Folder.FolderModel.Listing
Konrad Materka's avatar
Konrad Materka committed
173
        restoreMode: Binding.RestoreBinding
174
175
    }

176
177
178
179
180
    function makeBackButton() {
        return Qt.createQmlObject("BackButtonItem {}", main);
    }

    function doCd(row) {
181
        history.push({"url": url, "index": gridView.currentIndex, "yPosition": gridView.visibleArea.yPosition});
182
183
        updateHistory();
        dir.cd(row);
184
        gridView.currentIndex = -1;
185
186
187
    }

    function doBack() {
188
189
190
191
        goingBack = true;
        gridView.currentIndex = -1;
        lastPosition = history.pop();
        url = lastPosition.url;
192
193
194
195
196
197
        updateHistory();
    }

    // QML doesn't detect change in the array(history) property, so update it explicitly.
    function updateHistory() {
        history = history;
198
199
200
201
202
    }

    Connections {
        target: root

Konrad Materka's avatar
Konrad Materka committed
203
        function onIsPopupChanged() {
204
205
206
207
            if (backButton == null && root.useListViewMode) {
                backButton = makeBackButton();
            } else if (backButton != null) {
                backButton.destroy();
208
209
210
211
            }
        }
    }

Eike Hein's avatar
Eike Hein committed
212
213
    MouseEventListener {
        id: listener
214

215
        anchors {
216
            topMargin: backButton != null ? backButton.height : undefined
217
218
            fill: parent
        }
219

Eike Hein's avatar
Eike Hein committed
220
        property alias hoveredItem: gridView.hoveredItem
221

222
223
224
        property Item pressedItem: null
        property int pressX: -1
        property int pressY: -1
Eike Hein's avatar
Eike Hein committed
225
226
        property int dragX: -1
        property int dragY: -1
227
        property variant cPress: null
Eike Hein's avatar
Eike Hein committed
228
229
        property bool doubleClickInProgress: false

230
231
        acceptedButtons: {
            if (hoveredItem == null && main.isRootView) {
232
                return root.isPopup ? (Qt.LeftButton | Qt.MiddleButton | Qt.BackButton) : Qt.LeftButton;
233
234
            }

235
            return root.isPopup ? (Qt.LeftButton | Qt.MiddleButton | Qt.RightButton | Qt.BackButton)
236
237
238
                : (Qt.LeftButton | Qt.RightButton);
        }

Eike Hein's avatar
Eike Hein committed
239
240
        hoverEnabled: true

241
242
243
244
245
246
247
248
        onPressXChanged: {
            cPress = mapToItem(gridView.contentItem, pressX, pressY);
        }

        onPressYChanged: {
            cPress = mapToItem(gridView.contentItem, pressX, pressY);
        }

Eike Hein's avatar
Eike Hein committed
249
        onPressed: {
250
251
252
253
254
            // Ignore press events outside the viewport (i.e. on scrollbars).
            if (!scrollArea.viewport.contains(Qt.point(mouse.x,mouse.y))) {
                return;
            }

255
            scrollArea.focus = true;
256

257
            if (mouse.buttons & Qt.BackButton) {
Laurent Montel's avatar
Laurent Montel committed
258
                if (root.isPopup && dir.resolvedUrl !== dir.resolve(plasmoid.configuration.url)) {
259
                    doBack();
260
                    mouse.accepted = true;
261
262
263
264
265
                }

                return;
            }

Laurent Montel's avatar
Laurent Montel committed
266
            if (editor && childAt(mouse.x, mouse.y) !== editor) {
267
                editor.commit();
268
269
            }

270
271
272
273
274
275
276
277
278
279
280
281
282
283
            if (mouse.source === Qt.MouseEventSynthesizedByQt) {
                var index = gridView.indexAt(mouse.x, mouse.y);
                var indexItem = gridView.itemAtIndex(index);
                if (indexItem && indexItem.iconArea) {
                    gridView.currentIndex = index;
                    hoveredItem = indexItem;
                } else {
                    hoveredItem = null;
                }
                if (gridView.hoveredItem && gridView.hoveredItem.toolTip.active) {
                    gridView.hoveredItem.toolTip.hideToolTip();
                }
            }

284
285
286
287
288
            pressX = mouse.x;
            pressY = mouse.y;

            if (!hoveredItem || hoveredItem.blank) {
                if (!gridView.ctrlPressed) {
Eike Hein's avatar
Eike Hein committed
289
                    gridView.currentIndex = -1;
290
                    previouslySelectedItemIndex = -1;
291
292
                    dir.clearSelection();
                }
293

294
295
                if (mouse.buttons & Qt.RightButton) {
                    clearPressState();
296
                    dir.openContextMenu(main, mouse.modifiers);
297
                    mouse.accepted = true;
298
299
300
                }
            } else {
                pressedItem = hoveredItem;
Eike Hein's avatar
Eike Hein committed
301

302
                var pos = mapToItem(hoveredItem.actionsOverlay, mouse.x, mouse.y);
303

304
305
306
                if (!(pos.x <= hoveredItem.actionsOverlay.width && pos.y <= hoveredItem.actionsOverlay.height)) {
                    if (gridView.shiftPressed && gridView.currentIndex != -1) {
                        positioner.setRangeSelected(gridView.anchorIndex, hoveredItem.index);
307
                    } else {
308
                        // Deselecting everything else when one item is clicked is handled in onReleased in order to distinguish between drag and click
309
                        if (!gridView.ctrlPressed && !dir.isSelected(positioner.map(hoveredItem.index))) {
310
                            previouslySelectedItemIndex = -1;
311
312
313
314
315
                            dir.clearSelection();
                        }

                        if (gridView.ctrlPressed) {
                            dir.toggleSelected(positioner.map(hoveredItem.index));
316
                        } else {
317
318
                            dir.setSelected(positioner.map(hoveredItem.index));
                        }
319
                    }
320

321
                    gridView.currentIndex = hoveredItem.index;
322

323
                    if (mouse.buttons & Qt.RightButton) {
324
325
326
327
                        if (pressedItem.toolTip && pressedItem.toolTip.active) {
                            pressedItem.toolTip.hideToolTip();
                        }

328
                        clearPressState();
329

330
                        dir.openContextMenu(hoveredItem, mouse.modifiers);
331
                        mouse.accepted = true;
332
                    }
Eike Hein's avatar
Eike Hein committed
333
                }
334
            }
Eike Hein's avatar
Eike Hein committed
335
336

            main.pressed();
337
338
        }

339
        onCanceled: pressCanceled()
340
341

        onReleased: {
342
            if (hoveredItem && !hoveredItem.blank && mouse.button != Qt.RightButton) {
343
344
345
346
347
348
349
350
351
                var pos = mapToItem(hoveredItem.actionsOverlay, mouse.x, mouse.y);
                if (!(pos.x <= hoveredItem.actionsOverlay.width && pos.y <= hoveredItem.actionsOverlay.height)
                    && (!(gridView.shiftPressed && gridView.currentIndex != -1) && !gridView.ctrlPressed)) {
                        dir.clearSelection();
                        dir.setSelected(positioner.map(hoveredItem.index));
                }
            }
            pressCanceled();
        }
352

353
354
355
356
357
358
359
360
361
        onPressAndHold: {
            if (mouse.source == Qt.MouseEventSynthesizedByQt) {
                if (pressedItem) {
                    if (pressedItem.toolTip && pressedItem.toolTip.active) {
                        pressedItem.toolTip.hideToolTip();
                    }
                }
                clearPressState();
                if (hoveredItem) {
362
                    dir.openContextMenu(hoveredItem, mouse.modifiers);
363
364
365
366
                }
            }
        }

Eike Hein's avatar
Eike Hein committed
367
        onClicked: {
368
369
            clearPressState();

370
            if (mouse.button === Qt.RightButton ||
371
                (editor && childAt(mouse.x, mouse.y) === editor)) {
Eike Hein's avatar
Eike Hein committed
372
373
                return;
            }
374

375
            if (!hoveredItem || hoveredItem.blank || gridView.currentIndex == -1 || gridView.ctrlPressed || gridView.shiftPressed) {
376
377
                // Bug 357367: Replay mouse event, so containment actions assigned to left mouse button work.
                eventGenerator.sendMouseEvent(plasmoid, EventGenerator.MouseButtonPress, mouse.x, mouse.y, mouse.button, mouse.buttons, mouse.modifiers);
Eike Hein's avatar
Eike Hein committed
378
379
                return;
            }
380

381
382
383
384
            var pos = mapToItem(hoveredItem, mouse.x, mouse.y);

            // Moving from an item to its preview popup dialog doesn't unset hoveredItem
            // even though the cursor has left it, so we need to check whether the click
Yuri Chornoivan's avatar
Yuri Chornoivan committed
385
            // actually occurred inside the item we expect it in before going ahead. If it
386
387
388
389
            // didn't, clean up (e.g. dismissing the dialog as a side-effect of unsetting
            // hoveredItem) and abort.
            if (pos.x < 0 || pos.x > hoveredItem.width || pos.y < 0 || pos.y > hoveredItem.height) {
                hoveredItem = null;
390
                previouslySelectedItemIndex = -1;
391
392
393
394
395
396
397
398
399
400
401
402
                dir.clearSelection();

                return;
            // If the hoveredItem is clicked while having a preview popup dialog open,
            // only dismiss the dialog and abort.
            } else if (hoveredItem.popupDialog) {
                hoveredItem.closePopup();

                return;
            }

            pos = mapToItem(hoveredItem.actionsOverlay, mouse.x, mouse.y);
403

404
            if (!(pos.x <= hoveredItem.actionsOverlay.width && pos.y <= hoveredItem.actionsOverlay.height)) {
405
406
407
408
409
410
411
412
413

                // Clicked on the label of an already-selected item: rename it
                if (pos.x > hoveredItem.labelArea.x
                    && pos.x <= hoveredItem.labelArea.x + hoveredItem.labelArea.width
                    && pos.y > hoveredItem.labelArea.y
                    && pos.y <= hoveredItem.labelArea.y + hoveredItem.labelArea.height
                    && previouslySelectedItemIndex == gridView.currentIndex
                    && gridView.currentIndex != -1
                    && !Qt.styleHints.singleClickActivation
414
                    && plasmoid.configuration.renameInline
415
416
417
418
419
420
421
422
                    && !doubleClickInProgress
                ) {
                    rename();
                    return;
                }

                // Single-click mode and single-clicked on the item or
                // double-click mode and double-clicked on the item: open it
423
                if (Qt.styleHints.singleClickActivation || doubleClickInProgress || mouse.source == Qt.MouseEventSynthesizedByQt) {
Laurent Montel's avatar
Laurent Montel committed
424
                    var func = root.useListViewMode && (mouse.button === Qt.LeftButton) && hoveredItem.isDir ? doCd : dir.run;
425
                    func(positioner.map(gridView.currentIndex));
426
                    previouslySelectedItemIndex = gridView.currentIndex;
Eike Hein's avatar
Eike Hein committed
427
                    hoveredItem = null;
428
429
430
431
                }

                // None of the above: select it
                else {
Eike Hein's avatar
Eike Hein committed
432
                    doubleClickInProgress = true;
433
                    doubleClickTimer.interval = Qt.styleHints.mouseDoubleClickInterval;
Eike Hein's avatar
Eike Hein committed
434
                    doubleClickTimer.start();
435
                    previouslySelectedItemIndex = gridView.currentIndex;
Eike Hein's avatar
Eike Hein committed
436
437
438
                }
            }
        }
439

Eike Hein's avatar
Eike Hein committed
440
        onPositionChanged: {
441
442
443
444
445
            gridView.ctrlPressed = (mouse.modifiers & Qt.ControlModifier);
            gridView.shiftPressed = (mouse.modifiers & Qt.ShiftModifier);

            var cPos = mapToItem(gridView.contentItem, mouse.x, mouse.y);
            var item = gridView.itemAt(cPos.x, cPos.y);
446
            var leftEdge = Math.min(gridView.contentX, gridView.originX);
447

448
            if (!item || item.blank) {
449
                if (gridView.hoveredItem && !root.containsDrag && (!dialog || !dialog.containsDrag) && !gridView.hoveredItem.popupDialog) {
450
451
                    gridView.hoveredItem = null;
                }
452
            } else {
453
                var fPos = mapToItem(item.frame, mouse.x, mouse.y);
454

455
                if (fPos.x < 0 || fPos.y < 0 || fPos.x > item.frame.width || fPos.y > item.frame.height) {
456
457
458
459
460
461
                    gridView.hoveredItem = null;
                }
            }

            // Trigger autoscroll.
            if (pressX != -1) {
462
                gridView.scrollLeft = (mouse.x <= 0 && gridView.contentX > leftEdge);
463
464
465
466
467
468
469
470
471
472
473
474
                gridView.scrollRight = (mouse.x >= gridView.width
                    && gridView.contentX < gridView.contentItem.width - gridView.width);
                gridView.scrollUp = (mouse.y <= 0 && gridView.contentY > 0);
                gridView.scrollDown = (mouse.y >= gridView.height
                    && gridView.contentY < gridView.contentItem.height - gridView.height);
            }

            // Update rubberband geometry.
            if (main.rubberBand) {
                var rB = main.rubberBand;

                if (cPos.x < cPress.x) {
475
                    rB.x = Math.max(leftEdge, cPos.x);
476
477
478
                    rB.width = Math.abs(rB.x - cPress.x);
                } else {
                    rB.x = cPress.x;
479
                    var ceil = Math.max(gridView.width, gridView.contentItem.width) + leftEdge;
480
481
482
483
484
485
486
487
488
489
490
491
                    rB.width = Math.min(ceil - rB.x, Math.abs(rB.x - cPos.x));
                }

                if (cPos.y < cPress.y) {
                    rB.y = Math.max(0, cPos.y);
                    rB.height = Math.abs(rB.y - cPress.y);
                } else {
                    rB.y = cPress.y;
                    var ceil = Math.max(gridView.height, gridView.contentItem.height);
                    rB.height = Math.min(ceil - rB.y, Math.abs(rB.y - cPos.y));
                }

492
493
494
495
496
                // Ensure rubberband is at least 1px in size or else it will become
                // invisible and not match any items.
                rB.width = Math.max(1, rB.width);
                rB.height = Math.max(1, rB.height);

497
498
499
500
501
502
                gridView.rectangleSelect(rB.x, rB.y, rB.width, rB.height);

                return;
            }

            // Drag initiation.
503
            if (pressX != -1 && root.isDrag(pressX, pressY, mouse.x, mouse.y)) {
504
                if (pressedItem != null && dir.isSelected(positioner.map(pressedItem.index))) {
505
                    pressedItem.toolTip.hideToolTip();
Eike Hein's avatar
Eike Hein committed
506
507
                    dragX = mouse.x;
                    dragY = mouse.y;
508
                    gridView.verticalDropHitscanOffset = pressedItem.iconArea.y + (pressedItem.iconArea.height / 2)
509
                    dir.dragSelected(mouse.x, mouse.y);
510
511
                    dragX = -1;
                    dragY = -1;
512
513
                    clearPressState();
                } else {
514
                    // Disable rubberband in popup list view mode or while renaming
515
                    if (root.useListViewMode || (editor && editor.targetItem)) {
516
517
518
                        return;
                    }

519
                    dir.pinSelection();
520
                    main.rubberBand = rubberBandObject.createObject(gridView.contentItem, {x: cPress.x, y: cPress.y})
521
522
523
524
525
                    gridView.interactive = false;
                }
            }
        }

526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
        Component {
            id: rubberBandObject

            Folder.RubberBand {
                id: rubberBand

                width: 0
                height: 0
                z: 99999

                function close() {
                    opacityAnimation.restart()
                }

                OpacityAnimator {
                    id: opacityAnimation
                    target: rubberBand
                    to: 0
                    from: 1
                    duration: PlasmaCore.Units.shortDuration

                    // This easing curve has an elognated start, which works
                    // better than a standard easing curve for the rubberband
                    // animation, which fades out fast and is generally of a
                    // small area.
                    easing {
                        bezierCurve: [0.4, 0.0, 1, 1]
                        type: Easing.Bezier
                    }

                    onFinished: {
                        rubberBand.visible = false
                        rubberBand.enabled = false
                        rubberBand.destroy()
                    }
                }
            }
        }

565
566
567
        onContainsMouseChanged: {
            if (!containsMouse && !main.rubberBand) {
                clearPressState();
568
569
570
571

                if (gridView.hoveredItem && !gridView.hoveredItem.popupDialog) {
                    gridView.hoveredItem = null;
                }
Eike Hein's avatar
Eike Hein committed
572
573
            }
        }
574

Eike Hein's avatar
Eike Hein committed
575
        onHoveredItemChanged: {
576
            doubleClickInProgress = false;
577
578
579
580

            if (!hoveredItem) {
                hoverActivateTimer.stop();
            }
581
582
        }

583
584
        function pressCanceled() {
            if (main.rubberBand) {
585
586
587
                main.rubberBand.close()
                main.rubberBand = null

588
589
590
591
592
593
594
595
596
                gridView.interactive = true;
                gridView.cachedRectangleSelection = null;
                dir.unpinSelection();
            }

            clearPressState();
            gridView.cancelAutoscroll();
        }

597
598
599
600
601
602
        function clearPressState() {
            pressedItem = null;
            pressX = -1;
            pressY = -1;
        }

Eike Hein's avatar
Eike Hein committed
603
604
605
606
        Timer {
            id: doubleClickTimer

            onTriggered: {
607
                listener.doubleClickInProgress = false;
Eike Hein's avatar
Eike Hein committed
608
            }
609
610
        }

611
612
613
614
615
616
617
618
619
620
621
622
        Timer {
            id: hoverActivateTimer

            interval: root.hoverActivateDelay

            onTriggered: {
                if (!hoveredItem) {
                    return;
                }

                if (root.useListViewMode) {
                    doCd(index);
623
                } else if (plasmoid.configuration.popups) {
624
625
626
627
628
                    hoveredItem.openPopup();
                }
            }
        }

Eike Hein's avatar
Eike Hein committed
629
630
        PlasmaExtras.ScrollArea {
            id: scrollArea
631

632
            anchors.fill: parent
633

Eike Hein's avatar
Eike Hein committed
634
635
            focus: true

636
637
638
639
640
641
642
            property bool ready: false
            readonly property int viewportWidth: scrollArea.ready && viewport ? Math.ceil(viewport.width) : 0
            readonly property int viewportHeight: scrollArea.ready && viewport ? Math.ceil(viewport.height) : 0

            Component.onCompleted: {
                scrollArea.ready = true;
            }
643

Eike Hein's avatar
Eike Hein committed
644
645
            GridView {
                id: gridView
646

Eike Hein's avatar
Eike Hein committed
647
                property bool isRootView: false
648

649
                property int iconSize: makeIconSize()
650
                property int verticalDropHitscanOffset: 0
651

Eike Hein's avatar
Eike Hein committed
652
                property Item hoveredItem: null
653

Eike Hein's avatar
Eike Hein committed
654
655
656
                property int anchorIndex: 0
                property bool ctrlPressed: false
                property bool shiftPressed: false
657

658
659
                property bool overflowing: (visibleArea.heightRatio < 1.0 || visibleArea.widthRatio < 1.0)

660
661
662
663
664
665
666
                property bool scrollLeft: false
                property bool scrollRight: false
                property bool scrollUp: false
                property bool scrollDown: false

                property variant cachedRectangleSelection: null

Eike Hein's avatar
Eike Hein committed
667
                currentIndex: -1
668

Eike Hein's avatar
Eike Hein committed
669
670
                keyNavigationWraps: false
                boundsBehavior: Flickable.StopAtBounds
671

672
673
674
675
676
                function calcExtraSpacing(cellSize, containerSize) {
                    var availableColumns = Math.floor(containerSize / cellSize);
                    var extraSpacing = 0;
                    if (availableColumns > 0) {
                        var allColumnSize = availableColumns * cellSize;
677
                        var extraSpace = Math.max(containerSize - allColumnSize, 0);
678
679
                        extraSpacing = extraSpace / availableColumns;
                    }
680
                    return Math.floor(extraSpacing);
681
682
                }

683
                cellWidth: {
684
                    if (root.useListViewMode) {
685
                        return gridView.width;
686
                    } else {
687
                        var iconWidth = iconSize + (2 * PlasmaCore.Units.largeSpacing) + (2 * PlasmaCore.Units.smallSpacing);
688
                        if (root.isContainment && isRootView && scrollArea.viewportWidth > 0) {
689
                            var minIconWidth = Math.max(iconWidth, PlasmaCore.Units.iconSizes.small * ((plasmoid.configuration.labelWidth * 2) + 4));
690
691
                            var extraWidth = calcExtraSpacing(minIconWidth, scrollArea.viewportWidth);
                            return minIconWidth + extraWidth;
692
693
694
                        } else {
                            return iconWidth;
                        }
695
696
697
698
                    }
                }

                cellHeight: {
699
                    if (root.useListViewMode) {
Nate Graham's avatar
Nate Graham committed
700
                        return Math.ceil((Math.max(PlasmaCore.Theme.mSize(PlasmaCore.Theme.defaultFont).height, iconSize)
701
702
                            + Math.max(highlightItemSvg.margins.top + highlightItemSvg.margins.bottom,
                            listItemSvg.margins.top + listItemSvg.margins.bottom)) / 2) * 2;
703
                    } else {
Nate Graham's avatar
Nate Graham committed
704
                        var iconHeight = iconSize + (PlasmaCore.Theme.mSize(PlasmaCore.Theme.defaultFont).height * plasmoid.configuration.textLines) + (4 * PlasmaCore.Units.smallSpacing);
705
706
707
708
709
710
                        if (root.isContainment && isRootView && scrollArea.viewportHeight > 0) {
                            var extraHeight = calcExtraSpacing(iconHeight, scrollArea.viewportHeight);
                            return iconHeight + extraHeight;
                        } else {
                            return iconHeight;
                        }
711
712
                    }
                }
713

714
                delegate: FolderItemDelegate {
Eike Hein's avatar
Eike Hein committed
715
716
                    width: gridView.cellWidth
                    height: gridView.cellHeight
717
718
                }

719
                onContentXChanged: {
720
721
722
723
                    if (hoveredItem) {
                        hoverActivateTimer.stop();
                    }

724
                    cancelRename();
725

726
727
728
729
730
731
732
733
734
735
                    dir.setDragHotSpotScrollOffset(contentX, contentY);

                    if (contentX == 0) {
                        scrollLeft = false;
                    }

                    if (contentX == contentItem.width - width) {
                        scrollRight = false;
                    }

Yuri Chornoivan's avatar
Yuri Chornoivan committed
736
                    // Update rubberband geometry.
737
738
739
740
                    if (main.rubberBand) {
                        var rB = main.rubberBand;

                        if (scrollLeft) {
741
                            rB.x = Math.min(gridView.contentX, gridView.originX);
742
743
744
745
746
747
748
749
750
751
752
753
754
                            rB.width = listener.cPress.x;
                        }

                        if (scrollRight) {
                            var lastCol = gridView.contentX + gridView.width;
                            rB.width = lastCol - rB.x;
                        }

                        gridView.rectangleSelect(rB.x, rB.y, rB.width, rB.height);
                    }
                }

                onContentYChanged: {
755
756
757
758
                    if (hoveredItem) {
                        hoverActivateTimer.stop();
                    }

759
                    cancelRename();
760

761
762
763
764
765
766
767
768
769
770
                    dir.setDragHotSpotScrollOffset(contentX, contentY);

                    if (contentY == 0) {
                        scrollUp = false;
                    }

                    if (contentY == contentItem.height - height) {
                        scrollDown = false;
                    }

771
                    // Update rubberband geometry.
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
                    if (main.rubberBand) {
                        var rB = main.rubberBand;

                        if (scrollUp) {
                            rB.y = 0;
                            rB.height = listener.cPress.y;
                        }

                        if (scrollDown) {
                            var lastRow = gridView.contentY + gridView.height;
                            rB.height = lastRow - rB.y;
                        }

                        gridView.rectangleSelect(rB.x, rB.y, rB.width, rB.height);
                    }
                }

                onScrollLeftChanged: {
                    if (scrollLeft && gridView.visibleArea.widthRatio < 1.0) {
                        smoothX.enabled = true;
792
                        contentX = (gridView.flow == GridView.FlowLeftToRight) ? gridView.contentX : gridView.originX;
793
794
795
796
797
798
799
800
801
                    } else {
                        contentX = contentX;
                        smoothX.enabled = false;
                    }
                }

                onScrollRightChanged: {
                    if (scrollRight && gridView.visibleArea.widthRatio < 1.0) {
                        smoothX.enabled = true;
802
803
                        contentX = ((gridView.flow == GridView.FlowLeftToRight) ? gridView.contentX : gridView.originX)
                            + (contentItem.width - width);
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
                    } else {
                        contentX = contentX;
                        smoothX.enabled = false;
                    }
                }

                onScrollUpChanged: {
                    if (scrollUp && gridView.visibleArea.heightRatio < 1.0) {
                        smoothY.enabled = true;
                        contentY = 0;
                    } else {
                        contentY = contentY;
                        smoothY.enabled = false;
                    }
                }

                onScrollDownChanged: {
                    if (scrollDown && gridView.visibleArea.heightRatio < 1.0) {
                        smoothY.enabled = true;
                        contentY = contentItem.height - height;
                    } else {
                        contentY = contentY;
                        smoothY.enabled = false;
                    }
                }

                onFlowChanged: {
                    // FIXME TODO: Preserve positions.
                    if (positioner.enabled) {
                        positioner.reset();
                    }
                }

                onLayoutDirectionChanged: {
                    // FIXME TODO: Preserve positions.
                    if (positioner.enabled) {
                        positioner.reset();
                    }
                }

Eike Hein's avatar
Eike Hein committed
844
845
                onCurrentIndexChanged: {
                    positionViewAtIndex(currentIndex, GridView.Contain);
846
847
                }

848
                onCachedRectangleSelectionChanged: {
849
850
851
852
853
                    if (cachedRectangleSelection == null) {
                        return;
                    }

                    if (cachedRectangleSelection.length) {
854
855
856
                        // Set current index to start of selection.
                        // cachedRectangleSelection is pre-sorted.
                        currentIndex = cachedRectangleSelection[0];
857
                    }
858
859
860

                    dir.updateSelection(cachedRectangleSelection.map(positioner.map),
                        gridView.ctrlPressed);
861
862
                }

863
                function makeIconSize() {
864
                    if (root.useListViewMode) {
865
                        return PlasmaCore.Units.iconSizes.small;
866
867
868
869
870
                    }

                    return FolderTools.iconSizeFromTheme(plasmoid.configuration.iconSize);
                }

Eike Hein's avatar
Eike Hein committed
871
872
                function updateSelection(modifier) {
                    if (modifier & Qt.ShiftModifier) {
873
                        positioner.setRangeSelected(anchorIndex, currentIndex);
Eike Hein's avatar
Eike Hein committed
874
875
                    } else {
                        dir.clearSelection();
876
                        dir.setSelected(positioner.map(currentIndex));
877
878
879
880
                        if (currentIndex == -1) {
                            previouslySelectedItemIndex = -1;
                        }
                        previouslySelectedItemIndex = currentIndex;
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
                    }
                }

                function cancelAutoscroll() {
                    scrollLeft = false;
                    scrollRight = false;
                    scrollUp = false;
                    scrollDown = false;
                }

                function rectangleSelect(x, y, width, height) {
                    var rows = (gridView.flow == GridView.FlowLeftToRight);
                    var axis = rows ? gridView.width : gridView.height;
                    var step = rows ? cellWidth : cellHeight;
                    var perStripe = Math.floor(axis / step);
                    var stripes = Math.ceil(gridView.count / perStripe);
897
898
                    var cWidth = gridView.cellWidth - (2 * PlasmaCore.Units.smallSpacing);
                    var cHeight = gridView.cellHeight - (2 * PlasmaCore.Units.smallSpacing);
899
900
                    var midWidth = gridView.cellWidth / 2;
                    var midHeight = gridView.cellHeight / 2;
901
902
903
904
                    var indices = [];

                    for (var s = 0; s < stripes; s++) {
                        for (var i = 0; i < perStripe; i++) {
905
                            var index = (s * perStripe) + i;
906
907
908
909
910
911
912
913
914

                            if (index >= gridView.count) {
                                break;
                            }

                            if (positioner.isBlank(index)) {
                                continue;
                            }

915
916
                            var itemX = ((rows ? i : s) * gridView.cellWidth);
                            var itemY = ((rows ? s : i) * gridView.cellHeight);
917

918
                            if (gridView.effectiveLayoutDirection == Qt.RightToLeft) {
919
                                itemX -= (rows ? gridView.contentX : gridView.originX);
920
                                itemX += cWidth;
921
922
923
                                itemX = (rows ? gridView.width : gridView.contentItem.width) - itemX;
                            }

924
925
                            // Check if the rubberband intersects this cell first to avoid doing more
                            // expensive work.
926
                            if (main.rubberBand.intersects(Qt.rect(itemX + PlasmaCore.Units.smallSpacing, itemY + PlasmaCore.Units.smallSpacing,
927
928
929
930
931
932
933
934
935
936
                                cWidth, cHeight))) {
                                var item = gridView.contentItem.childAt(itemX + midWidth, itemY + midHeight);

                                // If this is a visible item, check for intersection with the actual
                                // icon or label rects for better feel.
                                if (item && item.iconArea) {
                                    var iconRect = Qt.rect(itemX + item.iconArea.x, itemY + item.iconArea.y,
                                        item.iconArea.width, item.iconArea.height);

                                    if (main.rubberBand.intersects(iconRect)) {
937
                                        indices.push(index);
938
939
940
941
942
943
944
                                        continue;
                                    }

                                    var labelRect = Qt.rect(itemX + item.labelArea.x, itemY + item.labelArea.y,
                                        item.labelArea.width, item.labelArea.height);

                                    if (main.rubberBand.intersects(labelRect)) {
945
                                        indices.push(index);
946
947
948
949
                                        continue;
                                    }
                                } else {
                                    // Otherwise be content with the cell intersection.
950
                                    indices.push(index);
951
                                }
952
953
954
955
956
                            }
                        }
                    }

                    gridView.cachedRectangleSelection = indices;
957
958
                }

959
                function runOrCdSelected() {
960
                    if (currentIndex != -1 && dir.hasSelection()) {
Eike Hein's avatar
Eike Hein committed
961
                        if (root.useListViewMode && currentItem.isDir) {
962
                            doCd(positioner.map(currentIndex));
963
964
965
                        } else {
                            dir.runSelected();
                        }
966
967
968
                    }
                }

969
970
971
                Behavior on contentX { id: smoothX; enabled: false; SmoothedAnimation { velocity: 700 } }
                Behavior on contentY { id: smoothY; enabled: false; SmoothedAnimation { velocity: 700 } }

972
973
974
975
976
977
978
979
                Keys.onReturnPressed: {
                    if (event.modifiers === Qt.AltModifier) {
                        dir.openPropertiesDialog();
                    } else {
                        runOrCdSelected();
                    }
                }
                Keys.onEnterPressed: Keys.returnPressed(event)
980

Eike Hein's avatar
Eike Hein committed
981
                Keys.onMenuPressed: {
982
                    if (currentIndex != -1 && dir.hasSelection() && currentItem) {
983
                        dir.setSelected(positioner.map(currentIndex));
984
                        dir.openContextMenu(currentItem.frame, event.modifiers);
985
986
987
                    } else {
                        // Otherwise let the containment handle it.
                        event.accepted = false;
Eike Hein's avatar
Eike Hein committed
988
                    }
989
990
                }

991
                Keys.onEscapePressed: {
992
                    if (!editor || !editor.targetItem) {
993
                        previouslySelectedItemIndex = -1;
994
                        dir.clearSelection();
995
                        event.accepted = false;
996
997
998
                    }
                }

999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
                Folder.ShortCut {
                    Component.onCompleted: {
                        installAsEventFilterFor(gridView);
                    }

                    onDeleteFile: {
                        dir.deleteSelected();
                    }
                    onRenameFile: {
                        rename();
                    }
                }

Eike Hein's avatar
Eike Hein committed
1012
                Keys.onPressed: {
1013
1014
                    event.accepted = true;

1015
                    if (event.matches(StandardKey.Delete)) {
1016
1017
1018
                        if (dir.hasSelection()) {
                            dir.action("trash").trigger();
                        }
Laurent Montel's avatar
Laurent Montel committed
1019
                    } else if (event.key === Qt.Key_Control) {
Eike Hein's avatar
Eike Hein committed
1020
                        ctrlPressed = true;
Laurent Montel's avatar
Laurent Montel committed
1021
                    } else if (event.key === Qt.Key_Shift) {
Eike Hein's avatar
Eike Hein committed
1022
                        shiftPressed = true;
1023

Eike Hein's avatar
Eike Hein committed
1024
1025
1026
                        if (currentIndex != -1) {
                            anchorIndex = currentIndex;
                        }
Laurent Montel's avatar
Laurent Montel committed
1027
                    } else if (event.key === Qt.Key_Home) {
Eike Hein's avatar
Eike Hein committed
1028
1029
                        currentIndex = 0;
                        updateSelection(event.modifiers);
Laurent Montel's avatar
Laurent Montel committed
1030
                    } else if (event.key === Qt.Key_End) {
Eike Hein's avatar
Eike Hein committed
1031
1032
                        currentIndex = count - 1;
                        updateSelection(event.modifiers);
1033
1034
1035
1036
1037
1038
                    } else if (event.matches(StandardKey.Copy)) {
                        dir.copy();
                    } else if (event.matches(StandardKey.Paste)) {
                        dir.paste();
                    } else if (event.matches(StandardKey.Cut)) {
                        dir.cut();
1039
1040
                    } else if (event.matches(StandardKey.Undo)) {
                        dir.undo();
1041
1042
                    } else if (event.matches(StandardKey.Refresh)) {
                        dir.refresh();
1043
1044
                    } else if (event.matches(StandardKey.SelectAll)) {
                        positioner.setRangeSelected(0, count - 1);
1045
1046
                    } else {
                        event.accepted = false;
Eike Hein's avatar
Eike Hein committed
1047
1048
                    }
                }
1049

Eike Hein's avatar
Eike Hein committed
1050
                Keys.onReleased: {
Laurent Montel's avatar
Laurent Montel committed
1051
                    if (event.key === Qt.Key_Control) {
1052
                        ctrlPressed = false;
Laurent Montel's avatar
Laurent Montel committed
1053
                    } else if (event.key === Qt.Key_Shift) {
Eike Hein's avatar
Eike Hein committed
1054
1055
1056
                        shiftPressed = false;
                        anchorIndex = 0;
                    }
Eike Hein's avatar