| 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 "imageengineobject.h" |
| 22 |
#include "imageengineapi.h" |
| 23 |
|
| 24 |
ImageEngineThreadObject::ImageEngineThreadObject() |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
void ImageEngineThreadObject::needStop(void *imageobject) |
| 29 |
{ |
| 30 |
if (nullptr == imageobject || ifCanStopThread(imageobject)) { |
| 31 |
bneedstop = true; |
| 32 |
bbackstop = true; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
bool ImageEngineThreadObject::ifCanStopThread(void *imgobject) |
| 37 |
{ |
| 38 |
Q_UNUSED(imgobject); |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
void ImageEngineThreadObject::run() |
| 43 |
{ |
| 44 |
runDetail(); //原本要run的内容 |
| 45 |
emit runFinished(); //告诉m_obj我run完了,这里不再像之前那样直接判断不为nullptr然后解引用m_obj,因为m_obj销毁后不会自动置为nullptr |
| 46 |
this->deleteLater(); //在消息传达后销毁自己 |
| 47 |
} |
| 48 |
|
| 49 |
ImageMountImportPathsObject::ImageMountImportPathsObject() |
| 50 |
{ |
| 51 |
ImageEngineApi::instance()->insertObject(this); |
| 52 |
} |
| 53 |
|
| 54 |
ImageMountImportPathsObject::~ImageMountImportPathsObject() |
| 55 |
{ |
| 56 |
ImageEngineApi::instance()->removeObject(this); |
| 57 |
clearAndStopThread(); |
| 58 |
} |
| 59 |
|
| 60 |
void ImageMountImportPathsObject::addThread(ImageEngineThreadObject *thread) |
| 61 |
{ |
| 62 |
m_threads.append(thread); |
| 63 |
} |
| 64 |
void ImageMountImportPathsObject::removeThread(ImageEngineThreadObject *thread) |
| 65 |
{ |
| 66 |
m_threads.removeOne(thread); |
| 67 |
} |
| 68 |
void ImageMountImportPathsObject::clearAndStopThread() |
| 69 |
{ |
| 70 |
for (auto thread : m_threads) { |
| 71 |
thread->needStop(this); |
| 72 |
} |
| 73 |
m_threads.clear(); |
| 74 |
} |
| 75 |
|
| 76 |
ImageMountGetPathsObject::ImageMountGetPathsObject() |
| 77 |
{ |
| 78 |
ImageEngineApi::instance()->insertObject(this); |
| 79 |
} |
| 80 |
ImageMountGetPathsObject::~ImageMountGetPathsObject() |
| 81 |
{ |
| 82 |
ImageEngineApi::instance()->removeObject(this); |
| 83 |
clearAndStopThread(); |
| 84 |
} |
| 85 |
|
| 86 |
void ImageMountGetPathsObject::addThread(ImageEngineThreadObject *thread) |
| 87 |
{ |
| 88 |
m_threads.append(thread); |
| 89 |
} |
| 90 |
|
| 91 |
void ImageMountGetPathsObject::removeThread(ImageEngineThreadObject *thread) |
| 92 |
{ |
| 93 |
m_threads.removeOne(thread); |
| 94 |
} |
| 95 |
|
| 96 |
void ImageMountGetPathsObject::clearAndStopThread() |
| 97 |
{ |
| 98 |
for (auto thread : m_threads) { |
| 99 |
if (thread) { |
| 100 |
thread->needStop(this); |
| 101 |
} |
| 102 |
} |
| 103 |
m_threads.clear(); |
| 104 |
} |
| 105 |
|
| 106 |
ImageEngineImportObject::ImageEngineImportObject() |
| 107 |
{ |
| 108 |
ImageEngineApi::instance()->insertObject(this); |
| 109 |
} |
| 110 |
|
| 111 |
ImageEngineImportObject::~ImageEngineImportObject() |
| 112 |
{ |
| 113 |
ImageEngineApi::instance()->removeObject(this); |
| 114 |
clearAndStopThread(); |
| 115 |
} |
| 116 |
|
| 117 |
void ImageEngineImportObject::addThread(ImageEngineThreadObject *thread) |
| 118 |
{ |
| 119 |
m_threads.append(thread); |
| 120 |
|
| 121 |
//建立信号槽连接,由于钻石继承问题,ImageEngineImportObject不能继承QObject,所以只能用lambda |
| 122 |
QObject::connect(thread, &ImageEngineThreadObject::runFinished, [this, thread]() { |
| 123 |
this->removeThread(thread); |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
void ImageEngineImportObject::removeThread(ImageEngineThreadObject *thread) |
| 128 |
{ |
| 129 |
if (m_threads.contains(thread)) { |
| 130 |
m_threads.removeOne(thread); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void ImageEngineImportObject::clearAndStopThread() |
| 135 |
{ |
| 136 |
for (const auto &thread : m_threads) { |
| 137 |
if (thread) { |
| 138 |
thread->needStop(this); |
| 139 |
} |
| 140 |
} |
| 141 |
m_threads.clear(); |
| 142 |
} |
| 143 |
|
| 144 |
ImageEngineObject::ImageEngineObject() |
| 145 |
{ |
| 146 |
ImageEngineApi::instance()->insertObject(this); |
| 147 |
m_threads.clear(); |
| 148 |
} |
| 149 |
|
| 150 |
ImageEngineObject::~ImageEngineObject() |
| 151 |
{ |
| 152 |
ImageEngineApi::instance()->removeObject(this); |
| 153 |
clearAndStopThread(); |
| 154 |
} |
| 155 |
|
| 156 |
void ImageEngineObject::addThread(ImageEngineThreadObject *thread) |
| 157 |
{ |
| 158 |
QMutexLocker mutex(&m_mutexthread); |
| 159 |
m_threads.append(thread); |
| 160 |
mutex.unlock(); |
| 161 |
//建立信号槽连接,由于钻石继承问题,ImageEngineObject不能继承QObject,所以只能用lambda |
| 162 |
QObject::connect(thread, &ImageEngineThreadObject::runFinished, [this, thread]() { |
| 163 |
this->removeThread(thread); |
| 164 |
}); |
| 165 |
} |
| 166 |
|
| 167 |
void ImageEngineObject::removeThread(ImageEngineThreadObject *thread) |
| 168 |
{ |
| 169 |
QMutexLocker mutex(&m_mutexthread); |
| 170 |
m_threads.removeOne(thread); |
| 171 |
mutex.unlock(); |
| 172 |
|
| 173 |
//主动切断连接,否则会去执行lambda,然后解引用已经销毁的this指针 |
| 174 |
QObject::disconnect(thread, &ImageEngineThreadObject::runFinished, nullptr, nullptr); |
| 175 |
} |
| 176 |
|
| 177 |
void ImageEngineObject::addCheckPath(QString &path) |
| 178 |
{ |
| 179 |
m_checkpath << path; |
| 180 |
} |
| 181 |
|
| 182 |
void ImageEngineObject::checkSelf() |
| 183 |
{ |
| 184 |
if (m_checkpath.size() < 1) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
for (auto file : m_pathlast) { |
| 189 |
if (QString::compare(file, m_checkpath.first()) == 0) { |
| 190 |
m_checkpath.removeFirst(); |
| 191 |
imageLoaded(file); |
| 192 |
m_pathlast.removeOne(file); |
| 193 |
checkSelf(); |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
//void ImageEngineObject::checkAndReturnPath(QString &path)//保证顺序排列 |
| 200 |
//{ |
| 201 |
// if (m_checkpath.size() < 1) { |
| 202 |
// return; |
| 203 |
// } |
| 204 |
// if (path == m_checkpath.first()) { |
| 205 |
// m_checkpath.removeFirst(); |
| 206 |
// imageLoaded(path); |
| 207 |
// checkSelf(); |
| 208 |
// } else { |
| 209 |
// m_pathlast << path; |
| 210 |
// } |
| 211 |
//} |
| 212 |
|
| 213 |
void ImageEngineObject::clearAndStopThread() |
| 214 |
{ |
| 215 |
QMutexLocker mutex(&m_mutexthread); |
| 216 |
for (auto thread : m_threads) { |
| 217 |
if (nullptr != thread /*&& ifObjectExist(thread)*/) { |
| 218 |
thread->needStop(this); |
| 219 |
//主动切断连接,否则会去执行lambda,然后解引用已经销毁的this指针 |
| 220 |
QObject::disconnect(thread, &ImageEngineThreadObject::runFinished, nullptr, nullptr); |
| 221 |
} |
| 222 |
} |
| 223 |
m_threads.clear(); |
| 224 |
m_checkpath.clear(); |
| 225 |
m_pathlast.clear(); |
| 226 |
} |
| 227 |
|
| 228 |
ImageCacheSaveObject::ImageCacheSaveObject() |
| 229 |
{ |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
bool ImageCacheSaveObject::add(const QString &path) |
| 234 |
{ |
| 235 |
QMutexLocker locker(&m_queuqMutex); |
| 236 |
requestQueue.append(path); |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
bool ImageCacheSaveObject::add(const QStringList &paths) |
| 241 |
{ |
| 242 |
QMutexLocker locker(&m_queuqMutex); |
| 243 |
requestQueue.append(paths); |
| 244 |
return true; |
| 245 |
} |
| 246 |
|
| 247 |
QString ImageCacheSaveObject::pop() |
| 248 |
{ |
| 249 |
QMutexLocker locker(&m_queuqMutex); |
| 250 |
if (requestQueue.empty()) |
| 251 |
return QString(); |
| 252 |
QString res = requestQueue.first(); |
| 253 |
requestQueue.pop_front(); |
| 254 |
return res; |
| 255 |
} |
| 256 |
|
| 257 |
bool ImageCacheSaveObject::isEmpty() |
| 258 |
{ |
| 259 |
QMutexLocker locker(&m_queuqMutex); |
| 260 |
return requestQueue.isEmpty(); |
| 261 |
} |