add Units from Plasma1

This commit is contained in:
Marco Martin 2013-05-15 17:57:13 +02:00
parent e07cbe99af
commit 75cbf80f47
3 changed files with 142 additions and 0 deletions

View File

@ -10,6 +10,7 @@ set(plasmacomponents_SRCS
enums.cpp
qmenu.cpp
qmenuitem.cpp
units.cpp
#../core/declarativeitemcontainer.cpp
)

View File

@ -0,0 +1,75 @@
/***************************************************************************
* Copyright 2013 Marco Martin <mart@kde.org > *
* *
* 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 <QApplication>
#include <QDebug>
#include <QDesktopWidget>
#include <QtGlobal>
#include <cmath>
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"

View File

@ -0,0 +1,66 @@
/***************************************************************************
* Copyright 2013 Marco Martin <mart@kde.org> *
* *
* 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 <QObject>
#include <Plasma/Theme>
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