diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 7f9351163..f37d0dfd4 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -101,6 +101,7 @@ set( libGuiSources resolvers/JSResolverHelper.cpp resolvers/ScriptEngine.cpp + utils/DpiScaler.cpp utils/ImageRegistry.cpp utils/WidgetDragFilter.cpp utils/XspfGenerator.cpp diff --git a/src/libtomahawk/utils/DpiScaler.cpp b/src/libtomahawk/utils/DpiScaler.cpp new file mode 100644 index 000000000..8b7897abc --- /dev/null +++ b/src/libtomahawk/utils/DpiScaler.cpp @@ -0,0 +1,62 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Teo Mrnjavac + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "DpiScaler.h" + + +namespace TomahawkUtils +{ + + +DpiScaler::DpiScaler( const QPaintDevice* that ) + : that( that ) +{ +} + + +QSize +DpiScaler::scaled( int w, int h ) const +{ + float ratioX = that->logicalDpiX() / 100.0; + float ratioY = that->logicalDpiY() / 100.0; + return QSize( qRound( w * ratioX ), qRound( h * ratioY ) ); +} + + +QSize +DpiScaler::scaled( const QSize& size ) const +{ + return scaled( size.width(), size.height() ); +} + + +int +DpiScaler::scaledX( int x ) const +{ + return scaled( x, 0 ).width(); +} + + +int +DpiScaler::scaledY( int y ) const +{ + return scaled( 0, y ).height(); +} + + +} diff --git a/src/libtomahawk/utils/DpiScaler.h b/src/libtomahawk/utils/DpiScaler.h new file mode 100644 index 000000000..b2598a149 --- /dev/null +++ b/src/libtomahawk/utils/DpiScaler.h @@ -0,0 +1,51 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Teo Mrnjavac + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef DPISCALER_H +#define DPISCALER_H + +#include "DllMacro.h" + +#include +#include + +namespace TomahawkUtils +{ + +/** + * @brief The DpiScaler class provides some convenience methods to recompute fixed pixel sizes + * into values suitable for different environment DPI settings. + * Usage: + * class Foo : public QWidget, private TomahawkUtils::DpiScaler {...}; + */ +class DLLEXPORT DpiScaler +{ +public: + DpiScaler( const QPaintDevice* that ); + QSize scaled( int w, int h ) const; + QSize scaled( const QSize& size ) const; + int scaledX( int x ) const; + int scaledY( int y ) const; + +private: + const QPaintDevice* that; +}; + +} + +#endif // DPISCALER_H