1
/***************************************************************************
2
qgsfiledropedit.cpp - File Dropable LineEdit
3
--------------------------------------
5
Copyright : (C) 2007 by Tom Elwertowski
6
Email : telwertowski at users dot sourceforge dot net
7
***************************************************************************
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. *
14
***************************************************************************/
17
#include "qgsfiledropedit.h"
24
\class QgsFileDropEdit
26
\brief The QgsDropNameEdit class provides a line edit widget which
29
Dropping can be limited to files only, files with a specific extension
30
or directories only. By default, dropping is limited to files only.
33
QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
39
setAcceptDrops( true );
42
QgsFileDropEdit::~QgsFileDropEdit()
46
Limit drops to directories.
48
void QgsFileDropEdit::setDirOnly( bool isDirOnly )
60
void QgsFileDropEdit::setFileOnly( bool isFileOnly )
62
mFileOnly = isFileOnly;
70
Limit drops to files with specified extension.
72
void QgsFileDropEdit::setSuffixFilter( const QString& suffix )
78
Return file name if object meets drop criteria.
80
QString QgsFileDropEdit::acceptableFilePath( QDropEvent *event ) const
83
if ( event->mimeData()->hasUrls() )
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();
95
Check if dragged object is acceptible. Called when a drag is in progress
96
and the mouse enters this widget.
98
void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
100
QString filePath = acceptableFilePath( event );
101
if ( !filePath.isEmpty() )
103
event->acceptProposedAction();
109
QLineEdit::dragEnterEvent( event );
114
Called when a drag is in progress and the mouse leaves this widget.
116
void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
118
QLineEdit::dragLeaveEvent( event );
125
Receive the dragged object. Called when the drag is dropped on this widget.
127
void QgsFileDropEdit::dropEvent( QDropEvent *event )
129
QString filePath = acceptableFilePath( event );
130
if ( !filePath.isEmpty() )
134
setFocus( Qt::MouseFocusReason );
135
event->acceptProposedAction();
141
QLineEdit::dropEvent( event );
146
Paints line edit with drag highlight in response to a paint event.
148
void QgsFileDropEdit::paintEvent( QPaintEvent *e )
150
QLineEdit::paintEvent( e );
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 );