QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfiledropedit.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfiledropedit.cpp - File Dropable LineEdit
3  --------------------------------------
4  Date : 31-Jan-2007
5  Copyright : (C) 2007 by Tom Elwertowski
6  Email : telwertowski at users dot sourceforge dot net
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsfiledropedit.h"
17 #include <QDropEvent>
18 #include <QFileInfo>
19 #include <QPainter>
20 #include <QUrl>
21 
33  : QLineEdit( parent )
34 {
35  mDirOnly = false;
36  mFileOnly = true;
37  mDragActive = false;
38  setAcceptDrops( true );
39 }
40 
42 {}
43 
47 void QgsFileDropEdit::setDirOnly( bool isDirOnly )
48 {
50  if ( mDirOnly )
51  {
52  mFileOnly = false;
53  }
54 }
55 
59 void QgsFileDropEdit::setFileOnly( bool isFileOnly )
60 {
62  if ( mFileOnly )
63  {
64  mDirOnly = false;
65  }
66 }
67 
71 void QgsFileDropEdit::setSuffixFilter( const QString& suffix )
72 {
73  mSuffix = suffix;
74 }
75 
79 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
80 {
81  QString path;
82  if ( event->mimeData()->hasUrls() )
83  {
84  QFileInfo file( event->mimeData()->urls().first().toLocalFile() );
85  if ( !(( mFileOnly && !file.isFile() ) ||
86  ( mDirOnly && !file.isDir() ) ||
87  ( !mSuffix.isEmpty() && mSuffix.compare( file.suffix(), Qt::CaseInsensitive ) ) ) )
88  path = file.filePath();
89  }
90  return path;
91 }
92 
97 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
98 {
99  QString filePath = acceptableFilePath( event );
100  if ( !filePath.isEmpty() )
101  {
102  event->acceptProposedAction();
103  mDragActive = true;
104  update();
105  }
106  else
107  {
108  QLineEdit::dragEnterEvent( event );
109  }
110 }
111 
115 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
116 {
117  QLineEdit::dragLeaveEvent( event );
118  event->accept();
119  mDragActive = false;
120  update();
121 }
122 
126 void QgsFileDropEdit::dropEvent( QDropEvent *event )
127 {
128  QString filePath = acceptableFilePath( event );
129  if ( !filePath.isEmpty() )
130  {
131  setText( filePath );
132  selectAll();
133  setFocus( Qt::MouseFocusReason );
134  event->acceptProposedAction();
135  mDragActive = false;
136  update();
137  }
138  else
139  {
140  QLineEdit::dropEvent( event );
141  }
142 }
143 
147 void QgsFileDropEdit::paintEvent( QPaintEvent *e )
148 {
150  if ( mDragActive )
151  {
152  QPainter p( this );
153  int width = 2; // width of highlight rectangle inside frame
154  p.setPen( QPen( palette().highlight(), width ) );
155  QRect r = rect().adjusted( width, width, -width, -width );
156  p.drawRect( r );
157  }
158 }