/
imageviewer.cpp
379 lines (324 loc) · 12.5 KB
/
imageviewer.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <QtWidgets>
#ifndef QT_NO_PRINTER
//#include <QPrintDialog>
#endif
#include "imageviewer.h"
#include <iostream>
ImageViewer::ImageViewer()
{
setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
img = new picture_holder(this) ;
img->setBackgroundRole(QPalette::Base);
img->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
img->setScaledContents(true);
scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Base);
scrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
scrollArea->setWidget(img);
setCentralWidget(scrollArea);
// setCentralWidget(x);
createActions();
createMenus();
resize(QGuiApplication::primaryScreen()->availableSize());
}
bool ImageViewer::loadFile(const QString &fileName)
{
QImageReader reader(fileName);
// reader.setAutoTransform(true);
const QImage image = reader.read();
if (image.isNull()) {
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot load %1.").arg(QDir::toNativeSeparators(fileName)));
setWindowFilePath(QString());
img->setPixmap(QPixmap());
img->adjustSize();
return false;
}
img->setPixmap(QPixmap::fromImage(image));
img->original = *(img->pixmap());
img->resetMap = img->original ;
img->scaleFactor = 1.0;
printAct->setEnabled(true);
fitToWindowAct->setEnabled(true);
updateActions();
if (!fitToWindowAct->isChecked())
img->adjustSize();
setWindowFilePath(fileName);
return true;
}
void ImageViewer::open()
{
img->scaleFactor = 1;
QStringList mimeTypeFilters;
foreach (const QByteArray &mimeTypeName, QImageReader::supportedMimeTypes())
mimeTypeFilters.append(mimeTypeName);
mimeTypeFilters.sort();
const QStringList picturesLocations = QStandardPaths::standardLocations(QStandardPaths::PicturesLocation);
QFileDialog dialog(this, tr("Open File"),
picturesLocations.isEmpty() ? QDir::currentPath() : picturesLocations.last());
dialog.setAcceptMode(QFileDialog::AcceptOpen);
dialog.setMimeTypeFilters(mimeTypeFilters);
dialog.selectMimeTypeFilter("image/jpeg");
while (dialog.exec() == QDialog::Accepted && !loadFile(dialog.selectedFiles().first())) {}
}
void ImageViewer::print()
{
// Q_ASSERT(img->pixmap());
//#if !defined(QT_NO_PRINTER) && !defined(QT_NO_PRINTDIALOG)
// QPrintDialog dialog(&printer, this);
// if (dialog.exec()) {
// QPainter painter(&printer);
// QRect rect = painter.viewport();
// QSize size = img->pixmap()->size();
// size.scale(rect.size(), Qt::KeepAspectRatio);
// painter.setViewport(rect.x(), rect.y(), size.width(), size.height());
// painter.setWindow(img->pixmap()->rect());
// painter.drawPixmap(0, 0, *img->pixmap());
// }
//#endif
}
void ImageViewer::zoomImg(double s){
img->scaleFactor *= s ;
undo_stack.push_back(*(img->pixmap()));
redo_stack.clear();
QImage ci (img->original.toImage());
QPixmap nwmp(QPixmap::fromImage(ci.scaled(ci.width()*img->scaleFactor , ci.height()*img->scaleFactor, Qt::IgnoreAspectRatio)));
img->resize(nwmp.size());
img->setPixmap(nwmp);
}
void ImageViewer::zoomIn()
{
zoomImg(1.1);
}
void ImageViewer::zoomOut()
{
zoomImg((double)(10.0/11.0));
}
void ImageViewer::normalSize()
{
img->scaleFactor = 1 ;
zoomImg(1.0);
}
void ImageViewer::fitToWindow()
{
bool fitToWindow = fitToWindowAct->isChecked();
scrollArea->setWidgetResizable(fitToWindow);
if (!fitToWindow) {
normalSize();
}
updateActions();
}
void ImageViewer::about()
{
QMessageBox::about(this, tr("About Image Viewer"),
tr("<p>The <b>Image Viewer</b> example shows how to combine QLabel "
"and QScrollArea to display an image. QLabel is typically used "
"for displaying a text, but it can also display an image. "
"QScrollArea provides a scrolling view around another widget. "
"If the child widget exceeds the size of the frame, QScrollArea "
"automatically provides scroll bars. </p><p>The example "
"demonstrates how QLabel's ability to scale its contents "
"(QLabel::scaledContents), and QScrollArea's ability to "
"automatically resize its contents "
"(QScrollArea::widgetResizable), can be used to implement "
"zooming and scaling features. </p><p>In addition the example "
"shows how to use QPainter to print an image.</p>"));
}
void ImageViewer::createActions()
{
openAct = new QAction(tr("&Open..."), this);
// openAct->setShortcut(tr("Ctrl+O"));
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
printAct = new QAction(tr("&Print..."), this);
// printAct->setShortcut(tr("Ctrl+P"));
printAct->setEnabled(false);
connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
exitAct = new QAction(tr("E&xit"), this);
// exitAct->setShortcut(tr("Ctrl+Q"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
zoomInAct = new QAction(tr("Zoom &In (10%)"), this);
// zoomInAct->setShortcut(tr("Ctrl++"));
zoomInAct->setEnabled(false);
connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
zoomOutAct = new QAction(tr("Zoom &Out (10%)"), this);
// zoomOutAct->setShortcut(tr("Ctrl+-"));
zoomOutAct->setEnabled(false);
connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
normalSizeAct = new QAction(tr("&Normal Size"), this);
// normalSizeAct->setShortcut(tr("Ctrl+S"));
normalSizeAct->setEnabled(false);
connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
fitToWindowAct = new QAction(tr("&Fit to Window"), this);
fitToWindowAct->setEnabled(false);
fitToWindowAct->setCheckable(true);
// fitToWindowAct->setShortcut(tr("Ctrl+F"));
connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
aboutAct = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
aboutQtAct = new QAction(tr("About &Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
crop = new QAction(tr("Crop"),this);
crop->setEnabled(false);
connect(crop,SIGNAL(triggered()),this,SLOT(saveSlot()));
rotate_left_90 = new QAction(tr("Rotate left"),this);
rotate_left_90->setEnabled(false);
rotate_right_90 = new QAction(tr("rotate Right"),this);
rotate_right_90->setEnabled(false);
QSignalMapper *sm = new QSignalMapper(this);
connect(rotate_left_90,SIGNAL(triggered()),sm,SLOT(map()));
connect(rotate_right_90,SIGNAL(triggered()),sm,SLOT(map()));
sm->setMapping(rotate_left_90,90);
sm->setMapping(rotate_right_90,-90);
connect (sm, SIGNAL(mapped(int)), this, SLOT(rotate_with_angle(int))) ;
rotate_custom = new QAction(tr("Custom rotation"),this);
rotate_custom->setEnabled(false);
connect(rotate_custom,SIGNAL(triggered()),this,SLOT(rotate_custom_slot()));
undo = new QAction(tr("Undo"),this);
connect(undo,SIGNAL(triggered()),this,SLOT(undo_slot()));
redo = new QAction(tr("Redo"),this);
connect(redo,SIGNAL(triggered()),this,SLOT(redo_slot()));
reset = new QAction(tr("Reset"),this);
connect(reset,SIGNAL(triggered()),this,SLOT(reset_slot()));
reset->setEnabled(false);
QToolBar *tb = new QToolBar ;
saveAs = new QAction(tr("Save As"),this);
connect(saveAs,SIGNAL(triggered()),this,SLOT(saveAs_slot()));
tb->addAction(openAct);
tb->addAction(zoomInAct);
tb->addAction(zoomOutAct);
tb->addAction(crop);
tb->addAction(rotate_left_90);
tb->addAction(rotate_right_90);
tb->addAction(undo);
tb->addAction(redo);
tb->addAction(reset);
addToolBar(tb);
}
void ImageViewer::createMenus()
{
fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAs);
// fileMenu->addAction(printAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
viewMenu = new QMenu(tr("&View"), this);
viewMenu->addAction(zoomInAct);
viewMenu->addAction(zoomOutAct);
// viewMenu->addAction(normalSizeAct);
viewMenu->addAction(crop);
viewMenu->addSeparator();
viewMenu->addAction(fitToWindowAct);
helpMenu = new QMenu(tr("&Help"), this);
helpMenu->addAction(aboutAct);
helpMenu->addAction(aboutQtAct);
rotateMenu = new QMenu(tr("Rotate"),this);
rotateMenu->addAction(rotate_left_90);
rotateMenu->addAction(rotate_right_90);
rotateMenu->addAction(rotate_custom);
editMenu = new QMenu(tr("Edit"),this);
editMenu->addAction(undo);
editMenu->addAction(redo);
editMenu->addAction(reset);
menuBar()->addMenu(fileMenu);
menuBar()->addMenu(editMenu);
menuBar()->addMenu(rotateMenu);
menuBar()->addMenu(viewMenu);
menuBar()->addMenu(helpMenu);
}
void ImageViewer::updateActions()
{
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
rotate_left_90->setEnabled(!fitToWindowAct->isChecked());
rotate_right_90->setEnabled(!fitToWindowAct->isChecked());
rotate_custom->setEnabled(!fitToWindowAct->isChecked());
crop->setEnabled(!fitToWindowAct->isChecked());
reset->setEnabled(!fitToWindowAct->isChecked());
}
void ImageViewer::saveSlot()
{
undo_stack.push_back(*(img->pixmap()));
redo_stack.clear();
img->selectionStarted = true ;
img->setCursor(Qt::CrossCursor);
}
void ImageViewer::scaleImage(double factor)
{
// Q_ASSERT(img->pixmap());
// scaleFactor *= factor;
// img->resize(scaleFactor * img->pixmap()->size());
// adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
// adjustScrollBar(scrollArea->verticalScrollBar(), factor);
// zoomInAct->setEnabled(scaleFactor < 3.0);
// zoomOutAct->setEnabled(scaleFactor > 0.333);
}
void ImageViewer::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
scrollBar->setValue(int(factor * scrollBar->value()
+ ((factor - 1) * scrollBar->pageStep()/2)));
}
void ImageViewer::rotate_with_angle(int a){
std::cout << "in rot with angle " << a << std::endl ;
if(a!=0){
std::cout << "in rotate" << std::endl ;
undo_stack.push_back(*(img->pixmap()));
redo_stack.clear();
QPixmap pixmap(*(img->pixmap()));
QMatrix rm;
rm.rotate(a);
pixmap = pixmap.transformed(rm);
if(a == 90 || a == -90){
std::cout << "rotating left and right " << std::endl ;
img->setPixmap(pixmap);
img->adjustSize();
}else{
int xoffset = (pixmap.width() - img->original.width()) / 2;
int yoffset = (pixmap.height() - img->original.height()) / 2;
pixmap = pixmap.copy(xoffset, yoffset, img->original.width(), img->original.height());
img->setPixmap(pixmap);
}
img->original = *(img->pixmap());
}
}
void ImageViewer::rotate_custom_slot(){
std::cout << "in rot" << std::endl ;
int ang = QInputDialog::getInt(this, tr("Custom Rotation"), tr("Enter angle"), 0, -2147483647, 2147483647, 1,&ok);
rotate_with_angle(ang);
}
void ImageViewer::undo_slot(){
std::cout << "in undo slot :D" << std::endl ;
if(undo_stack.size() == 0)
return ;
img->resize(undo_stack.back().size());
redo_stack.push_back(*(img->pixmap()));
img->setPixmap(undo_stack.back());\
undo_stack.pop_back();
std::cout << "undo : " <<undo_stack.size() << std::endl ;
std::cout << "redo : " <<redo_stack.size() << std::endl ;
}
void ImageViewer::redo_slot(){
if(redo_stack.size() == 0)
return ;
img->resize(redo_stack.back().size());
undo_stack.push_back(*(img->pixmap()));
img->setPixmap(redo_stack.back());\
redo_stack.pop_back();
std::cout << redo_stack.size() << std::endl ;
}
void ImageViewer::reset_slot(){
img->resize(img->resetMap.size());
img->setPixmap(img->resetMap);
img->original = img->resetMap ;
img->scaleFactor = 1 ;
}
void ImageViewer::saveAs_slot(){
if(!img->pixmap())
return ;
QString filename = QFileDialog::getSaveFileName(this, tr("Save As.."),"",tr("Joint Photographic Experts Group(*.jpg);;Bitmap image(*.bmp);;Portable Network Graphics(.png)") );
if( !filename.isNull() ){
QPixmap pixmap(*(img->pixmap()));
std::cout << pixmap.save(filename+".png") << std::endl ;
}
}