QGIS API Documentation  2.99.0-Master (b698612)
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 
24  : QLineEdit( parent )
25 {
26  mDirOnly = false;
27  mFileOnly = true;
28  mDragActive = false;
29  setAcceptDrops( true );
30 }
31 
33 {
34  mDirOnly = isDirOnly;
35  if ( mDirOnly )
36  {
37  mFileOnly = false;
38  }
39 }
40 
42 {
43  mFileOnly = isFileOnly;
44  if ( mFileOnly )
45  {
46  mDirOnly = false;
47  }
48 }
49 
50 void QgsFileDropEdit::setSuffixFilter( const QString &suffix )
51 {
52  mSuffix = suffix;
53 }
54 
55 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
56 {
57  QString path;
58  if ( event->mimeData()->hasUrls() )
59  {
60  QFileInfo file( event->mimeData()->urls().first().toLocalFile() );
61  if ( !( ( mFileOnly && !file.isFile() ) ||
62  ( mDirOnly && !file.isDir() ) ||
63  ( !mSuffix.isEmpty() && mSuffix.compare( file.suffix(), Qt::CaseInsensitive ) ) ) )
64  path = file.filePath();
65  }
66  return path;
67 }
68 
69 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
70 {
71  QString filePath = acceptableFilePath( event );
72  if ( !filePath.isEmpty() )
73  {
74  event->acceptProposedAction();
75  mDragActive = true;
76  update();
77  }
78  else
79  {
80  QLineEdit::dragEnterEvent( event );
81  }
82 }
83 
84 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
85 {
86  QLineEdit::dragLeaveEvent( event );
87  event->accept();
88  mDragActive = false;
89  update();
90 }
91 
92 void QgsFileDropEdit::dropEvent( QDropEvent *event )
93 {
94  QString filePath = acceptableFilePath( event );
95  if ( !filePath.isEmpty() )
96  {
97  setText( filePath );
98  selectAll();
99  setFocus( Qt::MouseFocusReason );
100  event->acceptProposedAction();
101  mDragActive = false;
102  update();
103  }
104  else
105  {
106  QLineEdit::dropEvent( event );
107  }
108 }
109 
110 void QgsFileDropEdit::paintEvent( QPaintEvent *e )
111 {
112  QLineEdit::paintEvent( e );
113  if ( mDragActive )
114  {
115  QPainter p( this );
116  int width = 2; // width of highlight rectangle inside frame
117  p.setPen( QPen( palette().highlight(), width ) );
118  QRect r = rect().adjusted( width, width, -width, -width );
119  p.drawRect( r );
120  }
121 }
QgsFileDropEdit(QWidget *parent=0)
bool isFileOnly() const
virtual void paintEvent(QPaintEvent *e) override
bool isDirOnly() const
virtual void dragEnterEvent(QDragEnterEvent *event) override
void setFileOnly(bool isFileOnly)
Limit drops to files.
void setSuffixFilter(const QString &suffix)
Limit drops to files with specified extension.
virtual void dragLeaveEvent(QDragLeaveEvent *event) override
void setDirOnly(bool isDirOnly)
Limit drops to directories.
virtual void dropEvent(QDropEvent *event) override