From 94a88b1d9acc1bfa3a6c94265d1605dce7cf2a3c Mon Sep 17 00:00:00 2001 From: telwertowski Date: Sun, 4 Feb 2007 09:13:34 +0000 Subject: [PATCH] Added QgsFileDropEdit class which extends QLineEdit to accept files dropped on the edit field. git-svn-id: http://svn.osgeo.org/qgis/trunk@6513 c8812cc2-4d05-0410-92ff-de0c093fc19c --- src/gui/CMakeLists.txt | 1 + src/gui/qgsfiledropedit.cpp | 159 ++++++++++++++++++ src/gui/qgsfiledropedit.h | 52 ++++++ .../qgsdelimitedtextplugingui.cpp | 1 + .../qgsdelimitedtextpluginguibase.ui | 7 +- src/plugins/georeferencer/pluginguibase.ui | 15 +- .../georeferencer/qgspointdialogbase.ui | 4 +- src/plugins/gps_importer/qgsgpsplugingui.cpp | 52 +++--- .../gps_importer/qgsgpspluginguibase.ui | 13 +- tools/mapserver_export/CMakeLists.txt | 6 +- tools/mapserver_export/qgsmapserverexport.cpp | 4 +- .../qgsmapserverexportbase.ui | 23 ++- 12 files changed, 292 insertions(+), 45 deletions(-) create mode 100644 src/gui/qgsfiledropedit.cpp create mode 100644 src/gui/qgsfiledropedit.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index bb43289dcb68..8ca05f309572 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -5,6 +5,7 @@ qgisinterface.cpp qgscolorbutton.cpp qgscursors.cpp qgsencodingfiledialog.cpp +qgsfiledropedit.cpp qgslayerprojectionselector.cpp qgsmapcanvas.cpp qgsmapcanvasitem.cpp diff --git a/src/gui/qgsfiledropedit.cpp b/src/gui/qgsfiledropedit.cpp new file mode 100644 index 000000000000..7f973f46e414 --- /dev/null +++ b/src/gui/qgsfiledropedit.cpp @@ -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 +#include +#include +#include + +/*! + \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); + } +} diff --git a/src/gui/qgsfiledropedit.h b/src/gui/qgsfiledropedit.h new file mode 100644 index 000000000000..6b91a615fb09 --- /dev/null +++ b/src/gui/qgsfiledropedit.h @@ -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 + +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 diff --git a/src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp b/src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp index dc0715775513..b5b1efcfb5a5 100644 --- a/src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp +++ b/src/plugins/delimited_text/qgsdelimitedtextplugingui.cpp @@ -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 diff --git a/src/plugins/delimited_text/qgsdelimitedtextpluginguibase.ui b/src/plugins/delimited_text/qgsdelimitedtextpluginguibase.ui index 85f61e5c7cee..13ab59746c8e 100644 --- a/src/plugins/delimited_text/qgsdelimitedtextpluginguibase.ui +++ b/src/plugins/delimited_text/qgsdelimitedtextpluginguibase.ui @@ -216,7 +216,7 @@ - + Full path to the delimited text file @@ -332,6 +332,11 @@
Qt3Support/Q3GroupBox
1 + + QgsFileDropEdit + QLineEdit +
qgsfiledropedit.h
+
Q3TextEdit Q3Frame diff --git a/src/plugins/georeferencer/pluginguibase.ui b/src/plugins/georeferencer/pluginguibase.ui index 6d706cfaae6e..fb3737c8566e 100644 --- a/src/plugins/georeferencer/pluginguibase.ui +++ b/src/plugins/georeferencer/pluginguibase.ui @@ -1,7 +1,4 @@ - - - QgsGeorefPluginGuiBase @@ -125,12 +122,12 @@ - ... + Browse...
- + 7 @@ -144,7 +141,13 @@ - + + + QgsFileDropEdit + QLineEdit +
qgsfiledropedit.h
+
+
diff --git a/src/plugins/georeferencer/qgspointdialogbase.ui b/src/plugins/georeferencer/qgspointdialogbase.ui index cc0e3e1d40cd..ab8738f44a53 100644 --- a/src/plugins/georeferencer/qgspointdialogbase.ui +++ b/src/plugins/georeferencer/qgspointdialogbase.ui @@ -228,14 +228,14 @@ - ... + Save As... - ... + Save As... diff --git a/src/plugins/gps_importer/qgsgpsplugingui.cpp b/src/plugins/gps_importer/qgsgpsplugingui.cpp index aa22d0f50fba..a26bfd87409d 100644 --- a/src/plugins/gps_importer/qgsgpsplugingui.cpp +++ b/src/plugins/gps_importer/qgsgpsplugingui.cpp @@ -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() { } @@ -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); } @@ -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); } @@ -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::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::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"); + } } } @@ -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); } diff --git a/src/plugins/gps_importer/qgsgpspluginguibase.ui b/src/plugins/gps_importer/qgsgpspluginguibase.ui index 39afe0358749..f84b00c469a1 100644 --- a/src/plugins/gps_importer/qgsgpspluginguibase.ui +++ b/src/plugins/gps_importer/qgsgpspluginguibase.ui @@ -104,7 +104,7 @@ p, li { white-space: pre-wrap; }
- + @@ -192,7 +192,7 @@ p, li { white-space: pre-wrap; } - Browse... + Save As... @@ -388,7 +388,7 @@ p, li { white-space: pre-wrap; } - Browse... + Save As... @@ -489,6 +489,13 @@ p, li { white-space: pre-wrap; } + + + QgsFileDropEdit + QLineEdit +
qgsfiledropedit.h
+
+
diff --git a/tools/mapserver_export/CMakeLists.txt b/tools/mapserver_export/CMakeLists.txt index 9c504ce8d854..d6c2914851ab 100644 --- a/tools/mapserver_export/CMakeLists.txt +++ b/tools/mapserver_export/CMakeLists.txt @@ -18,20 +18,22 @@ SET (MSEXPORT_UIS qgsmapserverexportbase.ui) QT4_WRAP_UI (MSEXPORT_UIS_H ${MSEXPORT_UIS}) -QT4_WRAP_CPP (MSEXPORT_MOC_SRCS ${MSEXPORT_MOC_HDRS} ${MSEXPORT_UIS_H}) +QT4_WRAP_CPP (MSEXPORT_MOC_SRCS ${MSEXPORT_MOC_HDRS}) INCLUDE_DIRECTORIES ( ${CMAKE_CURRENT_BINARY_DIR} ${PYTHON_INCLUDE_PATH} ${CMAKE_SOURCE_DIR}/src/core + ${CMAKE_SOURCE_DIR}/src/gui ) -ADD_EXECUTABLE (msexport MACOSX_BUNDLE ${MSEXPORT_SRCS} ${MSEXPORT_MOC_SRCS}) +ADD_EXECUTABLE (msexport MACOSX_BUNDLE ${MSEXPORT_SRCS} ${MSEXPORT_MOC_SRCS} ${MSEXPORT_UIS_H}) TARGET_LINK_LIBRARIES (msexport ${QT_LIBRARIES} ${PYTHON_LIBRARIES} qgis_core + qgis_gui ) diff --git a/tools/mapserver_export/qgsmapserverexport.cpp b/tools/mapserver_export/qgsmapserverexport.cpp index 58119a402091..3ba5560eb02c 100644 --- a/tools/mapserver_export/qgsmapserverexport.cpp +++ b/tools/mapserver_export/qgsmapserverexport.cpp @@ -39,7 +39,9 @@ QgsMapserverExport::QgsMapserverExport(QWidget * parent, Qt::WFlags fl) { setupUi(this); connect(this, SIGNAL(accepted()), this, SLOT(apply())); -// initialize python + // drag and drop filter + txtQgisFilePath->setSuffixFilter("qgs"); + // initialize python initPy(); qDebug("Reading setttings"); QSettings mySettings; diff --git a/tools/mapserver_export/qgsmapserverexportbase.ui b/tools/mapserver_export/qgsmapserverexportbase.ui index 46821f31fdd2..a30cd767899e 100644 --- a/tools/mapserver_export/qgsmapserverexportbase.ui +++ b/tools/mapserver_export/qgsmapserverexportbase.ui @@ -63,7 +63,7 @@
- + Path to the MapServer template file @@ -72,7 +72,7 @@ - Other... + Browse... @@ -103,12 +103,12 @@ - + - Other... + Browse... @@ -139,12 +139,12 @@ - + - Other... + Browse... @@ -366,12 +366,12 @@ - Other... + Browse... - + Full path to the QGIS project file to export to MapServer map format @@ -390,7 +390,7 @@ - Other... + Save As... @@ -425,6 +425,11 @@
Qt3Support/Q3GroupBox
1 + + QgsFileDropEdit + QLineEdit +
qgsfiledropedit.h
+
txtMapFilePath