Skip to content

Commit 274a902

Browse files
committed
[FEATURE] Show python scripts in browser
Double clicking (or dragging them onto canvas) executes the script
1 parent 695cf6b commit 274a902

File tree

4 files changed

+147
-7
lines changed

4 files changed

+147
-7
lines changed

src/app/qgisapp.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -981,11 +981,15 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
981981
registerCustomDropHandler( new QgsQlrDropHandler() );
982982
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsQptDataItemProvider() );
983983
registerCustomDropHandler( new QgsQptDropHandler() );
984-
985984
mSplash->showMessage( tr( "Starting Python" ), Qt::AlignHCenter | Qt::AlignBottom );
986985
qApp->processEvents();
987986
loadPythonSupport();
988987

988+
#ifdef WITH_BINDINGS
989+
QgsApplication::dataItemProviderRegistry()->addProvider( new QgsPyDataItemProvider() );
990+
registerCustomDropHandler( new QgsPyDropHandler() );
991+
#endif
992+
989993
// Create the plugin registry and load plugins
990994
// load any plugins that were running in the last session
991995
mSplash->showMessage( tr( "Restoring loaded plugins" ), Qt::AlignHCenter | Qt::AlignBottom );

src/app/qgisapp.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
215215
//! Open a composer template file and create a new composition
216216
void openTemplate( const QString &fileName );
217217

218+
/** Attempts to run a Python script
219+
* \param filePath full path to Python script
220+
* \since QGIS 2.7
221+
*/
222+
void runScript( const QString &filePath );
223+
218224
/** Opens a qgis project file
219225
\returns false if unable to open the project
220226
*/
@@ -1123,12 +1129,6 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
11231129
//! Open the project file corresponding to the
11241130
//! text)= of the given action.
11251131
void openProject( QAction *action );
1126-
1127-
/** Attempts to run a Python script
1128-
* \param filePath full path to Python script
1129-
* \since QGIS 2.7
1130-
*/
1131-
void runScript( const QString &filePath );
11321132
//! Save the map view as an image - user is prompted for image name using a dialog
11331133
void saveMapAsImage();
11341134
//! Save the map view as a pdf - user is prompted for image name using a dialog

src/app/qgsappbrowserproviders.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,97 @@ QList<QAction *> QgsQptDataItem::actions()
172172
} );
173173
return QList<QAction *>() << newLayout;
174174
}
175+
176+
//
177+
// QgsPyDataItem
178+
//
179+
180+
QgsPyDataItem::QgsPyDataItem( QgsDataItem *parent, const QString &name, const QString &path )
181+
: QgsDataItem( QgsDataItem::Custom, parent, name, path )
182+
{
183+
setState( QgsDataItem::Populated ); // no children
184+
setIconName( QStringLiteral( ":/images/icons/qgis-icon-16x16.png" ) );
185+
setToolTip( QDir::toNativeSeparators( path ) );
186+
}
187+
188+
bool QgsPyDataItem::hasDragEnabled() const
189+
{
190+
return true;
191+
}
192+
193+
QgsMimeDataUtils::Uri QgsPyDataItem::mimeUri() const
194+
{
195+
QgsMimeDataUtils::Uri u;
196+
u.layerType = QStringLiteral( "custom" );
197+
u.providerKey = QStringLiteral( "py" );
198+
u.name = name();
199+
u.uri = path();
200+
return u;
201+
}
202+
203+
bool QgsPyDataItem::handleDoubleClick()
204+
{
205+
QgisApp::instance()->runScript( path() );
206+
return true;
207+
}
208+
209+
QList<QAction *> QgsPyDataItem::actions()
210+
{
211+
QAction *runScript = new QAction( tr( "Run Script" ), this );
212+
connect( runScript, &QAction::triggered, this, [ = ]
213+
{
214+
QgisApp::instance()->runScript( path() );
215+
} );
216+
return QList<QAction *>() << runScript ;
217+
}
218+
219+
//
220+
// QgsPyDataItemProvider
221+
//
222+
223+
QString QgsPyDataItemProvider::name()
224+
{
225+
return QStringLiteral( "py" );
226+
}
227+
228+
int QgsPyDataItemProvider::capabilities()
229+
{
230+
return QgsDataProvider::File;
231+
}
232+
233+
QgsDataItem *QgsPyDataItemProvider::createDataItem( const QString &path, QgsDataItem *parentItem )
234+
{
235+
QFileInfo fileInfo( path );
236+
237+
if ( fileInfo.suffix().compare( QStringLiteral( "py" ), Qt::CaseInsensitive ) == 0 )
238+
{
239+
return new QgsPyDataItem( parentItem, fileInfo.fileName(), path );
240+
}
241+
return nullptr;
242+
}
243+
244+
//
245+
// QgsPyDropHandler
246+
//
247+
248+
QString QgsPyDropHandler::customUriProviderKey() const
249+
{
250+
return QStringLiteral( "py" );
251+
}
252+
253+
void QgsPyDropHandler::handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const
254+
{
255+
QString path = uri.uri;
256+
QgisApp::instance()->runScript( path );
257+
}
258+
259+
bool QgsPyDropHandler::handleFileDrop( const QString &file )
260+
{
261+
QFileInfo fi( file );
262+
if ( fi.completeSuffix().compare( QStringLiteral( "py" ), Qt::CaseInsensitive ) == 0 )
263+
{
264+
QgisApp::instance()->runScript( file );
265+
return true;
266+
}
267+
return false;
268+
}

src/app/qgsappbrowserproviders.h

+42
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,46 @@ class QgsQptDropHandler : public QgsCustomDropHandler
9898
};
9999

100100

101+
102+
/**
103+
* Custom data item for py Python scripts.
104+
*/
105+
class QgsPyDataItem : public QgsDataItem
106+
{
107+
Q_OBJECT
108+
109+
public:
110+
111+
QgsPyDataItem( QgsDataItem *parent, const QString &name, const QString &path );
112+
bool hasDragEnabled() const override;
113+
QgsMimeDataUtils::Uri mimeUri() const override;
114+
bool handleDoubleClick() override;
115+
QList< QAction * > actions() override;
116+
117+
118+
};
119+
120+
/**
121+
* Data item provider for showing Python py scripts in the browser.
122+
*/
123+
class QgsPyDataItemProvider : public QgsDataItemProvider
124+
{
125+
public:
126+
QString name() override;
127+
int capabilities() override;
128+
QgsDataItem *createDataItem( const QString &path, QgsDataItem *parentItem ) override;
129+
};
130+
131+
/**
132+
* Handles drag and drop of Python py scripts to app.
133+
*/
134+
class QgsPyDropHandler : public QgsCustomDropHandler
135+
{
136+
public:
137+
138+
QString customUriProviderKey() const override;
139+
void handleCustomUriDrop( const QgsMimeDataUtils::Uri &uri ) const override;
140+
bool handleFileDrop( const QString &file ) override;
141+
};
142+
101143
#endif // QGSAPPBROWSERPROVIDERS_H

0 commit comments

Comments
 (0)