/*
 * 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 .
 */
/*
 * QtStatusBar.cpp
 *
 *  Created on: Feb 17, 2010
 *      Author: crueger
 */
// include config.h
#ifdef HAVE_CONFIG_H
#include 
#endif
#include 
#include 
#include 
#include 
#include 
#include "QtStatusBar.hpp"
#include "CodePatterns/MemDebug.hpp"
#include "World.hpp"
#include "Actions/Process.hpp"
#define PLURAL_S(v) (((v)==1)?"":"s")
using namespace MoleCuilder;
QtStatusBar::QtStatusBar(QWidget *_parent) :
  QStatusBar(_parent),
  Observer("QtStatusBar"),
  atomCount(World::getInstance().numAtoms()),
  moleculeCount(World::getInstance().numMolecules()),
  parent(_parent),
  activeProcess("")
{
  World::getInstance().signOn(this);
  Process::AddObserver(this);
  statusLabel = new QLabel(this);
  statusLabel->setFrameStyle(QFrame::NoFrame | QFrame::Plain);
  addPermanentWidget(statusLabel);
  redrawStatus();
  qRegisterMetaType("std::string");
  connect(
      this, SIGNAL(redrawProgressBar(const std::string, const unsigned int, const unsigned int, const bool)),
      this, SLOT(updateProgressBar(const std::string, const unsigned int, const unsigned int, const bool)));
}
QtStatusBar::~QtStatusBar()
{
  Process::RemoveObserver(this);
  World::getInstance().signOff(this);
}
void QtStatusBar::update(Observable *subject){
  if (subject==World::getPointer()){
    atomCount = World::getInstance().numAtoms();
    moleculeCount = World::getInstance().numMolecules();
    redrawStatus();
  }
  else {
    // we probably have some process
    // as notify comes from ActionQueue's thread, we have to use signal/slots
    // to inform ourselves but within the main() thread to be able to add
    // the progressbar widget.
    Process *proc;
    if((proc=dynamic_cast(subject))){
      const bool StopStatus = proc->doesStop();
      emit redrawProgressBar(proc->getName(), proc->getMaxSteps(), proc->getCurrStep(), StopStatus);
    }
  }
}
void QtStatusBar::subjectKilled(Observable *subject){
  // Processes don't notify when they are killed
  atomCount = World::getInstance().numAtoms();
  moleculeCount = World::getInstance().numMolecules();
  World::getInstance().signOn(this);
  redrawStatus();
}
void QtStatusBar::redrawStatus(){
  stringstream sstr;
  sstr << "You have " << atomCount << " atom" << PLURAL_S(atomCount)
       <<" in " << moleculeCount << " molecule" << PLURAL_S(moleculeCount);
  statusLabel->setText(QString(sstr.str().c_str()));
}
void QtStatusBar::updateProgressBar(
    const std::string name,
    const unsigned int maxsteps,
    const unsigned int currentstep,
    const bool StopStatus)
{
  progressIndicator *ind=0;
  progressBars_t::iterator iter = progressBars.find(name);
  // see what we have to do with the process
  if (iter == progressBars.end()) {
    ind = new progressIndicator(name);
    ind->bar->setMaximum(maxsteps);
    progressBars.insert( std::make_pair(name,ind) );
  } else {
    ind = iter->second;
  }
  if (activeProcess != name) {
    addWidget(ind->container);
    activeProcess = name;
  }
  ind->bar->setValue(currentstep);
  parent->repaint();
  if ((iter != progressBars.end()) && StopStatus) {
    removeWidget(ind->container);
    activeProcess = std::string("");
    progressBars.erase(name);
    delete ind;
  }
}
QtStatusBar::progressIndicator::progressIndicator(const std::string &name){
  stringstream sstr;
  sstr << "Busy (" << name << ")";
  container = new QWidget();
  layout = new QHBoxLayout(container);
  label = new QLabel(QString(sstr.str().c_str()));
  bar = new QProgressBar();
  layout->addWidget(label);
  layout->addWidget(bar);
  container->setLayout(layout);
}
QtStatusBar::progressIndicator::~progressIndicator(){
  delete container;
}