Skip to content

Commit

Permalink
Centralize project preview creation
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed May 27, 2019
1 parent 573d5a0 commit 4baafa9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 27 deletions.
59 changes: 45 additions & 14 deletions src/app/qgsrecentprojectsitemsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,11 @@ QVariant QgsRecentProjectItemsModel::data( const QModelIndex &index, int role )
if ( filename.isEmpty() )
return QVariant();

QImage thumbnail( filename );
QgsProjectPreviewImage thumbnail( filename );
if ( thumbnail.isNull() )
return QVariant();

//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();

return QPixmap::fromImage( previewImage );
return thumbnail.pixmap();
}

case Qt::ToolTipRole:
Expand Down Expand Up @@ -254,3 +242,46 @@ void QgsRecentProjectItemsModel::recheckProject( const QModelIndex &index )
projectData.exists = QFile::exists( ( projectData.path ) );
projectData.checkedExists = true;
}

QgsProjectPreviewImage::QgsProjectPreviewImage() = default;

QgsProjectPreviewImage::QgsProjectPreviewImage( const QString &path )
{
loadImageFromFile( path );
}

QgsProjectPreviewImage::QgsProjectPreviewImage( const QImage &image )
{
mImage = image;
}

void QgsProjectPreviewImage::loadImageFromFile( const QString &path )
{
mImage = QImage( path );
}

void QgsProjectPreviewImage::setImage( const QImage &image )
{
mImage = image;
}

QPixmap QgsProjectPreviewImage::pixmap() const
{
//nicely round corners so users don't get paper cuts
QImage previewImage( mImage.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, mImage );
previewPainter.end();
return QPixmap::fromImage( previewImage );
}

bool QgsProjectPreviewImage::isNull() const
{
return mImage.isNull();
}
19 changes: 19 additions & 0 deletions src/app/qgsrecentprojectsitemsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
#include <QStringList>
#include <QStyledItemDelegate>

class QgsMapCanvas;

class QgsProjectPreviewImage
{
public:
QgsProjectPreviewImage();
QgsProjectPreviewImage( const QString &path );
QgsProjectPreviewImage( const QImage &image );

void loadImageFromFile( const QString &path );
void setImage( const QImage &image );
QPixmap pixmap() const;

bool isNull() const;

private:
QImage mImage;
};

class QgsRecentProjectItemDelegate : public QStyledItemDelegate
{
Q_OBJECT
Expand Down
27 changes: 14 additions & 13 deletions src/app/qgstemplateprojectsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ QgsTemplateProjectsModel::QgsTemplateProjectsModel( QObject *parent )
connect( &mFileSystemWatcher, &QFileSystemWatcher::directoryChanged, this, &QgsTemplateProjectsModel::scanDirectory );

setColumnCount( 1 );

QStandardItem *emptyProjectItem = new QStandardItem();

emptyProjectItem->setData( tr( "New empty project" ), QgsRecentProjectItemsModel::TitleRole );
QSize previewSize( 250, 177 );
QImage image( previewSize, QImage::Format_ARGB32 );
image.fill( Qt::white );
QgsProjectPreviewImage previewImage( image );
emptyProjectItem->setData( previewImage.pixmap(), Qt::DecorationRole );

appendRow( emptyProjectItem );
}

void QgsTemplateProjectsModel::scanDirectory( const QString &path )
Expand Down Expand Up @@ -78,21 +89,11 @@ void QgsTemplateProjectsModel::scanDirectory( const QString &path )

QString filename( mTemporaryDir.filePath( fileId ) + QDir::separator() + QStringLiteral( "preview.png" ) );

QImage thumbnail( filename );
QgsProjectPreviewImage 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( thumbnail.pixmap(), Qt::DecorationRole );
}
item->setData( file.baseName(), QgsRecentProjectItemsModel::TitleRole );
item->setData( file.filePath(), QgsRecentProjectItemsModel::NativePathRole );
Expand Down

0 comments on commit 4baafa9

Please sign in to comment.