Debian Codegraph @

src/album/controller/exporter.cpp

raw
1 /*
2 * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd.
3 *
4 * Author: ZhangYong <[email protected]>
5 *
6 * Maintainer: ZhangYong <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 //#include "application.h"
22 //#include "dbmanager/dbmanager.h"
23 #include "exporter.h"
24 #include "utils/imageutils.h"
25 #include "utils/unionimage.h"
26
27 #include <QFileDialog>
28 #include <QDir>
29 #include <QStandardPaths>
30 #include <QDebug>
31 #include <QMimeDatabase>
32
33 Exporter *Exporter::m_exporter = nullptr;
34 Exporter *Exporter::instance()
35 {
36 if (!m_exporter) {
37 m_exporter = new Exporter();
38 }
39
40 return m_exporter;
41 }
42
43
44 Exporter::Exporter(QObject *parent)
45 : QObject(parent)
46 , m_exportImageDialog(nullptr)
47 {
48 m_exportImageDialog = new CExportImageDialog();
49 }
50
51 //TODO: if some format is valid to read, but can't support to export, should add some process ?
52 //Such as: gif, svg, pbm, pgm
53 void Exporter::exportImage(const QStringList &imagePaths)
54 {
55 if (imagePaths.isEmpty()) {
56 return;
57 } else if (imagePaths.length() == 1) {
58 initValidFormatMap();
59 //QFileDialog exportDialog;
60 //Todo: need to filter the format of images.
61 QString imageName = QString("%1.%2").arg(QFileInfo(imagePaths.at(0)).baseName())
62 .arg(QFileInfo(imagePaths.at(0)).completeSuffix());
63 m_exportImageDialog->setPicFileName(imageName);
64 m_exportImageDialog->removeGifType();
65 QFileInfo info(imagePaths.at(0));
66 QMimeDatabase db;
67 QMimeType mt = db.mimeTypeForFile(info.filePath(), QMimeDatabase::MatchContent);
68 QMimeType mt1 = db.mimeTypeForFile(info.filePath(), QMimeDatabase::MatchExtension);
69
70 if (mt.name().startsWith("image/gif")) {
71 m_exportImageDialog->setGifType(imagePaths.at(0));
72 }
73
74 QImage tImg;
75 QString errMsg;
76 QPixmap pixmap;
77 using namespace UnionImage_NameSpace;
78 loadStaticImageFromFile(imagePaths.at(0), tImg, errMsg);
79 pixmap = QPixmap::fromImage(tImg);
80 m_exportImageDialog->showMe(pixmap);
81 } else {
82 popupDialogSaveImage(imagePaths);
83 }
84 }
85
86 void Exporter::exportAlbum(const QStringList &albumPaths, const QString &albumname)
87 {
88 QFileDialog exportDialog;
89 exportDialog.setFileMode(QFileDialog::DirectoryOnly);
90 exportDialog.setLabelText(QFileDialog::Accept, tr("Save"));
91 exportDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).at(0));
92
93 if (exportDialog.exec() == QDialog::Accepted) {
94 QString exportdir = exportDialog.directory().absolutePath();
95
96 QDir dir;
97 dir.mkdir(exportdir + "/" + albumname);
98 exportdir = exportdir + "/" + albumname;
99
100 int failcount = 0;
101 for (int j(0); j < albumPaths.length(); j++) {
102
103 if (utils::image::imageSupportRead(albumPaths[j])) {
104 QString savePath = QString("%1/%2.%3").arg(exportdir).arg(QFileInfo(albumPaths[j])
105 .baseName()).arg(QFileInfo(albumPaths[j]).completeSuffix());
106 bool isSucceed = QFile::copy(albumPaths[j], savePath);
107 emit dApp->signalM->sigExporting(albumPaths[j]);
108
109 if (!isSucceed) {
110 // qDebug() << "Export failed";
111 }
112 } else {
113 failcount++;
114 continue;
115 }
116 }
117 if (failcount == albumPaths.length()) {
118 emit dApp->signalM->AlbExportFailed();
119 } else {
120 emit dApp->signalM->AlbExportSuccess();
121 emit dApp->signalM->sigRestoreStatus();
122 }
123
124 }
125 }
126
127 void Exporter::popupDialogSaveImage(const QStringList &imagePaths)
128 {
129 QFileDialog exportDialog;
130 exportDialog.setWindowTitle(tr("Export Photos"));
131
132 exportDialog.setFileMode(QFileDialog::Directory);
133 exportDialog.setLabelText(QFileDialog::Reject, tr("Cancel"));
134 exportDialog.setLabelText(QFileDialog::Accept, tr("Save"));
135 exportDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::PicturesLocation).at(0));
136
137 if (exportDialog.exec() == QDialog::Accepted) {
138 QString exportdir = exportDialog.directory().absolutePath();
139
140 int failcount = 0;
141 bool bnewpath = false;
142 for (int j(0); j < imagePaths.length(); j++) {
143 if (utils::image::imageSupportRead(imagePaths[j])) {
144 QString savePath = QString("%1/%2.%3").arg(exportdir).arg(QFileInfo(imagePaths[j])
145 .baseName()).arg(QFileInfo(imagePaths[j]).completeSuffix());
146 QFileInfo fileinfo(savePath);
147 if (fileinfo.exists()) {
148 if (!fileinfo.isDir()) {
149 m_exportImageDialog->setPicFileName(savePath.mid(savePath.lastIndexOf("/") + 1));
150 m_exportImageDialog->showQuestionDialog(savePath, imagePaths[j]);
151 continue;
152 }
153 }
154
155 bool isSucceed = QFile::copy(imagePaths[j], savePath);
156 emit dApp->signalM->sigExporting(imagePaths[j]);
157 if (!isSucceed) {
158 failcount ++;
159 } else {
160 bnewpath = true;
161 }
162
163 } else {
164 failcount ++;
165 continue;
166 }
167 }
168 if (failcount == imagePaths.length()) {
169 emit dApp->signalM->ImgExportFailed();
170 } else {
171 if (bnewpath)
172 emit dApp->signalM->ImgExportSuccess();
173 emit dApp->signalM->sigRestoreStatus();
174 }
175 }
176 }
177
178 void Exporter::initValidFormatMap()
179 {
180 m_picFormatMap.insert("jpeg", "JPEG (*.jpeg)");
181 m_picFormatMap.insert("jpg", "JPG (*.jpg)");
182 m_picFormatMap.insert("bmp", "BMP (*.bmp)");
183 m_picFormatMap.insert("png", "PNG (*.png)");
184
185 m_picFormatMap.insert("ppm", "PGM (*.ppm)");
186 m_picFormatMap.insert("xbm", "XBM (*.xbm)");
187 m_picFormatMap.insert("xpm", "XPM (*.xpm)");
188
189 }