[FEATURE] Show python scripts in browser

Double clicking (or dragging them onto canvas) executes the script
This commit is contained in:
Nyall Dawson 2017-09-21 08:10:52 +10:00
parent 695cf6b5d5
commit 274a9024cd
4 changed files with 147 additions and 7 deletions

View File

@ -981,11 +981,15 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
registerCustomDropHandler( new QgsQlrDropHandler() );
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsQptDataItemProvider() );
registerCustomDropHandler( new QgsQptDropHandler() );
mSplash->showMessage( tr( "Starting Python" ), Qt::AlignHCenter | Qt::AlignBottom );
qApp->processEvents();
loadPythonSupport();
#ifdef WITH_BINDINGS
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsPyDataItemProvider() );
registerCustomDropHandler( new QgsPyDropHandler() );
#endif
// Create the plugin registry and load plugins
// load any plugins that were running in the last session
mSplash->showMessage( tr( "Restoring loaded plugins" ), Qt::AlignHCenter | Qt::AlignBottom );

View File

@ -215,6 +215,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Open a composer template file and create a new composition
void openTemplate( const QString &fileName );
/** Attempts to run a Python script
* \param filePath full path to Python script
* \since QGIS 2.7
*/
void runScript( const QString &filePath );
/** Opens a qgis project file
\returns false if unable to open the project
*/
@ -1123,12 +1129,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Open the project file corresponding to the
//! text)= of the given action.
void openProject( QAction *action );
/** Attempts to run a Python script
* \param filePath full path to Python script
* \since QGIS 2.7
*/
void runScript( const QString &filePath );
//! Save the map view as an image - user is prompted for image name using a dialog
void saveMapAsImage();
//! Save the map view as a pdf - user is prompted for image name using a dialog

View File

@ -172,3 +172,97 @@ QList<QAction *> QgsQptDataItem::actions()
} );
return QList<QAction *>() << newLayout;
}
//
// QgsPyDataItem
//
QgsPyDataItem::QgsPyDataItem( QgsDataItem *parent, const QString &name, const QString &path )
: QgsDataItem( QgsDataItem::Custom, parent, name, path )
{
setState( QgsDataItem::Populated ); // no children
setIconName( QStringLiteral( ":/images/icons/qgis-icon-16x16.png" ) );
setToolTip( QDir::toNativeSeparators( path ) );
}
bool QgsPyDataItem::hasDragEnabled() const
{
return true;
}
QgsMimeDataUtils::Uri QgsPyDataItem::mimeUri() const
{
QgsMimeDataUtils::Uri u;
u.layerType = QStringLiteral( "custom" );
u.providerKey = QStringLiteral( "py" );
u.name = name();
u.uri = path();
return u;
}
bool QgsPyDataItem::handleDoubleClick()
{
QgisApp::instance()->runScript( path() );
return true;
}
QList<QAction *> QgsPyDataItem::actions()
{
QAction *runScript = new QAction( tr( "Run Script" ), this );
connect( runScript, &QAction::triggered, this, [ = ]
{
QgisApp::instance()->runScript( path() );
} );
return QList<QAction *>() << runScript ;
}
//
// QgsPyDataItemProvider
//
QString QgsPyDataItemProvider::name()
{
return QStringLiteral( "py" );
}
int QgsPyDataItemProvider::capabilities()
{
return QgsDataProvider::File;
}
QgsDataItem *QgsPyDataItemProvider::createDataItem( const QString &path, QgsDataItem *parentItem )
{
QFileInfo fileInfo( path );
if ( fileInfo.suffix().compare( QStringLiteral( "py" ), Qt::CaseInsensitive ) == 0 )
{
return new QgsPyDataItem( parentItem, fileInfo.fileName(), path );
}
return nullptr;
}
//
// QgsPyDropHandler
//
QString QgsPyDropHandler::customUriProviderKey() const
{
return QStringLiteral( "py" );
}
void QgsPyDropHandler::handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const
{
QString path = uri.uri;
QgisApp::instance()->runScript( path );
}
bool QgsPyDropHandler::handleFileDrop( const QString &file )
{
QFileInfo fi( file );
if ( fi.completeSuffix().compare( QStringLiteral( "py" ), Qt::CaseInsensitive ) == 0 )
{
QgisApp::instance()->runScript( file );
return true;
}
return false;
}

View File

@ -98,4 +98,46 @@ class QgsQptDropHandler : public QgsCustomDropHandler
};
/**
* Custom data item for py Python scripts.
*/
class QgsPyDataItem : public QgsDataItem
{
Q_OBJECT
public:
QgsPyDataItem( QgsDataItem *parent, const QString &name, const QString &path );
bool hasDragEnabled() const override;
QgsMimeDataUtils::Uri mimeUri() const override;
bool handleDoubleClick() override;
QList< QAction * > actions() override;
};
/**
* Data item provider for showing Python py scripts in the browser.
*/
class QgsPyDataItemProvider : public QgsDataItemProvider
{
public:
QString name() override;
int capabilities() override;
QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override;
};
/**
* Handles drag and drop of Python py scripts to app.
*/
class QgsPyDropHandler : public QgsCustomDropHandler
{
public:
QString customUriProviderKey() const override;
void handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const override;
bool handleFileDrop( const QString &file ) override;
};
#endif // QGSAPPBROWSERPROVIDERS_H