Skip to content

Commit edecd49

Browse files
authoredOct 27, 2017
[FEATURE][welcome page] Add pin/unpin to list actions (fixes #16394)
1 parent 30aa5f5 commit edecd49

File tree

7 files changed

+74
-6
lines changed

7 files changed

+74
-6
lines changed
 

‎images/images.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@
413413
<file>themes/default/north_arrow.png</file>
414414
<file>themes/default/diagramNone.svg</file>
415415
<file>themes/default/pie-chart.svg</file>
416+
<file>themes/default/pin.svg</file>
416417
<file>themes/default/pluginExperimental.png</file>
417418
<file>themes/default/pluginDeprecated.png</file>
418419
<file>themes/default/propertyicons/action.svg</file>

‎images/themes/default/pin.svg

Lines changed: 1 addition & 0 deletions
Loading

‎src/app/qgisapp.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,16 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
736736
mRecentProjects.removeAt( row );
737737
saveRecentProjects();
738738
} );
739-
739+
connect( mWelcomePage, &QgsWelcomePage::projectPinned, this, [ this ]( int row )
740+
{
741+
mRecentProjects.at( row ).pin = true;
742+
saveRecentProjects();
743+
} );
744+
connect( mWelcomePage, &QgsWelcomePage::projectUnpinned, this, [ this ]( int row )
745+
{
746+
mRecentProjects.at( row ).pin = false;
747+
saveRecentProjects();
748+
} );
740749
endProfile();
741750

742751
mCentralContainer = new QStackedWidget;
@@ -1657,8 +1666,16 @@ void QgisApp::readRecentProjects()
16571666
data.path = settings.value( QStringLiteral( "path" ) ).toString();
16581667
data.previewImagePath = settings.value( QStringLiteral( "previewImage" ) ).toString();
16591668
data.crs = settings.value( QStringLiteral( "crs" ) ).toString();
1669+
data.pin = settings.value( QStringLiteral( "pin" ) ).toBool();
16601670
settings.endGroup();
1661-
mRecentProjects.append( data );
1671+
if ( data.pin )
1672+
{
1673+
mRecentProjects.prepend( data );
1674+
}
1675+
else
1676+
{
1677+
mRecentProjects.append( data );
1678+
}
16621679
}
16631680
settings.endGroup();
16641681
}
@@ -3876,9 +3893,16 @@ void QgisApp::saveRecentProjectPath( const QString &projectPath, bool savePrevie
38763893
// Prepend this file to the list
38773894
mRecentProjects.prepend( projectData );
38783895

3896+
// Count the number of pinned items, those shouldn't affect trimming
3897+
int pinnedCount = 0;
3898+
Q_FOREACH ( const QgsWelcomePageItemsModel::RecentProjectData &recentProject, mRecentProjects )
3899+
{
3900+
if ( recentProject.pin )
3901+
pinnedCount++;
3902+
}
38793903
// Keep the list to 10 items by trimming excess off the bottom
38803904
// And remove the associated image
3881-
while ( mRecentProjects.count() > 10 )
3905+
while ( mRecentProjects.count() > 10 + pinnedCount )
38823906
{
38833907
QFile( mRecentProjects.takeLast().previewImagePath ).remove();
38843908
}
@@ -3907,6 +3931,7 @@ void QgisApp::saveRecentProjects()
39073931
settings.setValue( QStringLiteral( "path" ), recentProject.path );
39083932
settings.setValue( QStringLiteral( "previewImage" ), recentProject.previewImagePath );
39093933
settings.setValue( QStringLiteral( "crs" ), recentProject.crs );
3934+
settings.setValue( QStringLiteral( "pin" ), recentProject.pin );
39103935
settings.endGroup();
39113936
}
39123937
}

‎src/app/qgswelcomepage.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ void QgsWelcomePage::showContextMenuForProjects( QPoint point )
117117
if ( !index.isValid() )
118118
return;
119119

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

