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
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;

View File

@ -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 );
@ -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 );
}
@ -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();
@ -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

View File

@ -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;
@ -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.
@ -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
};

View File

@ -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 );
@ -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;
}

View File

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