Changeset View
Changeset View
Standalone View
Standalone View
applets/notifications/thumbnailer.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2016 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) any later version. | ||||
8 | | ||||
9 | This library is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | Lesser General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU Lesser General Public | ||||
15 | License along with this library; if not, write to the Free Software | ||||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||
17 | */ | ||||
18 | | ||||
19 | #include "thumbnailer.h" | ||||
20 | | ||||
21 | #include <KIO/PreviewJob> | ||||
22 | | ||||
23 | #include <QApplication> | ||||
24 | #include <QClipboard> | ||||
25 | #include <QIcon> | ||||
26 | #include <QMimeData> | ||||
27 | #include <QMenu> | ||||
28 | #include <QQuickItem> | ||||
29 | #include <QTimer> | ||||
30 | #include <QQuickWindow> | ||||
31 | | ||||
32 | #include <KFileItemActions> | ||||
33 | #include <KFileItemListProperties> | ||||
34 | #include <KLocalizedString> | ||||
35 | #include <KProtocolManager> | ||||
36 | #include <KPropertiesDialog> | ||||
37 | #include <KUrlMimeData> | ||||
38 | | ||||
39 | #include <KIO/OpenFileManagerWindowJob> | ||||
40 | | ||||
41 | Thumbnailer::Thumbnailer(QObject *parent) : QObject(parent) | ||||
42 | { | ||||
43 | | ||||
44 | } | ||||
45 | | ||||
46 | Thumbnailer::~Thumbnailer() = default; | ||||
47 | | ||||
48 | void Thumbnailer::classBegin() | ||||
49 | { | ||||
50 | | ||||
51 | } | ||||
52 | | ||||
53 | void Thumbnailer::componentComplete() | ||||
54 | { | ||||
55 | m_inited = true; | ||||
56 | generatePreview(); | ||||
57 | } | ||||
58 | | ||||
59 | QUrl Thumbnailer::url() const | ||||
60 | { | ||||
61 | return m_url; | ||||
62 | } | ||||
63 | | ||||
64 | void Thumbnailer::setUrl(const QUrl &url) | ||||
65 | { | ||||
66 | if (m_url != url) { | ||||
67 | m_url = url; | ||||
68 | emit urlChanged(); | ||||
69 | | ||||
70 | generatePreview(); | ||||
71 | } | ||||
72 | } | ||||
73 | | ||||
74 | QSize Thumbnailer::size() const | ||||
75 | { | ||||
76 | return m_size; | ||||
77 | } | ||||
78 | | ||||
79 | void Thumbnailer::setSize(const QSize &size) | ||||
80 | { | ||||
81 | if (m_size != size) { | ||||
82 | m_size = size; | ||||
83 | emit sizeChanged(); | ||||
84 | | ||||
85 | generatePreview(); | ||||
86 | } | ||||
87 | } | ||||
88 | | ||||
89 | bool Thumbnailer::busy() const | ||||
90 | { | ||||
91 | return m_busy; | ||||
92 | } | ||||
93 | | ||||
94 | bool Thumbnailer::hasPreview() const | ||||
95 | { | ||||
96 | return !m_pixmap.isNull(); | ||||
97 | } | ||||
98 | | ||||
99 | QPixmap Thumbnailer::pixmap() const | ||||
100 | { | ||||
101 | return m_pixmap; | ||||
102 | } | ||||
103 | | ||||
104 | QSize Thumbnailer::pixmapSize() const | ||||
105 | { | ||||
106 | return m_pixmap.size(); | ||||
107 | } | ||||
108 | | ||||
109 | QString Thumbnailer::iconName() const | ||||
110 | { | ||||
111 | return m_iconName; | ||||
112 | } | ||||
113 | | ||||
114 | bool Thumbnailer::menuVisible() const | ||||
115 | { | ||||
116 | return m_menuVisible; | ||||
117 | } | ||||
118 | | ||||
119 | void Thumbnailer::generatePreview() | ||||
120 | { | ||||
121 | if (!m_inited) { | ||||
122 | return; | ||||
123 | } | ||||
124 | | ||||
125 | if (!m_url.isValid() || !m_url.isLocalFile() || !m_size.isValid()) { | ||||
126 | return; | ||||
127 | } | ||||
128 | | ||||
129 | auto maxSize = qMax(m_size.width(), m_size.height()); | ||||
130 | KIO::PreviewJob *job = KIO::filePreview(KFileItemList({KFileItem(m_url)}), QSize(maxSize,maxSize)); | ||||
131 | job->setScaleType(KIO::PreviewJob::Scaled); | ||||
132 | job->setIgnoreMaximumSize(true); | ||||
133 | | ||||
134 | connect(job, &KIO::PreviewJob::gotPreview, this, [this](const KFileItem &item, const QPixmap &preview) { | ||||
135 | Q_UNUSED(item); | ||||
136 | m_pixmap = preview; | ||||
137 | emit pixmapChanged(); | ||||
138 | | ||||
139 | if (!m_iconName.isEmpty()) { | ||||
140 | m_iconName.clear(); | ||||
141 | emit iconNameChanged(); | ||||
142 | } | ||||
143 | }); | ||||
144 | | ||||
145 | connect(job, &KIO::PreviewJob::failed, this, [this](const KFileItem &item) { | ||||
146 | m_pixmap = QPixmap(); | ||||
147 | emit pixmapChanged(); | ||||
148 | | ||||
149 | const QString &iconName = item.determineMimeType().iconName(); | ||||
150 | if (m_iconName != iconName) { | ||||
151 | m_iconName = iconName; | ||||
152 | emit iconNameChanged(); | ||||
153 | } | ||||
154 | }); | ||||
155 | | ||||
156 | connect(job, &KJob::result, this, [this] { | ||||
157 | m_busy = false; | ||||
158 | emit busyChanged(); | ||||
159 | }); | ||||
160 | | ||||
161 | m_busy = true; | ||||
162 | emit busyChanged(); | ||||
163 | | ||||
164 | job->start(); | ||||
165 | } |