Skip to content

Commit

Permalink
Move some browser directory action handling from core/gui to app
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 4, 2018
1 parent c9f9374 commit 48e7e20
Show file tree
Hide file tree
Showing 15 changed files with 344 additions and 130 deletions.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsbrowsermodel.sip.in
Expand Up @@ -184,6 +184,7 @@ Removes a favorite directory from its corresponding model index.
.. versionadded:: 3.0
%End


void updateProjectHome();

void hidePath( QgsDataItem *item );
Expand Down
4 changes: 0 additions & 4 deletions python/core/auto_generated/qgsdataitem.sip.in
Expand Up @@ -597,10 +597,6 @@ Constructor.
Check if the given path is hidden from the browser model
%End

virtual QList<QAction *> actions( QWidget *parent );



public slots:
virtual void childrenCreated();

Expand Down
20 changes: 15 additions & 5 deletions python/gui/auto_generated/qgsbrowserdockwidget.sip.in
Expand Up @@ -71,17 +71,23 @@ false if the index is not a map layer or could not be loaded.
Show context menu
%End

void addFavorite();
void addFavorite() /Deprecated/;
%Docstring
Add current item to favorite
Add current item to favorite.

.. deprecated:: will be removed in QGIS 4.0 - use the methods in QgsBrowserModel instead
%End

void addFavoriteDirectory();
%Docstring
Add directory from file dialog to favorite
%End
void removeFavorite();

void removeFavorite() /Deprecated/;
%Docstring
Remove from favorite
Remove from favorite.

.. deprecated:: will be removed in QGIS 4.0 - use the methods in QgsBrowserModel instead
%End

void refresh();
Expand Down Expand Up @@ -126,11 +132,15 @@ Show the layer properties
%Docstring
Hide current item
%End
void toggleFastScan();

void toggleFastScan() /Deprecated/;
%Docstring
Toggle fast scan

.. deprecated:: will be removed in QGIS 4.0
%End


void selectionChanged( const QItemSelection &selected, const QItemSelection &deselected );
%Docstring
Selection has changed
Expand Down
4 changes: 4 additions & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -173,6 +173,8 @@ SET(QGIS_APP_SRCS
qgsmaptoolregularpolygoncenterpoint.cpp
qgsmaptoolregularpolygoncentercorner.cpp

browser/qgsinbuiltdataitemproviders.cpp

layout/qgslayoutaddpagesdialog.cpp
layout/qgslayoutapputils.cpp
layout/qgslayoutatlaswidget.cpp
Expand Down Expand Up @@ -398,6 +400,8 @@ SET (QGIS_APP_MOC_HDRS
qgsmapthemes.h
qgshandlebadlayers.h

browser/qgsinbuiltdataitemproviders.h

layout/qgslayoutaddpagesdialog.h
layout/qgslayoutappmenuprovider.h
layout/qgslayoutatlaswidget.h
Expand Down
216 changes: 216 additions & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
@@ -0,0 +1,216 @@
/***************************************************************************
qgsinbuiltdataitemproviders.cpp
----------------------------
begin : October 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgsinbuiltdataitemproviders.h"
#include "qgsdataitem.h"
#include "qgssettings.h"
#include "qgsgui.h"
#include "qgsnative.h"
#include "qgisapp.h"
#include "qgsnewnamedialog.h"
#include "qgsbrowsermodel.h"
#include "qgsbrowserdockwidget_p.h"
#include <QMenu>
#include <QInputDialog>
#include <QMessageBox>
#include <QDesktopServices>

QgsAppDirectoryItemGuiProvider::QgsAppDirectoryItemGuiProvider()
{

}

QString QgsAppDirectoryItemGuiProvider::name()
{
return QStringLiteral( "directory_items" );
}

void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
{
if ( item->type() != QgsDataItem::Directory )
return;

QgsDirectoryItem *directoryItem = qobject_cast< QgsDirectoryItem * >( item );

QgsSettings settings;


QAction *createFolder = new QAction( tr( "New Directory…" ), menu );
connect( createFolder, &QAction::triggered, this, [ = ]
{
bool ok = false;

const QString name = QInputDialog::getText( QgisApp::instance(), tr( "Create Directory" ), tr( "Directory name" ), QLineEdit::Normal, QString(), &ok );
if ( ok && !name.isEmpty() )
{
QDir dir( directoryItem->dirPath() );
if ( QFileInfo::exists( dir.absoluteFilePath( name ) ) )
{
QMessageBox::critical( QgisApp::instance(), tr( "Create Directory" ), tr( "The path “%1” already exists." ).arg( QDir::toNativeSeparators( dir.absoluteFilePath( name ) ) ) );
}
else if ( !dir.mkdir( name ) )
{
QMessageBox::critical( QgisApp::instance(), tr( "Create Directory" ), tr( "Could not create directory “%1”." ).arg( QDir::toNativeSeparators( dir.absoluteFilePath( name ) ) ) );
}
else
{
directoryItem->refresh();
}
}
} );
menu->addAction( createFolder );

menu->addSeparator();

bool inFavDirs = item->parent() && item->parent()->type() == QgsDataItem::Favorites;
if ( item->parent() && !inFavDirs )
{
// only non-root directories can be added as favorites
QAction *addAsFavorite = new QAction( tr( "Add as a Favorite" ), menu );
addAsFavorite->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconFavourites.svg" ) ) );
menu->addAction( addAsFavorite );
connect( addAsFavorite, &QAction::triggered, this, [ = ]
{
addFavorite( directoryItem );
} );
}
else if ( inFavDirs )
{
if ( QgsFavoriteItem *favoriteItem = qobject_cast< QgsFavoriteItem * >( item ) )
{
QAction *actionRename = new QAction( tr( "Rename Favorite…" ), menu );
connect( actionRename, &QAction::triggered, this, [ = ]
{
renameFavorite( favoriteItem );
} );
menu->addAction( actionRename );
menu->addSeparator();
QAction *removeFavoriteAction = new QAction( tr( "Remove Favorite" ), menu );
connect( removeFavoriteAction, &QAction::triggered, this, [ = ]
{
removeFavorite( favoriteItem );
} );
menu->addAction( removeFavoriteAction );
menu->addSeparator();
}
}
QAction *hideAction = new QAction( tr( "Hide from Browser" ), menu );
connect( hideAction, &QAction::triggered, this, [ = ]
{
hideDirectory( directoryItem );
} );
menu->addAction( hideAction );

QAction *fastScanAction = new QAction( tr( "Fast Scan this Directory" ), menu );
connect( fastScanAction, &QAction::triggered, this, [ = ]
{
toggleFastScan( directoryItem );
} );
menu->addAction( fastScanAction );
fastScanAction->setCheckable( true );
fastScanAction->setChecked( settings.value( QStringLiteral( "qgis/scanItemsFastScanUris" ),
QStringList() ).toStringList().contains( item->path() ) );

menu->addSeparator();

QAction *openFolder = new QAction( tr( "Open Directory…" ), menu );
connect( openFolder, &QAction::triggered, this, [ = ]
{
QDesktopServices::openUrl( QUrl::fromLocalFile( directoryItem->dirPath() ) );
} );
menu->addAction( openFolder );

QAction *propertiesAction = new QAction( tr( "Properties…" ), menu );
connect( propertiesAction, &QAction::triggered, this, [ = ]
{
showProperties( directoryItem );
} );
menu->addAction( propertiesAction );

if ( QgsGui::nativePlatformInterface()->capabilities() & QgsNative::NativeFilePropertiesDialog )
{
if ( QgsDirectoryItem *dirItem = qobject_cast< QgsDirectoryItem * >( item ) )
{
QAction *action = menu->addAction( tr( "Directory Properties…" ) );
connect( action, &QAction::triggered, dirItem, [ dirItem ]
{
QgsGui::nativePlatformInterface()->showFileProperties( dirItem->dirPath() );
} );
}
}
}

void QgsAppDirectoryItemGuiProvider::addFavorite( QgsDirectoryItem *item )
{
if ( !item )
return;

QgisApp::instance()->browserModel()->addFavoriteDirectory( item->dirPath() );
}

void QgsAppDirectoryItemGuiProvider::removeFavorite( QgsFavoriteItem *favorite )
{
QgisApp::instance()->browserModel()->removeFavorite( favorite );
}

void QgsAppDirectoryItemGuiProvider::renameFavorite( QgsFavoriteItem *favorite )
{
QgsNewNameDialog dlg( tr( "favorite “%1”" ).arg( favorite->name() ), favorite->name() );
dlg.setWindowTitle( tr( "Rename Favorite" ) );
if ( dlg.exec() != QDialog::Accepted || dlg.name() == favorite->name() )
return;

favorite->rename( dlg.name() );
}

void QgsAppDirectoryItemGuiProvider::hideDirectory( QgsDirectoryItem *item )
{
if ( ! item )
return;

QgisApp::instance()->browserModel()->hidePath( item );
}

void QgsAppDirectoryItemGuiProvider::toggleFastScan( QgsDirectoryItem *item )
{
QgsSettings settings;
QStringList fastScanDirs = settings.value( QStringLiteral( "qgis/scanItemsFastScanUris" ),
QStringList() ).toStringList();
int idx = fastScanDirs.indexOf( item->path() );
if ( idx != -1 )
{
fastScanDirs.removeAt( idx );
}
else
{
fastScanDirs << item->path();
}
settings.setValue( QStringLiteral( "qgis/scanItemsFastScanUris" ), fastScanDirs );
}

void QgsAppDirectoryItemGuiProvider::showProperties( QgsDirectoryItem *item )
{
if ( ! item )
return;

QgsBrowserPropertiesDialog *dialog = new QgsBrowserPropertiesDialog( QStringLiteral( "browser" ), QgisApp::instance() );
dialog->setAttribute( Qt::WA_DeleteOnClose );

dialog->setItem( item );
dialog->show();
}
55 changes: 55 additions & 0 deletions src/app/browser/qgsinbuiltdataitemproviders.h
@@ -0,0 +1,55 @@
/***************************************************************************
qgsinbuiltdataitemproviders.h
--------------------------
begin : October 2018
copyright : (C) 2018 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSINBUILTDATAITEMPROVIDERS_H
#define QGSINBUILTDATAITEMPROVIDERS_H

#include "qgis_app.h"
#include "qgsdataitemguiprovider.h"
#include <QObject>

class QgsDirectoryItem;
class QgsFavoriteItem;

class QgsAppDirectoryItemGuiProvider : public QObject, public QgsDataItemGuiProvider
{
Q_OBJECT

public:

QgsAppDirectoryItemGuiProvider();

QString name() override;

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

private:

void addFavorite( QgsDirectoryItem *item );
void removeFavorite( QgsFavoriteItem *favorite );
void renameFavorite( QgsFavoriteItem *favorite );
void hideDirectory( QgsDirectoryItem *item );
void toggleFastScan( QgsDirectoryItem *item );
void showProperties( QgsDirectoryItem *item );
};



#endif // QGSINBUILTDATAITEMPROVIDERS_H


10 changes: 10 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -167,6 +167,7 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgscustomlayerorderwidget.h"
#include "qgscustomprojectiondialog.h"
#include "qgsdataitemproviderregistry.h"
#include "qgsdataitemguiproviderregistry.h"
#include "qgsdatasourceuri.h"
#include "qgsdatumtransformdialog.h"
#include "qgsdoublespinbox.h"
Expand Down Expand Up @@ -329,6 +330,8 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgsuserprofilemanager.h"
#include "qgsuserprofile.h"

#include "browser/qgsinbuiltdataitemproviders.h"

#include "qgssublayersdialog.h"
#include "ogr/qgsvectorlayersaveasdialog.h"

Expand Down Expand Up @@ -1022,6 +1025,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
mBrowserWidget->setObjectName( QStringLiteral( "Browser" ) );
mBrowserWidget->setMessageBar( mInfoBar );

QgsGui::instance()->dataItemGuiProviderRegistry()->addProvider( new QgsAppDirectoryItemGuiProvider() );

QShortcut *showBrowserDock = new QShortcut( QKeySequence( tr( "Ctrl+2" ) ), this );
connect( showBrowserDock, &QShortcut::activated, mBrowserWidget, &QgsDockWidget::toggleUserVisible );
showBrowserDock->setObjectName( QStringLiteral( "ShowBrowserPanel" ) );
Expand Down Expand Up @@ -1865,6 +1870,11 @@ void QgisApp::dataSourceManager( const QString &pageName )
}
}

QgsBrowserModel *QgisApp::browserModel()
{
return mBrowserModel;
}

QgisAppStyleSheet *QgisApp::styleSheetBuilder()
{
Q_ASSERT( mStyleSheetBuilder );
Expand Down

0 comments on commit 48e7e20

Please sign in to comment.