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 | std::cout << "Creating heading item " << heading << "." << std::endl;
|
---|
49 | heading->setText(QString(COLUMNNAMES[i]));
|
---|
50 | setHorizontalHeaderItem(i,heading);
|
---|
51 | }
|
---|
52 |
|
---|
53 | molecules = World::getInstance().getMolecules();
|
---|
54 | molecules->signOn(this);
|
---|
55 | update(molecules);
|
---|
56 |
|
---|
57 | connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
58 | connect(this,SIGNAL(cellClicked(int,int)),this,SLOT(cellSelected(int,int)));
|
---|
59 |
|
---|
60 | }
|
---|
61 |
|
---|
62 | QtWorldView::~QtWorldView()
|
---|
63 | {
|
---|
64 | molecules->signOff(this);
|
---|
65 | }
|
---|
66 |
|
---|
67 | void QtWorldView::update(Observable *publisher) {
|
---|
68 | int numMolecules = molecules->ListOfMolecules.size();
|
---|
69 | setRowCount(numMolecules);
|
---|
70 | molSelection.resize(numMolecules);
|
---|
71 | int i;
|
---|
72 | MoleculeList::iterator iter;
|
---|
73 | for(iter = molecules->ListOfMolecules.begin(),i=0;
|
---|
74 | iter != molecules->ListOfMolecules.end();
|
---|
75 | ++i,++iter) {
|
---|
76 |
|
---|
77 | const int index = (*iter)->IndexNr;
|
---|
78 | QTableWidgetItem *indexWidget = new QTableWidgetItem();
|
---|
79 | //std::cout << "Creating index item " << indexWidget << "." << std::endl;
|
---|
80 | indexWidget->setText(QString::number(index));
|
---|
81 | indexWidget->setData(Qt::UserRole,QVariant(index));
|
---|
82 | setVerticalHeaderItem(i,indexWidget);
|
---|
83 |
|
---|
84 | const string name = (*iter)->getName();
|
---|
85 | QTableWidgetItem *nameWidget = new QTableWidgetItem();
|
---|
86 | //std::cout << "Creating name item " << nameWidget << " at " << i << "," << NAME << "." << std::endl;
|
---|
87 | nameWidget->setText(QString(name.c_str()));
|
---|
88 | setItem(i,NAME,nameWidget);
|
---|
89 |
|
---|
90 | const int atomCount = (*iter)->getAtomCount();
|
---|
91 | QTableWidgetItem *countWidget= new QTableWidgetItem();
|
---|
92 | //std::cout << "Creating count item " << countWidget << " at " << i << "," << ATOMCOUNT << "." << std::endl;
|
---|
93 | countWidget->setText(QString::number(atomCount));
|
---|
94 | countWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
|
---|
95 | setItem(i,ATOMCOUNT,countWidget);
|
---|
96 |
|
---|
97 | const Formula formula = (*iter)->getFormula();
|
---|
98 | QTableWidgetItem *formulaWidget= new QTableWidgetItem();
|
---|
99 | //std::cout << "Creating formula item " << formulaWidget << " at " << i << "," << FORMULA << "." << std::endl;
|
---|
100 | formulaWidget->setText(QString(formula.toString().c_str()));
|
---|
101 | formulaWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
|
---|
102 | setItem(i,FORMULA,formulaWidget);
|
---|
103 |
|
---|
104 | molSelection[i]=nameWidget->isSelected();
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | void QtWorldView::subjectKilled(Observable *publisher) {
|
---|
109 | }
|
---|
110 |
|
---|
111 | void QtWorldView::moleculeChanged(int row, int column) {
|
---|
112 | int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
113 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
114 | string cellValue = item(row,NAME)->text().toStdString();
|
---|
115 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
116 | mol->setName(cellValue);
|
---|
117 | }
|
---|
118 | else if(cellValue==""){
|
---|
119 | item(row,NAME)->setText(QString(mol->getName().c_str()));
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | void QtWorldView::cellSelected(int row, int column){
|
---|
125 | //std::cout << "Selection in (" << row << "," << column << "): " << item(row,column) << std::endl;
|
---|
126 | bool state = item(row,column)->isSelected();
|
---|
127 | for(int i = 0; i<COLUMNCOUNT; i++){
|
---|
128 | //std::cout << "Setting " << i << "-th item " << item(row,i) << " in row " << row << " to state selected." << std::endl;
|
---|
129 | item(row,i)->setSelected(state);
|
---|
130 | }
|
---|
131 | // figure out which rows have changed
|
---|
132 | for(int i=0; i<rowCount();++i){
|
---|
133 | state = item(i,0)->isSelected();
|
---|
134 | if(molSelection[i]!=state){
|
---|
135 | int idx = verticalHeaderItem(i)->data(Qt::UserRole).toInt();
|
---|
136 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
137 | if(state){
|
---|
138 | emit moleculeSelected(mol);
|
---|
139 | }
|
---|
140 | else{
|
---|
141 | emit moleculeUnSelected(mol);
|
---|
142 | }
|
---|
143 | molSelection[i]=state;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|