Skip to content

Commit

Permalink
Fix tiny svg preview size on hidpi displays
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 26, 2017
1 parent db29686 commit 687adbf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
6 changes: 4 additions & 2 deletions python/gui/symbology-ng/qgssvgselectorwidget.sip
Expand Up @@ -24,18 +24,20 @@ class QgsSvgSelectorListModel : QAbstractListModel
%End
public:

QgsSvgSelectorListModel( QObject *parent /TransferThis/ );
QgsSvgSelectorListModel( QObject *parent /TransferThis/, int iconSize = 30 );
%Docstring
Constructor for QgsSvgSelectorListModel. All SVGs in folders from the application SVG
search paths will be shown.
\param parent parent object
\param iconSize desired size of SVG icons to create
%End

QgsSvgSelectorListModel( QObject *parent /TransferThis/, const QString &path );
QgsSvgSelectorListModel( QObject *parent /TransferThis/, const QString &path, int iconSize = 30 );
%Docstring
Constructor for creating a model for SVG files in a specific path.
\param parent parent object
\param path initial path, which is recursively searched
\param iconSize desired size of SVG icons to create
%End

virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;
Expand Down
13 changes: 9 additions & 4 deletions src/gui/symbology-ng/qgssvgselectorwidget.cpp
Expand Up @@ -214,18 +214,20 @@ void QgsSvgGroupLoader::loadGroup( const QString &parentPath )
// QgsSvgSelectorListModel
//

QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent )
QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent, int iconSize )
: QAbstractListModel( parent )
, mSvgLoader( new QgsSvgSelectorLoader( this ) )
, mIconSize( iconSize )
{
mSvgLoader->setPath( QString() );
connect( mSvgLoader, &QgsSvgSelectorLoader::foundSvgs, this, &QgsSvgSelectorListModel::addSvgs );
mSvgLoader->start();
}

QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent, const QString &path )
QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent, const QString &path, int iconSize )
: QAbstractListModel( parent )
, mSvgLoader( new QgsSvgSelectorLoader( this ) )
, mIconSize( iconSize )
{
mSvgLoader->setPath( path );
connect( mSvgLoader, &QgsSvgSelectorLoader::foundSvgs, this, &QgsSvgSelectorListModel::addSvgs );
Expand Down Expand Up @@ -263,7 +265,7 @@ QPixmap QgsSvgSelectorListModel::createPreview( const QString &entry ) const
strokeWidth = 0.2;

bool fitsInCache; // should always fit in cache at these sizes (i.e. under 559 px ^ 2, or half cache size)
const QImage &img = QgsApplication::svgCache()->svgAsImage( entry, 30.0, fill, stroke, strokeWidth, 3.5 /*appr. 88 dpi*/, fitsInCache );
const QImage &img = QgsApplication::svgCache()->svgAsImage( entry, mIconSize, fill, stroke, strokeWidth, 3.5 /*appr. 88 dpi*/, fitsInCache );
return QPixmap::fromImage( img );
}

Expand Down Expand Up @@ -375,6 +377,9 @@ QgsSvgSelectorWidget::QgsSvgSelectorWidget( QWidget *parent )
// TODO: in-code gui setup with option to vertically or horizontally stack SVG groups/images widgets
setupUi( this );

mIconSize = qMax( 30, qRound( Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "XXXX" ) ) ) );
mImagesListView->setGridSize( QSize( mIconSize * 1.2, mIconSize * 1.2 ) );

mGroupsTreeView->setHeaderHidden( true );
populateList();

Expand Down Expand Up @@ -436,7 +441,7 @@ void QgsSvgSelectorWidget::populateIcons( const QModelIndex &idx )
QString path = idx.data( Qt::UserRole + 1 ).toString();

QAbstractItemModel *oldModel = mImagesListView->model();
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( mImagesListView, path );
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( mImagesListView, path, mIconSize );
mImagesListView->setModel( m );
delete oldModel; //explicitly delete old model to force any background threads to stop

Expand Down
11 changes: 9 additions & 2 deletions src/gui/symbology-ng/qgssvgselectorwidget.h
Expand Up @@ -174,14 +174,16 @@ class GUI_EXPORT QgsSvgSelectorListModel : public QAbstractListModel
/** Constructor for QgsSvgSelectorListModel. All SVGs in folders from the application SVG
* search paths will be shown.
* \param parent parent object
* \param iconSize desired size of SVG icons to create
*/
QgsSvgSelectorListModel( QObject *parent SIP_TRANSFERTHIS );
QgsSvgSelectorListModel( QObject *parent SIP_TRANSFERTHIS, int iconSize = 30 );

/** Constructor for creating a model for SVG files in a specific path.
* \param parent parent object
* \param path initial path, which is recursively searched
* \param iconSize desired size of SVG icons to create
*/
QgsSvgSelectorListModel( QObject *parent SIP_TRANSFERTHIS, const QString &path );
QgsSvgSelectorListModel( QObject *parent SIP_TRANSFERTHIS, const QString &path, int iconSize = 30 );

int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
Expand All @@ -193,6 +195,8 @@ class GUI_EXPORT QgsSvgSelectorListModel : public QAbstractListModel
QPixmap createPreview( const QString &entry ) const;
QgsSvgSelectorLoader *mSvgLoader = nullptr;

int mIconSize = 30;

private slots:

/** Called to add SVG files to the model.
Expand Down Expand Up @@ -259,6 +263,9 @@ class GUI_EXPORT QgsSvgSelectorWidget : public QWidget, private Ui::WidgetSvgSel
void on_mFileLineEdit_textChanged( const QString &text );

private:

int mIconSize = 30;

QString mCurrentSvgPath; //!< Always stored as absolute path

};
Expand Down
5 changes: 4 additions & 1 deletion src/gui/symbology-ng/qgssymbollayerwidget.cpp
Expand Up @@ -1740,6 +1740,9 @@ QgsSvgMarkerSymbolLayerWidget::QgsSvgMarkerSymbolLayerWidget( const QgsVectorLay
spinOffsetY->setClearValue( 0.0 );
spinAngle->setClearValue( 0.0 );

mIconSize = qMax( 30, qRound( Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "XXXX" ) ) ) );
viewImages->setGridSize( QSize( mIconSize * 1.2, mIconSize * 1.2 ) );

populateList();

connect( viewImages->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsSvgMarkerSymbolLayerWidget::setName );
Expand Down Expand Up @@ -1779,7 +1782,7 @@ void QgsSvgMarkerSymbolLayerWidget::populateList()

// Initially load the icons in the List view without any grouping
oldModel = viewImages->model();
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( viewImages );
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( viewImages, mIconSize );
viewImages->setModel( m );
delete oldModel;
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/symbology-ng/qgssymbollayerwidget.h
Expand Up @@ -467,6 +467,7 @@ class GUI_EXPORT QgsSvgMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, pr
private:

std::shared_ptr< QgsMarkerSymbol > mAssistantPreviewSymbol;
int mIconSize = 30;

};

Expand Down

0 comments on commit 687adbf

Please sign in to comment.