QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 #include <QMimeData>
22 
34  : QLineEdit( parent )
35 {
36  mDirOnly = false;
37  mFileOnly = true;
38  mDragActive = false;
39  setAcceptDrops( true );
40 }
41 
43 {}
44 
48 void QgsFileDropEdit::setDirOnly( bool isDirOnly )
49 {
50  mDirOnly = isDirOnly;
51  if ( mDirOnly )
52  {
53  mFileOnly = false;
54  }
55 }
56 
60 void QgsFileDropEdit::setFileOnly( bool isFileOnly )
61 {
62  mFileOnly = isFileOnly;
63  if ( mFileOnly )
64  {
65  mDirOnly = false;
66  }
67 }
68 
72 void QgsFileDropEdit::setSuffixFilter( const QString& suffix )
73 {
74  mSuffix = suffix;
75 }
76 
80 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
81 {
82  QString path;
83  if ( event->mimeData()->hasUrls() )
84  {
85  QFileInfo file( event->mimeData()->urls().first().toLocalFile() );
86  if ( !(( mFileOnly && !file.isFile() ) ||
87  ( mDirOnly && !file.isDir() ) ||
88  ( !mSuffix.isEmpty() && mSuffix.compare( file.suffix(), Qt::CaseInsensitive ) ) ) )
89  path = file.filePath();
90  }
91  return path;
92 }
93 
98 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
99 {
100  QString filePath = acceptableFilePath( event );
101  if ( !filePath.isEmpty() )
102  {
103  event->acceptProposedAction();
104  mDragActive = true;
105  update();
106  }
107  else
108  {
109  QLineEdit::dragEnterEvent( event );
110  }
111 }
112 
116 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
117 {
118  QLineEdit::dragLeaveEvent( event );
119  event->accept();
120  mDragActive = false;
121  update();
122 }
123 
127 void QgsFileDropEdit::dropEvent( QDropEvent *event )
128 {
129  QString filePath = acceptableFilePath( event );
130  if ( !filePath.isEmpty() )
131  {
132  setText( filePath );
133  selectAll();
134  setFocus( Qt::MouseFocusReason );
135  event->acceptProposedAction();
136  mDragActive = false;
137  update();
138  }
139  else
140  {
141  QLineEdit::dropEvent( event );
142  }
143 }
144 
148 void QgsFileDropEdit::paintEvent( QPaintEvent *e )
149 {
150  QLineEdit::paintEvent( e );
151  if ( mDragActive )
152  {
153  QPainter p( this );
154  int width = 2; // width of highlight rectangle inside frame
155  p.setPen( QPen( palette().highlight(), width ) );
156  QRect r = rect().adjusted( width, width, -width, -width );
157  p.drawRect( r );
158  }
159 }
virtual ~QgsFileDropEdit()
virtual void dragLeaveEvent(QDragLeaveEvent *event)
QgsFileDropEdit(QWidget *parent=0)
bool isDirOnly() const
void setFileOnly(bool isFileOnly)
void setSuffixFilter(const QString &suffix)
QString file
Definition: qgssvgcache.cpp:76
void setDirOnly(bool isDirOnly)
virtual void dragEnterEvent(QDragEnterEvent *event)
virtual void dropEvent(QDropEvent *event)
bool isFileOnly() const
virtual void paintEvent(QPaintEvent *e)