Skip to content

Commit

Permalink
Allow QgsDataItems to override default double click behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 20, 2017
1 parent fcf2fca commit 8bce7a6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
9 changes: 9 additions & 0 deletions python/core/qgsdataitem.sip
Expand Up @@ -153,6 +153,15 @@ Create new data item.
:rtype: bool
%End

virtual bool handleDoubleClick();
%Docstring
Called when a user double clicks on the item. Subclasses should return true
if they have implemented a double click handler and do not want the default
double click behaviour for items.
.. versionadded:: 3.0
:rtype: bool
%End

virtual bool hasDragEnabled() const;
%Docstring
Returns true if the item may be dragged.
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgsdataitem.cpp
Expand Up @@ -510,6 +510,11 @@ bool QgsDataItem::equal( const QgsDataItem *other )
mPath == other->path() );
}

bool QgsDataItem::handleDoubleClick()
{
return false;
}

QgsDataItem::State QgsDataItem::state() const
{
return mState;
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -155,6 +155,14 @@ class CORE_EXPORT QgsDataItem : public QObject
*/
virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }

/**
* Called when a user double clicks on the item. Subclasses should return true
* if they have implemented a double click handler and do not want the default
* double click behaviour for items.
* \since QGIS 3.0
*/
virtual bool handleDoubleClick();

/** Returns true if the item may be dragged.
* Default implementation returns false.
* A draggable item has to implement mimeUri() that will be used to pass data.
Expand Down
14 changes: 13 additions & 1 deletion src/gui/qgsbrowserdockwidget.cpp
Expand Up @@ -97,7 +97,7 @@ QgsBrowserDockWidget::QgsBrowserDockWidget( const QString &name, QgsBrowserModel
connect( mLeFilter, &QgsFilterLineEdit::textChanged, this, &QgsBrowserDockWidget::setFilter );
connect( group, &QActionGroup::triggered, this, &QgsBrowserDockWidget::setFilterSyntax );
connect( mBrowserView, &QgsDockBrowserTreeView::customContextMenuRequested, this, &QgsBrowserDockWidget::showContextMenu );
connect( mBrowserView, &QgsDockBrowserTreeView::doubleClicked, this, &QgsBrowserDockWidget::addLayerAtIndex );
connect( mBrowserView, &QgsDockBrowserTreeView::doubleClicked, this, &QgsBrowserDockWidget::itemDoubleClicked );
connect( mSplitter, &QSplitter::splitterMoved, this, &QgsBrowserDockWidget::splitterMoved );
}

Expand Down Expand Up @@ -155,6 +155,18 @@ void QgsBrowserDockWidget::showEvent( QShowEvent *e )
QgsDockWidget::showEvent( e );
}

void QgsBrowserDockWidget::itemDoubleClicked( const QModelIndex &index )
{
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );
if ( !item )
return;

if ( item->handleDoubleClick() )
return;
else
addLayerAtIndex( index ); // default double click handler
}

void QgsBrowserDockWidget::showContextMenu( QPoint pt )
{
QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsbrowserdockwidget.h
Expand Up @@ -110,6 +110,9 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro
//! Show event override
void showEvent( QShowEvent *event ) override;

private slots:
void itemDoubleClicked( const QModelIndex &index );

private:
//! Refresh the model
void refreshModel( const QModelIndex &index );
Expand Down

0 comments on commit 8bce7a6

Please sign in to comment.