Skip to content

Commit

Permalink
Added QgsFileDropEdit class which extends QLineEdit to accept files d…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ qgisinterface.cpp
qgscolorbutton.cpp
qgscursors.cpp
qgsencodingfiledialog.cpp
qgsfiledropedit.cpp
qgslayerprojectionselector.cpp
qgsmapcanvas.cpp
qgsmapcanvasitem.cpp
Expand Down
159 changes: 159 additions & 0 deletions src/gui/qgsfiledropedit.cpp
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);
}
}
52 changes: 52 additions & 0 deletions src/gui/qgsfiledropedit.h
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
1 change: 1 addition & 0 deletions src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui(QgisInterface * _qI, QWidge
pbnOK = buttonBox->button(QDialogButtonBox::Ok);
pbnParse = buttonBox->addButton(tr("Parse"), QDialogButtonBox::ActionRole);
connect(pbnParse, SIGNAL(clicked()), this, SLOT(on_pbnParse_clicked()));
connect(txtFilePath, SIGNAL(textChanged(const QString&)), this, SLOT(on_pbnParse_clicked()));
enableButtons();
// at startup, fetch the last used delimiter and directory from
// settings
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/delimited_text/qgsdelimitedtextpluginguibase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="txtFilePath" >
<widget class="QgsFileDropEdit" name="txtFilePath" >
<property name="toolTip" >
<string>Full path to the delimited text file</string>
</property>
Expand Down Expand Up @@ -332,6 +332,11 @@
<header>Qt3Support/Q3GroupBox</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFileDropEdit</class>
<extends>QLineEdit</extends>
<header>qgsfiledropedit.h</header>
</customwidget>
<customwidget>
<class>Q3TextEdit</class>
<extends>Q3Frame</extends>
Expand Down
15 changes: 9 additions & 6 deletions src/plugins/georeferencer/pluginguibase.ui
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<ui version="4.0" >
<author></author>
<comment></comment>
<exportmacro></exportmacro>
<class>QgsGeorefPluginGuiBase</class>
<widget class="QDialog" name="QgsGeorefPluginGuiBase" >
<property name="geometry" >
Expand Down Expand Up @@ -125,12 +122,12 @@
</sizepolicy>
</property>
<property name="text" >
<string>...</string>
<string>Browse...</string>
</property>
</widget>
</item>
<item row="1" column="3" >
<widget class="QLineEdit" name="leSelectRaster" >
<widget class="QgsFileDropEdit" name="leSelectRaster" >
<property name="sizePolicy" >
<sizepolicy>
<hsizetype>7</hsizetype>
Expand All @@ -144,7 +141,13 @@
</layout>
</widget>
<layoutdefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
<customwidgets>
<customwidget>
<class>QgsFileDropEdit</class>
<extends>QLineEdit</extends>
<header>qgsfiledropedit.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="georeferencer.qrc" />
</resources>
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/georeferencer/qgspointdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,14 @@
<item row="2" column="7" >
<widget class="QPushButton" name="pbnSelectModifiedRaster" >
<property name="text" >
<string>...</string>
<string>Save As...</string>
</property>
</widget>
</item>
<item row="3" column="7" >
<widget class="QPushButton" name="pbnSelectWorldFile" >
<property name="text" >
<string>...</string>
<string>Save As...</string>
</property>
</widget>
</item>
Expand Down
52 changes: 31 additions & 21 deletions src/plugins/gps_importer/qgsgpsplugingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ QgsGPSPluginGui::QgsGPSPluginGui(const BabelMap& importers,
this, SLOT(enableRelevantControls()));
connect(tabWidget, SIGNAL(currentChanged(int)),
this, SLOT(enableRelevantControls()));
}

// drag and drop filter
leGPXFile->setSuffixFilter("gpx");
}

QgsGPSPluginGui::~QgsGPSPluginGui()
{
}
Expand Down Expand Up @@ -139,7 +143,8 @@ void QgsGPSPluginGui::on_pbnDLOutput_clicked()
tr("Choose a filename to save under"),
"." , //initial dir
tr("GPS eXchange format (*.gpx)"));
leDLOutput->setText(myFileNameQString);
if (!myFileNameQString.isEmpty())
leDLOutput->setText(myFileNameQString);
}


Expand Down Expand Up @@ -220,7 +225,8 @@ void QgsGPSPluginGui::on_pbnGPXSelectFile_clicked()
myFilterString, //filters to select
&myFileTypeQString); //the pointer to store selected filter
QgsLogger::debug("Selected filetype filter is : " + myFileTypeQString);
leGPXFile->setText(myFileNameQString);
if (!myFileNameQString.isEmpty())
leGPXFile->setText(myFileNameQString);
}


Expand All @@ -232,23 +238,26 @@ void QgsGPSPluginGui::on_pbnIMPInput_clicked() {
".", //initial dir
mBabelFilter,
&myFileType); //the pointer to store selected filter
mImpFormat = myFileType.left(myFileType.length() - 6);
std::map<QString, QgsBabelFormat*>::const_iterator iter;
iter = mImporters.find(mImpFormat);
if (iter == mImporters.end()) {
QgsLogger::warning("Unknown file format selected: " +
myFileType.left(myFileType.length() - 6));
}
else {
QgsLogger::debug(iter->first + " selected");
leIMPInput->setText(myFileName);
cmbIMPFeature->clear();
if (iter->second->supportsWaypoints())
cmbIMPFeature->insertItem("Waypoints");
if (iter->second->supportsRoutes())
cmbIMPFeature->insertItem("Routes");
if (iter->second->supportsTracks())
cmbIMPFeature->insertItem("Tracks");
if (!myFileName.isEmpty())
{
mImpFormat = myFileType.left(myFileType.length() - 6);
std::map<QString, QgsBabelFormat*>::const_iterator iter;
iter = mImporters.find(mImpFormat);
if (iter == mImporters.end()) {
QgsLogger::warning("Unknown file format selected: " +
myFileType.left(myFileType.length() - 6));
}
else {
QgsLogger::debug(iter->first + " selected");
leIMPInput->setText(myFileName);
cmbIMPFeature->clear();
if (iter->second->supportsWaypoints())
cmbIMPFeature->insertItem("Waypoints");
if (iter->second->supportsRoutes())
cmbIMPFeature->insertItem("Routes");
if (iter->second->supportsTracks())
cmbIMPFeature->insertItem("Tracks");
}
}
}

Expand All @@ -259,7 +268,8 @@ void QgsGPSPluginGui::on_pbnIMPOutput_clicked() {
tr("Choose a filename to save under"),
".", //initial dir
tr("GPS eXchange format (*.gpx)"));
leIMPOutput->setText(myFileNameQString);
if (!myFileNameQString.isEmpty())
leIMPOutput->setText(myFileNameQString);
}


Expand Down
Loading

0 comments on commit 1b53e28

Please sign in to comment.