Add method to handle browser item double clicks to QgsDataItemGuiProvider

And move handling of layer/project file double clicks in browser dock
from gui->app
This commit is contained in:
Nyall Dawson 2018-11-02 07:34:49 +10:00
parent 40443ebb3b
commit fa3a08e397
10 changed files with 121 additions and 11 deletions

View File

@ -61,12 +61,15 @@ Returns the message bar associated with the dock.
public slots:
bool addLayerAtIndex( const QModelIndex &index );
bool addLayerAtIndex( const QModelIndex &index ) /Deprecated/;
%Docstring
Adds the layer corresponding to the specified model ``index``.
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.
.. deprecated:: will be removed in QGIS 4.0 - retrieve the QgsLayerItem itself
and manually add to project.
%End
void showContextMenu( QPoint );

View File

@ -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.
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
};

View File

@ -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 )
{
QgsTemporaryCursorOverride cursor( Qt::WaitCursor );
@ -394,7 +411,7 @@ void QgsLayerItemGuiProvider::addLayersFromItems( const QList<QgsDataItem *> &it
if ( item && item->type() == QgsDataItem::Project )
{
if ( const QgsProjectItem *projectItem = qobject_cast<const QgsProjectItem *>( item ) )
QgisApp::instance()->openFile( projectItem->path(), QStringLiteral( "project" ) );
QgisApp::instance()->openProject( projectItem->path() );
return;
}
@ -426,3 +443,39 @@ void QgsLayerItemGuiProvider::showPropertiesForItem( QgsLayerItem *item )
dialog->setItem( item );
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;
}
}

View File

@ -94,6 +94,7 @@ class QgsLayerItemGuiProvider : public QObject, public QgsDataItemGuiProvider
void populateContextMenu( QgsDataItem *item, QMenu *menu,
const QList<QgsDataItem *> &selectedItems, QgsDataItemGuiContext context ) override;
bool handleDoubleClick( QgsDataItem *item, QgsDataItemGuiContext context ) override;
private:
@ -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

View File

@ -1027,6 +1027,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsAppDirectoryItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsProjectHomeItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsProjectItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsFavoritesItemGuiProvider() );
QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsLayerItemGuiProvider() );

View File

@ -204,7 +204,6 @@ class APP_EXPORT QgsProjectRootDataItem : public QgsProjectItem
*/
QgsProjectRootDataItem( QgsDataItem *parent, const QString &path );
QVector<QgsDataItem *> createChildren() override;
};
/**

View File

@ -171,11 +171,17 @@ void QgsBrowserDockWidget::itemDoubleClicked( const QModelIndex &index )
if ( !item )
return;
if ( item->handleDoubleClick() )
return;
else if ( addLayerAtIndex( index ) ) // default double-click handler
return;
else
QgsDataItemGuiContext context = createContext();
const QList< QgsDataItemGuiProvider * > providers = QgsGui::instance()->dataItemGuiProviderRegistry()->providers();
for ( QgsDataItemGuiProvider *provider : providers )
{
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
if ( mBrowserView->isExpanded( index ) )
@ -223,8 +229,7 @@ void QgsBrowserDockWidget::showContextMenu( QPoint pt )
menu->addActions( actions );
}
QgsDataItemGuiContext context;
context.setMessageBar( mMessageBar );
QgsDataItemGuiContext context = createContext();
const QList< QgsDataItemGuiProvider * > providers = QgsGui::instance()->dataItemGuiProviderRegistry()->providers();
for ( QgsDataItemGuiProvider *provider : providers )
@ -519,6 +524,13 @@ int QgsBrowserDockWidget::selectedItemsCount()
return 0;
}
QgsDataItemGuiContext QgsBrowserDockWidget::createContext()
{
QgsDataItemGuiContext context;
context.setMessageBar( mMessageBar );
return context;
}
void QgsBrowserDockWidget::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
{
Q_UNUSED( selected );

View File

@ -34,6 +34,7 @@ class QgsLayerItem;
class QgsDataItem;
class QgsBrowserProxyModel;
class QgsMessageBar;
class QgsDataItemGuiContext;
/**
* \ingroup gui
@ -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
* 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
void showContextMenu( QPoint );
@ -177,6 +181,8 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
//! Settings prefix (the object name)
QString settingsSection() { return objectName().toLower(); }
QgsDataItemGuiContext createContext();
QgsDockBrowserTreeView *mBrowserView = nullptr;
QgsBrowserModel *mModel = nullptr;
QgsBrowserProxyModel *mProxyModel = nullptr;

View File

@ -38,3 +38,8 @@ void QgsDataItemGuiProvider::populateContextMenu( QgsDataItem *, QMenu *, const
{
}
bool QgsDataItemGuiProvider::handleDoubleClick( QgsDataItem *, QgsDataItemGuiContext )
{
return false;
}

View File

@ -113,6 +113,13 @@ class GUI_EXPORT QgsDataItemGuiProvider
virtual void populateContextMenu( QgsDataItem *item, QMenu *menu,
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