• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaComponents

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • plasmacomponents
units.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013 Marco Martin <mart@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "units.h"
21 
22 #include <QApplication>
23 #include <QDebug>
24 #include <QDesktopWidget>
25 #include <QtGlobal>
26 #include <cmath>
27 
28 #include <Plasma/Theme>
29 
30 Units::Units (QObject *parent)
31  : QObject(parent),
32  m_gridUnit(-1)
33 {
34  themeChanged();
35  connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
36  this, SLOT(themeChanged()));
37 }
38 
39 Units::~Units()
40 {
41 }
42 
43 qreal Units::gridUnit() const
44 {
45  return m_gridUnit;
46 }
47 
48 qreal Units::dp(qreal value) const
49 {
50  //Usual "default" is 96 dpi
51  //that magic ratio follows the definition of "device independent pixel" by Microsoft
52  const qreal ratio = (qreal)QApplication::desktop()->physicalDpiX() / (qreal)96;
53 
54  if (value <= 2.0) {
55  return qRound(value * floor(ratio));
56  } else {
57  return qRound(value * ratio);
58  }
59 }
60 
61 qreal Units::gu(qreal value) const
62 {
63  return qRound(m_gridUnit * value);
64 }
65 
66 void Units::themeChanged()
67 {
68  const int gridUnit = QFontMetrics(Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont)).boundingRect("M").width();
69  if (gridUnit != m_gridUnit) {
70  m_gridUnit = gridUnit;
71  emit gridUnitChanged();
72  }
73 }
74 
75 #include "units.moc"
76 
Units::gu
Q_INVOKABLE qreal gu(qreal value) const
Definition: units.cpp:61
QPaintDevice::physicalDpiX
int physicalDpiX() const
Units::gridUnitChanged
void gridUnitChanged()
QFontMetrics
QFontMetrics::boundingRect
QRect boundingRect(QChar ch) const
QObject
Units::~Units
~Units()
Definition: units.cpp:39
Units::Units
Units(QObject *parent=0)
Definition: units.cpp:30
Units::dp
Q_INVOKABLE qreal dp(qreal value) const
Definition: units.cpp:48
units.h
QRect::width
int width() const
QApplication::desktop
QDesktopWidget * desktop()
Units::gridUnit
qreal gridUnit() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaComponents

Skip menu "PlasmaComponents"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal