void QgsBrowserDockWidget::showContextMenu( QPoint pt )
{
  QModelIndex index = mProxyModel->mapToSource( mBrowserView->indexAt( pt ) );
  QgsDataItem *item = mModel->dataItem( index );
  if ( !item )
    return;

  QMenu *menu = new QMenu( this );

  if ( item->type() == QgsDataItem::Directory )
  {
    QgsSettings settings;
    QStringList favDirs = settings.value( QStringLiteral( "browser/favourites" ) ).toStringList();
    bool inFavDirs = item->parent() && item->parent()->type() == QgsDataItem::Favorites;

    if ( item->parent() && !inFavDirs )
    {
      // only non-root directories can be added as favorites
      menu->addAction( tr( "Add as a Favorite" ), this, SLOT( addFavorite() ) );
    }
    else if ( inFavDirs )
    {
      // only favorites can be removed
      menu->addAction( tr( "Remove Favorite" ), this, SLOT( removeFavorite() ) );
    }
    menu->addAction( tr( "Properties..." ), this, SLOT( showProperties() ) );
    menu->addAction( tr( "Hide from Browser" ), this, SLOT( hideItem() ) );
    QAction *action = menu->addAction( tr( "Fast Scan this Directory" ), this, SLOT( toggleFastScan() ) );
    action->setCheckable( true );
    action->setChecked( settings.value( QStringLiteral( "qgis/scanItemsFastScanUris" ),
                                        QStringList() ).toStringList().contains( item->path() ) );
  }
  else if ( item->type() == QgsDataItem::Layer )
  {
    menu->addAction( tr( "Add Selected Layer(s)" ), this, SLOT( addSelectedLayers() ) );
    menu->addAction( tr( "Properties..." ), this, SLOT( showProperties() ) );
  }
  else if ( item->type() == QgsDataItem::Favorites )
  {
    menu->addAction( tr( "Add a Directory..." ), this, SLOT( addFavoriteDirectory() ) );
  }

  QList<QAction *> actions = item->actions();
  if ( !actions.isEmpty() )
  {
    if ( !menu->actions().isEmpty() )
      menu->addSeparator();
    // add action to the menu
    menu->addActions( actions );
  }

  if ( menu->actions().isEmpty() )
  {
    delete menu;
    return;
  }

  menu->popup( mBrowserView->mapToGlobal( pt ) );
}
void QgsBrowserDockWidget::showContextMenu( const QPoint & pt )
{
  QModelIndex idx = mBrowserView->indexAt( pt );
  QgsDataItem* item = mModel->dataItem( idx );
  if ( !item )
    return;

  QMenu *menu = new QMenu( this );

  if ( item->type() == QgsDataItem::Directory )
  {
    QSettings settings;
    QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
    bool inFavDirs = favDirs.contains( item->path() );

    if ( item->parent() != NULL && !inFavDirs )
    {
      // only non-root directories can be added as favourites
      menu->addAction( tr( "Add as a favourite" ), this, SLOT( addFavourite() ) );
    }
    else if ( inFavDirs )
    {
      // only favourites can be removed
      menu->addAction( tr( "Remove favourite" ), this, SLOT( removeFavourite() ) );
    }

  }
  else if ( item->type() == QgsDataItem::Layer )
  {
    menu->addAction( tr( "Add Layer" ), this, SLOT( addCurrentLayer( ) ) );
    menu->addAction( tr( "Add Selected Layers" ), this, SLOT( addSelectedLayers() ) );
    menu->addAction( tr( "Properties" ), this, SLOT( showProperties( ) ) );

  }
  else if ( item->type() == QgsDataItem::Favourites )
  {
    menu->addAction( tr( "Add a directory" ), this, SLOT( addFavouriteDirectory() ) );

  }

  QList<QAction*> actions = item->actions();
  if ( !actions.isEmpty() )
  {
    if ( !menu->actions().isEmpty() )
      menu->addSeparator();
    // add action to the menu
    menu->addActions( actions );
  }

  if ( menu->actions().count() == 0 )
  {
    delete menu;
    return;
  }

  menu->popup( mBrowserView->mapToGlobal( pt ) );
}
QgsBrowserDockWidget::QgsBrowserDockWidget( QWidget * parent ) :
    QDockWidget( parent ), mModel( NULL )
{
  setWindowTitle( tr( "Browser" ) );

  mBrowserView = new QgsBrowserTreeView( this );

  QToolButton* refreshButton = new QToolButton( this );
  refreshButton->setIcon( QgsApplication::getThemeIcon( "mActionDraw.png" ) );
  // remove this to save space
  refreshButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
  refreshButton->setText( tr( "Refresh" ) );
  refreshButton->setToolTip( tr( "Refresh" ) );
  refreshButton->setAutoRaise( true );
  connect( refreshButton, SIGNAL( clicked() ), this, SLOT( refresh() ) );

  QToolButton* addLayersButton = new QToolButton( this );
  addLayersButton->setIcon( QgsApplication::getThemeIcon( "mActionAddLayer.png" ) );
  // remove this to save space
  addLayersButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
  addLayersButton->setText( tr( "Add Selection" ) );
  addLayersButton->setToolTip( tr( "Add Selected Layers" ) );
  addLayersButton->setAutoRaise( true );
  connect( addLayersButton, SIGNAL( clicked() ), this, SLOT( addSelectedLayers() ) );

  QToolButton* collapseButton = new QToolButton( this );
  collapseButton->setIcon( QgsApplication::getThemeIcon( "mActionCollapseTree.png" ) );
  collapseButton->setToolTip( tr( "Collapse All" ) );
  collapseButton->setAutoRaise( true );
  connect( collapseButton, SIGNAL( clicked() ), mBrowserView, SLOT( collapseAll() ) );

  QVBoxLayout* layout = new QVBoxLayout();
  QHBoxLayout* hlayout = new QHBoxLayout();
  layout->setContentsMargins( 0, 0, 0, 0 );
  layout->setSpacing( 0 );
  hlayout->setContentsMargins( 0, 0, 0, 0 );
  hlayout->setSpacing( 5 );
  hlayout->setAlignment( Qt::AlignLeft );

  hlayout->addSpacing( 5 );
  hlayout->addWidget( refreshButton );
  hlayout->addSpacing( 5 );
  hlayout->addWidget( addLayersButton );
  hlayout->addStretch( );
  hlayout->addWidget( collapseButton );
  layout->addLayout( hlayout );
  layout->addWidget( mBrowserView );

  QWidget* innerWidget = new QWidget( this );
  innerWidget->setLayout( layout );
  setWidget( innerWidget );

  connect( mBrowserView, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( showContextMenu( const QPoint & ) ) );
  connect( mBrowserView, SIGNAL( doubleClicked( const QModelIndex& ) ), this, SLOT( addLayerAtIndex( const QModelIndex& ) ) );

}