-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a template list to the welcome screen
- Loading branch information
Showing
8 changed files
with
241 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/*************************************************************************** | ||
---------------------------------------------------- | ||
date : 16.5.2019 | ||
copyright : (C) 2019 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#include "qgstemplateprojectsmodel.h" | ||
#include "qgsziputils.h" | ||
#include "qgssettings.h" | ||
#include "qgsapplication.h" | ||
#include "qgis.h" | ||
#include "qgsrecentprojectsitemsmodel.h" | ||
|
||
#include <QStandardPaths> | ||
#include <QDir> | ||
#include <QCryptographicHash> | ||
#include <QPainter> | ||
|
||
#include <memory> | ||
|
||
|
||
QgsTemplateProjectsModel::QgsTemplateProjectsModel( QObject *parent ) | ||
: QStandardItemModel( parent ) | ||
{ | ||
const QStringList paths = QStandardPaths::standardLocations( QStandardPaths::AppDataLocation ); | ||
QString templateDirName = QgsSettings().value( QStringLiteral( "qgis/projectTemplateDir" ), | ||
QgsApplication::qgisSettingsDirPath() + QStringLiteral( "project_templates" ) ).toString(); | ||
|
||
for ( const QString &templatePath : paths ) | ||
{ | ||
const QString path = templatePath + QDir::separator() + QStringLiteral( "project_templates" ); | ||
scanDirectory( path ); | ||
mFileSystemWatcher.addPath( path ); | ||
} | ||
|
||
scanDirectory( templateDirName ); | ||
mFileSystemWatcher.addPath( templateDirName ); | ||
|
||
connect( &mFileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, &QgsTemplateProjectsModel::scanDirectory ); | ||
|
||
setColumnCount( 1 ); | ||
} | ||
|
||
void QgsTemplateProjectsModel::scanDirectory( const QString &path ) | ||
{ | ||
QDir dir = QDir( path ); | ||
const QFileInfoList files = dir.entryInfoList( QStringList() << QStringLiteral( "*.qgs" ) << QStringLiteral( "*.qgz" ) ); | ||
|
||
// Remove any template from this directory) | ||
for ( int i = rowCount() - 1; i >= 0; --i ) | ||
{ | ||
if ( index( i, 0 ).data( QgsRecentProjectItemsModel::NativePathRole ).toString().startsWith( path ) ) | ||
{ | ||
removeRow( i ); | ||
} | ||
} | ||
|
||
// Refill with templates from this directory | ||
for ( const QFileInfo &file : files ) | ||
{ | ||
std::unique_ptr<QStandardItem> item = qgis::make_unique<QStandardItem>( file.fileName() ) ; | ||
|
||
const QString fileId = QCryptographicHash::hash( file.filePath().toUtf8(), QCryptographicHash::Sha224 ).toHex(); | ||
|
||
QStringList files; | ||
QDir().mkpath( mTemporaryDir.filePath( fileId ) ); | ||
|
||
QgsZipUtils::unzip( file.filePath(), mTemporaryDir.filePath( fileId ), files ); | ||
|
||
QString filename( mTemporaryDir.filePath( fileId ) + QDir::separator() + QStringLiteral( "preview.png" ) ); | ||
|
||
QImage thumbnail( filename ); | ||
if ( !thumbnail.isNull() ) | ||
{ | ||
//nicely round corners so users don't get paper cuts | ||
QImage previewImage( thumbnail.size(), QImage::Format_ARGB32 ); | ||
previewImage.fill( Qt::transparent ); | ||
QPainter previewPainter( &previewImage ); | ||
previewPainter.setRenderHint( QPainter::Antialiasing, true ); | ||
previewPainter.setPen( Qt::NoPen ); | ||
previewPainter.setBrush( Qt::black ); | ||
previewPainter.drawRoundedRect( 0, 0, previewImage.width(), previewImage.height(), 8, 8 ); | ||
previewPainter.setCompositionMode( QPainter::CompositionMode_SourceIn ); | ||
previewPainter.drawImage( 0, 0, thumbnail ); | ||
previewPainter.end(); | ||
item->setData( QPixmap::fromImage( previewImage ), Qt::DecorationRole ); | ||
} | ||
item->setData( file.baseName(), QgsRecentProjectItemsModel::TitleRole ); | ||
item->setData( file.filePath(), QgsRecentProjectItemsModel::NativePathRole ); | ||
|
||
item->setFlags( Qt::ItemFlag::ItemIsSelectable | Qt::ItemFlag::ItemIsEnabled ) ; | ||
appendRow( item.release() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*************************************************************************** | ||
---------------------------------------------------- | ||
date : 16.5.2019 | ||
copyright : (C) 2019 by Matthias Kuhn | ||
email : matthias@opengis.ch | ||
*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
#ifndef QGSTEMPLATEPROJECTSMODEL_H | ||
#define QGSTEMPLATEPROJECTSMODEL_H | ||
|
||
#include <QStandardItemModel> | ||
#include <QFileSystemWatcher> | ||
#include <QTemporaryDir> | ||
|
||
class QgsTemplateProjectsModel : public QStandardItemModel | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
QgsTemplateProjectsModel( QObject *parent = nullptr ); | ||
|
||
private slots: | ||
|
||
private: | ||
void scanDirectory( const QString &path ); | ||
|
||
QFileSystemWatcher mFileSystemWatcher; | ||
|
||
QTemporaryDir mTemporaryDir; | ||
}; | ||
|
||
|
||
#endif // QGSTEMPLATEPROJECTSMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters