Skip to content

Commit

Permalink
[FEATURE][welcome page] Add pin/unpin to list actions (fixes #16394)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Oct 27, 2017
1 parent 30aa5f5 commit edecd49
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 6 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -413,6 +413,7 @@
<file>themes/default/north_arrow.png</file>
<file>themes/default/diagramNone.svg</file>
<file>themes/default/pie-chart.svg</file>
<file>themes/default/pin.svg</file>
<file>themes/default/pluginExperimental.png</file>
<file>themes/default/pluginDeprecated.png</file>
<file>themes/default/propertyicons/action.svg</file>
Expand Down
1 change: 1 addition & 0 deletions images/themes/default/pin.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 28 additions & 3 deletions src/app/qgisapp.cpp
Expand Up @@ -736,7 +736,16 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
mRecentProjects.removeAt( row );
saveRecentProjects();
} );

connect( mWelcomePage, &QgsWelcomePage::projectPinned, this, [ this ]( int row )
{
mRecentProjects.at( row ).pin = true;
saveRecentProjects();
} );
connect( mWelcomePage, &QgsWelcomePage::projectUnpinned, this, [ this ]( int row )
{
mRecentProjects.at( row ).pin = false;
saveRecentProjects();
} );
endProfile();

mCentralContainer = new QStackedWidget;
Expand Down Expand Up @@ -1657,8 +1666,16 @@ void QgisApp::readRecentProjects()
data.path = settings.value( QStringLiteral( "path" ) ).toString();
data.previewImagePath = settings.value( QStringLiteral( "previewImage" ) ).toString();
data.crs = settings.value( QStringLiteral( "crs" ) ).toString();
data.pin = settings.value( QStringLiteral( "pin" ) ).toBool();
settings.endGroup();
mRecentProjects.append( data );
if ( data.pin )
{
mRecentProjects.prepend( data );
}
else
{
mRecentProjects.append( data );
}
}
settings.endGroup();
}
Expand Down Expand Up @@ -3876,9 +3893,16 @@ void QgisApp::saveRecentProjectPath( const QString &projectPath, bool savePrevie
// Prepend this file to the list
mRecentProjects.prepend( projectData );

// Count the number of pinned items, those shouldn't affect trimming
int pinnedCount = 0;
Q_FOREACH ( const QgsWelcomePageItemsModel::RecentProjectData &recentProject, mRecentProjects )
{
if ( recentProject.pin )
pinnedCount++;
}
// Keep the list to 10 items by trimming excess off the bottom
// And remove the associated image
while ( mRecentProjects.count() > 10 )
while ( mRecentProjects.count() > 10 + pinnedCount )
{
QFile( mRecentProjects.takeLast().previewImagePath ).remove();
}
Expand Down Expand Up @@ -3907,6 +3931,7 @@ void QgisApp::saveRecentProjects()
settings.setValue( QStringLiteral( "path" ), recentProject.path );
settings.setValue( QStringLiteral( "previewImage" ), recentProject.previewImagePath );
settings.setValue( QStringLiteral( "crs" ), recentProject.crs );
settings.setValue( QStringLiteral( "pin" ), recentProject.pin );
settings.endGroup();
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/app/qgswelcomepage.cpp
Expand Up @@ -117,6 +117,7 @@ void QgsWelcomePage::showContextMenuForProjects( QPoint point )
if ( !index.isValid() )
return;

bool pin = mModel->data( index, QgsWelcomePageItemsModel::PinRole ).toBool();
QString path = mModel->data( index, QgsWelcomePageItemsModel::PathRole ).toString();
if ( path.isEmpty() )
return;
Expand All @@ -127,6 +128,26 @@ void QgsWelcomePage::showContextMenuForProjects( QPoint point )

if ( enabled )
{
if ( !pin )
{
QAction *pinAction = new QAction( tr( "Pin to List" ), menu );
connect( pinAction, &QAction::triggered, this, [this, index]
{
mModel->pinProject( index );
emit projectPinned( index.row() );
} );
menu->addAction( pinAction );
}
else
{
QAction *pinAction = new QAction( tr( "Unpin from List" ), menu );
connect( pinAction, &QAction::triggered, this, [this, index]
{
mModel->unpinProject( index );
emit projectUnpinned( index.row() );
} );
menu->addAction( pinAction );
}
QAction *openFolderAction = new QAction( tr( "Open Directory…" ), menu );
connect( openFolderAction, &QAction::triggered, this, [this, path]
{
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgswelcomepage.h
Expand Up @@ -37,6 +37,8 @@ class QgsWelcomePage : public QWidget

signals:
void projectRemoved( int row );
void projectPinned( int row );
void projectUnpinned( int row );

private slots:
void itemActivated( const QModelIndex &index );
Expand Down
18 changes: 16 additions & 2 deletions src/app/qgswelcomepageitemsmodel.cpp
Expand Up @@ -75,8 +75,9 @@ void QgsWelcomePageItemDelegate::paint( QPainter *painter, const QStyleOptionVie
int titleSize = QApplication::fontMetrics().height() * 1.1;
int textSize = titleSize * 0.85;

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 )
doc.setHtml( QStringLiteral( "<div style='font-size:%1px;'><span style='font-size:%2px;font-weight:bold;'>%3%4</span><br>%5<br>%6</div>" ).arg( textSize ).arg( titleSize )
.arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString(),
index.data( QgsWelcomePageItemsModel::PinRole ).toBool() == true ? QStringLiteral( "<img src=\"qrc:/images/themes/default/pin.svg\">" ) : QString(),
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 @@ -111,8 +112,9 @@ QSize QgsWelcomePageItemDelegate::sizeHint( const QStyleOptionViewItem &option,
int titleSize = QApplication::fontMetrics().height() * 1.1;
int textSize = titleSize * 0.85;

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 )
doc.setHtml( QStringLiteral( "<div style='font-size:%1px;'><span style='font-size:%2px;font-weight:bold;'>%3%4</span><br>%5<br>%6</div>" ).arg( textSize ).arg( titleSize )
.arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString(),
index.data( QgsWelcomePageItemsModel::PinRole ).toBool() == true ? QStringLiteral( "<img src=\"qrc:/images/themes/default/pin.svg\">" ) : QString(),
index.data( QgsWelcomePageItemsModel::NativePathRole ).toString(),
index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
doc.setTextWidth( width - ( !icon.isNull() ? icon.width() + 35 : 35 ) );
Expand Down Expand Up @@ -161,6 +163,8 @@ QVariant QgsWelcomePageItemsModel::data( const QModelIndex &index, int role ) co
{
return QString();
}
case PinRole:
return mRecentProjects.at( index.row() ).pin;
case Qt::DecorationRole:
{
QString filename( mRecentProjects.at( index.row() ).previewImagePath );
Expand Down Expand Up @@ -214,6 +218,16 @@ Qt::ItemFlags QgsWelcomePageItemsModel::flags( const QModelIndex &index ) const
return flags;
}

void QgsWelcomePageItemsModel::pinProject( const QModelIndex &index )
{
mRecentProjects.at( index.row() ).pin = true;
}

void QgsWelcomePageItemsModel::unpinProject( const QModelIndex &index )
{
mRecentProjects.at( index.row() ).pin = false;
}

void QgsWelcomePageItemsModel::removeProject( const QModelIndex &index )
{
mRecentProjects.removeAt( index.row() );
Expand Down
6 changes: 5 additions & 1 deletion src/app/qgswelcomepageitemsmodel.h
Expand Up @@ -40,7 +40,8 @@ class QgsWelcomePageItemsModel : public QAbstractListModel
TitleRole = Qt::UserRole + 1,
PathRole = Qt::UserRole + 2,
NativePathRole = Qt::UserRole + 3,
CrsRole = Qt::UserRole + 4
CrsRole = Qt::UserRole + 4,
PinRole = Qt::UserRole + 5
};

struct RecentProjectData
Expand All @@ -50,6 +51,7 @@ class QgsWelcomePageItemsModel : public QAbstractListModel
QString title;
QString previewImagePath;
QString crs;
mutable bool pin = false;
mutable bool checkedExists = false;
mutable bool exists = false;
};
Expand All @@ -62,6 +64,8 @@ class QgsWelcomePageItemsModel : public QAbstractListModel
QVariant data( const QModelIndex &index, int role ) const override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;

void pinProject( const QModelIndex &index );
void unpinProject( const QModelIndex &index );
void removeProject( const QModelIndex &index );
void recheckProject( const QModelIndex &index );

Expand Down

0 comments on commit edecd49

Please sign in to comment.