/*
* 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 .
*/
/*
* QtElementList.cpp
*
* Created on: Mar 6, 2012
* Author: ankele
*/
// include config.h
#ifdef HAVE_CONFIG_H
#include
#endif
#include "Views/Qt4/QtElementList.hpp"
#include
#include "CodePatterns/MemDebug.hpp"
#include "Atom/atom.hpp"
#include "Atom/AtomObserver.hpp"
#include "Element/element.hpp"
#include "Element/periodentafel.hpp"
#include "Descriptors/AtomTypeDescriptor.hpp"
#include
#include "Actions/SelectionAction/Atoms/AtomByElementAction.hpp"
#include "Actions/SelectionAction/Atoms/NotAtomByElementAction.hpp"
using namespace std;
const int QtElementList::COLUMNCOUNT = COLUMNTYPES_MAX;
const char *QtElementList::COLUMNNAMES[QtElementList::COLUMNCOUNT]={"Number","Name","Symbol","Mass","Occurrence"};
QtElementList::QtElementList(QWidget * _parent) :
QTreeWidget (_parent),
Observer("QtElementList")
{
setColumnCount(COLUMNCOUNT);
setSelectionMode(QAbstractItemView::MultiSelection);
QStringList header;
for(int i=0; igetChannelNo() == atom::ElementChanged) {
// updateElement(static_cast(*publisher));
dirty = true;
refill();
} else
ASSERT(0, "QtElementList::recieveNotification() - we are not enlisted to any other instance's channel.");
}
void QtElementList::updateElement(const atom &_atom)
{
// this function has not been tested and is not used because it is
// incompatible with the current refill scheme
AtomElementMap_t::iterator iter = AtomElementMap.find(_atom.getId());
const atomicNumber_t oldelement = iter->second;
const atomicNumber_t newelement = _atom.getElementNo();
iter->second = newelement;
QTreeWidgetItem *oldtreeItem = topLevelItem(oldelement-1);
QTreeWidgetItem *newtreeItem = topLevelItem(newelement-1);
ASSERT( (oldtreeItem != NULL) && (newtreeItem != NULL),
"QtElementList::updateElement() - missing element item.");
int count_old = oldtreeItem->text(OCCURRENCE).toInt();
int count_new = newtreeItem->text(OCCURRENCE).toInt();
setOccurrence(*oldtreeItem, count_old-1);
setOccurrence(*newtreeItem, count_new+1);
}
void QtElementList::setOccurrence(QTreeWidgetItem &_item, const int count)
{
if (count < 0) {
// something must be very wrong, rather refill
dirty = true;
refill();
} else {
if (count > 0) {
_item.setText(OCCURRENCE, QString::number(count));
} else {
_item.setText(OCCURRENCE, "none");
_item.setDisabled(true);
}
}
}
void QtElementList::refill() {
periodentafel *&periode = World::getInstance().getPeriode();
elementSelection.clear();
int i;
clear();
periodentafel::const_iterator iter;
for(iter = periode->begin(),i=0;
iter != periode->end();
++i,++iter) {
const element *e = iter->second;
int count = 0;
count = const_cast(World::getInstance()).
getAllAtoms(AtomByType(e)).size();
QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
treeItem->setText(NUMBER, QString::number(e->getAtomicNumber()));
treeItem->setText(NAME, QString(e->getName().c_str()));
treeItem->setText(SYMBOL, QString(e->getSymbol().c_str()));
treeItem->setText(MASS, QString::number(e->getMass()));
setOccurrence(*treeItem, count);
elementSelection.push_back(treeItem->isSelected());
}
dirty = false;
}
void QtElementList::paintEvent(QPaintEvent * event)
{
if (dirty)
refill();
QTreeWidget::paintEvent(event);
}
void QtElementList::subjectKilled(Observable *publisher) {
}
void QtElementList::rowSelected()
{
//std::cout << "rowSelected\n";
periodentafel *&periode = World::getInstance().getPeriode();
for (int i=0;iisSelected();
if (newSelection != elementSelection[i]){
elementSelection[i] = newSelection;
int atomicNumber = item->text(NUMBER).toInt();
const element *e = periode->FindElement(atomicNumber);
if (newSelection)
MoleCuilder::SelectionAtomByElement(e);
else
MoleCuilder::SelectionNotAtomByElement(e);
}
}
}