diff --git a/src/declarativeimports/plasmacomponents/CMakeLists.txt b/src/declarativeimports/plasmacomponents/CMakeLists.txt index 4334ea732..a7bdeb358 100644 --- a/src/declarativeimports/plasmacomponents/CMakeLists.txt +++ b/src/declarativeimports/plasmacomponents/CMakeLists.txt @@ -10,6 +10,7 @@ set(plasmacomponents_SRCS enums.cpp qmenu.cpp qmenuitem.cpp + units.cpp #../core/declarativeitemcontainer.cpp ) diff --git a/src/declarativeimports/plasmacomponents/units.cpp b/src/declarativeimports/plasmacomponents/units.cpp new file mode 100644 index 000000000..0f641f6e9 --- /dev/null +++ b/src/declarativeimports/plasmacomponents/units.cpp @@ -0,0 +1,75 @@ +/*************************************************************************** + * Copyright 2013 Marco Martin * + * * + * This program 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 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * + ***************************************************************************/ + +#include "units.h" + +#include +#include +#include +#include +#include + + +Units::Units (QObject *parent) + : QObject(parent), + m_gridUnit(-1) +{ + themeChanged(); + connect(&m_theme, SIGNAL(themeChanged()), + this, SLOT(themeChanged())); +} + +Units::~Units() +{ +} + +qreal Units::gridUnit() const +{ + return m_gridUnit; +} + +qreal Units::dp(qreal value) const +{ + //Usual "default" is 96 dpi + //that magic ratio follows the definition of "device independent pixel" by Microsoft + const qreal ratio = (qreal)QApplication::desktop()->physicalDpiX() / (qreal)96; + + if (value <= 2.0) { + return qRound(value * floor(ratio)); + } else { + return qRound(value * ratio); + } +} + +qreal Units::gu(qreal value) const +{ + return qRound(m_gridUnit * value); +} + +void Units::themeChanged() +{ + const int gridUnit = QFontMetrics(QApplication::font()).boundingRect("M").width(); + if (gridUnit != m_gridUnit) { + m_gridUnit = gridUnit; + emit gridUnitChanged(); + } +} + +#include "units.moc" + diff --git a/src/declarativeimports/plasmacomponents/units.h b/src/declarativeimports/plasmacomponents/units.h new file mode 100644 index 000000000..84c14e1bf --- /dev/null +++ b/src/declarativeimports/plasmacomponents/units.h @@ -0,0 +1,66 @@ +/*************************************************************************** + * Copyright 2013 Marco Martin * + * * + * This program 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 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * + ***************************************************************************/ + +#ifndef UNITS_H +#define UNITS_H + +#include + +#include + +class Units : public QObject +{ + Q_OBJECT + + /** + * The fundamental unit of space that should be used for sizes, expressed in pixels. + * Given the screen has an accurate DPI settings, it corresponds to a millimeter + */ + Q_PROPERTY(qreal gridUnit READ gridUnit NOTIFY gridUnitChanged()) + +public: + Units(QObject *parent = 0); + ~Units(); + + qreal gridUnit() const; + + /** + * @returns the number of pixels value density independent pixels correspond to. + */ + Q_INVOKABLE qreal dp(qreal value) const; + + /** + * @returns the number of pixels value grid units correspond to. + */ + Q_INVOKABLE qreal gu(qreal value) const; + + +Q_SIGNALS: + void gridUnitChanged(); + +private Q_SLOTS: + void themeChanged(); + +private: + int m_gridUnit; + Plasma::Theme m_theme; +}; + +#endif //UNITS_H +