Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add method to handle browser item double clicks to QgsDataItemGuiProv…
…ider

And move handling of layer/project file double clicks in browser dock
from gui->app
  • Loading branch information
nyalldawson committed Nov 4, 2018
1 parent 40443eb commit fa3a08e
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 11 deletions.
5 changes: 4 additions & 1 deletion python/gui/auto_generated/qgsbrowserdockwidget.sip.in
Expand Up @@ -61,12 +61,15 @@ Returns the message bar associated with the dock.


public slots: public slots:


bool addLayerAtIndex( const QModelIndex &index ); bool addLayerAtIndex( const QModelIndex &index ) /Deprecated/;
%Docstring %Docstring
Adds the layer corresponding to the specified model ``index``. Adds the layer corresponding to the specified model ``index``.


Returns true if the index was successfully intrepreted as a map layer and loaded, or Returns true if the index was successfully intrepreted as a map layer and loaded, or
false if the index is not a map layer or could not be loaded. false if the index is not a map layer or could not be loaded.

.. deprecated:: will be removed in QGIS 4.0 - retrieve the QgsLayerItem itself
and manually add to project.
%End %End


void showContextMenu( QPoint ); void showContextMenu( QPoint );
Expand Down
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsdataitemguiprovider.sip.in
Expand Up @@ -97,6 +97,13 @@ The ``context`` argument gives the wider context under which the context menu is
and contains accessors for useful objects like the application message bar. and contains accessors for useful objects like the application message bar.


The base class method has no effect. The base class method has no effect.
%End

virtual bool handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext context );
%Docstring
Called when a user double clicks on an ``item``. Providers should return true
if the double-click was handled and do not want other providers to handle the
double-click, and to prevent the default double-click behavior for items.
%End %End


}; };
Expand Down
55 changes: 54 additions & 1 deletion src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -383,6 +383,23 @@ void QgsLayerItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *men
} }
} }


bool QgsLayerItemGuiProvider::handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext )
{
if ( !item || item->type() != QgsDataItem::Layer )
return false;

if ( QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( item ) )
{
const QgsMimeDataUtils::UriList layerUriList = QgsMimeDataUtils::UriList() << layerItem->mimeUri();
QgisApp::instance()->handleDropUriList( layerUriList );
return true;
}
else
{
return false;
}
}

void QgsLayerItemGuiProvider::addLayersFromItems( const QList<QgsDataItem *> &items ) void QgsLayerItemGuiProvider::addLayersFromItems( const QList<QgsDataItem *> &items )
{ {
QgsTemporaryCursorOverride cursor( Qt::WaitCursor ); QgsTemporaryCursorOverride cursor( Qt::WaitCursor );
Expand All @@ -394,7 +411,7 @@ void QgsLayerItemGuiProvider::addLayersFromItems( const QList<QgsDataItem *> &it
if ( item && item->type() == QgsDataItem::Project ) if ( item && item->type() == QgsDataItem::Project )
{ {
if ( const QgsProjectItem *projectItem = qobject_cast<const QgsProjectItem *>( item ) ) if ( const QgsProjectItem *projectItem = qobject_cast<const QgsProjectItem *>( item ) )
QgisApp::instance()->openFile( projectItem->path(), QStringLiteral( "project" ) ); QgisApp::instance()->openProject( projectItem->path() );


return; return;
} }
Expand Down Expand Up @@ -426,3 +443,39 @@ void QgsLayerItemGuiProvider::showPropertiesForItem( QgsLayerItem *item )
dialog->setItem( item ); dialog->setItem( item );
dialog->show(); dialog->show();
} }

//
// QgsProjectItemGuiProvider
//

QString QgsProjectItemGuiProvider::name()
{
return QStringLiteral( "project_items" );
}

void QgsProjectItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context )
{
if ( !item || item->type() != QgsDataItem::Project )
return;

if ( QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item ) )
{
// TODO add actions
}
}

bool QgsProjectItemGuiProvider::handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext )
{
if ( !item || item->type() != QgsDataItem::Project )
return false;

if ( QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item ) )
{
QgisApp::instance()->openProject( projectItem->path() );
return true;
}
else
{
return false;
}
}
17 changes: 17 additions & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.h
Expand Up @@ -94,6 +94,7 @@ class QgsLayerItemGuiProvider : public QObject, public QgsDataItemGuiProvider


void populateContextMenu( QgsDataItem *item, QMenu *menu, void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override; const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override;
bool handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext context ) override;


private: private:


Expand All @@ -103,6 +104,22 @@ class QgsLayerItemGuiProvider : public QObject, public QgsDataItemGuiProvider
}; };




class QgsProjectItemGuiProvider : public QObject, public QgsDataItemGuiProvider
{
Q_OBJECT

public:

QgsProjectItemGuiProvider() = default;

QString name() override;

void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override;
bool handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext context ) override;

};

#endif // QGSINBUILTDATAITEMPROVIDERS_H #endif // QGSINBUILTDATAITEMPROVIDERS_H




1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1027,6 +1027,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh


QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsAppDirectoryItemGuiProvider() ); QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsAppDirectoryItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsProjectHomeItemGuiProvider() ); QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsProjectHomeItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsProjectItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsFavoritesItemGuiProvider() ); QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsFavoritesItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsLayerItemGuiProvider() ); QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsLayerItemGuiProvider() );


