/*
 * Project: MoleCuilder
 * Description: creates and alters molecular systems
 * Copyright (C)  2010-2012 University of Bonn. 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 .
 */
/*
 * QtShapeList.cpp
 *
 *  Created on: Sep 14, 2012
 *      Author: ankele
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include "Views/Qt4/QtShapeList.hpp"
#include 
#include "CodePatterns/MemDebug.hpp"
#include "CodePatterns/Observer/Notification.hpp"
#include "Shapes/ShapeFactory.hpp"
#include "Shapes/ShapeRegistry.hpp"
#include 
#include "Actions/SelectionAction/Shapes/ShapeByNameAction.hpp"
#include "Actions/SelectionAction/Shapes/NotShapeByNameAction.hpp"
using namespace std;
const int QtShapeList::COLUMNCOUNT = COLUMNTYPES_MAX;
const char *QtShapeList::COLUMNNAMES[QtShapeList::COLUMNCOUNT]={"Name","Type","Center","Radius"};
QtShapeList::QtShapeList(QWidget * _parent) :
    QTreeWidget (_parent),
    Observer("QtShapeList")
{
  setColumnCount(COLUMNCOUNT);
  setSelectionMode(QAbstractItemView::MultiSelection);
  QStringList header;
	for(int i=0; i(publisher) == ShapeRegistry::getPointer()) {
    switch (notification->getChannelNo()) {
      case ShapeRegistry::ShapeInserted:
      {
        refill(NULL);
        break;
      }
      case ShapeRegistry::ShapeRemoved:
      {
        refill(ShapeRegistry::getInstance().lastChanged());
        break;
      }
      case ShapeRegistry::SelectionChanged:
      {
        refill(NULL);
        break;
      }
      default:
        ASSERT(0, "QtShapeList::recieveNotification() - we cannot get here.");
        break;
    }
  } else
    ASSERT(0, "QtShapeList::recieveNotification() - received notification from unknown source.");
}
void QtShapeList::refill(::Shape *ignore) {
  clearing = true;
  clear();
  shapeSelection.clear();
  ShapeRegistry ® = ShapeRegistry::getInstance();
  ShapeFactory &factory = ShapeFactory::getInstance();
  ShapeRegistry::const_iterator iter;
  for (iter = reg.getBeginIter(); iter != reg.getEndIter(); iter ++){
    ::Shape *s = iter->second;
    if (s == ignore)
      continue;
    QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
    treeItem->setText(NAME, QString(s->getName().c_str()));
    treeItem->setText(TYPE, QString(factory.getShapeName(s->getType()).c_str()));
    treeItem->setText(CENTER, QString(toString(s->getCenter()).c_str()));
    treeItem->setText(RADIUS, QString::number(s->getRadius()));
    treeItem->setSelected(reg.isSelected(s));
    shapeSelection.push_back(reg.isSelected(s));
  }
  clearing = false;
}
#if 0
void QtShapeList::paintEvent(QPaintEvent * event)
{
  /*if (dirty)
    refill();*/
  QtShapeList::paintEvent(event);
}
#endif
void QtShapeList::subjectKilled(Observable *publisher) {
}
void QtShapeList::rowSelected()
{
  if (clearing)
    return;
  if (selecting)
    return;
  //std::cout << "rowSelected\n";
  selecting = true;
  for (int i=0;iisSelected();
    if (newSelection != shapeSelection[i]){
      shapeSelection[i] = newSelection;
      std::string shapeName = item->text(NAME).toStdString();
      if (newSelection)
        MoleCuilder::SelectionShapeByName(shapeName);
      else
        MoleCuilder::SelectionNotShapeByName(shapeName);
    }
  }
  selecting = false;
}