| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [b47bfc] | 8 | /*
 | 
|---|
| [4cf323d] | 9 |  * QtWorldView.cpp
 | 
|---|
| [b47bfc] | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Jan 21, 2010
 | 
|---|
 | 12 |  *      Author: crueger
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
| [bbbad5] | 19 | 
 | 
|---|
| [4cf323d] | 20 | #include "Views/Qt4/QtWorldView.hpp"
 | 
|---|
| [b47bfc] | 21 | 
 | 
|---|
 | 22 | #include <iostream>
 | 
|---|
 | 23 | 
 | 
|---|
| [bbbad5] | 24 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 25 | 
 | 
|---|
| [b47bfc] | 26 | #include "atom.hpp"
 | 
|---|
| [26cf178] | 27 | #include "Formula.hpp"
 | 
|---|
| [b47bfc] | 28 | #include "molecule.hpp"
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | using namespace std;
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | // maybe this should go with the definition of molecules
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | // some attributes need to be easier to find for molecules
 | 
|---|
 | 35 | // these attributes are skiped so far
 | 
|---|
| [4cf323d] | 36 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX;
 | 
|---|
 | 37 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula"/*,"Size"*/};
 | 
|---|
| [b47bfc] | 38 | 
 | 
|---|
| [4cf323d] | 39 | QtWorldView::QtWorldView(QWidget * _parent) :
 | 
|---|
| [b47bfc] | 40 |   QTableWidget (_parent),
 | 
|---|
| [4cf323d] | 41 |   Observer("QtWorldView")
 | 
|---|
| [b47bfc] | 42 | {
 | 
|---|
 | 43 |   setRowCount(0);
 | 
|---|
 | 44 |   setColumnCount(COLUMNCOUNT);
 | 
|---|
 | 45 | 
 | 
|---|
 | 46 |   for(int i=0; i<COLUMNCOUNT;++i) {
 | 
|---|
 | 47 |     QTableWidgetItem *heading = new QTableWidgetItem();
 | 
|---|
 | 48 |     heading->setText(QString(COLUMNNAMES[i]));
 | 
|---|
 | 49 |     setHorizontalHeaderItem(i,heading);
 | 
|---|
 | 50 |   }
 | 
|---|
 | 51 | 
 | 
|---|
| [01a51f9] | 52 |   molecules = World::getInstance().getMolecules();
 | 
|---|
| [b47bfc] | 53 |   molecules->signOn(this);
 | 
|---|
 | 54 |   update(molecules);
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |   connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
 | 
|---|
 | 57 |   connect(this,SIGNAL(cellClicked(int,int)),this,SLOT(cellSelected(int,int)));
 | 
|---|
 | 58 | 
 | 
|---|
 | 59 | }
 | 
|---|
 | 60 | 
 | 
|---|
| [4cf323d] | 61 | QtWorldView::~QtWorldView()
 | 
|---|
| [b47bfc] | 62 | {
 | 
|---|
 | 63 |   molecules->signOff(this);
 | 
|---|
 | 64 | }
 | 
|---|
 | 65 | 
 | 
