| 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 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * QtWorldView.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Jan 21, 2010 | 
|---|
| 12 | *      Author: crueger | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "Views/Qt4/QtWorldView.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include <iostream> | 
|---|
| 23 |  | 
|---|
| 24 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 25 |  | 
|---|
| 26 | #include "atom.hpp" | 
|---|
| 27 | #include "Formula.hpp" | 
|---|
| 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 | 
|---|
| 36 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX; | 
|---|
| 37 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula"/*,"Size"*/}; | 
|---|
| 38 |  | 
|---|
| 39 | QtWorldView::QtWorldView(QWidget * _parent) : | 
|---|
| 40 | QTableWidget (_parent), | 
|---|
| 41 | Observer("QtWorldView") | 
|---|
| 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 |  | 
|---|
| 52 | molecules = World::getInstance().getMolecules(); | 
|---|
| 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 |  | 
|---|
| 61 | QtWorldView::~QtWorldView() | 
|---|
| 62 | { | 
|---|
| 63 | molecules->signOff(this); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | void QtWorldView::update(Observable *publisher) { | 
|---|
| 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(); | 
|---|
| 78 | indexWidget->setText(QString::number(index)); | 
|---|
| 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(); | 
|---|
| 89 | countWidget->setText(QString::number(atomCount)); | 
|---|
| 90 | countWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled); | 
|---|
| 91 | setItem(i,ATOMS,countWidget); | 
|---|
| 92 |  | 
|---|
| 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 |  | 
|---|
| 99 | molSelection[i]=nameWidget->isSelected(); | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | void QtWorldView::subjectKilled(Observable *publisher) { | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void QtWorldView::moleculeChanged(int row, int column) { | 
|---|
| 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 |  | 
|---|
| 119 | void QtWorldView::cellSelected(int row, int column){ | 
|---|
| 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 | } | 
|---|