Kirigami-addons

ImageMaximizeDelegate.qml
1// SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
2// SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
3
4import QtQuick
5import QtQuick.Controls as QQC2
6import QtQuick.Layouts
7
8import org.kde.kirigami as Kirigami
9
10Item {
11 id: root
12
13 /**
14 * @brief The source for the image to be viewed.
15 */
16 required property string source
17
18 /**
19 * @brief The size of the source image.
20 *
21 * This is used to calculate the maximum size of the content and temporary image.
22 */
23 required property real sourceWidth
24
25 /**
26 * @brief The size of the source image.
27 *
28 * This is used to calculate the maximum size of the content and temporary image.
29 */
30 required property real sourceHeight
31
32 /**
33 * @brief Source for the temporary content.
34 *
35 * Typically used when downloading the image to show a thumbnail or other
36 * temporary image while the main image downloads.
37 */
38 required property string tempSource
39
40 /**
41 * @brief The caption for the item.
42 *
43 * Typically set to the filename if no caption is available.
44 *
45 * @note Declared here so that parent components can access this parameter
46 * when used as a listView delegate.
47 */
48 required property string caption
49
50 /**
51 * @brief The delegate type for this item.
52 *
53 * @note Declared here so that parent components can access this parameter
54 * when used as a listView delegate.
55 */
56 readonly property int type: AlbumModelItem.Image
57
58 /**
59 * @brief The padding around the content image.
60 *
61 * The padding is factored in when calculating the maximum size of the content
62 * image.
63 */
64 property var padding: Kirigami.Units.largeSpacing
65
66 /**
67 * @brief Multiple by which the image is scaled.
68 */
69 property var scaleFactor: 1
70
71 /**
72 * @brief The angle by which the content image is rotated.
73 *
74 * The default orientation of the image is 0 degrees.
75 */
76 property int rotationAngle: 0
77
78 /**
79 * @brief Emitted when the background space around the content item is clicked.
80 */
81 signal backgroundClicked()
82
83 /**
84 * @brief Emitted when the content image is right clicked.
85 */
86 signal itemRightClicked()
87
88 /**
89 * @brief Start media playback.
90 */
91 function play() {
92 image.paused = false
93 }
94
95 /**
96 * @brief Pause media playback.
97 */
98 function pause() {
99 image.paused = true
100 }
101
102 clip: true
103
104 // AnimatedImage so we can handle GIFs.
105 AnimatedImage {
106 id: image
107
108 property var rotationInsensitiveWidth: {
109 if (sourceWidth > 0) {
110 return Math.min(root.sourceWidth, root.width - root.padding * 2);
111 } else {
112 return Math.min(sourceSize.width, root.width - root.padding * 2);
113 }
114 }
115 property var rotationInsensitiveHeight: {
116 if (sourceHeight > 0) {
117 return Math.min(root.sourceHeight, root.height - root.padding * 2)
118 } else {
119 return Math.min(sourceSize.height, root.height - root.padding * 2)
120 }
121 }
122
123 anchors.centerIn: parent
124
125 source: root.source
126
127 width: root.rotationAngle % 180 === 0 ? rotationInsensitiveWidth : rotationInsensitiveHeight
128 height: root.rotationAngle % 180 === 0 ? rotationInsensitiveHeight : rotationInsensitiveWidth
129 fillMode: Image.PreserveAspectFit
130 clip: true
131
132 Behavior on width {
133 NumberAnimation {duration: Kirigami.Units.longDuration; easing.type: Easing.InOutCubic}
134 }
135 Behavior on height {
136 NumberAnimation {duration: Kirigami.Units.longDuration; easing.type: Easing.InOutCubic}
137 }
138
139 Image {
140 id: tempImage
141 anchors.centerIn: parent
142 visible: source && status === Image.Ready && image.status !== Image.Ready
143 width: root.sourceWidth > 0 || image.sourceSize.width > 0 ? root.sourceWidth : tempImage.sourceSize.width
144 height: root.sourceHeight > 0 || image.sourceSize.height > 0 ? root.sourceHeight : tempImage.sourceSize.height
145 source: root.tempSource
146 }
147
148 transform: [
149 Rotation {
150 origin.x: image.width / 2
151 origin.y: image.height / 2
152 angle: root.rotationAngle
153
154 Behavior on angle {
155 RotationAnimation {duration: Kirigami.Units.longDuration; easing.type: Easing.InOutCubic}
156 }
157 },
158 Scale {
159 origin.x: image.width / 2
160 origin.y: image.height / 2
161 xScale: root.scaleFactor
162 yScale: root.scaleFactor
163
164 Behavior on xScale {
165 NumberAnimation {duration: Kirigami.Units.longDuration; easing.type: Easing.InOutCubic}
166 }
167 Behavior on yScale {
168 NumberAnimation {duration: Kirigami.Units.longDuration; easing.type: Easing.InOutCubic}
169 }
170 }
171 ]
172
173 MouseArea {
174 anchors.fill: parent
175 acceptedButtons: Qt.RightButton
176 onClicked: root.itemRightClicked()
177 }
178 }
179 QQC2.BusyIndicator {
180 anchors.centerIn: parent
181 visible: image.status !== Image.Ready && tempImage.status !== Image.Ready
182 running: visible
183 }
184 MouseArea {
185 anchors.fill: parent
186 acceptedButtons: Qt.LeftButton
187 onClicked: root.backgroundClicked()
188 }
189}
Q_SCRIPTABLE CaptureState status()
KDOCTOOLS_EXPORT QString transform(const QString &file, const QString &stylesheet, const QList< const char * > &params=QList< const char * >())
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.