1 //////////////////////////////////////////////////////////////////////
2 //
3 // BeeBEEP Copyright (C) 2010-2021 Marco Mastroddi
4 //
5 // BeeBEEP is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published
7 // by the Free Software Foundation, either version 3 of the License,
8 // or (at your option) any later version.
9 //
10 // BeeBEEP is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with BeeBEEP. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Author: Marco Mastroddi <marco.mastroddi(AT)gmail.com>
19 //
20 // $Id: PreviewFileDialog.cpp 1455 2020-12-23 10:17:53Z mastroddi $
21 //
22 //////////////////////////////////////////////////////////////////////
23 
24 #include "BeeUtils.h"
25 #include "PreviewFileDialog.h"
26 #include "Settings.h"
27 
28 
PreviewFileDialog(QWidget * parent,const QString & caption,const QString & directory,const QString & filter)29 PreviewFileDialog::PreviewFileDialog( QWidget* parent, const QString& caption, const QString& directory, const QString& filter )
30   : QFileDialog( parent, caption, directory, filter )
31 {
32   setObjectName("PreviewFileDialog");
33   setOption( QFileDialog::DontUseNativeDialog, true );
34 
35   mp_preview = new QLabel( this );
36   mp_preview->setAlignment( Qt::AlignCenter );
37   mp_preview->setObjectName( "LabelPreview" );
38   mp_preview->setMinimumWidth( qMax( 100, Settings::instance().previewFileDialogImageSize() ) );
39   mp_preview->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
40   m_defaultPixmap = Bee::convertToGrayScale( QIcon( ":images/beebeep.png" ).pixmap( Settings::instance().previewFileDialogImageSize(), Settings::instance().previewFileDialogImageSize() ) );
41   setPixmapInPreview( m_defaultPixmap );
42 
43   {
44     QGridLayout *layout = (QGridLayout*)this->layout();
45     layout->addWidget( mp_preview, 0, layout->columnCount()+1, layout->rowCount()-1, 1, 0 );
46   }
47 
48   connect( this, SIGNAL( currentChanged( const QString& ) ), this, SLOT( onCurrentChanged( const QString& ) ) );
49 }
50 
onCurrentChanged(const QString & file_path)51 void PreviewFileDialog::onCurrentChanged( const QString& file_path )
52 {
53   QString file_suffix = Bee::suffixFromFile( file_path );
54   if( Bee::isFileTypeImage( file_suffix ) )
55   {
56     QPixmap pix = QPixmap( file_path );
57     if( !pix.isNull() )
58     {
59       setPixmapInPreview( pix );
60       return;
61     }
62   }
63 
64   setPixmapInPreview( m_defaultPixmap );
65 }
66 
setPixmapInPreview(const QPixmap & pix)67 void PreviewFileDialog::setPixmapInPreview( const QPixmap& pix )
68 {
69   int max_width = qMax( Settings::instance().previewFileDialogImageSize(), mp_preview->width() );
70   if( pix.width() > max_width || pix.height() > mp_preview->height() )
71   {
72     mp_preview->setPixmap( pix.scaled( max_width, mp_preview->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
73   }
74   else
75     mp_preview->setPixmap( pix );
76 }
77