<commit_before>// Begin CVS Header
//   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/UI/CQFunctionDM.cpp,v $
//   $Revision: 1.4 $
//   $Name:  $
//   $Author: shoops $
//   $Date: 2009/07/30 13:41:34 $
// End CVS Header
// Copyright (C) 2008 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

#include <QString>
#include <QList>

#include "CopasiDataModel/CCopasiDataModel.h"
#include "report/CCopasiRootContainer.h"
#include "function/CFunctionDB.h"

#include "CQMessageBox.h"
#include "CQFunctionDM.h"
#include "qtUtilities.h"

CQFunctionDM::CQFunctionDM(QObject *parent)
    : CQBaseDataModel(parent)

{
}

int CQFunctionDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
{
  return CCopasiRootContainer::getFunctionList()->loadedFunctions().size() + 1;
}
int CQFunctionDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
{
  return TOTAL_COLS_FUNCTIONS;
}

Qt::ItemFlags CQFunctionDM::flags(const QModelIndex &index) const
{
  if (!index.isValid())
    return Qt::ItemIsEnabled;

  if (!isDefaultRow(index) && isFunctionReadOnly(index))
    return QAbstractItemModel::flags(index);

  if (index.column() == COL_NAME_FUNCTIONS)
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
  else
    return QAbstractItemModel::flags(index);
}

bool CQFunctionDM::isFunctionReadOnly(const QModelIndex &index) const
{
  const CEvaluationTree *pFunc = CCopasiRootContainer::getFunctionList()->loadedFunctions()[index.row()];

  switch (pFunc->getType())
    {
      case CEvaluationTree::PreDefined:
      case CEvaluationTree::MassAction:
        return true;
        break;
      case CEvaluationTree::UserDefined:
      case CEvaluationTree::Function:
      case CEvaluationTree::Expression:
        return false;
        break;
    }

  return true;
}

QVariant CQFunctionDM::data(const QModelIndex &index, int role) const
{
  if (!index.isValid())
    return QVariant();

  if (index.row() >= rowCount())
    return QVariant();

  if (index.column() > 0 && role == Qt::ForegroundRole && !(flags(index) & Qt::ItemIsEditable))
    return QColor(Qt::darkGray);

  if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
      if (isDefaultRow(index))
        {
          switch (index.column())
            {
              case COL_ROW_NUMBER:
                return QVariant(index.row() + 1);
              case COL_NAME_FUNCTIONS:
                return QVariant(QString("No Name"));
              case COL_TYPE_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(CEvaluationTree::TypeName[4])));
              default:
                return QVariant(QString(""));
            }
        }
      else
        {
          const CFunction *pFunc = dynamic_cast<CFunction *>(CCopasiRootContainer::getFunctionList()->loadedFunctions()[index.row()]);

          if (pFunc == NULL)
            return QVariant();

          switch (index.column())
            {
              case COL_ROW_NUMBER:
                return QVariant(index.row() + 1);

              case COL_NAME_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(pFunc->getObjectName())));

              case COL_TYPE_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(CEvaluationTree::TypeName[pFunc->getType()])));

              case COL_MATH_DESC_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(pFunc->getInfix())));

              case COL_SBML_ID_FUNCTIONS:
                return QVariant();
            }
        }
    }

  return QVariant();
}

QVariant CQFunctionDM::headerData(int section, Qt::Orientation orientation,
                                  int role) const
{
  if (role != Qt::DisplayRole)
    return QVariant();

  if (orientation == Qt::Horizontal)
    {
      switch (section)
        {
          case COL_ROW_NUMBER:
            return QVariant(QString("#"));
          case COL_NAME_FUNCTIONS:
            return QVariant(QString("Name"));
          case COL_TYPE_FUNCTIONS:
            return QVariant(QString("Type"));
          case COL_MATH_DESC_FUNCTIONS:
            return QVariant(QString("Mathematical Description"));
          case COL_SBML_ID_FUNCTIONS:
            return QVariant(QString("SBML ID"));
          default:
            return QVariant();
        }
    }
  else
    return QString("%1").arg(section + 1);
}

