Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move Project Home GUI action handling from core to app
And remove more QWidget imports from core library
  • Loading branch information
nyalldawson committed Nov 4, 2018
1 parent 48e7e20 commit fab4154
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 37 deletions.
42 changes: 37 additions & 5 deletions src/app/browser/qgsinbuiltdataitemproviders.cpp
Expand Up @@ -29,11 +29,7 @@
#include <QInputDialog>
#include <QMessageBox>
#include <QDesktopServices>

QgsAppDirectoryItemGuiProvider::QgsAppDirectoryItemGuiProvider()
{

}
#include <QFileDialog>

QString QgsAppDirectoryItemGuiProvider::name()
{
Expand Down Expand Up @@ -214,3 +210,39 @@ void QgsAppDirectoryItemGuiProvider::showProperties( QgsDirectoryItem *item )
dialog->setItem( item );
dialog->show();
}


//
// QgsProjectHomeItemGuiProvider
//

QString QgsProjectHomeItemGuiProvider::name()
{
return QStringLiteral( "project_home_item" );
}

void QgsProjectHomeItemGuiProvider::populateContextMenu( QgsDataItem *item, QMenu *menu, const QList<QgsDataItem *> &, QgsDataItemGuiContext )
{
if ( !qobject_cast< QgsProjectHomeItem * >( item ) )
return;

if ( !menu->actions().empty() )
menu->insertSeparator( menu->actions().at( 0 ) );

QAction *setHome = new QAction( tr( "Set Project Home…" ), menu );
connect( setHome, &QAction::triggered, this, [ = ]
{
QString oldHome = QgsProject::instance()->homePath();
QString newPath = QFileDialog::getExistingDirectory( QgisApp::instance(), tr( "Select Project Home Directory" ), oldHome );
if ( !newPath.isEmpty() )
{
QgsProject::instance()->setPresetHomePath( newPath );
}
} );

// ensure item is the first one shown
if ( !menu->actions().empty() )
menu->insertAction( menu->actions().at( 0 ), setHome );
else
menu->addAction( setHome );
}
18 changes: 17 additions & 1 deletion src/app/browser/qgsinbuiltdataitemproviders.h
Expand Up @@ -31,7 +31,7 @@ class QgsAppDirectoryItemGuiProvider : public QObject, public QgsDataItemGuiProv

public:

QgsAppDirectoryItemGuiProvider();
QgsAppDirectoryItemGuiProvider() = default;

QString name() override;

Expand All @@ -49,6 +49,22 @@ class QgsAppDirectoryItemGuiProvider : public QObject, public QgsDataItemGuiProv
};


class QgsProjectHomeItemGuiProvider : public QObject, public QgsDataItemGuiProvider
{
Q_OBJECT

public:

QgsProjectHomeItemGuiProvider() = default;

QString name() override;

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

};



#endif // QGSINBUILTDATAITEMPROVIDERS_H

Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1026,6 +1026,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
mBrowserWidget->setMessageBar( mInfoBar );

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

QShortcut *showBrowserDock = new QShortcut( QKeySequence( tr( "Ctrl+2" ) ), this );
connect( showBrowserDock, &QShortcut::activated, mBrowserWidget, &QgsDockWidget::toggleUserVisible );
Expand Down
30 changes: 2 additions & 28 deletions src/core/qgsdataitem.cpp
Expand Up @@ -27,7 +27,6 @@
#include <QTreeWidgetItem>
#include <QVector>
#include <QStyle>
#include <QFileDialog>
#include <mutex>

#include "qgis.h"
Expand Down Expand Up @@ -524,7 +523,7 @@ int QgsDataItem::findItem( QVector<QgsDataItem *> items, QgsDataItem *item )
{
for ( int i = 0; i < items.size(); i++ )
{
Q_ASSERT_X( items[i], "findItem", QString( "item %1 is nullptr" ).arg( i ).toLatin1() );
Q_ASSERT_X( items[i], "findItem", QStringLiteral( "item %1 is nullptr" ).arg( i ).toLatin1() );
QgsDebugMsgLevel( QString::number( i ) + " : " + items[i]->mPath + " x " + item->mPath, 2 );
if ( items[i]->equal( item ) )
return i;
Expand Down Expand Up @@ -710,7 +709,7 @@ bool QgsLayerItem::equal( const QgsDataItem *other )
return false;
}
//const QgsLayerItem *o = qobject_cast<const QgsLayerItem *> ( other );
const QgsLayerItem *o = dynamic_cast<const QgsLayerItem *>( other );
const QgsLayerItem *o = qobject_cast<const QgsLayerItem *>( other );
if ( !o )
return false;

Expand Down Expand Up @@ -1545,31 +1544,6 @@ QVariant QgsProjectHomeItem::sortKey() const
return QStringLiteral( " 1" );
}

QList<QAction *> QgsProjectHomeItem::actions( QWidget *parent )
{
QList<QAction *> lst = QgsDirectoryItem::actions( parent );
QAction *separator = new QAction( parent );
separator->setSeparator( true );
lst.append( separator );

QAction *setHome = new QAction( tr( "Set Project Home…" ), parent );
connect( setHome, &QAction::triggered, this, [ = ]
{
QWidget *parentWindow = parent;
while ( parentWindow->parentWidget() )
parentWindow = parentWindow->parentWidget();

QString oldHome = QgsProject::instance()->homePath();
QString newPath = QFileDialog::getExistingDirectory( parentWindow, tr( "Select Project Home Directory" ), oldHome );
if ( !newPath.isEmpty() )
{
QgsProject::instance()->setPresetHomePath( newPath );
}
} );
lst << setHome;

return lst;
}

QgsFavoriteItem::QgsFavoriteItem( QgsFavoritesItem *parent, const QString &name, const QString &dirPath, const QString &path )
: QgsDirectoryItem( parent, name, dirPath, path )
Expand Down
3 changes: 0 additions & 3 deletions src/core/qgsdataitem.h
Expand Up @@ -787,9 +787,6 @@ class CORE_EXPORT QgsProjectHomeItem : public QgsDirectoryItem
QIcon icon() override;
QVariant sortKey() const override;

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


};

/**
Expand Down

0 comments on commit fab4154

Please sign in to comment.