-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added QgsFileDropEdit class which extends QLineEdit to accept files d…
…ropped on the edit field. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@6513 c8812cc2-4d05-0410-92ff-de0c093fc19c
- Loading branch information
telwertowski
committed
Feb 4, 2007
1 parent
2a42ccb
commit 1b53e28
Showing
12 changed files
with
292 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/*************************************************************************** | ||
qgsfiledropedit.cpp - File Dropable LineEdit | ||
-------------------------------------- | ||
Date : 31-Jan-2007 | ||
Copyright : (C) 2007 by Tom Elwertowski | ||
Email : telwertowski at users dot sourceforge dot net | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
/* $Id$ */ | ||
|
||
#include "qgsfiledropedit.h" | ||
#include <QDropEvent> | ||
#include <QFileInfo> | ||
#include <QPainter> | ||
#include <QUrl> | ||
|
||
/*! | ||
\class QgsFileDropEdit | ||
\brief The QgsDropNameEdit class provides a line edit widget which | ||
accepts file drops. | ||
Dropping can be limited to files only, files with a specific extension | ||
or directories only. By default, dropping is limited to files only. | ||
*/ | ||
|
||
QgsFileDropEdit::QgsFileDropEdit(QWidget *parent) | ||
: QLineEdit(parent) | ||
{ | ||
mDirOnly = false; | ||
mFileOnly = true; | ||
mDragActive = false; | ||
setAcceptDrops(true); | ||
} | ||
|
||
QgsFileDropEdit::~QgsFileDropEdit() | ||
{} | ||
|
||
/*! | ||
Limit drops to directories. | ||
*/ | ||
void QgsFileDropEdit::setDirOnly(bool dirOnly) | ||
{ | ||
mDirOnly = dirOnly; | ||
if (mDirOnly) | ||
{ | ||
mFileOnly = false; | ||
} | ||
} | ||
|
||
/*! | ||
Limit drops to files. | ||
*/ | ||
void QgsFileDropEdit::setFileOnly(bool fileOnly) | ||
{ | ||
mFileOnly = fileOnly; | ||
if (mFileOnly) | ||
{ | ||
mDirOnly = false; | ||
} | ||
} | ||
|
||
/*! | ||
Limit drops to files with specified extension. | ||
*/ | ||
void QgsFileDropEdit::setSuffixFilter(const QString& suffix) | ||
{ | ||
mSuffix = suffix; | ||
} | ||
|
||
/*! | ||
Return file name if object meets drop criteria. | ||
*/ | ||
QString QgsFileDropEdit::acceptableFilePath(QDropEvent *event) const | ||
{ | ||
QString path; | ||
if (event->mimeData()->hasUrls()) | ||
{ | ||
QFileInfo file(event->mimeData()->urls().first().toLocalFile()); | ||
if ( !( mFileOnly && !file.isFile() || | ||
mDirOnly && !file.isDir() || | ||
!mSuffix.isEmpty() && mSuffix.compare(file.suffix(), Qt::CaseInsensitive) ) ) | ||
path = file.filePath(); | ||
} | ||
return path; | ||
} | ||
|
||
/*! | ||
Check if dragged object is acceptible. Called when a drag is in progress | ||
and the mouse enters this widget. | ||
*/ | ||
void QgsFileDropEdit::dragEnterEvent(QDragEnterEvent *event) | ||
{ | ||
QString filePath = acceptableFilePath(event); | ||
if (!filePath.isEmpty()) | ||
{ | ||
event->acceptProposedAction(); | ||
mDragActive = true; | ||
update(); | ||
} | ||
else | ||
{ | ||
QLineEdit::dragEnterEvent(event); | ||
} | ||
} | ||
|
||
/*! | ||
Called when a drag is in progress and the mouse leaves this widget. | ||
*/ | ||
void QgsFileDropEdit::dragLeaveEvent(QDragLeaveEvent *event) | ||
{ | ||
QLineEdit::dragLeaveEvent(event); | ||
event->accept(); | ||
mDragActive = false; | ||
update(); | ||
} | ||
|
||
/*! | ||
Receive the dragged object. Called when the drag is dropped on this widget. | ||
*/ | ||
void QgsFileDropEdit::dropEvent(QDropEvent *event) | ||
{ | ||
QString filePath = acceptableFilePath(event); | ||
if (!filePath.isEmpty()) | ||
{ | ||
setText(filePath); | ||
selectAll(); | ||
setFocus(Qt::MouseFocusReason); | ||
event->acceptProposedAction(); | ||
mDragActive = false; | ||
update(); | ||
} | ||
else | ||
{ | ||
QLineEdit::dropEvent(event); | ||
} | ||
} | ||
|
||
/*! | ||
Paints line edit with drag highlight in response to a paint event. | ||
*/ | ||
void QgsFileDropEdit::paintEvent(QPaintEvent *e) | ||
{ | ||
QLineEdit::paintEvent(e); | ||
if (mDragActive) | ||
{ | ||
QPainter p(this); | ||
int width = 2; // width of highlight rectangle inside frame | ||
p.setPen(QPen(palette().highlight(), width)); | ||
QRect r = rect().adjusted(width, width, -width, -width); | ||
p.drawRect(r); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*************************************************************************** | ||
qgsfiledropedit.h - File Dropable LineEdit | ||
-------------------------------------- | ||
Date : 31-Jan-2007 | ||
Copyright : (C) 2007 by Tom Elwertowski | ||
Email : telwertowski at users dot sourceforge dot net | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
/* $Id$ */ | ||
#ifndef QGSFILEDROPEDIT_H | ||
#define QGSFILEDROPEDIT_H | ||
|
||
#include <QLineEdit> | ||
|
||
class GUI_EXPORT QgsFileDropEdit: public QLineEdit | ||
{ | ||
public: | ||
QgsFileDropEdit(QWidget *parent = 0); | ||
virtual ~QgsFileDropEdit(); | ||
|
||
bool dirOnly() const { return mDirOnly; } | ||
void setDirOnly(bool dirOnly); | ||
|
||
bool fileOnly() const { return mFileOnly; } | ||
void setFileOnly(bool fileOnly); | ||
|
||
const QString& suffixFilter() const { return mSuffix; } | ||
void setSuffixFilter( const QString& suffix); | ||
|
||
protected: | ||
|
||
virtual void dragEnterEvent(QDragEnterEvent *event); | ||
virtual void dragLeaveEvent(QDragLeaveEvent *event); | ||
virtual void dropEvent(QDropEvent *event); | ||
virtual void paintEvent(QPaintEvent *e); | ||
|
||
private: | ||
QString acceptableFilePath(QDropEvent *event) const; | ||
|
||
QString mSuffix; | ||
bool mDirOnly; | ||
bool mFileOnly; | ||
bool mDragActive; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.