Skip to content
Permalink
Browse files

[FEATURE] Add right click menu option to "open directory" to welcome …

…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 05e97b38bdf74a9b11107a23f45c9501d9c333d7
@@ -23,6 +23,7 @@
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QListView>
#include <QDesktopServices>

QgsWelcomePage::QgsWelcomePage( bool skipVersionCheck, QWidget *parent )
: QWidget( parent )
@@ -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 );

@@ -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()
@@ -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 ) );
}
@@ -22,6 +22,7 @@
#include "qgswelcomepageitemsmodel.h"

class QgsVersionInfo;
class QListView;

class QgsWelcomePage : public QWidget
{
@@ -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
@@ -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 ) );

@@ -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 ) );

@@ -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() )
@@ -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

0 comments on commit 05e97b3

Please sign in to comment.
You can’t perform that action at this time.