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