-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathImageDisplay.cpp
228 lines (194 loc) · 4.55 KB
/
ImageDisplay.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
#include "ImageDisplay.h"
ImageDisplay::ImageDisplay( QWidget* parent, int maxCurrentHeight, int maxCurrentWidth) : QWidget(parent)
{
maxCurHeight = maxCurrentHeight;
maxCurWidth = maxCurrentWidth;
action = new QAction(this);
action->setCheckable(true);
connect(action, SIGNAL(triggered()), this, SLOT(show()));
connect(action, SIGNAL(triggered()), this, SLOT(setFocus()));
isUntitled = true;
openFileFilters = tr("Image files (*.ppm *.png *.pgm *.jpg *.jpeg *.gif *.xpm *.bmp *.xbm *.tif)\n"
"All files (*)");
saveFileFilters = tr("Image files (*.bmp *.jpg *.jpeg *.png *.ppm *.tif *.xbm *.xpm)\n"
"All files (*)");
setWindowIcon(QPixmap(":/images/document.png"));
setAttribute(Qt::WA_DeleteOnClose);
vLayout = new QVBoxLayout(this);
imageLabel = new QLabel();
scrollArea = new QScrollArea();
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
vLayout->addWidget(scrollArea);
}
void ImageDisplay::setImage(QImage image)
{
if ( m_image == image )
return;
m_image = image;
imageLabel->setPixmap(QPixmap::fromImage(image));
imageLabel->adjustSize();
bool resized = false;
int newHeight = imageLabel->height();
int newWidth = imageLabel->width();
if (imageLabel->width() >= maxCurWidth)
{
newWidth = maxCurWidth-10;
resize(newWidth, newHeight);
resized = true;
}
if (imageLabel->height() >= maxCurHeight)
{
newHeight = maxCurHeight-44;
resize(newWidth, newHeight);
resized = true;
}
if (!resized)
resize(imageLabel->width() + 20, imageLabel->height() + 20);
emit imageChanged( m_image );
}
void ImageDisplay::closeEvent(QCloseEvent *event)
{
if (okToContinue())
{
event->accept();
}
else
{
event->ignore();
}
}
void ImageDisplay::imageWasModified()
{
setWindowModified(true);
}
QString ImageDisplay::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void ImageDisplay::setCurrentFile(const QString &fileName)
{
curFile = fileName;
isUntitled = false;
action->setText(strippedName(curFile));
setWindowTitle(strippedName(curFile) + "[*]");
setWindowModified(false);
}
bool ImageDisplay::okToContinue()
{
if (isWindowModified())
{
int r = QMessageBox::warning(this, tr("MDI Editor"),
tr("File %1 has been modified.\n"
"Do you want to save your changes?")
.arg(strippedName(curFile)),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No,
QMessageBox::Cancel | QMessageBox::Escape);
if (r == QMessageBox::Yes)
{
return save();
}
else if (r == QMessageBox::Cancel)
{
return false;
}
}
return true;
}
void ImageDisplay::newFile()
{
static int imageNumber = 1;
curFile = tr("Untitled%1.jpg").arg(imageNumber);
setWindowTitle(curFile + "[*]");
action->setText(curFile);
isUntitled = true;
ImageDisplay::curFile = curFile;
setWindowModified(true);
++imageNumber;
}
void ImageDisplay::newFile(QString curFile)
{
setWindowTitle(curFile + "[*]");
action->setText(curFile);
isUntitled = true;
ImageDisplay::curFile = curFile;
setWindowModified(true);
}
bool ImageDisplay::open()
{
QString fileName =
QFileDialog::getOpenFileName(this, tr("Open"), ".",
openFileFilters);
if (fileName.isEmpty())
return false;
return openFile(fileName);
}
bool ImageDisplay::save()
{
if (isUntitled)
{
return saveAs();
}
else
{
return writeFile(curFile);
}
}
bool ImageDisplay::saveAs()
{
QString fileName =
QFileDialog::getSaveFileName(this, tr("Save As"), curFile,
saveFileFilters);
if (fileName.isEmpty())
return false;
return writeFile(fileName);
}
bool ImageDisplay::openFile(const QString &fileName)
{
if (readFile(fileName))
{
setCurrentFile(fileName);
return true;
}
else
{
return false;
}
}
bool ImageDisplay::readFile(const QString &fileName)
{
QImage image(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("Image Viewer"),
tr("Cannot load %1.").arg(fileName));
return false;
}
setImage(image);
return true;
}
bool ImageDisplay::writeFile(const QString &fileName)
{
if (m_image.save(fileName, 0, -1))
{
setCurrentFile(fileName);
return true;
}
else
{
return false;
}
}
QImage ImageDisplay::getImage()
{
return m_image;
}
QString ImageDisplay::getCurFile()
{
return curFile;
}
QString ImageDisplay::getCurFileWithStrippedName()
{
return strippedName(curFile);
}