forked from linuxdeepin/image-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginbaseutils.cpp
More file actions
337 lines (305 loc) · 10.4 KB
/
pluginbaseutils.cpp
File metadata and controls
337 lines (305 loc) · 10.4 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "pluginbaseutils.h"
#include "unionimage.h"
#include "image-viewer_global.h"
#include "service/commonservice.h"
#include <stdio.h>
#include <fcntl.h>
#include <fstream>
#include <linux/fs.h>
#include <QApplication>
#include <QClipboard>
#include <QCryptographicHash>
#include <QDateTime>
#include <QDesktopServices>
#include <QDir>
#include <QFontMetrics>
#include <QFileInfo>
#include <QImage>
#include <QMimeData>
#include <QProcess>
#include <QUrl>
#include <QUrlQuery>
#include <QDebug>
#include <QTextStream>
#include <QtMath>
#include <QDirIterator>
#include <QImageReader>
#include <QMimeDatabase>
#include <DApplication>
#include <DDesktopServices>
DWIDGET_USE_NAMESPACE
namespace pluginUtils {
namespace base {
const QString DATETIME_FORMAT_NORMAL = "yyyy.MM.dd";
const QString DATETIME_FORMAT_EXIF = "yyyy:MM:dd HH:mm:ss";
//int stringHeight(const QFont &f, const QString &str)
//{
// QFontMetrics fm(f);
// return fm.boundingRect(str).height();
//}
//
//QDateTime stringToDateTime(const QString &time)
//{
// QDateTime dt = QDateTime::fromString(time, DATETIME_FORMAT_EXIF);
// if (! dt.isValid()) {
// dt = QDateTime::fromString(time, DATETIME_FORMAT_NORMAL);
// }
// return dt;
//}
//
//void showInFileManager(const QString &path)
//{
// if (path.isEmpty() || !QFile::exists(path)) {
// return;
// }
// QString m_Path = static_cast<QString>(path);
//
// QStringList spc {"#", "&", "@", "!", "?"};
// for (QString c : spc) {
// m_Path.replace(c, QUrl::toPercentEncoding(c));
// }
// QUrl url = QUrl::fromUserInput(/*"\"" + */m_Path/* + "\""*/);
// url.setPath(m_Path, QUrl::TolerantMode);
// Dtk::Widget::DDesktopServices::showFileItem(url);
//}
//void copyOneImageToClipboard(const QString &path)
//{
// QImage img(path);
// if (img.isNull())
// return;
//// Q_ASSERT(!img.isNull());
// QClipboard *cb = QApplication::clipboard();
// cb->setImage(img, QClipboard::Clipboard);
//}
//void copyImageToClipboard(const QStringList &paths)
//{
// // Get clipboard
// QClipboard *cb = qApp->clipboard();
//
// // Ownership of the new data is transferred to the clipboard.
// QMimeData *newMimeData = new QMimeData();
// QByteArray gnomeFormat = QByteArray("copy\n");
// QString text;
// QList<QUrl> dataUrls;
// for (QString path : paths) {
// if (!path.isEmpty())
// text += path + '\n';
// dataUrls << QUrl::fromLocalFile(path);
// gnomeFormat.append(QUrl::fromLocalFile(path).toEncoded()).append("\n");
// }
//
// newMimeData->setText(text.endsWith('\n') ? text.left(text.length() - 1) : text);
// newMimeData->setUrls(dataUrls);
// gnomeFormat.remove(gnomeFormat.length() - 1, 1);
// newMimeData->setData("x-special/gnome-copied-files", gnomeFormat);
//
// // Copy Image Date
//// QImage img(paths.first());
//// Q_ASSERT(!img.isNull());
//// newMimeData->setImageData(img);
//
// // Set the mimedata
// cb->setMimeData(newMimeData, QClipboard::Clipboard);
//}
//QString getFileContent(const QString &file)
//{
// QFile f(file);
// QString fileContent = "";
// if (f.open(QFile::ReadOnly)) {
// fileContent = QLatin1String(f.readAll());
// f.close();
// }
// return fileContent;
//}
//
//QString SpliteText(const QString &text, const QFont &font, int nLabelSize)
//{
////LMH0424,之前是递归,现在改了算法,判断换行
// QFontMetrics fm(font);
// double dobuleTextSize = fm.horizontalAdvance(text);
// double dobuleLabelSize = nLabelSize;
// if (dobuleTextSize > dobuleLabelSize && dobuleLabelSize > 0 && dobuleTextSize < 10000) {
// double splitCount = dobuleTextSize / dobuleLabelSize;
// int nCount = int(splitCount + 1);
// QString textSplite;
// QString textTotal = text;
// for (int index = 0; index < nCount; ++index) {
// int nPos = 0;
// long nOffset = 0;
// for (int i = 0; i < text.size(); i++) {
// nOffset += fm.width(text.at(i));
// if (nOffset >= nLabelSize) {
// nPos = i;
// break;
// }
// }
// nPos = (nPos - 1 < 0) ? 0 : nPos - 1;
// QString qstrLeftData;
// if (nCount - 1 == index) {
// qstrLeftData = textTotal;
// textSplite += qstrLeftData;
// } else {
// qstrLeftData = textTotal.left(nPos);
// textSplite += qstrLeftData + "\n";
// }
// textTotal = textTotal.mid(nPos);
// }
// return textSplite;
// } else {
// return text;
// }
//}
//QString hash(const QString &str)
//{
// return QString(QCryptographicHash::hash(str.toUtf8(), QCryptographicHash::Md5).toHex());
//}
bool checkMimeData(const QMimeData *mimeData)
{
qDebug() << "Checking mime data for drag and drop";
if (!mimeData->hasUrls()) {
qDebug() << "No URLs found in mime data";
return false;
}
QList<QUrl> urlList = mimeData->urls();
if (1 > urlList.size()) {
qDebug() << "Empty URL list in mime data";
return false;
}
bool result = false;
//遍历URL,只要存在图片就允许拖入
for (QUrl url : urlList) {
//判断图片更新
QString path = url.toLocalFile();
if (path.isEmpty()) {
path = url.path();
}
qDebug() << "Processing URL path:" << path;
QFileInfo fileinfo(path);
if (fileinfo.isDir()) {
if (LibCommonService::instance()->getImgViewerType() == imageViewerSpace::ImgViewerType::ImgViewerTypeAlbum) { //相册模式的时候额外允许文件夹拖入
qDebug() << "Album mode: Directory drag allowed";
result = true;
break;
} else {
qDebug() << "Non-album mode: Directory drag not allowed";
continue;
}
} else {
QFileInfo info(path);
QMimeDatabase db;
QMimeType mt = db.mimeTypeForFile(info.filePath(), QMimeDatabase::MatchContent);
QMimeType mt1 = db.mimeTypeForFile(info.filePath(), QMimeDatabase::MatchExtension);
QString str = info.suffix().toLower();
qDebug() << "File suffix:" << str << "MIME type (content):" << mt.name() << "MIME type (extension):" << mt1.name();
if (str.isEmpty()) {
if (mt.name().startsWith("image/") || mt.name().startsWith("video/x-mng")) {
if (supportedImageFormats().contains(str, Qt::CaseInsensitive)) {
qDebug() << "Empty suffix but supported format found";
result = true;
break;
} else if (str.isEmpty()) {
qDebug() << "Empty suffix but valid image MIME type";
result = true;
break;
}
}
} else {
if (mt1.name().startsWith("image/") || mt1.name().startsWith("video/x-mng")) {
qDebug() << "Valid image format with extension";
result = true;
break;
}
}
continue;
}
}
qDebug() << "Mime data check result:" << result;
return result;
}
//QPixmap renderSVG(const QString &filePath, const QSize &size)
//{
// QImageReader reader;
// QPixmap pixmap;
// reader.setFileName(filePath);
// if (reader.canRead() && reader.imageCount() > 0) {
// const qreal ratio = dApp->getDAppNew()->devicePixelRatio();
// reader.setScaledSize(size * ratio);
// pixmap = QPixmap::fromImage(reader.read());
// pixmap.setDevicePixelRatio(ratio);
// } else {
// pixmap.load(filePath);
// }
// return pixmap;
//}
QString mkMutiDir(const QString &path) //创建多级目录
{
qDebug() << "Creating directory structure for:" << path;
QDir dir(path);
if (dir.exists(path)) {
qDebug() << "Directory already exists:" << path;
return path;
}
QString parentDir = mkMutiDir(path.mid(0, path.lastIndexOf('/')));
QString dirname = path.mid(path.lastIndexOf('/') + 1);
QDir parentPath(parentDir);
if (!dirname.isEmpty()) {
qDebug() << "Creating subdirectory:" << dirname << "in" << parentDir;
parentPath.mkpath(dirname);
}
return parentDir + "/" + dirname;
}
bool imageSupportRead(const QString &path)
{
qDebug() << "Checking if image format is supported for:" << path;
const QString suffix = QFileInfo(path).suffix();
// take them here for good.
QStringList errorList;
errorList << "X3F";
if (errorList.indexOf(suffix.toUpper()) != -1) {
qWarning() << "Unsupported format:" << suffix;
return false;
}
bool supported = LibUnionImage_NameSpace::unionImageSupportFormat().contains(suffix.toUpper());
qDebug() << "Format support check result:" << supported;
return supported;
}
const QFileInfoList getImagesInfo(const QString &dir, bool recursive)
{
qDebug() << "Getting image information from directory:" << dir << "recursive:" << recursive;
QFileInfoList infos;
if (! recursive) {
qDebug() << "Non-recursive directory scan";
auto nsl = QDir(dir).entryInfoList(QDir::Files);
for (QFileInfo info : nsl) {
if (imageSupportRead(info.absoluteFilePath())) {
infos << info;
}
}
qDebug() << "Found" << infos.size() << "images in directory";
return infos;
}
qDebug() << "Recursive directory scan";
QDirIterator dirIterator(dir,
QDir::Files,
QDirIterator::Subdirectories);
while (dirIterator.hasNext()) {
dirIterator.next();
if (imageSupportRead(dirIterator.fileInfo().absoluteFilePath())) {
infos << dirIterator.fileInfo();
}
}
qDebug() << "Found" << infos.size() << "images in directory and subdirectories";
return infos;
}
QStringList supportedImageFormats()
{
qDebug() << "Getting list of supported image formats";
QStringList formats = LibUnionImage_NameSpace::unionImageSupportFormat();
qDebug() << "Supported formats:" << formats;
return formats;
}
} // namespace base
} // namespace utils