/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * QtWorldView.cpp * * Created on: Jan 21, 2010 * Author: crueger */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Views/Qt4/QtWorldView.hpp" #include #include "CodePatterns/MemDebug.hpp" #include "atom.hpp" #include "Formula.hpp" #include "molecule.hpp" using namespace std; // maybe this should go with the definition of molecules // some attributes need to be easier to find for molecules // these attributes are skiped so far const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX; const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula"/*,"Size"*/}; QtWorldView::QtWorldView(QWidget * _parent) : QTableWidget (_parent), Observer("QtWorldView") { setRowCount(0); setColumnCount(COLUMNCOUNT); for(int i=0; isetText(QString(COLUMNNAMES[i])); setHorizontalHeaderItem(i,heading); } molecules = World::getInstance().getMolecules(); molecules->signOn(this); update(molecules); connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int))); connect(this,SIGNAL(cellClicked(int,int)),this,SLOT(cellSelected(int,int))); } QtWorldView::~QtWorldView() { molecules->signOff(this); } void QtWorldView::update(Observable *publisher) { int numMolecules = molecules->ListOfMolecules.size(); setRowCount(numMolecules); molSelection.resize(numMolecules); int i; MoleculeList::iterator iter; for(iter = molecules->ListOfMolecules.begin(),i=0; iter != molecules->ListOfMolecules.end(); ++i,++iter) { const int index = (*iter)->IndexNr; QTableWidgetItem *indexWidget = new QTableWidgetItem(); indexWidget->setText(QString::number(index)); indexWidget->setData(Qt::UserRole,QVariant(index)); setVerticalHeaderItem(i,indexWidget); const string name = (*iter)->getName(); QTableWidgetItem *nameWidget = new QTableWidgetItem(); nameWidget->setText(QString(name.c_str())); setItem(i,NAME,nameWidget); const int atomCount = (*iter)->getAtomCount(); QTableWidgetItem *countWidget= new QTableWidgetItem(); countWidget->setText(QString::number(atomCount)); countWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); setItem(i,ATOMS,countWidget); const Formula formula = (*iter)->getFormula(); QTableWidgetItem *formulaWidget= new QTableWidgetItem(); formulaWidget->setText(QString(formula.toString().c_str())); formulaWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); setItem(i,ATOMS,formulaWidget); molSelection[i]=nameWidget->isSelected(); } } void QtWorldView::subjectKilled(Observable *publisher) { } void QtWorldView::moleculeChanged(int row, int column) { int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt(); molecule *mol = molecules->ReturnIndex(idx); string cellValue = item(row,NAME)->text().toStdString(); if(mol->getName() != cellValue && cellValue !="") { mol->setName(cellValue); } else if(cellValue==""){ item(row,NAME)->setText(QString(mol->getName().c_str())); } } void QtWorldView::cellSelected(int row, int column){ bool state = item(row,column)->isSelected(); for(int i = 0; isetSelected(state); } // figure out which rows have changed for(int i=0; iisSelected(); if(molSelection[i]!=state){ int idx = verticalHeaderItem(i)->data(Qt::UserRole).toInt(); molecule *mol = molecules->ReturnIndex(idx); if(state){ emit moleculeSelected(mol); } else{ emit moleculeUnSelected(mol); } molSelection[i]=state; } } }