[bcf653] | 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 |
|
---|
[b47bfc] | 8 | /*
|
---|
[4cf323d] | 9 | * QtWorldView.cpp
|
---|
[b47bfc] | 10 | *
|
---|
| 11 | * Created on: Jan 21, 2010
|
---|
| 12 | * Author: crueger
|
---|
| 13 | */
|
---|
| 14 |
|
---|
[bf3817] | 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
[bbbad5] | 19 |
|
---|
[4cf323d] | 20 | #include "Views/Qt4/QtWorldView.hpp"
|
---|
[b47bfc] | 21 |
|
---|
| 22 | #include <iostream>
|
---|
| 23 |
|
---|
[ad011c] | 24 | #include "CodePatterns/MemDebug.hpp"
|
---|
[bbbad5] | 25 |
|
---|
[b47bfc] | 26 | #include "atom.hpp"
|
---|
[26cf17] | 27 | #include "Formula.hpp"
|
---|
[b47bfc] | 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
|
---|
[4cf323d] | 36 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX;
|
---|
| 37 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula"/*,"Size"*/};
|
---|
[b47bfc] | 38 |
|
---|
[4cf323d] | 39 | QtWorldView::QtWorldView(QWidget * _parent) :
|
---|
[b47bfc] | 40 | QTableWidget (_parent),
|
---|
[4cf323d] | 41 | Observer("QtWorldView")
|
---|
[b47bfc] | 42 | {
|
---|
| 43 | setRowCount(0);
|
---|
| 44 | setColumnCount(COLUMNCOUNT);
|
---|
| 45 |
|
---|
| 46 | for(int i=0; i<COLUMNCOUNT;++i) {
|
---|
| 47 | QTableWidgetItem *heading = new QTableWidgetItem();
|
---|
[47812b] | 48 | std::cout << "Creating heading item " << heading << "." << std::endl;
|
---|
[b47bfc] | 49 | heading->setText(QString(COLUMNNAMES[i]));
|
---|
| 50 | setHorizontalHeaderItem(i,heading);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[01a51f9] | 53 | molecules = World::getInstance().getMolecules();
|
---|
[b47bfc] | 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 |
|
---|
[4cf323d] | 62 | QtWorldView::~QtWorldView()
|
---|
[b47bfc] | 63 | {
|
---|
| 64 | molecules->signOff(this);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[4cf323d] | 67 | void QtWorldView::update(Observable *publisher) {
|
---|
[b47bfc] | 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();
|
---|
[47812b] | 79 | //std::cout << "Creating index item " << indexWidget << "." << std::endl;
|
---|
[26cf17] | 80 | indexWidget->setText(QString::number(index));
|
---|
[b47bfc] | 81 | indexWidget->setData(Qt::UserRole,QVariant(index));
|
---|
| 82 | setVerticalHeaderItem(i,indexWidget);
|
---|
| 83 |
|
---|
| 84 | const string name = (*iter)->getName();
|
---|
| 85 | QTableWidgetItem *nameWidget = new QTableWidgetItem();
|
---|
[47812b] | 86 | //std::cout << "Creating name item " << nameWidget << " at " << i << "," << NAME << "." << std::endl;
|
---|
[b47bfc] | 87 | nameWidget->setText(QString(name.c_str()));
|
---|
| 88 | setItem(i,NAME,nameWidget);
|
---|
| 89 |
|
---|
| 90 | const int atomCount = (*iter)->getAtomCount();
|
---|
| 91 | QTableWidgetItem *countWidget= new QTableWidgetItem();
|
---|
[47812b] | 92 | //std::cout << "Creating count item " << countWidget << " at " << i << "," << ATOMCOUNT << "." << std::endl;
|
---|
[26cf17] | 93 | countWidget->setText(QString::number(atomCount));
|
---|
[b47bfc] | 94 | countWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
|
---|
[47812b] | 95 | setItem(i,ATOMCOUNT,countWidget);
|
---|
[b47bfc] | 96 |
|
---|
[26cf17] | 97 | const Formula formula = (*iter)->getFormula();
|
---|
| 98 | QTableWidgetItem *formulaWidget= new QTableWidgetItem();
|
---|
[47812b] | 99 | //std::cout << "Creating formula item " << formulaWidget << " at " << i << "," << FORMULA << "." << std::endl;
|
---|
[26cf17] | 100 | formulaWidget->setText(QString(formula.toString().c_str()));
|
---|
| 101 | formulaWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
|
---|
[47812b] | 102 | setItem(i,FORMULA,formulaWidget);
|
---|
[26cf17] | 103 |
|
---|
[b47bfc] | 104 | molSelection[i]=nameWidget->isSelected();
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[4cf323d] | 108 | void QtWorldView::subjectKilled(Observable *publisher) {
|
---|
[b47bfc] | 109 | }
|
---|
| 110 |
|
---|
[4cf323d] | 111 | void QtWorldView::moleculeChanged(int row, int column) {
|
---|
[b47bfc] | 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 |
|
---|
[4cf323d] | 124 | void QtWorldView::cellSelected(int row, int column){
|
---|
[47812b] | 125 | //std::cout << "Selection in (" << row << "," << column << "): " << item(row,column) << std::endl;
|
---|
[b47bfc] | 126 | bool state = item(row,column)->isSelected();
|
---|
| 127 | for(int i = 0; i<COLUMNCOUNT; i++){
|
---|
[47812b] | 128 | //std::cout << "Setting " << i << "-th item " << item(row,i) << " in row " << row << " to state selected." << std::endl;
|
---|
[b47bfc] | 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 | }
|
---|