Skip to content

Commit

Permalink
[hidpi] Auto-scale cursors based on DPIs
Browse files Browse the repository at this point in the history
This should work well on different DPI screens.

Still needs some testing on the various platforms.
  • Loading branch information
elpaso committed Nov 27, 2017
1 parent 7ae8e16 commit 346ab60
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
24 changes: 13 additions & 11 deletions images/themes/default/cursors/mSampler.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions python/core/qgsapplication.sip
Expand Up @@ -360,6 +360,8 @@ Returns the path to the default theme directory.
%Docstring
Helper to get a theme cursor. It will fall back to the
default theme if the active theme does not have the required icon.
Cursors are automatically scaled to look like a 16px cursor on 96dpi
screens.
:rtype: QCursor
%End

Expand Down
11 changes: 5 additions & 6 deletions src/core/qgsapplication.cpp
Expand Up @@ -487,10 +487,7 @@ QCursor QgsApplication::getThemeCursor( const Cursor &cursor )
if ( app && app->mCursorCache.contains( cursor ) )
return app->mCursorCache.value( cursor );

// Cursor are supposed to be 32x32 as it seems to be the
// most cross-platform size
// If we want to make this size user-configurable or make
// a better guess: we might use fontMetrics
// All calculations are done on 32x32 icons
// Defaults to center, individual cursors may override
int activeX = 16;
int activeY = 16;
Expand Down Expand Up @@ -520,7 +517,7 @@ QCursor QgsApplication::getThemeCursor( const Cursor &cursor )
break;
case Sampler:
activeX = 0;
activeY = 0;
activeY = 32;
name = QStringLiteral( "mSampler.svg" );
break;
// No default
Expand All @@ -533,7 +530,9 @@ QCursor QgsApplication::getThemeCursor( const Cursor &cursor )
// Check if an icon exists for this cursor (the O.S. default cursor will be used if it does not)
if ( ! icon.isNull( ) )
{
_cursor = QCursor( icon.pixmap( 32, 32 ), activeX, activeY );
// Apply scaling
float scale( ( float ) app->fontMetrics().height() / 32 );
_cursor = QCursor( icon.pixmap( std::ceil( scale * 32 ), std::ceil( scale * 32 ) ), std::ceil( scale * activeX ), std::ceil( scale * activeY ) );
}
if ( app )
app->mCursorCache.insert( cursor, _cursor );
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -329,6 +329,8 @@ class CORE_EXPORT QgsApplication : public QApplication
/**
* Helper to get a theme cursor. It will fall back to the
* default theme if the active theme does not have the required icon.
* Cursors are automatically scaled to look like a 16px cursor on 96dpi
* screens.
*/
static QCursor getThemeCursor( const Cursor &cursor );

Expand Down

0 comments on commit 346ab60

Please sign in to comment.