mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
[FEATURE] Add/remove favourite dirs in browser dock
This commit is contained in:
parent
5345f520a9
commit
a8a90fef55
@ -1,6 +1,8 @@
|
||||
#include "qgsbrowserdockwidget.h"
|
||||
|
||||
#include <QTreeView>
|
||||
#include <QMenu>
|
||||
#include <QSettings>
|
||||
|
||||
#include "qgsbrowsermodel.h"
|
||||
#include "qgsdataitem.h"
|
||||
@ -18,8 +20,10 @@ QgsBrowserDockWidget::QgsBrowserDockWidget( QWidget * parent ) :
|
||||
mBrowserView->setDragEnabled( true );
|
||||
mBrowserView->setDragDropMode( QTreeView::DragOnly );
|
||||
mBrowserView->setSelectionMode( QAbstractItemView::ExtendedSelection );
|
||||
mBrowserView->setContextMenuPolicy( Qt::CustomContextMenu );
|
||||
setWidget( mBrowserView );
|
||||
|
||||
connect( mBrowserView, SIGNAL( customContextMenuRequested( const QPoint & ) ), this, SLOT( showContextMenu( const QPoint & ) ) );
|
||||
//connect( mBrowserView, SIGNAL( clicked( const QModelIndex& ) ), this, SLOT( itemClicked( const QModelIndex& ) ) );
|
||||
connect( mBrowserView, SIGNAL( doubleClicked( const QModelIndex& ) ), this, SLOT( itemClicked( const QModelIndex& ) ) );
|
||||
}
|
||||
@ -101,3 +105,77 @@ void QgsBrowserDockWidget::itemClicked( const QModelIndex& index )
|
||||
// add layer to the application
|
||||
QgsMapLayerRegistry::instance()->addMapLayer( layer );
|
||||
}
|
||||
|
||||
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() ) );
|
||||
}
|
||||
}
|
||||
|
||||
if ( menu->actions().count() == 0 )
|
||||
{
|
||||
delete menu;
|
||||
return;
|
||||
}
|
||||
|
||||
menu->popup( mBrowserView->mapToGlobal( pt ) );
|
||||
}
|
||||
|
||||
void QgsBrowserDockWidget::addFavourite()
|
||||
{
|
||||
QgsDataItem* item = mModel->dataItem( mBrowserView->currentIndex() );
|
||||
if ( !item )
|
||||
return;
|
||||
if ( item->type() != QgsDataItem::Directory )
|
||||
return;
|
||||
|
||||
QString newFavDir = item->path();
|
||||
|
||||
QSettings settings;
|
||||
QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
|
||||
favDirs.append( newFavDir );
|
||||
settings.setValue( "/browser/favourites", favDirs );
|
||||
|
||||
// reload the browser model so that the newly added favourite directory is shown
|
||||
mModel->reload();
|
||||
}
|
||||
|
||||
void QgsBrowserDockWidget::removeFavourite()
|
||||
{
|
||||
QgsDataItem* item = mModel->dataItem( mBrowserView->currentIndex() );
|
||||
if ( !item )
|
||||
return;
|
||||
if ( item->type() != QgsDataItem::Directory )
|
||||
return;
|
||||
|
||||
QString favDir = item->path();
|
||||
|
||||
QSettings settings;
|
||||
QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
|
||||
favDirs.removeAll( favDir );
|
||||
settings.setValue( "/browser/favourites", favDirs );
|
||||
|
||||
// reload the browser model so that the favourite directory is not shown anymore
|
||||
mModel->reload();
|
||||
}
|
||||
|
@ -17,6 +17,10 @@ class QgsBrowserDockWidget : public QDockWidget
|
||||
|
||||
public slots:
|
||||
void itemClicked( const QModelIndex& index );
|
||||
void showContextMenu( const QPoint & );
|
||||
|
||||
void addFavourite();
|
||||
void removeFavourite();
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -10,11 +10,21 @@
|
||||
|
||||
#include "qgsbrowsermodel.h"
|
||||
|
||||
#include <QSettings>
|
||||
|
||||
QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
|
||||
QAbstractItemModel( parent )
|
||||
{
|
||||
addRootItems();
|
||||
}
|
||||
|
||||
QgsBrowserModel::~QgsBrowserModel()
|
||||
{
|
||||
removeRootItems();
|
||||
}
|
||||
|
||||
void QgsBrowserModel::addRootItems()
|
||||
{
|
||||
// give the home directory a prominent first place
|
||||
QgsDirectoryItem *item = new QgsDirectoryItem( NULL, tr( "Home" ), QDir::homePath() );
|
||||
QStyle *style = QApplication::style();
|
||||
@ -23,6 +33,16 @@ QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
|
||||
connectItem( item );
|
||||
mRootItems << item;
|
||||
|
||||
// add favourite directories
|
||||
QSettings settings;
|
||||
QStringList favDirs = settings.value( "/browser/favourites", QVariant() ).toStringList();
|
||||
foreach( QString favDir, favDirs )
|
||||
{
|
||||
QgsDirectoryItem *item = new QgsDirectoryItem( NULL, favDir, favDir );
|
||||
connectItem( item );
|
||||
mRootItems << item;
|
||||
}
|
||||
|
||||
foreach( QFileInfo drive, QDir::drives() )
|
||||
{
|
||||
QString path = drive.absolutePath();
|
||||
@ -70,12 +90,14 @@ QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
|
||||
}
|
||||
}
|
||||
|
||||
QgsBrowserModel::~QgsBrowserModel()
|
||||
void QgsBrowserModel::removeRootItems()
|
||||
{
|
||||
foreach( QgsDataItem* item, mRootItems )
|
||||
{
|
||||
delete item;
|
||||
}
|
||||
|
||||
mRootItems.clear();
|
||||
}
|
||||
|
||||
|
||||
@ -201,6 +223,13 @@ QModelIndex QgsBrowserModel::findPath( QString path )
|
||||
return QModelIndex(); // not found
|
||||
}
|
||||
|
||||
void QgsBrowserModel::reload()
|
||||
{
|
||||
removeRootItems();
|
||||
addRootItems();
|
||||
reset(); // Qt4.6 brings better methods beginResetModel + endResetModel
|
||||
}
|
||||
|
||||
/* Refresh dir path */
|
||||
void QgsBrowserModel::refresh( QString path )
|
||||
{
|
||||
|
@ -56,6 +56,9 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
|
||||
|
||||
bool hasChildren( const QModelIndex &parent = QModelIndex() ) const;
|
||||
|
||||
// Reload the whole model
|
||||
void reload();
|
||||
|
||||
// Refresh item specified by path
|
||||
void refresh( QString path );
|
||||
|
||||
@ -80,6 +83,11 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
|
||||
void endRemoveItems();
|
||||
|
||||
protected:
|
||||
|
||||
// populates the model
|
||||
void addRootItems();
|
||||
void removeRootItems();
|
||||
|
||||
QVector<QgsDataItem*> mRootItems;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user