|---|
| [4cf323d] | 66 | void QtWorldView::update(Observable *publisher) {
 | 
|---|
| [b47bfc] | 67 |   int numMolecules = molecules->ListOfMolecules.size();
 | 
|---|
 | 68 |   setRowCount(numMolecules);
 | 
|---|
 | 69 |   molSelection.resize(numMolecules);
 | 
|---|
 | 70 |   int i;
 | 
|---|
 | 71 |   MoleculeList::iterator iter;
 | 
|---|
 | 72 |   for(iter = molecules->ListOfMolecules.begin(),i=0;
 | 
|---|
 | 73 |       iter != molecules->ListOfMolecules.end();
 | 
|---|
 | 74 |       ++i,++iter) {
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |     const int index = (*iter)->IndexNr;
 | 
|---|
 | 77 |     QTableWidgetItem *indexWidget = new QTableWidgetItem();
 | 
|---|
| [26cf178] | 78 |     indexWidget->setText(QString::number(index));
 | 
|---|
| [b47bfc] | 79 |     indexWidget->setData(Qt::UserRole,QVariant(index));
 | 
|---|
 | 80 |     setVerticalHeaderItem(i,indexWidget);
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |     const string name = (*iter)->getName();
 | 
|---|
 | 83 |     QTableWidgetItem *nameWidget = new QTableWidgetItem();
 | 
|---|
 | 84 |     nameWidget->setText(QString(name.c_str()));
 | 
|---|
 | 85 |     setItem(i,NAME,nameWidget);
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |     const int atomCount = (*iter)->getAtomCount();
 | 
|---|
 | 88 |     QTableWidgetItem *countWidget= new QTableWidgetItem();
 | 
|---|
| [26cf178] | 89 |     countWidget->setText(QString::number(atomCount));
 | 
|---|
| [b47bfc] | 90 |     countWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
 | 
|---|
 | 91 |     setItem(i,ATOMS,countWidget);
 | 
|---|
 | 92 | 
 | 
|---|
| [26cf178] | 93 |     const Formula formula = (*iter)->getFormula();
 | 
|---|
 | 94 |     QTableWidgetItem *formulaWidget= new QTableWidgetItem();
 | 
|---|
 | 95 |     formulaWidget->setText(QString(formula.toString().c_str()));
 | 
|---|
 | 96 |     formulaWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
 | 
|---|
 | 97 |     setItem(i,ATOMS,formulaWidget);
 | 
|---|
 | 98 | 
 | 
|---|
| [b47bfc] | 99 |     molSelection[i]=nameWidget->isSelected();
 | 
|---|
 | 100 |   }
 | 
|---|
 | 101 | }
 | 
|---|
 | 102 | 
 | 
|---|
| [4cf323d] | 103 | void QtWorldView::subjectKilled(Observable *publisher) {
 | 
|---|
| [b47bfc] | 104 | }
 | 
|---|
 | 105 | 
 | 
|---|
| [4cf323d] | 106 | void QtWorldView::moleculeChanged(int row, int column) {
 | 
|---|
| [b47bfc] | 107 |   int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
 | 
|---|
 | 108 |   molecule *mol = molecules->ReturnIndex(idx);
 | 
|---|
 | 109 |   string cellValue = item(row,NAME)->text().toStdString();
 | 
|---|
 | 110 |   if(mol->getName() != cellValue && cellValue !="") {
 | 
|---|
 | 111 |     mol->setName(cellValue);
 | 
|---|
 | 112 |   }
 | 
|---|
 | 113 |   else if(cellValue==""){
 | 
|---|
 | 114 |     item(row,NAME)->setText(QString(mol->getName().c_str()));
 | 
|---|
 | 115 |   }
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | 
 | 
|---|
| [4cf323d] | 119 | void QtWorldView::cellSelected(int row, int column){
 | 
|---|
| [b47bfc] | 120 |   bool state = item(row,column)->isSelected();
 | 
|---|
 | 121 |   for(int i = 0; i<COLUMNCOUNT; i++){
 | 
|---|
 | 122 |     item(row,i)->setSelected(state);
 | 
|---|
 | 123 |   }
 | 
|---|
 | 124 |   // figure out which rows have changed
 | 
|---|
 | 125 |   for(int i=0; i<rowCount();++i){
 | 
|---|
 | 126 |     state = item(i,0)->isSelected();
 | 
|---|
 | 127 |     if(molSelection[i]!=state){
 | 
|---|
 | 128 |       int idx = verticalHeaderItem(i)->data(Qt::UserRole).toInt();
 | 
|---|
 | 129 |       molecule *mol = molecules->ReturnIndex(idx);
 | 
|---|
 | 130 |       if(state){
 | 
|---|
 | 131 |         emit moleculeSelected(mol);
 | 
|---|
 | 132 |       }
 | 
|---|
 | 133 |       else{
 | 
|---|
 | 134 |         emit moleculeUnSelected(mol);
 | 
|---|
 | 135 |       }
 | 
|---|
 | 136 |       molSelection[i]=state;
 | 
|---|
 | 137 |     }
 | 
|---|
 | 138 |   }
 | 
|---|
 | 139 | }
 | 
|---|