128129
if ( enabled )
129130
{
131+
if ( !pin )
132+
{
133+
QAction *pinAction = new QAction( tr( "Pin to List" ), menu );
134+
connect( pinAction, &QAction::triggered, this, [this, index]
135+
{
136+
mModel->pinProject( index );
137+
emit projectPinned( index.row() );
138+
} );
139+
menu->addAction( pinAction );
140+
}
141+
else
142+
{
143+
QAction *pinAction = new QAction( tr( "Unpin from List" ), menu );
144+
connect( pinAction, &QAction::triggered, this, [this, index]
145+
{
146+
mModel->unpinProject( index );
147+
emit projectUnpinned( index.row() );
148+
} );
149+
menu->addAction( pinAction );
150+
}
130151
QAction *openFolderAction = new QAction( tr( "Open Directory…" ), menu );
131152
connect( openFolderAction, &QAction::triggered, this, [this, path]
132153
{

‎src/app/qgswelcomepage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class QgsWelcomePage : public QWidget
3737

3838
signals:
3939
void projectRemoved( int row );
40+
void projectPinned( int row );
41+
void projectUnpinned( int row );
4042

4143
private slots:
4244
void itemActivated( const QModelIndex &index );

‎src/app/qgswelcomepageitemsmodel.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ void QgsWelcomePageItemDelegate::paint( QPainter *painter, const QStyleOptionVie
7575
int titleSize = QApplication::fontMetrics().height() * 1.1;
7676
int textSize = titleSize * 0.85;
7777

78-
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 )
78+
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 )
7979
.arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString(),
80+
index.data( QgsWelcomePageItemsModel::PinRole ).toBool() == true ? QStringLiteral( "<img src=\"qrc:/images/themes/default/pin.svg\">" ) : QString(),
8081
index.data( QgsWelcomePageItemsModel::NativePathRole ).toString(),
8182
index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
8283
doc.setTextWidth( option.rect.width() - ( !icon.isNull() ? icon.width() + 35 : 35 ) );
@@ -111,8 +112,9 @@ QSize QgsWelcomePageItemDelegate::sizeHint( const QStyleOptionViewItem &option,
111112
int titleSize = QApplication::fontMetrics().height() * 1.1;
112113
int textSize = titleSize * 0.85;
113114

114-
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 )
115+
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 )
115116
.arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString(),
117+
index.data( QgsWelcomePageItemsModel::PinRole ).toBool() == true ? QStringLiteral( "<img src=\"qrc:/images/themes/default/pin.svg\">" ) : QString(),
116118
index.data( QgsWelcomePageItemsModel::NativePathRole ).toString(),
117119
index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
118120
doc.setTextWidth( width - ( !icon.isNull() ? icon.width() + 35 : 35 ) );
@@ -161,6 +163,8 @@ QVariant QgsWelcomePageItemsModel::data( const QModelIndex &index, int role ) co
161163
{
162164
return QString();
163165
}
166+
case PinRole:
167+
return mRecentProjects.at( index.row() ).pin;
164168
case Qt::DecorationRole:
165169
{
166170
QString filename( mRecentProjects.at( index.row() ).previewImagePath );
@@ -214,6 +218,16 @@ Qt::ItemFlags QgsWelcomePageItemsModel::flags( const QModelIndex &index ) const
214218
return flags;
215219
}
216220

221+
void QgsWelcomePageItemsModel::pinProject( const QModelIndex &index )
222+
{
223+
mRecentProjects.at( index.row() ).pin = true;
224+
}
225+
226+
void QgsWelcomePageItemsModel::unpinProject( const QModelIndex &index )
227+
{
228+
mRecentProjects.at( index.row() ).pin = false;
229+
}
230+
217231
void QgsWelcomePageItemsModel::removeProject( const QModelIndex &index )
218232
{
219233
mRecentProjects.removeAt( index.row() );

‎src/app/qgswelcomepageitemsmodel.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class QgsWelcomePageItemsModel : public QAbstractListModel
4040
TitleRole = Qt::UserRole + 1,
4141
PathRole = Qt::UserRole + 2,
4242
NativePathRole = Qt::UserRole + 3,
43-
CrsRole = Qt::UserRole + 4
43+
CrsRole = Qt::UserRole + 4,
44+
PinRole = Qt::UserRole + 5
4445
};
4546

4647
struct RecentProjectData
@@ -50,6 +51,7 @@ class QgsWelcomePageItemsModel : public QAbstractListModel
5051
QString title;
5152
QString previewImagePath;
5253
QString crs;
54+
mutable bool pin = false;
5355
mutable bool checkedExists = false;
5456
mutable bool exists = false;
5557
};
@@ -62,6 +64,8 @@ class QgsWelcomePageItemsModel : public QAbstractListModel
6264
QVariant data( const QModelIndex &index, int role ) const override;
6365
Qt::ItemFlags flags( const QModelIndex &index ) const override;
6466

67+
void pinProject( const QModelIndex &index );
68+
void unpinProject( const QModelIndex &index );
6569
void removeProject( const QModelIndex &index );
6670
void recheckProject( const QModelIndex &index );
6771

0 commit comments

Comments
 (0)
Please sign in to comment.