fixes and improvements
[Sak.git] / pixmapviewer.cpp
blobf3f5d9f82ed16da33456b4bfb0e84c612d9e0876
1 #include "pixmapviewer.h"
4 void PixmapViewer::paintEvent(QPaintEvent* )
6 QPainter p(this);
7 QSize s = size();
8 if (m_p.isNull()) {
9 int edge = qMin(s.width(), s.height()) - 4;
10 int offx = (s.width() - edge) / 2;
11 int offy = (s.height() - edge) / 2;
12 QRect r(offx, offy, edge, edge);
13 p.fillRect(r, Qt::white);
14 p.drawLine(edge + offx, offy, offx, offy+edge);
15 p.drawLine(offx, offy, offx+edge, offy+edge);
16 p.drawRect(r);
17 } else {
18 double ratiox = (double)s.width()/(double)m_p.width();
19 double ratioy = (double)s.height()/(double)m_p.height();
20 double ratio = qMin(ratiox, ratioy);
21 int w = (int)(ratio*m_p.width());
22 int h = (int)(ratio*m_p.height());
23 int offx = (s.width() - w)/2;
24 int offy = (s.height() - h)/2;
25 QRect r(offx, offy, w, h);
26 p.drawPixmap(r, m_p);
30 //void PixmapViewer::keyPressEvent(QKeyEvent * e)
31 //{
32 // if (e->key() == Qt::Key_Cancel || e->key() == Qt::Key_Backspace || e->key() == Qt::Key_Delete) {
33 // setPixmap(QPixmap());
34 // }
35 //}
38 void PixmapViewer::mousePressEvent(QMouseEvent* e)
40 if (e->buttons()) {
41 if (e->buttons() & Qt::RightButton) {
42 if ( QMessageBox::warning(0, "Unset image?", QString("Unset image?")) ) {
43 setPixmap(QPixmap());
44 emit changed();
46 } else {
47 QString file = QFileDialog::getOpenFileName(0, "Select an image");
48 QPixmap tmp;
49 if (!file.isEmpty()) {
50 if ( tmp.load ( file ) ) {
51 setPixmap(tmp);
52 emit changed();
53 } else {
54 QMessageBox::warning(0, "Failure loading image", QString("Failed to load image file %1").arg(file));