Quantum GIS API Documentation  1.7.4
src/gui/qgsfiledropedit.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgsfiledropedit.cpp - File Dropable LineEdit
00003      --------------------------------------
00004     Date                 : 31-Jan-2007
00005     Copyright            : (C) 2007 by Tom Elwertowski
00006     Email                : telwertowski at users dot sourceforge dot net
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 /* $Id$ */
00016 
00017 #include "qgsfiledropedit.h"
00018 #include <QDropEvent>
00019 #include <QFileInfo>
00020 #include <QPainter>
00021 #include <QUrl>
00022 
00033 QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
00034     : QLineEdit( parent )
00035 {
00036   mDirOnly = false;
00037   mFileOnly = true;
00038   mDragActive = false;
00039   setAcceptDrops( true );
00040 }
00041 
00042 QgsFileDropEdit::~QgsFileDropEdit()
00043 {}
00044 
00048 void QgsFileDropEdit::setDirOnly( bool isDirOnly )
00049 {
00050   mDirOnly = isDirOnly;
00051   if ( mDirOnly )
00052   {
00053     mFileOnly = false;
00054   }
00055 }
00056 
00060 void QgsFileDropEdit::setFileOnly( bool isFileOnly )
00061 {
00062   mFileOnly = isFileOnly;
00063   if ( mFileOnly )
00064   {
00065     mDirOnly = false;
00066   }
00067 }
00068 
00072 void QgsFileDropEdit::setSuffixFilter( const QString& suffix )
00073 {
00074   mSuffix = suffix;
00075 }
00076 
00080 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
00081 {
00082   QString path;
00083   if ( event->mimeData()->hasUrls() )
00084   {
00085     QFileInfo file( event->mimeData()->urls().first().toLocalFile() );
00086     if ( !(( mFileOnly && !file.isFile() ) ||
00087            ( mDirOnly && !file.isDir() ) ||
00088            ( !mSuffix.isEmpty() && mSuffix.compare( file.suffix(), Qt::CaseInsensitive ) ) ) )
00089       path = file.filePath();
00090   }
00091   return path;
00092 }
00093 
00098 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
00099 {
00100   QString filePath = acceptableFilePath( event );
00101   if ( !filePath.isEmpty() )
00102   {
00103     event->acceptProposedAction();
00104     mDragActive = true;
00105     update();
00106   }
00107   else
00108   {
00109     QLineEdit::dragEnterEvent( event );
00110   }
00111 }
00112 
00116 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
00117 {
00118   QLineEdit::dragLeaveEvent( event );
00119   event->accept();
00120   mDragActive = false;
00121   update();
00122 }
00123 
00127 void QgsFileDropEdit::dropEvent( QDropEvent *event )
00128 {
00129   QString filePath = acceptableFilePath( event );
00130   if ( !filePath.isEmpty() )
00131   {
00132     setText( filePath );
00133     selectAll();
00134     setFocus( Qt::MouseFocusReason );
00135     event->acceptProposedAction();
00136     mDragActive = false;
00137     update();
00138   }
00139   else
00140   {
00141     QLineEdit::dropEvent( event );
00142   }
00143 }
00144 
00148 void QgsFileDropEdit::paintEvent( QPaintEvent *e )
00149 {
00150   QLineEdit::paintEvent( e );
00151   if ( mDragActive )
00152   {
00153     QPainter p( this );
00154     int width = 2;  // width of highlight rectangle inside frame
00155     p.setPen( QPen( palette().highlight(), width ) );
00156     QRect r = rect().adjusted( width, width, -width, -width );
00157     p.drawRect( r );
00158   }
00159 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines