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
|
/*
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.
*/
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QFileInfo>
#include <QLinearGradient>
#include <QDesktopWidget>
#include <QApplication>
#include <QDir>
#include <QWheelEvent>
#include <algorithm>
#include "pictureviewer.h"
#include "pictureviewerinfoitem.h"
#include "helper.h"
PictureViewer::PictureViewer(QWidget *parent) : QGraphicsView(parent), mCurrentPic(0), mInfoItem(0) {
mScene = new QGraphicsScene(this);
setScene(mScene);
}
void PictureViewer::showPic(const QString &path){
QFileInfo fi(path);
if(!fi.exists() || fi.isDir()){
return;
}
QPixmap img(path);
if(img.isNull()){
return;
}
if(mCurrentPic){
mScene->removeItem(mCurrentPic);
mScene->removeItem(mInfoItem);
delete mCurrentPic;
delete mInfoItem;
mCurrentPic = 0;
mInfoItem = 0;
}
setDir(path);
if(!isVisible()){
show();
}
QSize maxSize;
QDesktopWidget *desktopWidget = QApplication::desktop();
QSize desktopSize = desktopWidget->availableGeometry().size();
if((img.height() > desktopSize.height()) || (img.width() > desktopSize.height())){
maxSize = desktopSize;
}else{
maxSize = img.size();
}
if(img.width() > maxSize.width() || img.height() > maxSize.height()){
img = img.scaled(maxSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
}
resize(img.size() + QSize(20, 20));
setSceneRect(viewport()->rect());
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(img);
mCurrentPic = item;
item->setPos(center(img));
mScene->addItem(item);
PictureviewerInfoItem *infoItem = new PictureviewerInfoItem(fi.fileName());
QRectF infoItemRect = infoItem->boundingRect();
int infoWidth = infoItemRect.size().width();
int viewportWidth = viewport()->size().width();
int center = (viewportWidth - infoWidth) / 2;
mInfoItem = infoItem;
QPoint infoPoint = QPoint(center, 10);
infoItem->setPos(infoPoint);
mScene->addItem(infoItem);
}
void PictureViewer::next(){
if(mCurrentEntry == mDirEntries.constEnd()){
return;
}
QString nextPath;
while(mCurrentEntry != mDirEntries.constEnd()){
++mCurrentEntry;
if(mCurrentEntry == mDirEntries.constEnd()){
break;
}
QFileInfo fi = *mCurrentEntry;
if(isPic(fi.absoluteFilePath())){
nextPath = fi.absoluteFilePath();
break;
}
}
if(!nextPath.isEmpty()){
showPic(nextPath);
}
}
void PictureViewer::previous(){
if(mCurrentEntry == mDirEntries.constBegin()){
return;
}
QString nextPath;
while(mCurrentEntry != mDirEntries.constBegin()){
--mCurrentEntry;
QFileInfo fi = *mCurrentEntry;
if(isPic(fi.absoluteFilePath())){
nextPath = fi.absoluteFilePath();
break;
}
}
if(!nextPath.isEmpty()){
showPic(nextPath);
}
}
void PictureViewer::wheelEvent(QWheelEvent *event){
int steps = event->delta() / 8 / 15;
if(steps < 0){
next();
}else{
previous();
}
}
void PictureViewer::setGradient(){
QColor c1(255, 7, 15);
QColor c2(80, 55, 250);
QLinearGradient g(QPointF(0, 0), sceneRect().bottomRight());
g.setColorAt(0, c1);
g.setColorAt(1, c2);
setBackgroundBrush(QBrush(g));
}
void PictureViewer::setDir(const QString &path){
QFileInfo fi(path);
QString dir = fi.absolutePath();
if(dir == mCurrentDir || !fi.exists()){
return;
}
mCurrentDir = dir;
QDir currentDir = QDir(mCurrentDir);
mDirEntries = currentDir.entryInfoList(QDir::Files, QDir::Name);
std::sort(mDirEntries.begin(), mDirEntries.end(), Helper::SortFileInfoList());
mCurrentEntry = std::find_if(mDirEntries.constBegin(), mDirEntries.constEnd(), std::bind2nd(Helper::FileInfoListContains(), fi.fileName()));
}
bool PictureViewer::isPic(const QString &path){
QFileInfo fi(path);
if(!fi.exists() || !fi.isFile()){
return false;
}
QString mime = Helper::mimeType(path);
if(mime.toLower().startsWith("image")){
return true;
}
return false;
}
QPointF PictureViewer::center(const QPixmap &pic){
QSize viewportSize = viewport()->size();
float x = (viewportSize.width() - pic.width()) / 2;
float y = (viewportSize.height() - pic.height()) / 2;
return QPointF(x, y);
}
|