-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixmapviewer.cpp
More file actions
51 lines (47 loc) · 1.61 KB
/
pixmapviewer.cpp
File metadata and controls
51 lines (47 loc) · 1.61 KB
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
#include "pixmapviewer.h"
void PixmapViewer::paintEvent(QPaintEvent* )
{
QPainter p(this);
QSize s = size();
if (m_p.isNull()) {
int edge = qMin(s.width(), s.height()) - 4;
int offx = (s.width() - edge) / 2;
int offy = (s.height() - edge) / 2;
QRect r(offx, offy, edge, edge);
p.fillRect(r, Qt::white);
p.drawLine(edge + offx, offy, offx, offy+edge);
p.drawLine(offx, offy, offx+edge, offy+edge);
p.drawRect(r);
} else {
double ratiox = (double)s.width()/(double)m_p.width();
double ratioy = (double)s.height()/(double)m_p.height();
double ratio = qMin(ratiox, ratioy);
int w = (int)(ratio*m_p.width());
int h = (int)(ratio*m_p.height());
int offx = (s.width() - w)/2;
int offy = (s.height() - h)/2;
QRect r(offx, offy, w, h);
p.drawPixmap(r, m_p);
}
}
void PixmapViewer::mousePressEvent(QMouseEvent* e)
{
if (e->buttons()) {
if (e->buttons() & Qt::RightButton) {
if ( QMessageBox::warning(0, "Unset image?", QString("Unset image?")) ) {
setPixmap(QPixmap());
emit changed();
}
} else {
QString file = QFileDialog::getOpenFileName(0, "Select an image");
QPixmap tmp;
if (!file.isEmpty()) {
if (tmp.load ( file ) ) {
setPixmap(tmp);
QMessageBox::warning(0, "Failure loading image", QString("Failed load image file %1").arg(file));
emit changed();
}
}
}
}
}