-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageviewer.cpp
131 lines (101 loc) · 3.24 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
#include "imageviewer.h"
#include "./ui_imageviewer.h"
ImageViewer::ImageViewer(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ImageViewer)
{
ui->setupUi(this);
createAction();
createConnection();
}
ImageViewer::~ImageViewer()
{
delete ui;
}
bool ImageViewer::loadImage(const QStringList& fileList)
{
for(size_t i=0;i<fileList.size();i++){
QImage *img = new QImage();
bool isSuccess = img->load(fileList[i]);
if (!isSuccess){
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot open %1").arg(QDir::toNativeSeparators(fileList[i])));
return false;
}// end if
imagesList.append(img);
QImage scaledImg = processImage(*img);
setImage(scaledImg);
}// end for
return true;
}// end loadImage
bool ImageViewer::saveImage(const QString &fileName)
{
QImageWriter writer(fileName);
if(!writer.write(*imagesList[0])){
QMessageBox::information(this, QGuiApplication::applicationDisplayName(),
tr("Cannot write %1: %2").arg(QDir::toNativeSeparators(fileName), writer.errorString()));
return false;
}
const QString successMessage = tr("Image: %1 saved to disk.").arg(QDir::toNativeSeparators(fileName));
statusBar()->showMessage(successMessage);
return true;
}
void ImageViewer::setImage(const QImage& img)
{
QLabel *imageLabel = new QLabel();
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setPixmap(QPixmap::fromImage(img));
ui->gridLayout->addWidget(imageLabel);
}
void ImageViewer::createAction()
{
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
loadAct=fileMenu->addAction(tr("&Load"), this, &ImageViewer::doOpen);
loadAct->setShortcuts(QKeySequence::Open);
saveAct = fileMenu->addAction(tr("&Save"), this, &ImageViewer::doSave);
saveAct->setShortcuts(QKeySequence::Save);
exitAct = fileMenu->addAction(tr("&Exit"), this, &ImageViewer::doExit);
exitAct->setShortcuts(QKeySequence::Cancel);
}
void ImageViewer::createConnection()
{
connect(ui->colorBtn, &QRadioButton::toggled, this, &ImageViewer::doColor);
connect(ui->grayBtn, &QRadioButton::toggled, this, &ImageViewer::doGray);
connect(ui->binBtn, &QRadioButton::toggled, this, &ImageViewer::doBin);
}
QImage ImageViewer::processImage(QImage& img)
{
QImage scaledImg = img.scaled(640, 480);
return scaledImg;
}
void ImageViewer::doOpen()
{
QFileDialog dialog(this, tr("Open File"));
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg *jpeg)"));
while (dialog.exec() == QDialog::Accepted && !loadImage(dialog.selectedFiles())) {}
}
void ImageViewer::doExit()
{
QApplication::quit();
}
void ImageViewer::doColor()
{
qDebug()<<"color img ";
}
void ImageViewer::doGray()
{
qDebug() <<"WTF!!";
for(size_t i =0;i<imagesList.size();i++){
QImage gray = imagesList[i]->convertToFormat(QImage::Format_Grayscale8);
setImage(gray);
}
}
void ImageViewer::doBin()
{
qDebug()<<"bin img ";
}
void ImageViewer::doSave()
{
QFileDialog dialog(this, tr("Open File"));
while (dialog.exec() == QDialog::Accepted && !saveImage(dialog.selectedFiles().first())) {}
}