File indexing completed on 2024-05-05 04:48:22

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Seb Ruiz <ruiz@kde.org>                                           *
0003  * Copyright (c) 2010 Rick W. Chen <stuffcorpse@archlinux.us>                           *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #include "CoverViewDialog.h"
0019 
0020 #include "core/meta/Meta.h"
0021 #include "widgets/PixmapViewer.h"
0022 
0023 #include <QApplication>
0024 #include <QDialog>
0025 #include <KLocalizedString>
0026 #include <KWindowSystem>
0027 
0028 #include <QDesktopWidget>
0029 #include <QHBoxLayout>
0030 #include <KConfigGroup>
0031 
0032 CoverViewDialog::CoverViewDialog( Meta::AlbumPtr album, QWidget *parent )
0033     : QDialog( parent )
0034     , m_title( i18n( "Cover View" ) )
0035     , m_size( album->image().size() )
0036     , m_zoom( 100 )
0037 {
0038     setAttribute( Qt::WA_DeleteOnClose );
0039     updateCaption();
0040     createViewer( album->image(), parent );
0041 }
0042 
0043 CoverViewDialog::CoverViewDialog( const QImage &image, QWidget *parent )
0044     : QDialog( parent )
0045     , m_title( i18n( "Cover View" ) )
0046     , m_size( image.size() )
0047     , m_zoom( 100 )
0048 {
0049     setAttribute( Qt::WA_DeleteOnClose );
0050     updateCaption();
0051     createViewer( image, parent );
0052 }
0053 
0054 void
0055 CoverViewDialog::updateCaption()
0056 {
0057     QString width   = QString::number( m_size.width() );
0058     QString height  = QString::number( m_size.height() );
0059     QString zoom    = QString::number( m_zoom );
0060     QString size    = QStringLiteral( "%1x%2" ).arg( width, height );
0061     QString caption = QStringLiteral( "%1 - %2 - %3%" ).arg( m_title, size, zoom );
0062     setWindowTitle( caption );
0063 }
0064 
0065 void
0066 CoverViewDialog::zoomFactorChanged( qreal value )
0067 {
0068     m_zoom = 100 * value;
0069     updateCaption();
0070 }
0071 
0072 void
0073 CoverViewDialog::createViewer( const QImage &image, const QWidget *widget )
0074 {
0075     int screenNumber = QApplication::desktop()->screenNumber( widget );
0076     PixmapViewer *pixmapViewer = new PixmapViewer( this, QPixmap::fromImage(image), screenNumber );
0077     QHBoxLayout *layout = new QHBoxLayout( this );
0078     layout->addWidget( pixmapViewer );
0079     layout->setSizeConstraint( QLayout::SetFixedSize );
0080     layout->setContentsMargins( 0, 0, 0, 0 );
0081     connect( pixmapViewer, &PixmapViewer::zoomFactorChanged, this, &CoverViewDialog::zoomFactorChanged );
0082 
0083     qreal zoom = pixmapViewer->zoomFactor();
0084     zoomFactorChanged( zoom );
0085     QPoint topLeft = mapFromParent( widget->geometry().center() );
0086     topLeft -= QPoint( image.width() * zoom / 2, image.height() * zoom / 2 );
0087     move( topLeft );
0088     activateWindow();
0089     raise();
0090 }
0091