Skip to content

Commit

Permalink
[FEATURE] Add right click menu option to "open directory" to welcome …
Browse files Browse the repository at this point in the history
…page entries

Allows easy opening of the folder containing a project from the welcome page,
e.g. to check if a newer version of the project is in the same folder.
  • Loading branch information
nyalldawson committed Oct 25, 2017
1 parent 1c8803c commit 05e97b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
41 changes: 34 additions & 7 deletions src/app/qgswelcomepage.cpp
Expand Up @@ -23,6 +23,7 @@
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QListView>
#include <QDesktopServices>

QgsWelcomePage::QgsWelcomePage( bool skipVersionCheck, QWidget *parent )
: QWidget( parent )
Expand Down Expand Up @@ -50,14 +51,16 @@ QgsWelcomePage::QgsWelcomePage( bool skipVersionCheck, QWidget *parent )

recentProjectsContainer->layout()->addWidget( recentProjectsTitle );

QListView *recentProjectsListView = new QListView();
recentProjectsListView->setResizeMode( QListView::Adjust );
mRecentProjectsListView = new QListView();
mRecentProjectsListView->setResizeMode( QListView::Adjust );
mRecentProjectsListView->setContextMenuPolicy( Qt::CustomContextMenu );
connect( mRecentProjectsListView, &QListView::customContextMenuRequested, this, &QgsWelcomePage::showContextMenuForProjects );

mModel = new QgsWelcomePageItemsModel( recentProjectsListView );
recentProjectsListView->setModel( mModel );
recentProjectsListView->setItemDelegate( new QgsWelcomePageItemDelegate( recentProjectsListView ) );
mModel = new QgsWelcomePageItemsModel( mRecentProjectsListView );
mRecentProjectsListView->setModel( mModel );
mRecentProjectsListView->setItemDelegate( new QgsWelcomePageItemDelegate( mRecentProjectsListView ) );

recentProjectsContainer->layout()->addWidget( recentProjectsListView );
recentProjectsContainer->layout()->addWidget( mRecentProjectsListView );

layout->addWidget( recentProjectsContainer );

Expand All @@ -72,7 +75,7 @@ QgsWelcomePage::QgsWelcomePage( bool skipVersionCheck, QWidget *parent )
mVersionInfo->checkVersion();
}

connect( recentProjectsListView, &QAbstractItemView::activated, this, &QgsWelcomePage::itemActivated );
connect( mRecentProjectsListView, &QAbstractItemView::activated, this, &QgsWelcomePage::itemActivated );
}

QgsWelcomePage::~QgsWelcomePage()
Expand Down Expand Up @@ -107,3 +110,27 @@ void QgsWelcomePage::versionInfoReceived()
"}" );
}
}

void QgsWelcomePage::showContextMenuForProjects( QPoint point )
{
QModelIndex index = mRecentProjectsListView->indexAt( point );
if ( !index.isValid() )
return;

QString path = mModel->data( index, QgsWelcomePageItemsModel::PathRole ).toString();
if ( path.isEmpty() )
return;

QMenu *menu = new QMenu( this );

QAction *openFolderAction = new QAction( tr( "Open Directory…" ), menu );
connect( openFolderAction, &QAction::triggered, this, [this, path]
{
QFileInfo fi( path );
QString folder = fi.path();
QDesktopServices::openUrl( QUrl::fromLocalFile( folder ) );
} );
menu->addAction( openFolderAction );

menu->popup( mapToGlobal( point ) );
}
3 changes: 3 additions & 0 deletions src/app/qgswelcomepage.h
Expand Up @@ -22,6 +22,7 @@
#include "qgswelcomepageitemsmodel.h"

class QgsVersionInfo;
class QListView;

class QgsWelcomePage : public QWidget
{
Expand All @@ -37,11 +38,13 @@ class QgsWelcomePage : public QWidget
private slots:
void itemActivated( const QModelIndex &index );
void versionInfoReceived();
void showContextMenuForProjects( QPoint point );

private:
QgsWelcomePageItemsModel *mModel = nullptr;
QLabel *mVersionInformation = nullptr;
QgsVersionInfo *mVersionInfo = nullptr;
QListView *mRecentProjectsListView = nullptr;
};

#endif // QGSWELCOMEDIALOG_H
6 changes: 4 additions & 2 deletions src/app/qgswelcomepageitemsmodel.cpp
Expand Up @@ -77,7 +77,7 @@ void QgsWelcomePageItemDelegate::paint( QPainter *painter, const QStyleOptionVie

doc.setHtml( QStringLiteral( "<div style='font-size:%1px;'><span style='font-size:%2px;font-weight:bold;'>%3</span><br>%4<br>%5</div>" ).arg( textSize ).arg( titleSize )
.arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString(),
index.data( QgsWelcomePageItemsModel::PathRole ).toString(),
index.data( QgsWelcomePageItemsModel::NativePathRole ).toString(),
index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
doc.setTextWidth( option.rect.width() - ( !icon.isNull() ? icon.width() + 35 : 35 ) );

Expand Down Expand Up @@ -113,7 +113,7 @@ QSize QgsWelcomePageItemDelegate::sizeHint( const QStyleOptionViewItem &option,

doc.setHtml( QStringLiteral( "<div style='font-size:%1px;'><span style='font-size:%2px;font-weight:bold;'>%3</span><br>%4<br>%5</div>" ).arg( textSize ).arg( titleSize )
.arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString(),
index.data( QgsWelcomePageItemsModel::PathRole ).toString(),
index.data( QgsWelcomePageItemsModel::NativePathRole ).toString(),
index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
doc.setTextWidth( width - ( !icon.isNull() ? icon.width() + 35 : 35 ) );

Expand Down Expand Up @@ -148,6 +148,8 @@ QVariant QgsWelcomePageItemsModel::data( const QModelIndex &index, int role ) co
case TitleRole:
return mRecentProjects.at( index.row() ).title != mRecentProjects.at( index.row() ).path ? mRecentProjects.at( index.row() ).title : QFileInfo( mRecentProjects.at( index.row() ).path ).completeBaseName();
case PathRole:
return mRecentProjects.at( index.row() ).path;
case NativePathRole:
return QDir::toNativeSeparators( mRecentProjects.at( index.row() ).path );
case CrsRole:
if ( !mRecentProjects.at( index.row() ).crs.isEmpty() )
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgswelcomepageitemsmodel.h
Expand Up @@ -39,7 +39,8 @@ class QgsWelcomePageItemsModel : public QAbstractListModel
{
TitleRole = Qt::UserRole + 1,
PathRole = Qt::UserRole + 2,
CrsRole = Qt::UserRole + 3
NativePathRole = Qt::UserRole + 3,
CrsRole = Qt::UserRole + 4
};

struct RecentProjectData
Expand Down

0 comments on commit 05e97b3

Please sign in to comment.