Debian Codegraph @

src/album/dbmanager/DBandImgOperate.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 "DBandImgOperate.h"
22 #include "dbmanager.h"
23 #include "application.h"
24 #include "controller/signalmanager.h"
25 #include "utils/baseutils.h"
26 #include "utils/unionimage.h"
27 #include <QDebug>
28 #include <QDir>
29 #include <QMutex>
30 #include <QSqlDatabase>
31 #include <QSqlError>
32 #include <QSqlQuery>
33 #include <QStandardPaths>
34 #include <QDirIterator>
35
36 #include "movieservice.h"
37 #include "imagedataservice.h"
38
39 DBandImgOperate::DBandImgOperate(QObject *parent)
40 {
41 Q_UNUSED(parent);
42 m_ImgPaths.clear();
43 m_couldRun.store(true);
44 }
45
46 DBandImgOperate::~DBandImgOperate()
47 {
48 }
49
50 void DBandImgOperate::setThreadShouldStop()
51 {
52 m_couldRun.store(false);
53 }
54
55 //QPixmap DBandImgOperate::loadOneThumbnail(const QString &imagepath)
56 //{
57 // if (!QFileInfo(imagepath).exists()) {
58 // emit fileIsNotExist(imagepath);
59 // return QPixmap();
60 // }
61 // using namespace UnionImage_NameSpace;
62 // QImage tImg;
63 // QString srcPath = imagepath;
64 // QString thumbnailPath = albumGlobal::CACHE_PATH + imagepath;
65 // QFileInfo file(thumbnailPath);
66 // QString errMsg;
67 // QFileInfo srcfi(srcPath);
68 // if (file.exists()) {
69 // if (!loadStaticImageFromFile(thumbnailPath, tImg, errMsg, "PNG")) {
70 // qDebug() << errMsg;
71 // }
72 // } else {
73 // if (!loadStaticImageFromFile(srcPath, tImg, errMsg)) {
74 // qDebug() << errMsg;
75 // }
76 // }
77 // if (0 != tImg.height() && 0 != tImg.width() && (tImg.height() / tImg.width()) < 10 && (tImg.width() / tImg.height()) < 10) {
78 // bool cache_exist = false;
79 // if (tImg.height() != 200 && tImg.width() != 200) {
80 // if (tImg.height() >= tImg.width()) {
81 // cache_exist = true;
82 // tImg = tImg.scaledToWidth(200, Qt::FastTransformation);
83 // } else if (tImg.height() <= tImg.width()) {
84 // cache_exist = true;
85 // tImg = tImg.scaledToHeight(200, Qt::FastTransformation);
86 // }
87 // }
88 // if (!cache_exist) {
89 // if ((static_cast<float>(tImg.height()) / (static_cast<float>(tImg.width()))) > 3) {
90 // tImg = tImg.scaledToWidth(200, Qt::FastTransformation);
91 // } else {
92 // tImg = tImg.scaledToHeight(200, Qt::FastTransformation);
93 // }
94 // }
95 // }
96
97 // if (!file.exists()) {
98 // utils::base::mkMutiDir(thumbnailPath.mid(0, thumbnailPath.lastIndexOf('/')));
99 // tImg.save(thumbnailPath, "PNG");
100 // }
101
102 // QPixmap pixmap = QPixmap::fromImage(tImg);
103 // return pixmap;
104 //}
105
106 //void DBandImgOperate::loadOneImg(QString imagepath)
107 //{
108 // loadOneImgForce(imagepath, false);
109 //}
110
111 void DBandImgOperate::loadOneImgForce(QString imagepath, bool refresh)
112 {
113 if (!QFileInfo(imagepath).exists()) {
114 emit fileIsNotExist(imagepath);
115 return;
116 }
117 using namespace UnionImage_NameSpace;
118 QImage tImg;
119 QString srcPath = imagepath;
120 //缩略图路径
121 QString thumbnailPath = utils::base::filePathToThumbnailPath(imagepath);
122 QFileInfo file(thumbnailPath);
123 QString errMsg;
124 QFileInfo srcfi(srcPath);
125 if (file.exists() && !refresh) {
126 if (!loadStaticImageFromFile(thumbnailPath, tImg, errMsg, "PNG")) {
127 qDebug() << errMsg;
128 }
129 if (utils::base::isVideo(imagepath)) {
130 //获取视频信息 demo
131 MovieInfo mi = MovieService::instance()->getMovieInfo(QUrl::fromLocalFile(imagepath));
132 ImageDataService::instance()->addMovieDurationStr(imagepath, mi.durationStr());
133 }
134 } else {
135 if (utils::base::isVideo(imagepath)) {
136 tImg = MovieService::instance()->getMovieCover(QUrl::fromLocalFile(imagepath));
137 //获取视频信息 demo
138 MovieInfo mi = MovieService::instance()->getMovieInfo(QUrl::fromLocalFile(imagepath));
139 ImageDataService::instance()->addMovieDurationStr(imagepath, mi.durationStr());
140 } else {
141 if (!loadStaticImageFromFile(srcPath, tImg, errMsg)) {
142 qDebug() << errMsg;
143 return;
144 }
145 }
146
147 }
148
149 if (0 != tImg.height() && 0 != tImg.width() && (tImg.height() / tImg.width()) < 10 && (tImg.width() / tImg.height()) < 10) {
150 bool cache_exist = false;
151 if (tImg.height() != 200 && tImg.width() != 200) {
152 if (tImg.height() >= tImg.width()) {
153 cache_exist = true;
154 tImg = tImg.scaledToWidth(200, Qt::FastTransformation);
155 } else if (tImg.height() <= tImg.width()) {
156 cache_exist = true;
157 tImg = tImg.scaledToHeight(200, Qt::FastTransformation);
158 }
159 }
160 if (!cache_exist) {
161 if ((static_cast<float>(tImg.height()) / (static_cast<float>(tImg.width()))) > 3) {
162 tImg = tImg.scaledToWidth(200, Qt::FastTransformation);
163 } else {
164 tImg = tImg.scaledToHeight(200, Qt::FastTransformation);
165 }
166 }
167 }
168
169 if (!file.exists()) {
170 utils::base::mkMutiDir(thumbnailPath.mid(0, thumbnailPath.lastIndexOf('/')));
171 tImg.save(thumbnailPath, "PNG");
172 }
173
174 QPixmap pixmap = QPixmap::fromImage(tImg);
175 emit sigOneImgReady(imagepath, pixmap);
176 }
177
178 void DBandImgOperate::rotateImageFile(int angel, const QString &path)
179 {
180 QString errMsg;
181 //如果角度为0,不选择,重新加载
182 if (angel != 0) {
183 if (!UnionImage_NameSpace::rotateImageFile(angel, path, errMsg)) {
184 qDebug() << errMsg;
185 return;
186 }
187 };
188 loadOneImgForce(path, true);
189 }
190
191 void DBandImgOperate::sltLoadThumbnailByNum(QVector<DBImgInfo> infos, int num)
192 {
193 // 从硬盘上加载图片到缓存中
194 int size = infos.size();
195 QStringList list;
196 for (int i = 0; i < num; i++) {
197 if (i < size) {
198 DBImgInfo dbInfo = infos[i];
199
200 list << dbInfo.filePath;
201 // if (dbInfo.fileType == DbFileTypePic) {
202 // infos[i].imgpixmap = loadOneThumbnail(dbInfo.filePath);
203 // } else if (dbInfo.fileType == DbFileTypeVideo) {
204 // QString spath = albumGlobal::CACHE_PATH + dbInfo.filePath;
205 // QFileInfo temDir(dbInfo.filePath);
206 // QString fileName = temDir.fileName();
207 // spath = albumGlobal::CACHE_PATH + temDir.path() + "/" + temDir.baseName() + ".PNG";
208 // infos[i].imgpixmap = loadOneThumbnail(spath);
209 // }
210 }
211 }
212 // ImageDataService::instance()->readThumbnailByPaths(list);
213 emit sig80ImgInfosReady(infos);
214 }
215
216 void DBandImgOperate::sltLoadMountFileList(const QString &path)
217 {
218 QString strPath = path;
219 if (!m_PhonePicFileMap.contains(strPath)) {
220 m_couldRun.store(true);
221 //获取所选文件类型过滤器
222 QStringList filters;
223 for (QString i : UnionImage_NameSpace::unionImageSupportFormat()) {
224 filters << "*." + i;
225 }
226
227 for (QString i : utils::base::m_videoFiletypes) {
228 filters << i;
229 }
230 //定义迭代器并设置过滤器,包括子目录:QDirIterator::Subdirectories
231 QDirIterator dir_iterator(strPath,
232 filters,
233 QDir::Files | QDir::NoSymLinks,
234 QDirIterator::Subdirectories);
235 QStringList allfiles;
236 while (dir_iterator.hasNext()) {
237 //需要停止则跳出循环
238 if (!m_couldRun.load()) {
239 break;
240 }
241 dir_iterator.next();
242 QFileInfo fileInfo = dir_iterator.fileInfo();
243 allfiles << fileInfo.filePath();
244 if (allfiles.size() == 50) {
245 emit sigMountFileListLoadReady(strPath, allfiles);
246 }
247 }
248 //重置标志位,可以执行线程
249 m_couldRun.store(true);
250 m_PhonePicFileMap[strPath] = allfiles;
251 emit sigMountFileListLoadReady(strPath, allfiles);
252 } else {
253 //已加载过的设备,直接发送缓存的路径
254 emit sigMountFileListLoadReady(strPath, m_PhonePicFileMap[strPath]);
255 }
256 }
257
258 //void DBandImgOperate::getAllInfos()
259 //{
260 // DBImgInfoList infos;
261 // QSqlDatabase db = DBManager::instance()->getDatabase();
262 // if (! db.isValid()) {
263 // emit sigAllImgInfosReady(infos);
264 // return;
265 // }
266 // QSqlQuery query(db);
267 // query.setForwardOnly(true);
268 // bool b = query.prepare("SELECT FilePath, FileName, Dir, Time, ChangeTime, ImportTime, FileType "
269 // "FROM ImageTable3");
270 // if (!b || ! query.exec()) {
271 // emit sigAllImgInfosReady(infos);
272 // return;
273 // } else {
274 // using namespace utils::base;
275 // while (query.next()) {
276 // DBImgInfo info;
277 // info.filePath = query.value(0).toString();
278 // info.fileName = query.value(1).toString();
279 // info.dirHash = query.value(2).toString();
280 // info.time = stringToDateTime(query.value(3).toString());
281 // info.changeTime = QDateTime::fromString(query.value(4).toString(), DATETIME_FORMAT_DATABASE);
282 // info.importTime = QDateTime::fromString(query.value(5).toString(), DATETIME_FORMAT_DATABASE);
283 // info.itemType = static_cast<ItemType>(query.value(6).toInt());
284 // infos << info;
285 // }
286 // }
287 // emit sigAllImgInfosReady(infos);
288 //}
289