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