Skip to content

Commit

Permalink
Move and unify scaleIconSize into QgsApplication::scaleIconSize
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Oct 15, 2020
1 parent 76cfbab commit 0607504
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
11 changes: 11 additions & 0 deletions python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -935,6 +935,17 @@ Do not include generated variables (like system name, user name etc.)
Set a single custom expression variable.

.. versionadded:: 3.0
%End

static int scaleIconSize( int standardSize );
%Docstring
Scales an icon size to compensate for display pixel density, making the icon
size hi-dpi friendly, whilst still resulting in pixel-perfect sizes for low-dpi
displays.

``standardSize`` should be set to a standard icon size, e.g. 16, 24, 48, etc.

.. versionadded:: 3.16
%End

int maxConcurrentConnectionsPerPool() const;
Expand Down
11 changes: 2 additions & 9 deletions src/core/layertree/qgslayertreemodel.cpp
Expand Up @@ -13,18 +13,16 @@
* *
***************************************************************************/

#include <QApplication>
#include <QDesktopWidget>
#include <QMimeData>
#include <QTextStream>

#include "qgslayertreemodel.h"

#include "qgsapplication.h"
#include "qgslayertree.h"
#include "qgslayertreeutils.h"
#include "qgslayertreemodellegendnode.h"
#include "qgsproject.h"
#include "qgsapplication.h"
#include "qgsdataitem.h"
#include "qgsmaphittest.h"
#include "qgsmaplayer.h"
Expand Down Expand Up @@ -723,12 +721,7 @@ void QgsLayerTreeModel::setLayerStyleOverrides( const QMap<QString, QString> &ov

int QgsLayerTreeModel::scaleIconSize( int standardSize )
{
QFontMetrics fm( ( QFont() ) );
const double scale = 1.1 * standardSize / 24;
int scaledIconSize = static_cast< int >( std::floor( std::max( Qgis::UI_SCALE_FACTOR * fm.height() * scale, static_cast< double >( standardSize ) ) ) );
if ( QApplication::desktop() )
scaledIconSize *= QApplication::desktop()->devicePixelRatio();
return scaledIconSize;
return QgsApplication::scaleIconSize( standardSize );
}

void QgsLayerTreeModel::nodeWillAddChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo )
Expand Down
13 changes: 12 additions & 1 deletion src/core/qgsapplication.cpp
Expand Up @@ -75,6 +75,8 @@

#include "layout/qgspagesizeregistry.h"

#include <QApplication>
#include <QDesktopWidget>
#include <QDir>
#include <QFile>
#include <QFileInfo>
Expand Down Expand Up @@ -1276,7 +1278,6 @@ void QgsApplication::initQgis()

}


QgsAuthManager *QgsApplication::authManager()
{
if ( auto *lInstance = instance() )
Expand Down Expand Up @@ -1821,6 +1822,16 @@ void QgsApplication::setCustomVariable( const QString &name, const QVariant &val
emit instance()->customVariablesChanged();
}

int QgsApplication::scaleIconSize( int standardSize )
{
QFontMetrics fm( ( QFont() ) );
const double scale = 1.1 * standardSize / 24;
int scaledIconSize = static_cast< int >( std::floor( std::max( Qgis::UI_SCALE_FACTOR * fm.height() * scale, static_cast< double >( standardSize ) ) ) );
if ( QApplication::desktop() )
scaledIconSize *= QApplication::desktop()->devicePixelRatio();
return scaledIconSize;
}

int QgsApplication::maxConcurrentConnectionsPerPool() const
{
return CONN_POOL_MAX_CONCURRENT_CONNS;
Expand Down
11 changes: 11 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -871,6 +871,17 @@ class CORE_EXPORT QgsApplication : public QApplication
*/
static void setCustomVariable( const QString &name, const QVariant &value );

/**
* Scales an icon size to compensate for display pixel density, making the icon
* size hi-dpi friendly, whilst still resulting in pixel-perfect sizes for low-dpi
* displays.
*
* \a standardSize should be set to a standard icon size, e.g. 16, 24, 48, etc.
*
* \since QGIS 3.16
*/
static int scaleIconSize( int standardSize );

/**
* The maximum number of concurrent connections per connections pool.
*
Expand Down
5 changes: 2 additions & 3 deletions src/gui/qgsguiutils.cpp
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/
#include "qgsguiutils.h"

#include "qgsapplication.h"
#include "qgssettings.h"
#include "qgsencodingfiledialog.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -243,9 +244,7 @@ namespace QgsGuiUtils

int scaleIconSize( int standardSize )
{
QFontMetrics fm( ( QFont() ) );
const double scale = 1.1 * standardSize / 24;
return static_cast< int >( std::floor( std::max( Qgis::UI_SCALE_FACTOR * fm.height() * scale, static_cast< double >( standardSize ) ) ) );
return QgsApplication::scaleIconSize( standardSize );
}

QSize iconSize( bool dockableToolbar )
Expand Down

0 comments on commit 0607504

Please sign in to comment.