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