~elopio/gallery-app/revert_workaround-1302706-click_toolbar_button_failure

« back to all changes in this revision

Viewing changes to rc/qml/MediaViewer/PhotoEditor/CropInteractor.qml

  • Committer: Leo Arias
  • Date: 2015-05-15 08:05:23 UTC
  • mfrom: (954.1.241 gallery-app)
  • Revision ID: leo.arias@canonical.com-20150515080523-i2of3vr8h7dioj59
Now the toolbar object is not needed at all.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 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 <chaz@yorba.org>
 
18
 * Lucas Beeler <lucas@yorba.org>
 
19
 */
 
20
 
 
21
import QtQuick 2.3
 
22
import Ubuntu.Components 1.1
 
23
import "GraphicsRoutines.js" as GraphicsRoutines
 
24
 
 
25
/*!
 
26
*/
 
27
Rectangle {
 
28
    id: cropInteractor
 
29
    objectName: "cropInteractor"
 
30
 
 
31
    color: "black"
 
32
 
 
33
    property alias targetPhoto: original.source
 
34
 
 
35
    property string matteColor: "black"
 
36
    property real matteOpacity: 0.6
 
37
 
 
38
    // Note: each element of the cropped rect will be in the range [0,1], since
 
39
    // in the UI we aren't using direct photo pixel values.
 
40
    signal cropped(variant rect)
 
41
    signal canceled()
 
42
 
 
43
    function computeRectSet() {
 
44
        var actualImage = Qt.rect(
 
45
            (original.width - original.paintedWidth) / 2.0,
 
46
            (original.height - original.paintedHeight) / 2.0,
 
47
            original.paintedWidth,
 
48
            original.paintedHeight
 
49
        );
 
50
        var photoPreview = GraphicsRoutines.fitRect(viewport, actualImage);
 
51
 
 
52
        var unfitCrop = Qt.rect(0, 0, photoPreview.width, photoPreview.height);
 
53
        var cropFrame = GraphicsRoutines.fitRect(viewport, unfitCrop);
 
54
 
 
55
        var photoExtent = Qt.rect(cropFrame.x, cropFrame.y,
 
56
                                  cropFrame.scaleFactor * photoPreview.width,
 
57
                                  cropFrame.scaleFactor * photoPreview.height);
 
58
 
 
59
        return {
 
60
            photoPreview: photoPreview,
 
61
            cropFrame: cropFrame,
 
62
            photoExtent: photoExtent,
 
63
            photoExtentScale: cropFrame.scaleFactor
 
64
        };
 
65
    }
 
66
 
 
67
    Item {
 
68
        id: viewport
 
69
 
 
70
        anchors.fill: parent
 
71
        anchors.margins: units.gu(6)
 
72
        z: 1
 
73
    }
 
74
 
 
75
    CropOverlay {
 
76
        id: overlay
 
77
        objectName: "cropOverlay"
 
78
 
 
79
        property real minSize: units.gu(4)
 
80
 
 
81
        anchors.fill: parent;
 
82
        visible: false;
 
83
 
 
84
        photo: original
 
85
        viewport: viewport
 
86
 
 
87
        matteColor: cropInteractor.matteColor
 
88
        matteOpacity: cropInteractor.matteOpacity
 
89
 
 
90
        z: 16
 
91
 
 
92
        onMatteRegionPressed: {
 
93
            cropInteractor.canceled();
 
94
        }
 
95
 
 
96
        onCropButtonPressed: {
 
97
            original.visible = false;
 
98
            overlay.visible = false;
 
99
            original.scale = 1.0;
 
100
            var r = overlay.getRelativeFrameRect()
 
101
            cropInteractor.cropped(overlay.getRelativeFrameRect());
 
102
        }
 
103
    }
 
104
 
 
105
    Image {
 
106
        id: original
 
107
 
 
108
        x: viewport.x
 
109
        y: viewport.y
 
110
        width: viewport.width
 
111
        height: viewport.height
 
112
        transformOrigin: Item.TopLeft
 
113
        fillMode: Image.PreserveAspectFit
 
114
        cache: false
 
115
        sourceSize {
 
116
            width: original.width
 
117
            height: original.height
 
118
        }
 
119
 
 
120
        onStatusChanged: {
 
121
            if (status == Image.Ready) {
 
122
                var rects = computeRectSet();
 
123
 
 
124
                overlay.initialFrameX = rects.cropFrame.x;
 
125
                overlay.initialFrameY = rects.cropFrame.y;
 
126
                overlay.initialFrameWidth = rects.cropFrame.width;
 
127
                overlay.initialFrameHeight = rects.cropFrame.height;
 
128
 
 
129
                overlay.resetFor(rects);
 
130
                overlay.visible = true;
 
131
 
 
132
                x = rects.photoExtent.x;
 
133
                y = rects.photoExtent.y;
 
134
                width = rects.photoPreview.width;
 
135
                height = rects.photoPreview.height;
 
136
                scale = rects.photoExtentScale;
 
137
            }
 
138
        }
 
139
    }
 
140
}