/*
* 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
#include "Views/Qt4/ElementList/QtElementList.hpp"
#include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.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(QtObservedInstanceBoard *_board, QWidget * _parent) :
QTreeWidget (_parent),
atomobserver(_board)
{
qRegisterMetaType("QtObservedAtom::ptr");
setColumnCount(COLUMNCOUNT);
setSelectionMode(QAbstractItemView::MultiSelection);
QStringList header;
for(int i=0; ibegin(),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());
// insertTopLevelItem(e->getAtomicNumber()-1, treeItem);
}
}
dirty = true;
connect(&atomobserver, SIGNAL(ElementChanged(const QtObservedAtom::ptr)),
this, SLOT(changeElement(const QtObservedAtom::ptr)));
connect(&atomobserver, SIGNAL(AtomInserted(const QtObservedAtom::ptr)),
this, SLOT(addAtomElement(const QtObservedAtom::ptr)));
connect(&atomobserver, SIGNAL(AtomRemoved(const atomId_t)),
this, SLOT(removeAtomElement(const atomId_t)));
connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
// connect(this,SIGNAL(changed()),this,SLOT(update()));
}
QtElementList::~QtElementList()
{
}
void QtElementList::changeElement(const QtObservedAtom::ptr _atom)
{
boost::recursive_mutex::scoped_lock lock(refill_mutex);
const atomicNumber_t newelement = _atom->getAtomElement();
atomicNumber_t oldelement = -1;
const atomId_t atomid = _atom->getAtomIndex();
QTreeWidgetItem *newtreeItem = topLevelItem(newelement-1);
ASSERT( newtreeItem != NULL,
"QtElementList::updateElement() - new element item not present.");
AtomElementMap_t::iterator iter = AtomElementMap.find(atomid);
if (iter == AtomElementMap.end()) {
AtomElementMap.insert( std::make_pair(atomid, -1));
iter = AtomElementMap.find(atomid);
} else
oldelement = iter->second;
iter->second = newelement;
// reduce old occurence
if (oldelement != (atomicNumber_t)-1) {
QTreeWidgetItem *oldtreeItem = topLevelItem(oldelement-1);
ASSERT( oldtreeItem != NULL,
"QtElementList::updateElement() - old element item not present.");
const int count_old = oldtreeItem->text(OCCURRENCE).toInt();
setOccurrence(*oldtreeItem, count_old-1);
}
// increase new occurence
const int count_new = newtreeItem->text(OCCURRENCE).toInt();
setOccurrence(*newtreeItem, count_new+1);
}
void QtElementList::setOccurrence(QTreeWidgetItem &_item, const int count)
{
ASSERT (count >= 0,
"QtElementList::setOccurrence() - count for an elment < 0.");
if (count > 0) {
_item.setText(OCCURRENCE, QString::number(count));
if (_item.isDisabled())
_item.setDisabled(false);
} else {
_item.setText(OCCURRENCE, "none");
_item.setDisabled(true);
}
}
void QtElementList::addAtomElement(const QtObservedAtom::ptr _atom)
{
boost::recursive_mutex::scoped_lock lock(refill_mutex);
changeElement(_atom);
}
void QtElementList::removeAtomElement(const atomId_t _atomid)
{
boost::recursive_mutex::scoped_lock lock(refill_mutex);
const AtomElementMap_t::iterator iter = AtomElementMap.find(_atomid);
ASSERT (iter != AtomElementMap.end(),
"QtElementList::addAtomElement() - atom #"+toString(_atomid)
+" is not present in elementlookup map.");
if (iter != AtomElementMap.end()) {
QTreeWidgetItem *oldtreeItem = topLevelItem(iter->second-1);
const int count_old = oldtreeItem->text(OCCURRENCE).toInt();
setOccurrence(*oldtreeItem, count_old-1);
AtomElementMap.erase(iter);
}
}
void QtElementList::paintEvent(QPaintEvent * event)
{
// if (dirty)
// refill();
QTreeWidget::paintEvent(event);
}
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);
}
}
}