1 /****************************************************************************************
2 * Copyright (c) 2005 Eyal Lotem <eyal.lotem@gmail.com> *
3 * Copyright (c) 2005 Alexandre Pereira de Oliveira <aleprj@gmail.com> *
4 * Copyright (c) 2007 Seb Ruiz <ruiz@kde.org> *
5 * Copyright (c) 2009 Pascal Pollet <pascal@bongosoft.de> *
6 * *
7 * This program is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License as published by the Free Software *
9 * Foundation; either version 2 of the License, or (at your option) any later *
10 * version. *
11 * *
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License along with *
17 * this program. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************************/
19
20 #include "PixmapViewer.h"
21
22 #include <QApplication>
23
24 #include <QDesktopWidget>
25 #include <QMouseEvent>
26 #include <QLabel>
27 #include <QPixmap>
28 #include <QPainter>
29 #include <QWheelEvent>
30
31
PixmapViewer(QWidget * parent,const QPixmap & pix,int screenNumber)32 PixmapViewer::PixmapViewer( QWidget *parent, const QPixmap &pix, int screenNumber )
33 : QWidget( parent )
34 , m_pixmap( pix )
35 {
36 m_zoomFactor = 1.0; // initial zoom
37
38 int screenWidth = QApplication::desktop()->availableGeometry( screenNumber ).width();
39 int screenHeight = QApplication::desktop()->availableGeometry( screenNumber ).height();
40 if( screenWidth < m_pixmap.width() || screenHeight < m_pixmap.height() )
41 {
42 qreal zoomFactorX = qreal(screenWidth) / m_pixmap.width();
43 qreal zoomFactorY = qreal(screenHeight) / m_pixmap.height();
44 m_zoomFactor = qMin( zoomFactorX, zoomFactorY ) * 0.8;
45 }
46 setMinimumSize( m_pixmap.width() * m_zoomFactor, m_pixmap.height() * m_zoomFactor );
47 }
48
~PixmapViewer()49 PixmapViewer::~PixmapViewer()
50 {
51 }
52
53 qreal
zoomFactor() const54 PixmapViewer::zoomFactor() const
55 {
56 return m_zoomFactor;
57 }
58
59 void
setZoomFactor(qreal f)60 PixmapViewer::setZoomFactor( qreal f )
61 {
62 int w, h;
63
64 if( f == m_zoomFactor )
65 return;
66
67 m_zoomFactor = f;
68 Q_EMIT( zoomFactorChanged( m_zoomFactor ) );
69
70 w = m_pixmap.width() * m_zoomFactor;
71 h = m_pixmap.height() * m_zoomFactor;
72 setMinimumSize( w, h );
73
74 QWidget *p = dynamic_cast<QWidget*>( parent() );
75 if( p )
76 resize( p->width(), p->height() );
77 }
78
79 void
paintEvent(QPaintEvent * event)80 PixmapViewer::paintEvent( QPaintEvent *event )
81 {
82 int xoffset, yoffset;
83
84 if( width() > m_pixmap.width() * m_zoomFactor )
85 {
86 xoffset = ( width() - m_pixmap.width() * m_zoomFactor ) / 2;
87 }
88 else
89 {
90 xoffset = 0;
91 }
92
93 if( height() > m_pixmap.height() * m_zoomFactor )
94 {
95 yoffset = ( height() - m_pixmap.height() * m_zoomFactor ) / 2;
96 }
97 else
98 {
99 yoffset = 0;
100 }
101
102 QPainter p( this );
103 p.save();
104 p.translate( xoffset, yoffset );
105 p.scale( m_zoomFactor, m_zoomFactor );
106 p.drawPixmap( 0, 0, m_pixmap );
107 p.restore();
108
109 event->accept();
110 }
111
112 void
wheelEvent(QWheelEvent * event)113 PixmapViewer::wheelEvent( QWheelEvent *event )
114 {
115 qreal f = m_zoomFactor + 0.001 * event->delta();
116 qreal ratio = 32.0 / m_pixmap.width();
117 if( f < ratio )
118 f = ratio;
119 setZoomFactor( f );
120 event->accept();
121 }
122
123