Fix tiny svg preview size on hidpi displays

This commit is contained in:
Nyall Dawson 2017-07-27 09:00:44 +10:00
parent db2968601f
commit 687adbf669
5 changed files with 27 additions and 9 deletions

View File

@ -24,18 +24,20 @@ class QgsSvgSelectorListModel : QAbstractListModel
%End %End
public: public:
QgsSvgSelectorListModel( QObject *parent /TransferThis/ ); QgsSvgSelectorListModel( QObject *parent /TransferThis/, int iconSize = 30 );
%Docstring %Docstring
Constructor for QgsSvgSelectorListModel. All SVGs in folders from the application SVG Constructor for QgsSvgSelectorListModel. All SVGs in folders from the application SVG
search paths will be shown. search paths will be shown.
\param parent parent object \param parent parent object
\param iconSize desired size of SVG icons to create
%End %End
QgsSvgSelectorListModel( QObject *parent /TransferThis/, const QString &path ); QgsSvgSelectorListModel( QObject *parent /TransferThis/, const QString &path, int iconSize = 30 );
%Docstring %Docstring
Constructor for creating a model for SVG files in a specific path. Constructor for creating a model for SVG files in a specific path.
\param parent parent object \param parent parent object
\param path initial path, which is recursively searched \param path initial path, which is recursively searched
\param iconSize desired size of SVG icons to create
%End %End
virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const; virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const;

View File

@ -214,18 +214,20 @@ void QgsSvgGroupLoader::loadGroup( const QString &parentPath )
// QgsSvgSelectorListModel // QgsSvgSelectorListModel
// //
QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent ) QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent, int iconSize )
: QAbstractListModel( parent ) : QAbstractListModel( parent )
, mSvgLoader( new QgsSvgSelectorLoader( this ) ) , mSvgLoader( new QgsSvgSelectorLoader( this ) )
, mIconSize( iconSize )
{ {
mSvgLoader->setPath( QString() ); mSvgLoader->setPath( QString() );
connect( mSvgLoader, &QgsSvgSelectorLoader::foundSvgs, this, &QgsSvgSelectorListModel::addSvgs ); connect( mSvgLoader, &QgsSvgSelectorLoader::foundSvgs, this, &QgsSvgSelectorListModel::addSvgs );
mSvgLoader->start(); mSvgLoader->start();
} }
QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent, const QString &path ) QgsSvgSelectorListModel::QgsSvgSelectorListModel( QObject *parent, const QString &path, int iconSize )
: QAbstractListModel( parent ) : QAbstractListModel( parent )
, mSvgLoader( new QgsSvgSelectorLoader( this ) ) , mSvgLoader( new QgsSvgSelectorLoader( this ) )
, mIconSize( iconSize )
{ {
mSvgLoader->setPath( path ); mSvgLoader->setPath( path );
connect( mSvgLoader, &QgsSvgSelectorLoader::foundSvgs, this, &QgsSvgSelectorListModel::addSvgs ); connect( mSvgLoader, &QgsSvgSelectorLoader::foundSvgs, this, &QgsSvgSelectorListModel::addSvgs );
@ -263,7 +265,7 @@ QPixmap QgsSvgSelectorListModel::createPreview( const QString &entry ) const
strokeWidth = 0.2; strokeWidth = 0.2;
bool fitsInCache; // should always fit in cache at these sizes (i.e. under 559 px ^ 2, or half cache size) 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 ); return QPixmap::fromImage( img );
} }
@ -375,6 +377,9 @@ QgsSvgSelectorWidget::QgsSvgSelectorWidget( QWidget *parent )
// TODO: in-code gui setup with option to vertically or horizontally stack SVG groups/images widgets // TODO: in-code gui setup with option to vertically or horizontally stack SVG groups/images widgets
setupUi( this ); 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 ); mGroupsTreeView->setHeaderHidden( true );
populateList(); populateList();
@ -436,7 +441,7 @@ void QgsSvgSelectorWidget::populateIcons( const QModelIndex &idx )
QString path = idx.data( Qt::UserRole + 1 ).toString(); QString path = idx.data( Qt::UserRole + 1 ).toString();
QAbstractItemModel *oldModel = mImagesListView->model(); QAbstractItemModel *oldModel = mImagesListView->model();
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( mImagesListView, path ); QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( mImagesListView, path, mIconSize );
mImagesListView->setModel( m ); mImagesListView->setModel( m );
delete oldModel; //explicitly delete old model to force any background threads to stop delete oldModel; //explicitly delete old model to force any background threads to stop

View File

@ -174,14 +174,16 @@ class GUI_EXPORT QgsSvgSelectorListModel : public QAbstractListModel
/** Constructor for QgsSvgSelectorListModel. All SVGs in folders from the application SVG /** Constructor for QgsSvgSelectorListModel. All SVGs in folders from the application SVG
* search paths will be shown. * search paths will be shown.
* \param parent parent object * \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. /** Constructor for creating a model for SVG files in a specific path.
* \param parent parent object * \param parent parent object
* \param path initial path, which is recursively searched * \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; int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override; QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
@ -193,6 +195,8 @@ class GUI_EXPORT QgsSvgSelectorListModel : public QAbstractListModel
QPixmap createPreview( const QString &entry ) const; QPixmap createPreview( const QString &entry ) const;
QgsSvgSelectorLoader *mSvgLoader = nullptr; QgsSvgSelectorLoader *mSvgLoader = nullptr;
int mIconSize = 30;
private slots: private slots:
/** Called to add SVG files to the model. /** Called to add SVG files to the model.
@ -259,6 +263,9 @@ class GUI_EXPORT QgsSvgSelectorWidget : public QWidget, private Ui::WidgetSvgSel
void on_mFileLineEdit_textChanged( const QString &text ); void on_mFileLineEdit_textChanged( const QString &text );
private: private:
int mIconSize = 30;
QString mCurrentSvgPath; //!< Always stored as absolute path QString mCurrentSvgPath; //!< Always stored as absolute path
}; };

View File

@ -1740,6 +1740,9 @@ QgsSvgMarkerSymbolLayerWidget::QgsSvgMarkerSymbolLayerWidget( const QgsVectorLay
spinOffsetY->setClearValue( 0.0 ); spinOffsetY->setClearValue( 0.0 );
spinAngle->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(); populateList();
connect( viewImages->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsSvgMarkerSymbolLayerWidget::setName ); connect( viewImages->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsSvgMarkerSymbolLayerWidget::setName );
@ -1779,7 +1782,7 @@ void QgsSvgMarkerSymbolLayerWidget::populateList()
// Initially load the icons in the List view without any grouping // Initially load the icons in the List view without any grouping
oldModel = viewImages->model(); oldModel = viewImages->model();
QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( viewImages ); QgsSvgSelectorListModel *m = new QgsSvgSelectorListModel( viewImages, mIconSize );
viewImages->setModel( m ); viewImages->setModel( m );
delete oldModel; delete oldModel;
} }

View File

@ -467,6 +467,7 @@ class GUI_EXPORT QgsSvgMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, pr
private: private:
std::shared_ptr< QgsMarkerSymbol > mAssistantPreviewSymbol; std::shared_ptr< QgsMarkerSymbol > mAssistantPreviewSymbol;
int mIconSize = 30;
}; };