QGIS API Documentation  2.99.0-Master (0a63d1f)
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 
46 {
47  mDirOnly = isDirOnly;
48  if ( mDirOnly )
49  {
50  mFileOnly = false;
51  }
52 }
53 
58 {
59  mFileOnly = isFileOnly;
60  if ( mFileOnly )
61  {
62  mDirOnly = false;
63  }
64 }
65 
69 void QgsFileDropEdit::setSuffixFilter( const QString& suffix )
70 {
71  mSuffix = suffix;
72 }
73 
77 QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
78 {
79  QString path;
80  if ( event->mimeData()->hasUrls() )
81  {
82  QFileInfo file( event->mimeData()->urls().first().toLocalFile() );
83  if ( !(( mFileOnly && !file.isFile() ) ||
84  ( mDirOnly && !file.isDir() ) ||
85  ( !mSuffix.isEmpty() && mSuffix.compare( file.suffix(), Qt::CaseInsensitive ) ) ) )
86  path = file.filePath();
87  }
88  return path;
89 }
90 
95 void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
96 {
97  QString filePath = acceptableFilePath( event );
98  if ( !filePath.isEmpty() )
99  {
100  event->acceptProposedAction();
101  mDragActive = true;
102  update();
103  }
104  else
105  {
106  QLineEdit::dragEnterEvent( event );
107  }
108 }
109 
113 void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
114 {
115  QLineEdit::dragLeaveEvent( event );
116  event->accept();
117  mDragActive = false;
118  update();
119 }
120 
124 void QgsFileDropEdit::dropEvent( QDropEvent *event )
125 {
126  QString filePath = acceptableFilePath( event );
127  if ( !filePath.isEmpty() )
128  {
129  setText( filePath );
130  selectAll();
131  setFocus( Qt::MouseFocusReason );
132  event->acceptProposedAction();
133  mDragActive = false;
134  update();
135  }
136  else
137  {
138  QLineEdit::dropEvent( event );
139  }
140 }
141 
145 void QgsFileDropEdit::paintEvent( QPaintEvent *e )
146 {
147  QLineEdit::paintEvent( e );
148  if ( mDragActive )
149  {
150  QPainter p( this );
151  int width = 2; // width of highlight rectangle inside frame
152  p.setPen( QPen( palette().highlight(), width ) );
153  QRect r = rect().adjusted( width, width, -width, -width );
154  p.drawRect( r );
155  }
156 }
QgsFileDropEdit(QWidget *parent=nullptr)
bool isFileOnly() const
virtual void paintEvent(QPaintEvent *e) override
bool isDirOnly() const
virtual void dragEnterEvent(QDragEnterEvent *event) override
void setFileOnly(bool isFileOnly)
void setSuffixFilter(const QString &suffix)
virtual void dragLeaveEvent(QDragLeaveEvent *event) override
void setDirOnly(bool isDirOnly)
virtual void dropEvent(QDropEvent *event) override