QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
49 {
50  mDirOnly = isDirOnly;
51  if ( mDirOnly )
52  {
53  mFileOnly = false;
54  }
55 }
56 
61 {
62  mFileOnly = isFileOnly;
63  if ( mFileOnly )
64  {
65  mDirOnly = false;
66  }
67 }
68 
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 
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 
117 {
118  QLineEdit::dragLeaveEvent( event );
119  event->accept();
120  mDragActive = false;
121  update();
122 }
123 
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 
149 {
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 }
const QPalette & palette() const
const QMimeData * mimeData() const
virtual void dragEnterEvent(QDragEnterEvent *e)
void setText(const QString &)
virtual bool event(QEvent *e)
virtual ~QgsFileDropEdit()
QgsFileDropEdit(QWidget *parent=nullptr)
void update()
int width() const
bool isFileOnly() const
virtual void paintEvent(QPaintEvent *e) override
void drawRect(const QRectF &rectangle)
bool isDirOnly() const
virtual void dragEnterEvent(QDragEnterEvent *event) override
void setFileOnly(bool isFileOnly)
void setPen(const QColor &color)
void setFocus()
bool isEmpty() const
void setSuffixFilter(const QString &suffix)
void selectAll()
virtual void paintEvent(QPaintEvent *)
QRect rect() const
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const
void setAcceptDrops(bool on)
virtual void dragLeaveEvent(QDragLeaveEvent *e)
bool hasUrls() const
virtual void dragLeaveEvent(QDragLeaveEvent *event) override
QList< QUrl > urls() const
void setDirOnly(bool isDirOnly)
virtual void dropEvent(QDropEvent *event) override
int compare(const QString &other) const
virtual void dropEvent(QDropEvent *e)