• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • misc
unit.cc
Go to the documentation of this file.
1 /*
2  This file is part of Kig, a KDE program for Interactive Geometry...
3  Copyright (C) 2006 Pino Toscano <toscano.pino@tiscali.it>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #include "unit.h"
22 
23 #include <kdebug.h>
24 #include <klocale.h>
25 
26 Unit::Unit( double value, Unit::MetricalUnit unit, int dpi )
27 {
28  mvalue = value;
29  munit = unit;
30  mdpi = dpi;
31 }
32 
33 Unit::~Unit()
34 {
35 }
36 
37 void Unit::setValue( double value )
38 {
39  mvalue = value;
40 }
41 
42 double Unit::value() const
43 {
44  return mvalue;
45 }
46 
47 void Unit::setUnit( Unit::MetricalUnit unit )
48 {
49  munit = unit;
50 }
51 
52 void Unit::convertTo( Unit::MetricalUnit unit )
53 {
54  mvalue = convert( mvalue, munit, unit, mdpi );
55  munit = unit;
56 }
57 
58 Unit::MetricalUnit Unit::unit() const
59 {
60  return munit;
61 }
62 
63 double Unit::getValue( Unit::MetricalUnit unit ) const
64 {
65  return convert( mvalue, munit, unit, mdpi );
66 }
67 
68 void Unit::setDpi( int dpi )
69 {
70  mdpi = dpi;
71 }
72 
73 int Unit::dpi() const
74 {
75  return mdpi;
76 }
77 
78 Unit& Unit::operator=( const Unit& u )
79 {
80  mvalue = u.mvalue;
81  munit = u.munit;
82  mdpi = u.mdpi;
83  return *this;
84 }
85 
86 double Unit::convert( double value, Unit::MetricalUnit from, Unit::MetricalUnit to, int dpi )
87 {
88  switch ( from )
89  {
90  case pixel:
91  {
92  if ( to == cm )
93  return value / dpi * 2.53995;
94  if ( to == in )
95  return value / dpi;
96  break;
97  }
98  case cm:
99  {
100  if ( to == pixel )
101  return value * 0.3937 * dpi;
102  if ( to == in )
103  return value * 0.3937;
104  break;
105  }
106  case in:
107  {
108  if ( to == pixel )
109  return value * dpi;
110  if ( to == cm )
111  return value * 2.53995;
112  break;
113  }
114  }
115  return value;
116 }
117 
118 QStringList Unit::unitList()
119 {
120  QStringList ul;
121  ul << i18nc( "Translators: Pixel", "pixel" );
122  ul << i18nc( "Translators: Centimeter", "cm" );
123  ul << i18nc( "Translators: Inch", "in" );
124  return ul;
125 }
126 
127 Unit::MetricalUnit Unit::intToUnit( int index )
128 {
129  if ( index == 0 )
130  return pixel;
131  else if ( index == 1 )
132  return cm;
133  else if ( index == 2 )
134  return in;
135  kDebug() << "No measure unit with index " << index;
136  return pixel;
137 }
138 
139 int Unit::precision( Unit::MetricalUnit unit )
140 {
141  if ( unit == Unit::pixel )
142  return 0;
143  else
144  return 2;
145 }
Unit::setUnit
void setUnit(Unit::MetricalUnit unit)
Set the unit of the current object to unit, but it doesn't convert the value to the new unit...
Definition: unit.cc:47
Unit::setDpi
void setDpi(int dpi)
Definition: unit.cc:68
Unit::MetricalUnit
MetricalUnit
The kinds of metrical units we support.
Definition: unit.h:36
Unit::cm
Definition: unit.h:36
Unit::unitList
static QStringList unitList()
Get a list of the supported metrical units.
Definition: unit.cc:118
Unit::getValue
double getValue(Unit::MetricalUnit unit) const
Definition: unit.cc:63
Unit::unit
Unit::MetricalUnit unit() const
Definition: unit.cc:58
Unit::convertTo
void convertTo(Unit::MetricalUnit unit)
Set the unit of the current object to unit and convert the value to the new unit using convert()...
Definition: unit.cc:52
Unit::pixel
Definition: unit.h:36
Unit::setValue
void setValue(double value)
Definition: unit.cc:37
Unit::convert
static double convert(double value, Unit::MetricalUnit from, Unit::MetricalUnit to, int dpi=1)
The most useful method of this class: convert the specified value from the unit from to the unit to...
Definition: unit.cc:86
Unit::Unit
Unit(double value=0.0, Unit::MetricalUnit unit=cm, int dpi=1)
Definition: unit.cc:26
Unit::dpi
int dpi() const
Definition: unit.cc:73
Unit::precision
static int precision(Unit::MetricalUnit unit)
How many decimals the unit have.
Definition: unit.cc:139
unit.h
Unit::in
Definition: unit.h:36
Unit::intToUnit
static Unit::MetricalUnit intToUnit(int index)
Definition: unit.cc:127
Unit::operator=
Unit & operator=(const Unit &u)
Definition: unit.cc:78
Unit::~Unit
~Unit()
Definition: unit.cc:33
Unit::value
double value() const
Definition: unit.cc:42
Unit
This small class server as helper to perform conversions between metrical units.
Definition: unit.h:30
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

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