1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 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/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","Occurrence"/*,"Size"*/};
|
---|
39 |
|
---|
40 | QtWorldView::QtWorldView(QWidget * _parent) :
|
---|
41 | QTreeWidget (_parent),
|
---|
42 | Observer("QtWorldView")
|
---|
43 | {
|
---|
44 | setColumnCount(COLUMNCOUNT);
|
---|
45 | setSelectionMode(QAbstractItemView::MultiSelection);
|
---|
46 |
|
---|
47 | QStringList header;
|
---|
48 | for(int i=0; i<COLUMNCOUNT;++i)
|
---|
49 | header << COLUMNNAMES[i];
|
---|
50 | setHeaderLabels(header);
|
---|
51 |
|
---|
52 | molecules = World::getInstance().getMolecules();
|
---|
53 | molecules->signOn(this);
|
---|
54 | update(molecules);
|
---|
55 |
|
---|
56 | //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
|
---|
57 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | QtWorldView::~QtWorldView()
|
---|
62 | {
|
---|
63 | molecules->signOff(this);
|
---|
64 | }
|
---|
65 |
|
---|
66 | void QtWorldView::update(Observable *publisher) {
|
---|
67 | int numMolecules = molecules->ListOfMolecules.size();
|
---|
68 | clear();
|
---|
69 | molSelection.resize(numMolecules);
|
---|
70 |
|
---|
71 | // list of (unique) formulas in the world
|
---|
72 | std::vector<Formula> formula;
|
---|
73 |
|
---|
74 | int i;
|
---|
75 | MoleculeList::iterator iter;
|
---|
76 | for(iter = molecules->ListOfMolecules.begin(),i=0;
|
---|
77 | iter != molecules->ListOfMolecules.end();
|
---|
78 | ++i,++iter) {
|
---|
79 |
|
---|
80 | // find group if already in list
|
---|
81 | QTreeWidgetItem *groupItem = NULL;
|
---|
82 | for (unsigned int j=0;j<formula.size();j++)
|
---|
83 | if ((*iter)->getFormula() == formula[j]){
|
---|
84 | groupItem = topLevelItem(j);
|
---|
85 | break;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // new molecule type -> create new group
|
---|
89 | if (!groupItem){
|
---|
90 | formula.push_back((*iter)->getFormula());
|
---|
91 | groupItem = new QTreeWidgetItem(this);
|
---|
92 | groupItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
93 | groupItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
94 | groupItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
95 | groupItem->setText(3, "0");
|
---|
96 | }
|
---|
97 |
|
---|
98 | // add molecule
|
---|
99 | QTreeWidgetItem *molItem = new QTreeWidgetItem(groupItem);
|
---|
100 | molItem->setText(0, QString((*iter)->getName().c_str()));
|
---|
101 | molItem->setText(1, QString::number((*iter)->getAtomCount()));
|
---|
102 | molItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
|
---|
103 | const int index = (*iter)->IndexNr;
|
---|
104 | molItem->setData(0, Qt::UserRole, QVariant(index));
|
---|
105 |
|
---|
106 |
|
---|
107 | // increase group occurrence
|
---|
108 | int count = groupItem->text(3).toInt() + 1;
|
---|
109 | groupItem->setText(3, QString::number(count));
|
---|
110 |
|
---|
111 | //molSelection[i]=nameWidget->isSelected();
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | void QtWorldView::subjectKilled(Observable *publisher) {
|
---|
116 | }
|
---|
117 |
|
---|
118 | void QtWorldView::moleculeChanged() {
|
---|
119 | /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
|
---|
120 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
121 | string cellValue = item(row,NAME)->text().toStdString();
|
---|
122 | if(mol->getName() != cellValue && cellValue !="") {
|
---|
123 | mol->setName(cellValue);
|
---|
124 | }
|
---|
125 | else if(cellValue==""){
|
---|
126 | item(row,NAME)->setText(QString(mol->getName().c_str()));
|
---|
127 | }*/
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | void QtWorldView::rowSelected(){
|
---|
132 | //std::cout << "Selection in (" << row << "," << column << "): " << item(row,column) << std::endl;
|
---|
133 | /*bool state = item(row,column)->isSelected();
|
---|
134 | for(int i = 0; i<COLUMNCOUNT; i++){
|
---|
135 | //std::cout << "Setting " << i << "-th item " << item(row,i) << " in row " << row << " to state selected." << std::endl;
|
---|
136 | item(row,i)->setSelected(state);
|
---|
137 | }
|
---|
138 | // figure out which rows have changed
|
---|
139 | for(int i=0; i<rowCount();++i){
|
---|
140 | state = item(i,0)->isSelected();
|
---|
141 | if(molSelection[i]!=state){
|
---|
142 | int idx = verticalHeaderItem(i)->data(Qt::UserRole).toInt();
|
---|
143 | molecule *mol = molecules->ReturnIndex(idx);
|
---|
144 | if(state){
|
---|
145 | emit moleculeSelected(mol);
|
---|
146 | }
|
---|
147 | else{
|
---|
148 | emit moleculeUnSelected(mol);
|
---|
149 | }
|
---|
150 | molSelection[i]=state;
|
---|
151 | }
|
---|
152 | }*/
|
---|
153 | }
|
---|