AbaKus – a complex calculator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

106 lines
2.7 KiB

  1. /*
  2. * valuemanager.cpp - part of abakus
  3. * Copyright (C) 2004, 2005 Michael Pyne <michael.pyne@kdemail.net>
  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 02110-1301 USA
  18. */
  19. #include <kdebug.h>
  20. #include <klocale.h>
  21. #include <tqregexp.h>
  22. #include "numerictypes.h"
  23. #include "valuemanager.h"
  24. ValueManager *ValueManager::m_manager = 0;
  25. ValueManager *ValueManager::instance()
  26. {
  27. if(!m_manager)
  28. m_manager = new ValueManager;
  29. return m_manager;
  30. }
  31. ValueManager::ValueManager(TQObject *parent, const char *name) :
  32. TQObject(parent, name)
  33. {
  34. m_values.insert("pi", Abakus::number_t::PI);
  35. m_values.insert("e", Abakus::number_t::E);
  36. }
  37. Abakus::number_t ValueManager::value(const TQString &name) const
  38. {
  39. return m_values[name];
  40. }
  41. bool ValueManager::isValueSet(const TQString &name) const
  42. {
  43. return m_values.contains(name);
  44. }
  45. bool ValueManager::isValueReadOnly(const TQString &name) const
  46. {
  47. TQRegExp readOnlyValues("^(ans|pi|e|stackCount)$");
  48. return name.find(readOnlyValues) != -1;
  49. }
  50. void ValueManager::setValue(const TQString &name, const Abakus::number_t value)
  51. {
  52. if(m_values.contains(name) && this->value(name) != value)
  53. emit signalValueChanged(name, value);
  54. else if(!m_values.contains(name))
  55. emit signalValueAdded(name, value);
  56. m_values.replace(name, value);
  57. }
  58. void ValueManager::removeValue(const TQString &name)
  59. {
  60. if(m_values.contains(name))
  61. emit signalValueRemoved(name);
  62. m_values.remove(name);
  63. }
  64. void ValueManager::slotRemoveUserVariables()
  65. {
  66. TQStringList vars = valueNames();
  67. for(TQStringList::ConstIterator var = vars.constBegin(); var != vars.constEnd(); ++var)
  68. if(!isValueReadOnly(*var))
  69. removeValue(*var);
  70. }
  71. TQStringList ValueManager::valueNames() const
  72. {
  73. return m_values.keys();
  74. }
  75. TQString ValueManager::description(const TQString &valueName)
  76. {
  77. if(valueName == "e")
  78. return i18n("Natural exponential base - 2.7182818");
  79. if(valueName == "pi")
  80. return i18n("pi (π) - 3.1415926");
  81. return TQString();
  82. }
  83. #include "valuemanager.moc"
  84. // vim: set et ts=8 sw=4 encoding=utf-8: