| [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"
 | 
|---|
| [b14efe] | 30 | #include "Actions/SelectionAction/Molecules/MoleculeByIdAction.hpp"
 | 
|---|
 | 31 | #include "Actions/SelectionAction/Molecules/NotMoleculeByIdAction.hpp"
 | 
|---|
| [b47bfc] | 32 | 
 | 
|---|
 | 33 | using namespace std;
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | // maybe this should go with the definition of molecules
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | // some attributes need to be easier to find for molecules
 | 
|---|
 | 38 | // these attributes are skiped so far
 | 
|---|
| [4cf323d] | 39 | const int QtWorldView::COLUMNCOUNT = COLUMNTYPES_MAX;
 | 
|---|
| [79b59b] | 40 | const char *QtWorldView::COLUMNNAMES[QtWorldView::COLUMNCOUNT]={"Name","Atoms","Formula","Occurrence"/*,"Size"*/};
 | 
|---|
| [b47bfc] | 41 | 
 | 
|---|
| [4cf323d] | 42 | QtWorldView::QtWorldView(QWidget * _parent) :
 | 
|---|
| [79b59b] | 43 |   QTreeWidget (_parent),
 | 
|---|
| [4cf323d] | 44 |   Observer("QtWorldView")
 | 
|---|
| [b47bfc] | 45 | {
 | 
|---|
 | 46 |   setColumnCount(COLUMNCOUNT);
 | 
|---|
| [79b59b] | 47 |   setSelectionMode(QAbstractItemView::MultiSelection);
 | 
|---|
| [b47bfc] | 48 | 
 | 
|---|
| [79b59b] | 49 |   QStringList header;
 | 
|---|
 | 50 |   for(int i=0; i<COLUMNCOUNT;++i)
 | 
|---|
 | 51 |     header << COLUMNNAMES[i];
 | 
|---|
 | 52 |   setHeaderLabels(header);
 | 
|---|
| [b47bfc] | 53 | 
 | 
|---|
| [01a51f9] | 54 |   molecules = World::getInstance().getMolecules();
 | 
|---|
| [b47bfc] | 55 |   molecules->signOn(this);
 | 
|---|
| [a39006] | 56 | 
 | 
|---|
 | 57 |   dirty = true;
 | 
|---|
 | 58 |   refill();
 | 
|---|
| [b47bfc] | 59 | 
 | 
|---|
| [79b59b] | 60 |   //connect(this,SIGNAL(cellChanged(int,int)),this,SLOT(moleculeChanged(int,int)));
 | 
|---|
 | 61 |   connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
 | 
|---|
| [b47bfc] | 62 | 
 | 
|---|
 | 63 | }
 | 
|---|
 | 64 | 
 | 
|---|
| [4cf323d] | 65 | QtWorldView::~QtWorldView()
 | 
|---|
| [b47bfc] | 66 | {
 | 
|---|
 | 67 |   molecules->signOff(this);
 | 
|---|
 | 68 | }
 | 
|---|
 | 69 | 
 | 
|---|
| [4cf323d] | 70 | void QtWorldView::update(Observable *publisher) {
 | 
|---|
| [a39006] | 71 | 
 | 
|---|
 | 72 |   dirty = true;
 | 
|---|
 | 73 | 
 | 
|---|
 | 74 |   // force an update from Qt...
 | 
|---|
 | 75 |   clear();
 | 
|---|
 | 76 | }
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 | void QtWorldView::refill() {
 | 
|---|
| [b47bfc] | 79 |   int numMolecules = molecules->ListOfMolecules.size();
 | 
|---|
| [79b59b] | 80 |   clear();
 | 
|---|
| [b47bfc] | 81 |   molSelection.resize(numMolecules);
 | 
|---|
| [79b59b] | 82 | 
 | 
|---|
 | 83 |   // list of (unique) formulas in the world
 | 
|---|
 | 84 |   std::vector<Formula> formula;
 | 
|---|
 | 85 | 
 | 
|---|
| [b47bfc] | 86 |   int i;
 | 
|---|
 | 87 |   MoleculeList::iterator iter;
 | 
|---|
 | 88 |   for(iter = molecules->ListOfMolecules.begin(),i=0;
 | 
|---|
 | 89 |       iter != molecules->ListOfMolecules.end();
 | 
|---|
 | 90 |       ++i,++iter) {
 | 
|---|
 | 91 | 
 | 
|---|
| [79b59b] | 92 |     // find group if already in list
 | 
|---|
 | 93 |     QTreeWidgetItem *groupItem = NULL;
 | 
|---|
 | 94 |     for (unsigned int j=0;j<formula.size();j++)
 | 
|---|
 | 95 |       if ((*iter)->getFormula() == formula[j]){
 | 
|---|
 | 96 |         groupItem = topLevelItem(j);
 | 
|---|
 | 97 |         break;
 | 
|---|
 | 98 |       }
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |     // new molecule type -> create new group
 | 
|---|
 | 101 |     if (!groupItem){
 | 
|---|
 | 102 |       formula.push_back((*iter)->getFormula());
 | 
|---|
 | 103 |       groupItem = new QTreeWidgetItem(this);
 | 
|---|
 | 104 |       groupItem->setText(0, QString((*iter)->getName().c_str()));
 | 
|---|
 | 105 |       groupItem->setText(1, QString::number((*iter)->getAtomCount()));
 | 
|---|
 | 106 |       groupItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
 | 
|---|
 | 107 |       groupItem->setText(3, "0");
 | 
|---|
 | 108 |     }
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |     // add molecule
 | 
|---|
 | 111 |     QTreeWidgetItem *molItem = new QTreeWidgetItem(groupItem);
 | 
|---|
 | 112 |     molItem->setText(0, QString((*iter)->getName().c_str()));
 | 
|---|
 | 113 |     molItem->setText(1, QString::number((*iter)->getAtomCount()));
 | 
|---|
 | 114 |     molItem->setText(2, QString((*iter)->getFormula().toString().c_str()));
 | 
|---|
| [b47bfc] | 115 |     const int index = (*iter)->IndexNr;
 | 
|---|
| [79b59b] | 116 |     molItem->setData(0, Qt::UserRole, QVariant(index));
 | 
|---|
| [b14efe] | 117 |     molItem->setSelected(World::getInstance().isSelected(*iter));
 | 
|---|
| [79b59b] | 118 | 
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |     // increase group occurrence
 | 
|---|
 | 121 |     int count = groupItem->text(3).toInt() + 1;
 | 
|---|
 | 122 |     groupItem->setText(3, QString::number(count));
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 |     //molSelection[i]=nameWidget->isSelected();
 | 
|---|
| [b47bfc] | 125 |   }
 | 
|---|
| [a39006] | 126 |   dirty = false;
 | 
|---|
 | 127 | }
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 | void QtWorldView::paintEvent(QPaintEvent * event)
 | 
|---|
 | 130 | {
 | 
|---|
 | 131 |   if (dirty)
 | 
|---|
 | 132 |     refill();
 | 
|---|
 | 133 |   QTreeWidget::paintEvent(event);
 | 
|---|
| [b47bfc] | 134 | }
 | 
|---|
 | 135 | 
 | 
|---|
| [4cf323d] | 136 | void QtWorldView::subjectKilled(Observable *publisher) {
 | 
|---|
| [b47bfc] | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
| [79b59b] | 139 | void QtWorldView::moleculeChanged() {
 | 
|---|
 | 140 |   /*int idx = verticalHeaderItem(row)->data(Qt::UserRole).toInt();
 | 
|---|
| [b47bfc] | 141 |   molecule *mol = molecules->ReturnIndex(idx);
 | 
|---|
 | 142 |   string cellValue = item(row,NAME)->text().toStdString();
 | 
|---|
 | 143 |   if(mol->getName() != cellValue && cellValue !="") {
 | 
|---|
 | 144 |     mol->setName(cellValue);
 | 
|---|
 | 145 |   }
 | 
|---|
 | 146 |   else if(cellValue==""){
 | 
|---|
 | 147 |     item(row,NAME)->setText(QString(mol->getName().c_str()));
 | 
|---|
| [79b59b] | 148 |   }*/
 | 
|---|
| [b47bfc] | 149 | }
 | 
|---|
 | 150 | 
 | 
|---|
 | 151 | 
 | 
|---|
| [79b59b] | 152 | void QtWorldView::rowSelected(){
 | 
|---|
| [b14efe] | 153 | 
 | 
|---|
 | 154 |   // lookup all molecules in the treeWidget
 | 
|---|
 | 155 |   for (int i=0;i<topLevelItemCount();i++){
 | 
|---|
 | 156 |     QTreeWidgetItem *top = topLevelItem(i);
 | 
|---|
 | 157 |     for (int j=0;j<top->childCount();j++){
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 |       // molecules are 1 level below the top
 | 
|---|
 | 160 |       QTreeWidgetItem *molItem = top->child(j);
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 |       // molecule index stored as user data
 | 
|---|
 | 163 |       int index = molItem->data(0, Qt::UserRole).toInt();
 | 
|---|
 | 164 |       molecule *mol = molecules->ReturnIndex(index);
 | 
|---|
 | 165 |       ASSERT(mol, "QtWorldView::rowSelected()");
 | 
|---|
 | 166 | 
 | 
|---|
 | 167 |       // selection changed by user?
 | 
|---|
 | 168 |       bool molSelectedWorld = World::getInstance().isSelected(mol);
 | 
|---|
 | 169 |       bool molSelectedList = molItem->isSelected();
 | 
|---|
 | 170 |       //std::cout << molSelectedWorld << " " << molSelectedList << std::endl;
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 |       if (molSelectedWorld != molSelectedList){
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 |         // apply new selection state
 | 
|---|
 | 175 |         if (molSelectedList){
 | 
|---|
 | 176 |           //std::cout << "select molecule" << std::endl;
 | 
|---|
 | 177 |           MoleCuilder::SelectionMoleculeById(mol->getId());
 | 
|---|
 | 178 |         }else{
 | 
|---|
 | 179 |           //std::cout << "unselect molecule" << std::endl;
 | 
|---|
 | 180 |           MoleCuilder::SelectionNotMoleculeById(mol->getId());
 | 
|---|
 | 181 |         }
 | 
|---|
| [b47bfc] | 182 |       }
 | 
|---|
 | 183 |     }
 | 
|---|
| [b14efe] | 184 |   }
 | 
|---|
| [b47bfc] | 185 | }
 | 
|---|