/* -*- C++ -*-

Copyright (C) 2000-2002 Francesco Bresciani <fbresciani@users.sourceforge.net>
This file is part of Kemistry

Kemistry 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, or (at your option)
any later version.

Kemistry 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 Kemistry; see the file COPYING.  If not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


#ifndef KMOLCALCDATA_H
#define KMOLCALCDATA_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <qdom.h>
#include <qdict.h>
#include <qmap.h>

#include <klocale.h>

class KMolCalcSubUnit
{
public:
  KMolCalcSubUnit();

  KMolCalcSubUnit(const QString& name, const QString& symbol);

  QString name() const {return i18n( _name );}

  QString symbol() const {return _symbol ;}

  virtual double mass();

  virtual ~KMolCalcSubUnit() {}

  virtual unsigned int elementCount(const QString & /*symbol */) const {return 0;}

  static KMolCalcSubUnit *
  createFromDomElement(const QDomElement& element);

  virtual QDomElement toDomElement(QDomDocument &document) = 0;

  bool  userDefined() const {return _userDefined;}
  void setUserDefined(bool val) {_userDefined = val;}

protected:
  QString _name;
  QString _symbol;
  double _mass;
  bool _userDefined;
};

class KMolCalcElement : public KMolCalcSubUnit
{
public:
  KMolCalcElement(const QString& name, const QString& symbol, double mass);
  KMolCalcElement(const QDomElement& element);

  virtual QDomElement toDomElement(QDomDocument &document);

  virtual unsigned int elementCount(const QString& symbol);

};

class KMolCalcGroup : public KMolCalcSubUnit
{
public:
  KMolCalcGroup(const QString &name, const QString &symbol,
                const QString& formula, const QString &parsedFormula);
  KMolCalcGroup(const QDomElement& element);

  QString formula() const {return _formula;}
  QString parsedFormula() const {return _parsedFormula;}

  virtual QDomElement toDomElement(QDomDocument &document);

  virtual double mass();
  virtual double elementCount(const QString& symbol);

  virtual QMap<QString, double> elementMap()
  {
    return _elementsCount;
  }

protected:
  QMap<QString, double> _elementsCount;
  QString _formula;
  QString _parsedFormula;
};

class KMolCalcData
{
public:
  static KMolCalcData* pointer();

  void saveData();

  bool editable() const
        {return _editable; }

  void setEditable(bool editable = true)
        {_editable = editable;}

    bool modified() const
        {
            return _modified;
        }

    void setModified(bool modified = true)
        {
            _modified = modified;
        }
  uint dataCount() const {return _subUnitsData.count();}

  KMolCalcSubUnit* data(const QString&) const;
  KMolCalcElement* element(uint atomicNumber) const;

  const QDict<KMolCalcSubUnit>& dataDictionary() const;

  void removeSubUnit(const QString& symbol);

  void addSubUnit(const QString& symbol, KMolCalcSubUnit* subunit);

  ~KMolCalcData();

private:
  KMolCalcData();
  void loadDataFile(const QString& filename, bool local);
  void saveUserDataFile(const QString& filename);

private:
    static KMolCalcData* s_self;
    bool _editable;
    bool _modified;

  QDict<KMolCalcSubUnit> _subUnitsData;

};

#endif // KMOLCALCDATA_H

