forked from yuezhao/ezviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicmanager.cpp
268 lines (220 loc) · 7.46 KB
/
picmanager.cpp
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
/****************************************************************************
* EZ Viewer
* Copyright (C) 2012 huangezhao. CHINA.
* Contact: huangezhao ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
#include "picmanager.h"
#include "config.h"
#include "global.h"
#include "osrelated.h"
#include "tooltip.h"
#include <QApplication>
#include <QMessageBox>
#include <QDir>
#include <QFile>
PicManager::PicManager(QWidget *parent)
: ImageViewer(parent), curImage(ImageFactory::getImageWrapper(QString::null)),
listMode(FileNameListMode), currentIndex(-1), fsWatcher(this)
{
// state = NoFileNoPicture;
connect(&fsWatcher, SIGNAL(directoryChanged(QString)),
SLOT(directoryChanged()));
connect(&fsWatcher, SIGNAL(fileChanged(QString)),
SLOT(fileChanged(QString)));
}
PicManager::~PicManager()
{
ImageFactory::freeAllCache();
}
void PicManager::updateFileNameList(const QString &curfile)
{
QFileInfo fileInfo(curfile);
curDir = fileInfo.absolutePath();
if(!curDir.endsWith('/'))
curDir.append('/');
list = QDir(curDir, Config::supportFormats(), Config::DefaultDirSortFlag, QDir::Files)
.entryList();
currentIndex = list.indexOf(fileInfo.fileName());
}
void PicManager::updateFullPathList(const QString &file)
{
int index = list.indexOf(file);
//! If file name has been filtered and simplified, does index != -1 needed??
if(!QFile::exists(file) && index != -1)
list.removeAt(index);
currentIndex = list.indexOf(file);
}
void PicManager::updateFileIndex(int oldIndex)
{
if(currentIndex != -1) //! verify if currentIndex is valid.
return;
if(list.isEmpty()){
noFileToShow();
return;
}
if(oldIndex >= list.size())
currentIndex = list.size() - 1;
else if(oldIndex != -1)
currentIndex = oldIndex;
else
currentIndex = 0;
readFile(currentIndex);
preReadingNextPic(); ///
}
void PicManager::directoryChanged()
{
int oldIndex = currentIndex;
updateFileNameList(curPath);
updateFileIndex(oldIndex);
}
void PicManager::fileChanged(const QString &filePath)
{
int oldIndex = currentIndex;
updateFullPathList(filePath);
updateFileIndex(oldIndex);
}
QString PicManager::getPathAtIndex(int index) const
{
QString file = list.at(index);
bool isAbsolute = file.contains('/') || file.contains('\\'); ///
return isAbsolute ? file : curDir + file;
}
void PicManager::readFile(const QString &fullPath)
{
QFileInfo fileInfo(fullPath);
if(curPath == fileInfo.absoluteFilePath() )//! if the image needs refresh?
return;
curPath = fileInfo.absoluteFilePath();
curName = fileInfo.fileName();
curImage->recycle();
curImage = ImageFactory::getImageWrapper(curPath);
while(!curImage->getReady()){
qApp->processEvents(QEventLoop::AllEvents);
// wait for pre-reading in another thread
}
if(curImage->isAnimation()) {
connect(curImage, SIGNAL(animationUpdated()), SLOT(updateAnimation()));
curImage->startAnimation();
} else if (curImage->frameCount() > 1){ // like ico format
connect(curImage, SIGNAL(frameUpdated()), SLOT(updateImage()));
}
QImage image = curImage->currentImage();
QString errorMsg = image.isNull() ? Global::LoadFileErrorInfo().arg(curPath)
: QString::null;
loadImage(image, errorMsg);
// state = image.isNull() ? FileNoPicture : FilePicture;
emit imageChanged(curName);
}
void PicManager::noFileToShow()
{
curImage->recycle();
curImage = ImageFactory::getImageWrapper(QString::null);
curPath = curName = QString::null;
// state = NoFileNoPicture;
loadImage(QImage());
emit imageChanged(QString::null); //
}
void PicManager::openFile(const QString &file)
{
// if(!QFileInfo(file).isFile()) return;
QStringList oldWatchList(fsWatcher.files() + fsWatcher.directories());
if(!oldWatchList.empty())
fsWatcher.removePaths(oldWatchList);
listMode = FileNameListMode;
updateFileNameList(file);
// make sure if file is no a picture, this will show error message.
readFile(file); //readFile(list.at(currentIndex));
preReadingNextPic();
fsWatcher.addPath(curDir);
if(!QFileInfo(curDir).isRoot())// watch the parent dir, will get notice when rename current dir.
fsWatcher.addPath(QFileInfo(curDir).absolutePath());
}
void PicManager::openFiles(const QStringList &fileList)
{
//! ?? check if is file and if is exist, remove from list if no file.
if(fileList.empty()) return;
if(fileList.size() == 1){
openFile(fileList.first());
return;
}
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QStringList oldWatchList(fsWatcher.files() + fsWatcher.directories());
if(!oldWatchList.empty())
fsWatcher.removePaths(oldWatchList);
listMode = FullPathListMode;
curDir = "";
list = fileList;
currentIndex = 0;
readFile(currentIndex);
preReadingNextPic();
fsWatcher.addPaths(list); // run in background thread ?
QApplication::restoreOverrideCursor();
}
bool PicManager::prePic()
{
// maybe current file is not a picture, and current dir has no any picture also, so we need check if(list.size() < 1).
if(!hasFile() || list.size() < 1) return false;
if (!curImage->getReady()) return false;
currentIndex = getPreIndex(currentIndex);
readFile(currentIndex);
preReadingPrePic();
return true;
}
bool PicManager::nextPic()
{
if(!hasFile() || list.size() < 1) return false; //
if (!curImage->getReady()) return false;
currentIndex = getNextIndex(currentIndex);
readFile(currentIndex);
preReadingNextPic();
return true;
}
void PicManager::updateAnimation()
{
updatePixmap(curImage->currentImage());
}
void PicManager::updateImage()
{
loadImage(curImage->currentImage());
}
void PicManager::hideEvent ( QHideEvent * /* event */ )
{
setAnimationPaused(true);
}
void PicManager::showEvent ( QShowEvent * /* event */ )
{
setAnimationPaused(false);
}
void PicManager::deleteFile(bool needAsk)
{
if(!hasFile() || !QFile::exists(curPath)) return;
if (!curImage->getReady()) return;
if(needAsk){
int ret = QMessageBox::question(
0, tr("Delete File"),
tr("Are you sure to delete file '%1'?").arg(curName),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(ret == QMessageBox::No)
return;
}
curImage->recycle();
OSRelated::moveFile2Trash(curPath); ///
// if (!QFile::exists(curPath)) {
// QPoint site = mapToGlobal(pos()) + QPoint(10, height() - 40);
// ToolTip::showText(site, tr("Delete file success!"));
// }
}