Expand Down
1 change: 0 additions & 1 deletion src/app/qgsappbrowserproviders.h
Expand Up @@ -204,7 +204,6 @@ class APP_EXPORT QgsProjectRootDataItem : public QgsProjectItem
*/ */
QgsProjectRootDataItem( QgsDataItem *parent, const QString &path ); QgsProjectRootDataItem( QgsDataItem *parent, const QString &path );
QVector<QgsDataItem *> createChildren() override; QVector<QgsDataItem *> createChildren() override;

}; };


/** /**
Expand Down
26 changes: 19 additions & 7 deletions src/gui/qgsbrowserdockwidget.cpp
Expand Up @@ -171,11 +171,17 @@ void QgsBrowserDockWidget::itemDoubleClicked( const QModelIndex &index )
if ( !item ) if ( !item )
return; return;


if ( item->handleDoubleClick() ) QgsDataItemGuiContext context = createContext();
return;
else if ( addLayerAtIndex( index ) ) // default double-click handler const QList< QgsDataItemGuiProvider * > providers = QgsGui::instance()->dataItemGuiProviderRegistry()->providers();
return; for ( QgsDataItemGuiProvider *provider : providers )
else {
if ( provider->handleDoubleClick( item, context ) )
return;
}

// if no providers overrode the double click handling for this item, we give the item itself a chance
if ( !item->handleDoubleClick() )
{ {
// double-click not handled by browser model, so use as default view expand behavior // double-click not handled by browser model, so use as default view expand behavior
if ( mBrowserView->isExpanded( index ) ) if ( mBrowserView->isExpanded( index ) )
Expand Down Expand Up @@ -223,8 +229,7 @@ void QgsBrowserDockWidget::showContextMenu( QPoint pt )
menu->addActions( actions ); menu->addActions( actions );
} }


QgsDataItemGuiContext context; QgsDataItemGuiContext context = createContext();
context.setMessageBar( mMessageBar );


const QList< QgsDataItemGuiProvider * > providers = QgsGui::instance()->dataItemGuiProviderRegistry()->providers(); const QList< QgsDataItemGuiProvider * > providers = QgsGui::instance()->dataItemGuiProviderRegistry()->providers();
for ( QgsDataItemGuiProvider *provider : providers ) for ( QgsDataItemGuiProvider *provider : providers )
Expand Down Expand Up @@ -519,6 +524,13 @@ int QgsBrowserDockWidget::selectedItemsCount()
return 0; return 0;
} }


QgsDataItemGuiContext QgsBrowserDockWidget::createContext()
{
QgsDataItemGuiContext context;
context.setMessageBar( mMessageBar );
return context;
}

void QgsBrowserDockWidget::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected ) void QgsBrowserDockWidget::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
{ {
Q_UNUSED( selected ); Q_UNUSED( selected );
Expand Down
8 changes: 7 additions & 1 deletion src/gui/qgsbrowserdockwidget.h
Expand Up @@ -34,6 +34,7 @@ class QgsLayerItem;
class QgsDataItem; class QgsDataItem;
class QgsBrowserProxyModel; class QgsBrowserProxyModel;
class QgsMessageBar; class QgsMessageBar;
class QgsDataItemGuiContext;


/** /**
* \ingroup gui * \ingroup gui
Expand Down Expand Up @@ -87,8 +88,11 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
* *
* Returns true if the index was successfully intrepreted as a map layer and loaded, or * Returns true if the index was successfully intrepreted as a map layer and loaded, or
* false if the index is not a map layer or could not be loaded. * false if the index is not a map layer or could not be loaded.
*
* \deprecated will be removed in QGIS 4.0 - retrieve the QgsLayerItem itself
* and manually add to project.
*/ */
bool addLayerAtIndex( const QModelIndex &index ); Q_DECL_DEPRECATED bool addLayerAtIndex( const QModelIndex &index ) SIP_DEPRECATED;


//! Show context menu //! Show context menu
void showContextMenu( QPoint ); void showContextMenu( QPoint );
Expand Down Expand Up @@ -177,6 +181,8 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
//! Settings prefix (the object name) //! Settings prefix (the object name)
QString settingsSection() { return objectName().toLower(); } QString settingsSection() { return objectName().toLower(); }


QgsDataItemGuiContext createContext();

QgsDockBrowserTreeView *mBrowserView = nullptr; QgsDockBrowserTreeView *mBrowserView = nullptr;
QgsBrowserModel *mModel = nullptr; QgsBrowserModel *mModel = nullptr;
QgsBrowserProxyModel *mProxyModel = nullptr; QgsBrowserProxyModel *mProxyModel = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsdataitemguiprovider.cpp
Expand Up @@ -38,3 +38,8 @@ void QgsDataItemGuiProvider::populateContextMenu( QgsDataItem *, QMenu *, const
{ {


} }

bool QgsDataItemGuiProvider::handleDoubleClick( QgsDataItem *, QgsDataItemGuiContext )
{
return false;
}
7 changes: 7 additions & 0 deletions src/gui/qgsdataitemguiprovider.h
Expand Up @@ -113,6 +113,13 @@ class GUI_EXPORT QgsDataItemGuiProvider
virtual void populateContextMenu( QgsDataItem *item, QMenu *menu, virtual void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ); const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context );


/**
* Called when a user double clicks on an \a item. Providers should return true
* if the double-click was handled and do not want other providers to handle the
* double-click, and to prevent the default double-click behavior for items.
*/
virtual bool handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext context );

}; };


#endif // QGSDATAITEMGUIPROVIDER_H #endif // QGSDATAITEMGUIPROVIDER_H

0 comments on commit fa3a08e

Please sign in to comment.