bool CQFunctionDM::setData(const QModelIndex &index, const QVariant &value,
                           int role)
{
  if (index.isValid() && role == Qt::EditRole)
    {
      bool defaultRow = isDefaultRow(index);

      if (defaultRow)
        {
          if (index.data() != value)
            insertRow();
          else
            return false;
        }

      CFunction *pFunc = dynamic_cast<CFunction *>(CCopasiRootContainer::getFunctionList()->loadedFunctions()[index.row()]);

      if (pFunc == NULL)
        return false;

      if (index.column() == COL_NAME_FUNCTIONS)
        pFunc->setObjectName(TO_UTF8(value.toString()));
      else if (index.column() == COL_TYPE_FUNCTIONS)
        {
          if (index.data() != value)
            {
              QString msg;
              msg = "Type must not be changed for '" + FROM_UTF8(pFunc->getObjectName()) + "'.\n";

              CQMessageBox::information(NULL,
                                        "Unable to change Function Type",
                                        msg,
                                        QMessageBox::Ok, QMessageBox::Ok);<commit_msg>pFunc uses CEvaluationTree* instead of CFunction* which solves the last two empty rows problem.<commit_after>// Begin CVS Header
//   $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/UI/CQFunctionDM.cpp,v $
//   $Revision: 1.5 $
//   $Name:  $
//   $Author: aekamal $
//   $Date: 2009/09/18 18:01:08 $
// End CVS Header
// Copyright (C) 2008 by <NAME>, Virginia Tech Intellectual
// Properties, Inc., EML Research, gGmbH, University of Heidelberg,
// and The University of Manchester.
// All rights reserved.

#include <QString>
#include <QList>

#include "CopasiDataModel/CCopasiDataModel.h"
#include "report/CCopasiRootContainer.h"
#include "function/CFunctionDB.h"

#include "CQMessageBox.h"
#include "CQFunctionDM.h"
#include "qtUtilities.h"

CQFunctionDM::CQFunctionDM(QObject *parent)
    : CQBaseDataModel(parent)

{
}

int CQFunctionDM::rowCount(const QModelIndex& C_UNUSED(parent)) const
{
  return CCopasiRootContainer::getFunctionList()->loadedFunctions().size() + 1;
}
int CQFunctionDM::columnCount(const QModelIndex& C_UNUSED(parent)) const
{
  return TOTAL_COLS_FUNCTIONS;
}

Qt::ItemFlags CQFunctionDM::flags(const QModelIndex &index) const
{
  if (!index.isValid())
    return Qt::ItemIsEnabled;

  if (!isDefaultRow(index) && isFunctionReadOnly(index))
    return QAbstractItemModel::flags(index);

  if (index.column() == COL_NAME_FUNCTIONS)
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
  else
    return QAbstractItemModel::flags(index);
}

bool CQFunctionDM::isFunctionReadOnly(const QModelIndex &index) const
{
  const CEvaluationTree *pFunc = CCopasiRootContainer::getFunctionList()->loadedFunctions()[index.row()];

  switch (pFunc->getType())
    {
      case CEvaluationTree::PreDefined:
      case CEvaluationTree::MassAction:
        return true;
        break;
      case CEvaluationTree::UserDefined:
      case CEvaluationTree::Function:
      case CEvaluationTree::Expression:
        return false;
        break;
    }

  return true;
}

QVariant CQFunctionDM::data(const QModelIndex &index, int role) const
{
  if (!index.isValid())
    return QVariant();

  if (index.row() >= rowCount())
    return QVariant();

  if (index.column() > 0 && role == Qt::ForegroundRole && !(flags(index) & Qt::ItemIsEditable))
    return QColor(Qt::darkGray);

  if (role == Qt::DisplayRole || role == Qt::EditRole)
    {
      if (isDefaultRow(index))
        {
          switch (index.column())
            {
              case COL_ROW_NUMBER:
                return QVariant(index.row() + 1);
              case COL_NAME_FUNCTIONS:
                return QVariant(QString("No Name"));
              case COL_TYPE_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(CEvaluationTree::TypeName[4])));
              default:
                return QVariant(QString(""));
            }
        }
      else
        {
          const CEvaluationTree *pFunc = CCopasiRootContainer::getFunctionList()->loadedFunctions()[index.row()];

          if (pFunc == NULL)
            return QVariant();

          switch (index.column())
            {
              case COL_ROW_NUMBER:
                return QVariant(index.row() + 1);

              case COL_NAME_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(pFunc->getObjectName())));

              case COL_TYPE_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(CEvaluationTree::TypeName[pFunc->getType()])));

              case COL_MATH_DESC_FUNCTIONS:
                return QVariant(QString(FROM_UTF8(pFunc->getInfix())));

              case COL_SBML_ID_FUNCTIONS:
                return QVariant();
            }
        }
    }

  return QVariant();
}

QVariant CQFunctionDM::headerData(int section, Qt::Orientation orientation,
                                  int role) const
{
  if (role != Qt::DisplayRole)
    return QVariant();

  if (orientation == Qt::Horizontal)
    {
      switch (section)
        {
          case COL_ROW_NUMBER:
            return QVariant(QString("#"));
          case COL_NAME_FUNCTIONS:
            return QVariant(QString("Name"));
          case COL_TYPE_FUNCTIONS:
            return QVariant(QString("Type"));
          case COL_MATH_DESC_FUNCTIONS:
            return QVariant(QString("Mathematical Description"));
          case COL_SBML_ID_FUNCTIONS:
            return QVariant(QString("SBML ID"));
          default:
            return QVariant();
        }
    }
  else
    return QString("%1").arg(section + 1);
}

bool CQFunctionDM::setData(const QModelIndex &index, const QVariant &value,
                           int role)
{
  if (index.isValid() && role == Qt::EditRole)
    {
      bool defaultRow = isDefaultRow(index);

      if (defaultRow)
        {
          if (index.data() != value)
            insertRow();
          else
            return false;
        }

      CEvaluationTree *pFunc = CCopasiRootContainer::getFunctionList()->loadedFunctions()[index.row()];

      if (pFunc == NULL)
        return false;

      if (index.column() == COL_NAME_FUNCTIONS)
        pFunc->setObjectName(TO_UTF8(value.toString()));
      else if (index.column() == COL_TYPE_FUNCTIONS)
        {
          if (index.data() != value)
            {
              QString msg;
              msg = "Type must not be changed for '" + FROM_UTF8(pFunc->getObjectName()) + "'.\n";

              CQMessageBox::information(NULL,
                                        "Unable to change Function Type",
                                        msg,
                                        QMessageBox::Ok, QMessageBox::Ok);
{'fraction_non_alphanumeric': 0.12094819071563258, 'fraction_numerical': 0.005926191972703601, 'mean_word_length': 2.400916030534351, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4522496', 'n_tokens_mistral': 3339, 'n_tokens_neox': 3248, 'n_words': 662}