Delegate.qml 5.52 KB
Newer Older
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
/*
 *  Copyright 2019 Marco Martin <mart@kde.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  2.010-1301, USA.
 */

import QtQuick 2.4
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.3 as Controls

import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.kquickcontrolsaddons 2.0

Marco Martin's avatar
Marco Martin committed
28
import org.kde.plasma.private.containmentlayoutmanager 1.0 as ContainmentLayoutManager 
29

Marco Martin's avatar
Marco Martin committed
30
ContainmentLayoutManager.ItemContainer {
31 32 33 34 35 36
    id: delegate

    z: dragging ? 1 : 0

    property var modelData: typeof model !== "undefined" ? model : null

Marco Martin's avatar
Marco Martin committed
37 38 39 40
    leftPadding: units.smallSpacing * 2
    topPadding: units.smallSpacing * 2
    rightPadding: units.smallSpacing * 2
    bottomPadding: units.smallSpacing * 2
41 42 43

    opacity: dragging ? 0.4 : 1

44 45 46
    property real dragCenterX
    property real dragCenterY

Marco Martin's avatar
Marco Martin committed
47 48 49 50 51 52
    editModeCondition: ContainmentLayoutManager.ItemContainer.AfterPressAndHold//model.ApplicationOnDesktopRole ? ContainmentLayoutManager.ItemContainer.AfterPressAndHold: ContainmentLayoutManager.ItemContainer.Manual
    onEditModeChanged: {//FIXME: remove
        plasmoid.editMode = editMode
    }
    onDragActiveChanged: {
        if (dragActive) {
53
            // Must be 0, 0 as at this point dragCenterX and dragCenterY are on the drag before"
54
            launcherDragManager.showSpacer(delegate, 0, 0);
55 56 57 58
        } else {
            launcherDragManager.positionItem(delegate, dragCenterX, dragCenterY);
            plasmoid.editMode = false;
            editMode = false;
Marco Martin's avatar
Marco Martin committed
59
        }
60
    }
Marco Martin's avatar
Marco Martin committed
61

Marco Martin's avatar
Marco Martin committed
62 63 64 65
    onUserDrag: {
       // newPosition
        var newRow = 0;

66 67
        dragCenterX = dragCenter.x;
        dragCenterY = dragCenter.y;
68 69
        var newContainer = launcherDragManager.containerForItem(delegate, dragCenter.x, dragCenter.y);

Marco Martin's avatar
Marco Martin committed
70
        // Put it in the favorites strip
71
        if (newContainer == favoriteStrip) {
Marco Martin's avatar
Marco Martin committed
72 73 74
            var pos = favoriteStrip.mapFromItem(delegate, 0, 0);
            newRow = Math.floor((pos.x + dragCenter.x) / delegate.width);

Marco Martin's avatar
Marco Martin committed
75 76 77
            plasmoid.nativeInterface.applicationListModel.setFavoriteItem(index, true);


Marco Martin's avatar
Marco Martin committed
78
        // Put it on desktop
79
        } else if (newContainer == appletsLayout) {
Marco Martin's avatar
Marco Martin committed
80 81 82 83 84 85 86 87
            var pos = appletsLayout.mapFromItem(delegate, 0, 0);
            plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, true);
            delegate.x = pos.x
            delegate.y = pos.y
            return;
    
        // Put it in the general view
        } else {
88 89
            newRow = Math.round(newContainer.flow.width / delegate.width) * Math.floor((delegate.y + dragCenter.y) / delegate.height) + Math.floor((delegate.x + dragCenter.x) / delegate.width) + favoriteStrip.count;

Marco Martin's avatar
Marco Martin committed
90 91
            plasmoid.nativeInterface.applicationListModel.setFavoriteItem(index, false);
            plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, false);
Marco Martin's avatar
Marco Martin committed
92
        }
93

94 95
        launcherDragManager.showSpacer(delegate, dragCenter.x, dragCenter.y);

Marco Martin's avatar
Marco Martin committed
96 97 98 99 100 101
        plasmoid.nativeInterface.applicationListModel.setDesktopItem(index, false);

        plasmoid.nativeInterface.applicationListModel.moveItem(modelData.index, newRow);
    }

    contentItem: MouseArea {
102 103 104 105 106 107 108 109 110 111 112 113 114
        onClicked: {
            if (modelData.ApplicationStartupNotifyRole) {
                clickFedbackAnimation.target = delegate;
                clickFedbackAnimation.running = true;
                feedbackWindow.title = modelData.ApplicationNameRole;
                feedbackWindow.state = "open";
            }
            plasmoid.nativeInterface.applicationListModel.runApplication(modelData.ApplicationStorageIdRole);
        }

        ColumnLayout {
            anchors.fill: parent
            spacing: 0
Marco Martin's avatar
Marco Martin committed
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
            PlasmaCore.IconItem {
                id: icon

                Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
                Layout.fillWidth: true
                Layout.preferredHeight: parent.height - root.reservedSpaceForLabel

                source: modelData ? modelData.ApplicationIconRole : ""
                scale: root.reorderingApps && dragDelegate && !dragging ? 0.6 : 1
                Behavior on scale {
                    NumberAnimation {
                        duration: units.longDuration
                        easing.type: Easing.InOutQuad
                    }
                }
            }

            PlasmaComponents.Label {
                id: label
                visible: text.length > 0

                Layout.fillWidth: true
                Layout.fillHeight: true
                wrapMode: Text.WordWrap
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignTop
                maximumLineCount: 2
                elide: Text.ElideRight

                text: modelData ? modelData.ApplicationNameRole : ""
                font.pixelSize: theme.defaultFont.pixelSize
                color: PlasmaCore.ColorScope.textColor
            }
        }
    }
}