File indexing completed on 2024-05-05 04:49:27

0001 /****************************************************************************************
0002  * Copyright (c) 2005 Eyal Lotem <eyal.lotem@gmail.com>                                 *
0003  * Copyright (c) 2005 Alexandre Pereira de Oliveira <aleprj@gmail.com>                  *
0004  * Copyright (c) 2007 Seb Ruiz <ruiz@kde.org>                                           *
0005  * Copyright (c) 2009 Pascal Pollet <pascal@bongosoft.de>                               *
0006  *                                                                                      *
0007  * This program is free software; you can redistribute it and/or modify it under        *
0008  * the terms of the GNU General Public License as published by the Free Software        *
0009  * Foundation; either version 2 of the License, or (at your option) any later           *
0010  * version.                                                                             *
0011  *                                                                                      *
0012  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0013  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0014  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0015  *                                                                                      *
0016  * You should have received a copy of the GNU General Public License along with         *
0017  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0018  ****************************************************************************************/
0019 
0020 #include "PixmapViewer.h"
0021 
0022 #include <QApplication>
0023 
0024 #include <QDesktopWidget>
0025 #include <QMouseEvent>
0026 #include <QLabel>
0027 #include <QPixmap>
0028 #include <QPainter>
0029 #include <QWheelEvent>
0030 
0031 
0032 PixmapViewer::PixmapViewer( QWidget *parent, const QPixmap &pix, int screenNumber )
0033     : QWidget( parent )
0034     , m_pixmap( pix )
0035 {
0036     m_zoomFactor = 1.0; // initial zoom
0037 
0038     int screenWidth = QApplication::desktop()->availableGeometry( screenNumber ).width();
0039     int screenHeight = QApplication::desktop()->availableGeometry( screenNumber ).height();
0040     if( screenWidth < m_pixmap.width() || screenHeight < m_pixmap.height() )
0041     {
0042         qreal zoomFactorX = qreal(screenWidth) / m_pixmap.width();
0043         qreal zoomFactorY = qreal(screenHeight) / m_pixmap.height();
0044         m_zoomFactor = qMin( zoomFactorX, zoomFactorY ) * 0.8;
0045     }
0046     setMinimumSize( m_pixmap.width() * m_zoomFactor, m_pixmap.height() * m_zoomFactor );
0047 }
0048 
0049 PixmapViewer::~PixmapViewer()
0050 {
0051 }
0052 
0053 qreal
0054 PixmapViewer::zoomFactor() const
0055 {
0056     return m_zoomFactor;
0057 }
0058 
0059 void
0060 PixmapViewer::setZoomFactor( qreal f )
0061 {
0062     int w, h;
0063 
0064     if( f == m_zoomFactor )
0065         return;
0066 
0067     m_zoomFactor = f;
0068     Q_EMIT( zoomFactorChanged( m_zoomFactor ) );
0069 
0070     w = m_pixmap.width() * m_zoomFactor;
0071     h = m_pixmap.height() * m_zoomFactor;
0072     setMinimumSize( w, h );
0073 
0074     QWidget *p = dynamic_cast<QWidget*>( parent() );
0075     if( p )
0076         resize( p->width(), p->height() );
0077 }
0078 
0079 void
0080 PixmapViewer::paintEvent( QPaintEvent *event )
0081 {
0082     int xoffset, yoffset;
0083 
0084     if( width() > m_pixmap.width() * m_zoomFactor )
0085     {
0086         xoffset = ( width() - m_pixmap.width() * m_zoomFactor ) / 2;
0087     }
0088     else
0089     {
0090         xoffset = 0;
0091     }
0092 
0093     if( height() > m_pixmap.height() * m_zoomFactor )
0094     {
0095         yoffset = ( height() - m_pixmap.height() * m_zoomFactor ) / 2;
0096     }
0097     else
0098     {
0099         yoffset = 0;
0100     }
0101     
0102     QPainter p( this );
0103     p.save();
0104     p.translate( xoffset, yoffset );
0105     p.scale( m_zoomFactor, m_zoomFactor );
0106     p.drawPixmap( 0, 0, m_pixmap );
0107     p.restore();
0108 
0109     event->accept();
0110 }
0111 
0112 void
0113 PixmapViewer::wheelEvent( QWheelEvent *event )
0114 {
0115     qreal f = m_zoomFactor + 0.001 * event->angleDelta().y(); //FIXME: check if .x() must be used
0116     qreal ratio = 32.0 / m_pixmap.width();
0117     if( f < ratio )
0118         f = ratio;
0119     setZoomFactor( f );
0120     event->accept();
0121 }
0122