/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2013 Frederik Heber. All rights reserved.
 * 
 *
 *   This file is part of MoleCuilder.
 *
 *    MoleCuilder 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 of the License, or
 *    (at your option) any later version.
 *
 *    MoleCuilder 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 MoleCuilder.  If not, see .
 */
/*
 * QtFragmentList.cpp
 *
 *  Created on: Sep 03, 2013
 *      Author: heber
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "Views/Qt4/QtFragmentList.hpp"
#include 
#include 
#include 
#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Log.hpp"
#include "Descriptors/AtomIdDescriptor.hpp"
#include "Formula.hpp"
#include "Fragmentation/KeySetsContainer.hpp"
#include "Fragmentation/Summation/IndexSetContainer.hpp"
#include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
#include "Fragmentation/Summation/Containers/MPQCData.hpp"
#include "World.hpp"
using namespace std;
const int QtFragmentList::COLUMNCOUNT = COLUMNTYPES_MAX;
const char *QtFragmentList::COLUMNNAMES[QtFragmentList::COLUMNCOUNT]={"Number","KeySet","Formula","Energy value","Energy contribution"};
QtFragmentList::QtFragmentList(QWidget * _parent) :
    QTreeWidget (_parent),
    Observer("QtFragmentList")
{
  setColumnCount(COLUMNCOUNT);
  QStringList header;
	for(int i=0; isetText(NUMBER, QString::number(count));
    {
      std::stringstream output;
      output << **keyiter;
      treeItem->setText(KEYSET, QString(output.str().c_str()));
    }
    {
      std::stringstream output;
      Formula formula;
      for (IndexSet::const_iterator indexiter = (*keyiter)->begin();
          indexiter != (*keyiter)->end();
          ++indexiter) {
        const atom * const _atom = const_cast(World::getInstance()).
            getAtom(AtomById(*indexiter));
        if (_atom != NULL)
          formula.addElements(_atom->getElementNo(), 1);
        else
          ELOG(2, "QtFragmentList::refill() - Could not find id " << *indexiter << " for formula.");
      }
      output << formula.toString();
      treeItem->setText(FORMULA, QString(output.str().c_str()));
    }
    if (summedshortrange.empty()) {
      {
        std::stringstream output;
        output << resultiter->second.energies.total;
        treeItem->setText(ENERGYVALUE, QString(output.str().c_str()));
      }
      {
        std::stringstream output;
        output << "";
        treeItem->setText(ENERGYCONTRIBUTION, QString(output.str().c_str()));
      }
    } else {
      FragmentationShortRangeResults::summedshortrange_t::const_iterator sumresiter =
          summedshortrange.find(*keyiter);
      ASSERT( sumresiter != summedshortrange.end(),
          "QtFragmentList::refill() - "+toString(*keyiter)+" not present in summed results.");
      {
        std::stringstream output;
        output << sumresiter->second.first.energies.total;
        treeItem->setText(ENERGYVALUE, QString(output.str().c_str()));
      }
      {
        std::stringstream output;
        output << sumresiter->second.second.energies.total;
        treeItem->setText(ENERGYCONTRIBUTION, QString(output.str().c_str()));
      }
    }
    FragmentSelection.push_back(treeItem->isSelected());
    indexsets.push_back( **keyiter );
  }
  dirty = false;
  clearing = false;
}
void QtFragmentList::paintEvent(QPaintEvent * event)
{
  if (dirty)
    refill();
  QTreeWidget::paintEvent(event);
}
void QtFragmentList::subjectKilled(Observable *publisher)
{
  // as a new instance should always already be present ... just sign on
  FragmentationResultContainer &fragments =
      FragmentationResultContainer::getInstance();
  fragments.signOn(this);
}
void QtFragmentList::rowSelected()
{
  //std::cout << "rowSelected\n";
  // unselect
  for (int i=0;iisSelected();
    if (newSelection != FragmentSelection[i]){
      // get the keyset and select the specific atoms
      if (!newSelection)
        for (IndexSet::const_iterator iter = indexsets[i].begin();
            iter != indexsets[i].end();
            ++iter)
          World::getInstance().unselectAtom(*iter);
    }
  }
  // select all
  for (int i=0;iisSelected();
    if (newSelection != FragmentSelection[i]){
      FragmentSelection[i] = newSelection;
      // get the keyset and select the specific atoms
      if (newSelection)
        for (IndexSet::const_iterator iter = indexsets[i].begin();
            iter != indexsets[i].end();
            ++iter)
          World::getInstance().selectAtom(*iter);
    }
  }
}