Quantum GIS API Documentation
1.8
|
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 00016 #include "qgsfiledropedit.h" 00017 #include <QDropEvent> 00018 #include <QFileInfo> 00019 #include <QPainter> 00020 #include <QUrl> 00021 00032 QgsFileDropEdit::QgsFileDropEdit( QWidget *parent ) 00033 : QLineEdit( parent ) 00034 { 00035 mDirOnly = false; 00036 mFileOnly = true; 00037 mDragActive = false; 00038 setAcceptDrops( true ); 00039 } 00040 00041 QgsFileDropEdit::~QgsFileDropEdit() 00042 {} 00043 00047 void QgsFileDropEdit::setDirOnly( bool isDirOnly ) 00048 { 00049 mDirOnly = isDirOnly; 00050 if ( mDirOnly ) 00051 { 00052 mFileOnly = false; 00053 } 00054 } 00055 00059 void QgsFileDropEdit::setFileOnly( bool isFileOnly ) 00060 { 00061 mFileOnly = isFileOnly; 00062 if ( mFileOnly ) 00063 { 00064 mDirOnly = false; 00065 } 00066 } 00067 00071 void QgsFileDropEdit::setSuffixFilter( const QString& suffix ) 00072 { 00073 mSuffix = suffix; 00074 } 00075 00079 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const 00080 { 00081 QString path; 00082 if ( event->mimeData()->hasUrls() ) 00083 { 00084 QFileInfo file( event->mimeData()->urls().first().toLocalFile() ); 00085 if ( !(( mFileOnly && !file.isFile() ) || 00086 ( mDirOnly && !file.isDir() ) || 00087 ( !mSuffix.isEmpty() && mSuffix.compare( file.suffix(), Qt::CaseInsensitive ) ) ) ) 00088 path = file.filePath(); 00089 } 00090 return path; 00091 } 00092 00097 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event ) 00098 { 00099 QString filePath = acceptableFilePath( event ); 00100 if ( !filePath.isEmpty() ) 00101 { 00102 event->acceptProposedAction(); 00103 mDragActive = true; 00104 update(); 00105 } 00106 else 00107 { 00108 QLineEdit::dragEnterEvent( event ); 00109 } 00110 } 00111 00115 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event ) 00116 { 00117 QLineEdit::dragLeaveEvent( event ); 00118 event->accept(); 00119 mDragActive = false; 00120 update(); 00121 } 00122 00126 void QgsFileDropEdit::dropEvent( QDropEvent *event ) 00127 { 00128 QString filePath = acceptableFilePath( event ); 00129 if ( !filePath.isEmpty() ) 00130 { 00131 setText( filePath ); 00132 selectAll(); 00133 setFocus( Qt::MouseFocusReason ); 00134 event->acceptProposedAction(); 00135 mDragActive = false; 00136 update(); 00137 } 00138 else 00139 { 00140 QLineEdit::dropEvent( event ); 00141 } 00142 } 00143 00147 void QgsFileDropEdit::paintEvent( QPaintEvent *e ) 00148 { 00149 QLineEdit::paintEvent( e ); 00150 if ( mDragActive ) 00151 { 00152 QPainter p( this ); 00153 int width = 2; // width of highlight rectangle inside frame 00154 p.setPen( QPen( palette().highlight(), width ) ); 00155 QRect r = rect().adjusted( width, width, -width, -width ); 00156 p.drawRect( r ); 00157 } 00158 }