| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
| [0aa122] | 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| [bcf653] | 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 | 
 | 
|---|
| [6f0841] | 26 | #include "Atom/atom.hpp"
 | 
|---|
| [26cf178] | 27 | #include "Formula.hpp"
 | 
|---|
| [b47bfc] | 28 | #include "molecule.hpp"
 | 
|---|
| [42127c] | 29 | #include "MoleculeListClass.hpp"
 | 
|---|
| [b47bfc] | 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
 | 
|---|
| [4cf323d] | 37 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX;
 | 
|---|
 | 38 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula"/*,"Size"*/};
 | 
|---|
| [b47bfc] | 39 | 
 | 
|---|
| [4cf323d] | 40 | QtWorldView::QtWorldView(QWidget * _parent) :
 | 
|---|
| [b47bfc] | 41 |   QTableWidget (_parent),
 | 
|---|
| [4cf323d] | 42 |   Observer("QtWorldView")
 | 
|---|
| [b47bfc] | 43 | {
 | 
|---|
 | 44 |   setRowCount(0);
 | 
|---|
 | 45 |   setColumnCount(COLUMNCOUNT);
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |   for(int i=0; i<COLUMNCOUNT;++i) {
 | 
|---|
 | 48 |     QTableWidgetItem *heading = new QTableWidgetItem();
 | 
|---|
| [47812b] | 49 |     std::cout << "Creating heading item " << heading << "." << std::endl;
 | 
|---|
| [b47bfc] | 50 |     heading->setText(QString(COLUMNNAMES[i]));
 | 
|---|
 | 51 |     setHorizontalHeaderItem(i,heading);
 | 
|---|
 | 52 |   }
 | 
|---|
 | 53 | 
 | 
|---|
| [01a51f9] | 54 |   molecules = World::getInstance().getMolecules();
 | 
|---|
| [b47bfc] | 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 | 
 | 
|---|
| [4cf323d] | 63 | QtWorldView::~QtWorldView()
 | 
|---|
| [b47bfc] | 64 | {
 | 
|---|
 | 65 |   molecules->signOff(this);
 | 
|---|
 | 66 | }
 | 
|---|
 | 67 | 
 | 
|---|
| [4cf323d] | 68 | void QtWorldView::update(Observable *publisher) {
 | 
|---|
| [b47bfc] | 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();
 | 
|---|
| [47812b] | 80 |     //std::cout << "Creating index item " << indexWidget << "." << std::endl;
 | 
|---|
| [26cf178] | 81 |     indexWidget->setText(QString::number(index));
 | 
|---|
| [b47bfc] | 82 |     indexWidget->setData(Qt::UserRole,QVariant(index));
 | 
|---|
 | 83 |     setVerticalHeaderItem(i,indexWidget);
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |     const string name = (*iter)->getName();
 | 
|---|
 | 86 |     QTableWidgetItem *nameWidget = new QTableWidgetItem();
 | 
|---|
| [47812b] | 87 |     //std::cout << "Creating name item " << nameWidget << " at " << i << "," << NAME << "." << std::endl;
 | 
|---|
| [b47bfc] | 88 |     nameWidget->setText(QString(name.c_str()));
 | 
|---|
 | 89 |     setItem(i,NAME,nameWidget);
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |     const int atomCount = (*iter)->getAtomCount();
 | 
|---|
 | 92 |     QTableWidgetItem *countWidget= new QTableWidgetItem();
 | 
|---|
| [47812b] | 93 |     //std::cout << "Creating count item " << countWidget << " at " << i << "," << ATOMCOUNT << "." << std::endl;
 | 
|---|
| [26cf178] | 94 |     countWidget->setText(QString::number(atomCount));
 | 
|---|
| [b47bfc] | 95 |     countWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
 | 
|---|
| [47812b] | 96 |     setItem(i,ATOMCOUNT,countWidget);
 | 
|---|
| [b47bfc] | 97 | 
 | 
|---|
| [26cf178] | 98 |     const Formula formula = (*iter)->getFormula();
 | 
|---|
 | 99 |     QTableWidgetItem *formulaWidget= new QTableWidgetItem();
 | 
|---|
| [47812b] | 100 |     //std::cout << "Creating formula item " << formulaWidget << " at " << i << "," << FORMULA << "." << std::endl;
 | 
|---|
| [26cf178] | 101 |     formulaWidget->setText(QString(formula.toString().c_str()));
 | 
|---|
 | 102 |     formulaWidget->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
 | 
|---|
| [47812b] | 103 |     setItem(i,FORMULA,formulaWidget);
 | 
|---|
| [26cf178] | 104 | 
 | 
|---|
| [b47bfc] | 105 |     molSelection[i]=nameWidget->isSelected();
 | 
|---|
 | 106 |   }
 | 
|---|
 | 107 | }
 | 
|---|
 | 108 | 
 | 
|---|
| [4cf323d] | 109 | void QtWorldView::subjectKilled(Observable *publisher) {
 | 
|---|
| [b47bfc] | 110 | }
 | 
|---|
 | 111 | 
 | 
|---|
| [4cf323d] | 112 | void QtWorldView::moleculeChanged(int row, int column) {
 | 
|---|
| [b47bfc] | 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 | 
 | 
|---|
| [4cf323d] | 125 | void QtWorldView::cellSelected(int row, int column){
 | 
|---|
| [47812b] | 126 |   //std::cout << "Selection in (" << row << "," << column << "): " << item(row,column) << std::endl;
 | 
|---|
| [b47bfc] | 127 |   bool state = item(row,column)->isSelected();
 | 
|---|
 | 128 |   for(int i = 0; i<COLUMNCOUNT; i++){
 | 
|---|
| [47812b] | 129 |     //std::cout << "Setting " << i << "-th item " << item(row,i) << " in row " << row << " to state selected." << std::endl;
 | 
|---|
| [b47bfc] | 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 | }
 | 
|---|