-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageview.cpp
194 lines (136 loc) · 3.56 KB
/
imageview.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
#include "imageview.h"
#include <QGraphicsPixmapItem>
#include <QPixmap>
#include <QFileDialog>
ImageView::ImageView()
:QGraphicsView()
{
image = NULL;
watcher = NULL;
item = NULL;
createShortcuts();
fileName = "";
notifBar = new NotificationBar(this);
scene = new QGraphicsScene();
setScene(scene);
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setRenderHint(QPainter::Antialiasing, true);
setRenderHint( QPainter::HighQualityAntialiasing, true);
}
ImageView::~ImageView()
{
delete watcher;
delete item;
delete image;
delete notifBar;
delete scene;
delete opShort;
delete fitShort;
}
void ImageView::createShortcuts(){
opShort = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_O), this, SLOT(open()));
fitShort = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_F), this, SLOT(fitToImage()));
}
void ImageView::fitToImage(){
if(image != NULL && ! image->isNull()){
//TODO: dézoomer/zoomer :s
this->resize(image->width()+2,image->height()+2);
scene->setSceneRect(item->mapRectToScene(0,0,image->width(),image->height()));
}
}
void ImageView::open(){
QString path = QFileDialog::getOpenFileName(this,"select an image");
openNewFile(path);
}
void ImageView::openNewFile(const QString &path){
load(path);
listen(path);
updateTitle();
}
void ImageView::updateTitle(){
setWindowTitle("Simple Image Viewer: " + fileName);
}
void ImageView::load(const QString &path){
//TODO: pourquoi c'est appellé 2 fois ?
delete image;
image = new QPixmap();
image->load(path);
if( ! image->isNull()){
fileName = QFileInfo(path).fileName();//I know it's overkill but I'm lazy
notifBar->hide();
delete item;
item = new QGraphicsPixmapItem();
item->setPixmap(*image);
item->setTransformationMode(Qt::SmoothTransformation);
item->setFlag(QGraphicsItem::ItemIsMovable);
scene->clear();
scene->addItem(item);
}
else{
notify("invalid path: " + path);
}
}
void ImageView::notify(const QString &message){
notifBar->notify(message);
}
void ImageView::listen(const QString &path){
delete watcher;
watcher = new QFileSystemWatcher;
watcher->addPath(path);
ImageView::connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(load(const QString &)));
}
bool ImageView::eventFilter(QObject *obj, QEvent *event){
if( event->type() == QEvent::Wheel ){
QWheelEvent *wheel = static_cast<QWheelEvent*>(event);
if( wheel->modifiers() == Qt::ControlModifier ){
if(wheel->delta() > 0){
zoomIn();
}
else{
zoomOut();
}
}
}
return QObject::eventFilter(obj, event);
}
void ImageView::keyPressEvent(QKeyEvent* k){
switch(k->key()){
case Qt::Key_Plus:
zoomIn();
break;
case Qt::Key_Minus:
zoomOut();
break;
//TODO: touches perso modifiables
/*
case Qt::Key_Right:
right();
break;
case Qt::Key_Left:
left();
break;
case Qt::Key_Up:
up();
break;
case Qt::Key_Down:
down();
break;
*/
}
QGraphicsView::keyPressEvent(k);
}
void ImageView::zoomIn(){
scale(1.25,1.25);
}
void ImageView::zoomOut(){
scale(0.75,0.75);
}
void ImageView::right(){
}
void ImageView::left(){
}
void ImageView::up(){
}
void ImageView::down(){
}