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

KmPlot

  • kde-4.x
  • kdeedu
  • kmplot
  • kmplot
constants.cpp
Go to the documentation of this file.
1 /*
2 * KmPlot - a math. function plotter for the KDE-Desktop
3 *
4 * Copyright (C) 1998, 1999, 2000, 2002 Klaus-Dieter Möller <[email protected]>
5 * 2006 David Saxton <[email protected]>
6 *
7 * This file is part of the KDE Project.
8 * KmPlot is part of the KDE-EDU Project.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 *
24 */
25 
26 
27 #include "constants.h"
28 #include "xparser.h"
29 
30 #include <KConfig>
31 
32 
33 //BEGIN class Constant
34 Constant::Constant( )
35 {
36  // By default, have both types
37  // This minimizes loss of information
38  type = Document | Global;
39 }
40 //END class Constant
41 
42 
43 
44 //BEGIN class Constants
45 Constants::Constants()
46 {
47 }
48 
49 
50 Constants::~Constants()
51 {
52 }
53 
54 
55 Value Constants::value( const QString & name ) const
56 {
57  return m_constants[ name ].value;
58 }
59 
60 
61 bool Constants::have( const QString & name ) const
62 {
63  return m_constants.contains( name );
64 }
65 
66 
67 void Constants::remove( const QString & name )
68 {
69  if ( m_constants.remove( name ) > 0 )
70  emit constantsChanged();
71 }
72 
73 
74 void Constants::add( const QString & name, const Constant & constant )
75 {
76  m_constants[name] = constant;
77  emit constantsChanged();
78 }
79 
80 
81 ConstantList Constants::list( int type ) const
82 {
83  ConstantList list;
84 
85  for ( ConstantList::const_iterator it = m_constants.begin(); it != m_constants.end(); ++it )
86  {
87  if ( type & it.value().type )
88  list.insert( it.key(), it.value() );
89  }
90 
91  return list;
92 }
93 
94 
95 bool Constants::isValidName( const QString & name ) const
96 {
97  // Don't allow empty names
98  if ( name.isEmpty() )
99  return false;
100 
101  // Don't allow constants names that are already used by a function
102  if ( XParser::self()->predefinedFunctions( true ).contains( name ) ||
103  XParser::self()->userFunctions().contains( name ) )
104  return false;
105 
106  // special cases: don't allow predefined constants either
107  if ( name == QLatin1String("pi") || name == PiSymbol || name == QLatin1String("e") || name == InfinitySymbol )
108  return false;
109 
110  // Now make sure that the constant name contains only letters
111  for ( int i = 0; i < name.length(); ++i )
112  {
113  if ( !name.at(i).isLetter() )
114  return false;
115  }
116 
117  // All ok!
118  return true;
119 }
120 
121 
122 QString Constants::generateUniqueName() const
123 {
124  QString name;
125  int at = 0;
126  while (true)
127  {
128  at++;
129  name.resize( at );
130  for ( char c = 'A'; c <= 'Z'; ++c )
131  {
132  name[at-1] = c;
133  if ( isValidName(name) && !have(name) )
134  return name;
135  }
136  }
137 }
138 
139 
140 void Constants::load()
141 {
143 
144  KConfig conf (QStringLiteral("kcalcrc"), KConfig::SimpleConfig);
145  KConfigGroup group = conf.group("UserConstants");
146  QString tmp;
147 
148  for( int i=0; ;i++)
149  {
150  tmp.setNum(i);
151  QString name = group.readEntry("nameConstant"+tmp, QStringLiteral(" "));
152  QString expression = group.readEntry("expressionConstant"+tmp, QStringLiteral(" "));
153  QString value = group.readEntry("valueConstant"+tmp, QStringLiteral(" ") );
154 
155  if ( name == QLatin1String(" ") )
156  return;
157 
158  if ( name.isEmpty() )
159  continue;
160 
161  if ( expression == QLatin1String(" ") )
162  {
163  // Old config file
164  expression = value;
165  }
166 
167  if ( !isValidName( name ) || have( name ) )
168  name = generateUniqueName();
169 
170  Constant constant;
171  constant.value = expression;
172  constant.type = Constant::Global;
173 
174  add( name, constant );
175  }
176 }
177 
178 void Constants::save()
179 {
180  KConfig conf (QStringLiteral("kcalcrc"), KConfig::SimpleConfig);
181  conf.deleteGroup("Constants");
182 
183  // remove any previously saved constants
184  conf.deleteGroup( "UserConstants" );
185 
186  KConfigGroup group = conf.group("UserConstants");
187  QString tmp;
188 
189  ConstantList global = list( Constant::Global );
190 
191  int i = 0;
192  for ( ConstantList::iterator it = global.begin(); it != global.end(); ++it )
193  {
194  tmp.setNum(i);
195  group.writeEntry( "nameConstant"+tmp, it.key() ) ;
196  group.writeEntry( "expressionConstant"+tmp, it.value().value.expression() );
197  group.writeEntry( "valueConstant"+tmp, it.value().value.value() );
198 
199  i++;
200  }
201 }
202 //END class Constants
Constants::generateUniqueName
QString generateUniqueName() const
Definition: constants.cpp:122
QMap::contains
bool contains(const Key &key) const
Constants::constantsChanged
void constantsChanged()
Emitted when a constant is added or removed, or the value of an existing constant has changed...
PiSymbol
#define PiSymbol
Definition: parser.h:42
QMap
constants.h
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
Constants::load
void load()
Load the constants at the start.
Definition: constants.cpp:140
Constant::Global
The constant is to be saved globally in the application settings.
Definition: constants.h:50
Constants::value
Value value(const QString &name) const
Definition: constants.cpp:55
Constant::Constant
Constant()
Definition: constants.cpp:34
XParser::self
static XParser * self()
Definition: xparser.cpp:50
QChar::isLetter
bool isLetter() const
QObject::name
const char * name() const
QString::resize
void resize(int size)
Constants::m_constants
ConstantList m_constants
Definition: constants.h:132
Constants::list
ConstantList list(int type) const
Definition: constants.cpp:81
Parser::predefinedFunctions
QStringList predefinedFunctions(bool includeAliases) const
Definition: parser.cpp:173
Constant::value
Value value
The actual value of the constant.
Definition: constants.h:57
Constant::Document
The constant is part of the document.
Definition: constants.h:49
InfinitySymbol
#define InfinitySymbol
Definition: parser.h:43
QString::isEmpty
bool isEmpty() const
QMap::const_iterator
Constants::add
void add(const QString &name, const Constant &constant)
Adds the constant to the internal list (overwriting any previous constant with the same name)...
Definition: constants.cpp:74
Constants::remove
void remove(const QString &name)
Removes the constant with the given name from the constants list.
Definition: constants.cpp:67
QString
xparser.h
QMap::end
iterator end()
QMap::begin
iterator begin()
Constants::Constants
Constants()
Definition: constants.cpp:45
Constants::have
bool have(const QString &name) const
Definition: constants.cpp:61
Constant::type
int type
The OR'ed types.
Definition: constants.h:61
QLatin1String
Value
This stores a string which evaluates directly to a number (i.e.
Definition: function.h:53
QString::setNum
QString & setNum(short n, int base)
QString::at
const QChar at(int position) const
Constant
Stores the details of a constant other than its name.
Definition: constants.h:39
QString::length
int length() const
Parser::userFunctions
QStringList userFunctions() const
Definition: parser.cpp:191
QMap::insert
iterator insert(const Key &key, const T &value)
Constants::save
void save()
Save the constants when closing the program.
Definition: constants.cpp:178
Constants::~Constants
virtual ~Constants()
Definition: constants.cpp:50
QMap::iterator
QMap::value
const T value(const Key &key) const
QMap::remove
int remove(const Key &key)
Constants::isValidName
bool isValidName(const QString &name) const
Definition: constants.cpp:95
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Thu Dec 5 2019 03:23:21 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KmPlot

Skip menu "KmPlot"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  •   KmPlot
  • libkeduvocdocument
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   src
  •   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