1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
#include "ImageFileDialog.h"
#include "CustomPixmap.h"
#include "FileDialog.h"
#include "File.h"
#include "Debug.h"
#include "Util.h"
#include "XmlOptions.h"
#include <QFileInfo>
#include <QUrl>
#include <QImage>
#include <QLayout>
#include <QLabel>
#include <QMimeData>
#include <QPushButton>
#include <QSplitter>
//______________________________________________________________________
ImageFileDialog::ImageFileDialog( QWidget* parent ):
QFileDialog( parent )
{
Debug::Throw( "ImageFileDialog::ImageFileDialog.\n" );
// no size grip, ever
setSizeGripEnabled( false );
// working directory
if( !FileDialog::_workingDirectory().isEmpty() && QFileInfo( FileDialog::_workingDirectory() ).isDir() )
{ setDirectory( QDir( FileDialog::_workingDirectory() ) ); }
Debug::Throw() << "ImageFileDialog::ImageFileDialog - working directory: " << FileDialog::_workingDirectory() << endl;
connect( this, SIGNAL(directoryEntered(QString)), SLOT(_saveWorkingDirectory(QString)) );
// add image display
QSplitter* splitter = findChild<QSplitter*>( "splitter" );
if( splitter )
{
QWidget *main( new QWidget );
splitter->addWidget( main );
QVBoxLayout *vLayout = new QVBoxLayout;
main->setLayout( vLayout );
vLayout->setSpacing(5);
vLayout->setMargin(0);
vLayout->addWidget( preview_ = new Label( main ), 1 );
preview_->setAlignment( Qt::AlignCenter );
preview_->setFrameStyle( QFrame::StyledPanel|QFrame::Sunken );
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->setSpacing(5);
hLayout->setMargin(0);
vLayout->addLayout( hLayout );
hLayout->addWidget( automaticPreview_ = new QCheckBox( tr( "Automatic preview" ), main ) );
automaticPreview_->setChecked( true );
QPushButton* button = new QPushButton( tr( "Preview" ), main );
hLayout->addWidget( button );
connect( button, SIGNAL(clicked()), SLOT(_preview()) );
} else Debug::Throw(0) << "ImageFileDialog::ImageFileDialog - unable to find splitter." << endl;
connect( this, SIGNAL(currentChanged (QString)), SLOT(_currentChanged(QString)) );
}
//______________________________________________________________________
ImageFileDialog::Label::Label( QWidget* parent ):
QLabel( parent ),
Counter( "ImageFileDialog::Label" )
{ setAcceptDrops( true ); }
//______________________________________________________________________
void ImageFileDialog::Label::dragEnterEvent( QDragEnterEvent *event )
{
Debug::Throw( "ImageFileDialog::Label::dragEnterEvent.\n" );
if (event->mimeData()->hasUrls()) event->acceptProposedAction();
}
//______________________________________________________________________
void ImageFileDialog::Label::dropEvent( QDropEvent *event )
{
Debug::Throw( "ImageFileDialog::Label::dropEvent.\n" );
// check if event is valid
if( !event->mimeData()->hasUrls() ) return;
// loop over event URLs
for( const auto& url:event->mimeData()->urls() )
{
QFileInfo fileInfo( url.toLocalFile() );
if( fileInfo.exists() )
{
ImageFileDialog& dialog( *static_cast<ImageFileDialog*>( window() ) );
if( fileInfo.isDir() ) dialog.setDirectory( fileInfo.filePath() );
else {
dialog.setDirectory( fileInfo.path() );
dialog.selectFile( File( fileInfo.fileName() ) );
if( dialog.automaticPreview_ && dialog.automaticPreview_->isChecked() )
{ dialog._currentChanged( fileInfo.filePath() ); }
}
event->acceptProposedAction();
return;
}
}
}
//______________________________________________________________________
void ImageFileDialog::_saveWorkingDirectory( QString directory )
{
Debug::Throw() << "ImageFileDialog::_saveWorkingDirectory - directory: " << directory << endl;
FileDialog::_workingDirectory() = File( QFileInfo( directory ).absolutePath() );
}
//______________________________________________________________________
void ImageFileDialog::_currentChanged( const QString& value )
{
Debug::Throw( "ImageFileDialog::_currentChanged.\n" );
currentPath_ = File( value );
if( automaticPreview_ && automaticPreview_->isChecked() ) _preview();
}
//______________________________________________________________________
void ImageFileDialog::_preview()
{
Debug::Throw( "ImageFileDialog::_preview.\n" );
// nothing if no preview
if( !preview_ ) return;
// try load svg
CustomPixmap pixmap;
if( currentPath_.endsWith( ".svg" ) || currentPath_.endsWith( ".svgz" ) )
{
QSize size( 0.8*preview_->width(), 0.8*preview_->height() );
pixmap = CustomPixmap( QIcon( currentPath_ ).pixmap( size ) );
} else pixmap = CustomPixmap( currentPath_ );
// check and scale
if( pixmap.isNull() ) preview_->setPixmap( QPixmap() );
else {
if( pixmap.width() > preview_->width()*0.8 || pixmap.height() > preview_->height()*0.8 )
{ pixmap = CustomPixmap( pixmap.scaled( int(preview_->width()*0.8), int(preview_->height()*0.8), Qt::KeepAspectRatio, Qt::SmoothTransformation ) ); }
preview_->setPixmap( pixmap );
}
}
|