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