mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-16 00:05:45 -04:00
[processing] Accept drag and dropped paths for output parameter values
from either explorer or browser (if you want to write to an existing path, it's a great shortcut!)
This commit is contained in:
parent
c12590f87e
commit
b106e723e2
@ -65,6 +65,14 @@ Emitted whenever the "skip output" option is toggled in the widget.
|
|||||||
%Docstring
|
%Docstring
|
||||||
Emitted whenever the destination value is changed in the widget.
|
Emitted whenever the destination value is changed in the widget.
|
||||||
%End
|
%End
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void dragEnterEvent( QDragEnterEvent *event );
|
||||||
|
|
||||||
|
virtual void dragLeaveEvent( QDragLeaveEvent *event );
|
||||||
|
|
||||||
|
virtual void dropEvent( QDropEvent *event );
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -38,6 +38,8 @@ QgsProcessingLayerOutputDestinationWidget::QgsProcessingLayerOutputDestinationWi
|
|||||||
|
|
||||||
setupUi( this );
|
setupUi( this );
|
||||||
|
|
||||||
|
leText->setClearButtonEnabled( false );
|
||||||
|
|
||||||
connect( leText, &QLineEdit::textEdited, this, &QgsProcessingLayerOutputDestinationWidget::textChanged );
|
connect( leText, &QLineEdit::textEdited, this, &QgsProcessingLayerOutputDestinationWidget::textChanged );
|
||||||
|
|
||||||
mMenu = new QMenu( this );
|
mMenu = new QMenu( this );
|
||||||
@ -62,6 +64,9 @@ QgsProcessingLayerOutputDestinationWidget::QgsProcessingLayerOutputDestinationWi
|
|||||||
}
|
}
|
||||||
|
|
||||||
setToolTip( mParameter->toolTip() );
|
setToolTip( mParameter->toolTip() );
|
||||||
|
|
||||||
|
setAcceptDrops( true );
|
||||||
|
leText->setAcceptDrops( false );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool QgsProcessingLayerOutputDestinationWidget::outputIsSkipped() const
|
bool QgsProcessingLayerOutputDestinationWidget::outputIsSkipped() const
|
||||||
@ -447,5 +452,113 @@ void QgsProcessingLayerOutputDestinationWidget::textChanged( const QString &text
|
|||||||
emit destinationChanged();
|
emit destinationChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString QgsProcessingLayerOutputDestinationWidget::mimeDataToPath( const QMimeData *data )
|
||||||
|
{
|
||||||
|
const QgsMimeDataUtils::UriList uriList = QgsMimeDataUtils::decodeUriList( data );
|
||||||
|
for ( const QgsMimeDataUtils::Uri &u : uriList )
|
||||||
|
{
|
||||||
|
if ( ( mParameter->type() == QgsProcessingParameterFeatureSink::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterVectorDestination::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterFileDestination::typeName() )
|
||||||
|
&& u.layerType == QLatin1String( "vector" ) && u.providerKey == QLatin1String( "ogr" ) )
|
||||||
|
{
|
||||||
|
return u.uri;
|
||||||
|
}
|
||||||
|
else if ( ( mParameter->type() == QgsProcessingParameterRasterDestination::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterFileDestination::typeName() )
|
||||||
|
&& u.layerType == QLatin1String( "raster" ) && u.providerKey == QLatin1String( "gdal" ) )
|
||||||
|
return u.uri;
|
||||||
|
#if 0
|
||||||
|
else if ( ( mParameter->type() == QgsProcessingParameterMeshDestination::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterFileDestination::typeName() )
|
||||||
|
&& u.layerType == QLatin1String( "mesh" ) && u.providerKey == QLatin1String( "mdal" ) )
|
||||||
|
return u.uri;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
else if ( mParameter->type() == QgsProcessingParameterFolderDestination::typeName()
|
||||||
|
&& u.layerType == QLatin1String( "directory" ) )
|
||||||
|
{
|
||||||
|
return u.uri;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( !uriList.isEmpty() )
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
// files dragged from file explorer, outside of QGIS
|
||||||
|
QStringList rawPaths;
|
||||||
|
if ( data->hasUrls() )
|
||||||
|
{
|
||||||
|
const QList< QUrl > urls = data->urls();
|
||||||
|
rawPaths.reserve( urls.count() );
|
||||||
|
for ( const QUrl &url : urls )
|
||||||
|
{
|
||||||
|
const QString local = url.toLocalFile();
|
||||||
|
if ( !rawPaths.contains( local ) )
|
||||||
|
rawPaths.append( local );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( !data->text().isEmpty() && !rawPaths.contains( data->text() ) )
|
||||||
|
rawPaths.append( data->text() );
|
||||||
|
|
||||||
|
for ( const QString &path : qgis::as_const( rawPaths ) )
|
||||||
|
{
|
||||||
|
QFileInfo file( path );
|
||||||
|
if ( file.isFile() && ( mParameter->type() == QgsProcessingParameterFeatureSink::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterVectorDestination::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterRasterDestination::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterVectorDestination::typeName()
|
||||||
|
|| mParameter->type() == QgsProcessingParameterFileDestination::typeName() ) )
|
||||||
|
{
|
||||||
|
// TODO - we should check to see if it's a valid extension for the parameter, but that's non-trivial
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
else if ( file.isDir() && ( mParameter->type() == QgsProcessingParameterFolderDestination::typeName() ) )
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsProcessingLayerOutputDestinationWidget::dragEnterEvent( QDragEnterEvent *event )
|
||||||
|
{
|
||||||
|
if ( !( event->possibleActions() & Qt::CopyAction ) )
|
||||||
|
return;
|
||||||
|
|
||||||
|
const QString path = mimeDataToPath( event->mimeData() );
|
||||||
|
if ( !path.isEmpty() )
|
||||||
|
{
|
||||||
|
// dragged an acceptable path, phew
|
||||||
|
event->setDropAction( Qt::CopyAction );
|
||||||
|
event->accept();
|
||||||
|
leText->setHighlighted( true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsProcessingLayerOutputDestinationWidget::dragLeaveEvent( QDragLeaveEvent *event )
|
||||||
|
{
|
||||||
|
QWidget::dragLeaveEvent( event );
|
||||||
|
if ( leText->isHighlighted() )
|
||||||
|
{
|
||||||
|
event->accept();
|
||||||
|
leText->setHighlighted( false );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void QgsProcessingLayerOutputDestinationWidget::dropEvent( QDropEvent *event )
|
||||||
|
{
|
||||||
|
if ( !( event->possibleActions() & Qt::CopyAction ) )
|
||||||
|
return;
|
||||||
|
|
||||||
|
const QString path = mimeDataToPath( event->mimeData() );
|
||||||
|
if ( !path.isEmpty() )
|
||||||
|
{
|
||||||
|
// dropped an acceptable path, phew
|
||||||
|
setFocus( Qt::MouseFocusReason );
|
||||||
|
event->setDropAction( Qt::CopyAction );
|
||||||
|
event->accept();
|
||||||
|
setValue( path );
|
||||||
|
}
|
||||||
|
leText->setHighlighted( false );
|
||||||
|
}
|
||||||
|
|
||||||
///@endcond
|
///@endcond
|
||||||
|
@ -77,6 +77,11 @@ class GUI_EXPORT QgsProcessingLayerOutputDestinationWidget : public QWidget, pri
|
|||||||
* Emitted whenever the destination value is changed in the widget.
|
* Emitted whenever the destination value is changed in the widget.
|
||||||
*/
|
*/
|
||||||
void destinationChanged();
|
void destinationChanged();
|
||||||
|
protected:
|
||||||
|
|
||||||
|
void dragEnterEvent( QDragEnterEvent *event ) override;
|
||||||
|
void dragLeaveEvent( QDragLeaveEvent *event ) override;
|
||||||
|
void dropEvent( QDropEvent *event ) override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
@ -92,6 +97,8 @@ class GUI_EXPORT QgsProcessingLayerOutputDestinationWidget : public QWidget, pri
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
QString mimeDataToPath( const QMimeData *data );
|
||||||
|
|
||||||
const QgsProcessingDestinationParameter *mParameter = nullptr;
|
const QgsProcessingDestinationParameter *mParameter = nullptr;
|
||||||
QMenu *mMenu = nullptr;
|
QMenu *mMenu = nullptr;
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
<number>0</number>
|
<number>0</number>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLineEdit" name="leText">
|
<widget class="QgsHighlightableLineEdit" name="leText">
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||||
<horstretch>0</horstretch>
|
<horstretch>0</horstretch>
|
||||||
@ -54,6 +54,13 @@
|
|||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>QgsHighlightableLineEdit</class>
|
||||||
|
<extends>QLineEdit</extends>
|
||||||
|
<header>qgshighlightablelineedit.h</header>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
</ui>
|
</ui>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user