| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2012 University of Bonn. All rights reserved.
 | 
|---|
| 5 |  * 
 | 
|---|
| 6 |  *
 | 
|---|
| 7 |  *   This file is part of MoleCuilder.
 | 
|---|
| 8 |  *
 | 
|---|
| 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
| 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
| 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
| 12 |  *    (at your option) any later version.
 | 
|---|
| 13 |  *
 | 
|---|
| 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
| 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 17 |  *    GNU General Public License for more details.
 | 
|---|
| 18 |  *
 | 
|---|
| 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
| 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | /*
 | 
|---|
| 24 |  * QtElementList.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 6, 2012
 | 
|---|
| 27 |  *      Author: ankele
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "Views/Qt4/QtElementList.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <iostream>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | #include "Atom/atom.hpp"
 | 
|---|
| 42 | #include "Formula.hpp"
 | 
|---|
| 43 | #include "molecule.hpp"
 | 
|---|
| 44 | #include "MoleculeListClass.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "Element/element.hpp"
 | 
|---|
| 47 | #include "Element/periodentafel.hpp"
 | 
|---|
| 48 | #include "Descriptors/AtomTypeDescriptor.hpp"
 | 
|---|
| 49 | #include <QAbstractItemView>
 | 
|---|
| 50 | #include "Actions/SelectionAction/Atoms/AtomByElementAction.hpp"
 | 
|---|
| 51 | #include "Actions/SelectionAction/Atoms/NotAtomByElementAction.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | using namespace std;
 | 
|---|
| 54 | 
 | 
|---|
| 55 | const int QtElementList::COLUMNCOUNT = COLUMNTYPES_MAX;
 | 
|---|
| 56 | const char *QtElementList::COLUMNNAMES[QtElementList::COLUMNCOUNT]={"Number","Name","Symbol","Mass","Occurrence"};
 | 
|---|
| 57 | 
 | 
|---|
| 58 | QtElementList::QtElementList(QWidget * _parent) :
 | 
|---|
| 59 |     QTreeWidget (_parent),
 | 
|---|
| 60 |     Observer("QtElementList")
 | 
|---|
| 61 | {
 | 
|---|
| 62 |   setColumnCount(COLUMNCOUNT);
 | 
|---|
| 63 |   setSelectionMode(QAbstractItemView::MultiSelection);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |   QStringList header;
 | 
|---|
| 66 |         for(int i=0; i<COLUMNCOUNT;++i)
 | 
|---|
| 67 |           header << COLUMNNAMES[i];
 | 
|---|
| 68 |         setHeaderLabels(header);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         dirty = true;
 | 
|---|
| 71 |         refill();
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         molecules = World::getInstance().getMolecules();
 | 
|---|
| 74 |         molecules->signOn(this);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected()));
 | 
|---|
| 77 |   connect(this,SIGNAL(changed()),this,SLOT(update()));
 | 
|---|
| 78 | }
 | 
|---|
| 79 | 
 | 
|---|
| 80 | QtElementList::~QtElementList()
 | 
|---|
| 81 | {
 | 
|---|
| 82 |         molecules->signOff(this);
 | 
|---|
| 83 | }
 | 
|---|
| 84 | 
 | 
|---|
| 85 | void QtElementList::update(Observable *publisher) {
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   dirty = true;
 | 
|---|
| 88 | 
 | 
|---|
| 89 |   // force an update from Qt...
 | 
|---|
| 90 |   clear();
 | 
|---|
| 91 |   //emit changed(); doesn't work!?!
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | void QtElementList::refill() {
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   periodentafel *&periode = World::getInstance().getPeriode();
 | 
|---|
| 97 | 
 | 
|---|
| 98 |   elementSelection.clear();
 | 
|---|
| 99 | 
 | 
|---|
| 100 |   int i;
 | 
|---|
| 101 | 
 | 
|---|
| 102 |   clear();
 | 
|---|
| 103 |   periodentafel::const_iterator iter;
 | 
|---|
| 104 |   for(iter = periode->begin(),i=0;
 | 
|---|
| 105 |       iter != periode->end();
 | 
|---|
| 106 |       ++i,++iter) {
 | 
|---|
| 107 |     const element *e = iter->second;
 | 
|---|
| 108 |     int count = 0;
 | 
|---|
| 109 |     count = World::getInstance().getAllAtoms(AtomByType(e)).size();
 | 
|---|
| 110 | 
 | 
|---|
| 111 |     QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
 | 
|---|
| 112 |     treeItem->setText(NUMBER, QString::number(e->getAtomicNumber()));
 | 
|---|
| 113 |     treeItem->setText(NAME, QString(e->getName().c_str()));
 | 
|---|
| 114 |     treeItem->setText(SYMBOL, QString(e->getSymbol().c_str()));
 | 
|---|
| 115 |     treeItem->setText(MASS, QString::number(e->getMass()));
 | 
|---|
| 116 |     if (count > 0){
 | 
|---|
| 117 |       treeItem->setText(OCCURRENCE, QString::number(count));
 | 
|---|
| 118 |     }else{
 | 
|---|
| 119 |       treeItem->setText(OCCURRENCE, "none");
 | 
|---|
| 120 |       treeItem->setDisabled(true);
 | 
|---|
| 121 |     }
 | 
|---|
| 122 |     elementSelection.push_back(treeItem->isSelected());
 | 
|---|
| 123 |   }
 | 
|---|
| 124 |   dirty = false;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | void QtElementList::paintEvent(QPaintEvent * event)
 | 
|---|
| 128 | {
 | 
|---|
| 129 |   if (dirty)
 | 
|---|
| 130 |     refill();
 | 
|---|
| 131 |   QTreeWidget::paintEvent(event);
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void QtElementList::subjectKilled(Observable *publisher) {
 | 
|---|
| 135 | }
 | 
|---|
| 136 | 
 | 
|---|
| 137 | 
 | 
|---|
| 138 | void QtElementList::rowSelected()
 | 
|---|
| 139 | {
 | 
|---|
| 140 |   //std::cout << "rowSelected\n";
 | 
|---|
| 141 |   periodentafel *&periode = World::getInstance().getPeriode();
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   for (int i=0;i<topLevelItemCount();i++){
 | 
|---|
| 144 |     QTreeWidgetItem *item = topLevelItem(i);
 | 
|---|
| 145 |     bool newSelection = item->isSelected();
 | 
|---|
| 146 |     if (newSelection != elementSelection[i]){
 | 
|---|
| 147 |       elementSelection[i] = newSelection;
 | 
|---|
| 148 | 
 | 
|---|
| 149 |       int atomicNumber = item->text(NUMBER).toInt();
 | 
|---|
| 150 |       const element *e = periode->FindElement(atomicNumber);
 | 
|---|
| 151 |       if (newSelection)
 | 
|---|
| 152 |         MoleCuilder::SelectionAtomByElement(e);
 | 
|---|
| 153 |       else
 | 
|---|
| 154 |         MoleCuilder::SelectionNotAtomByElement(e);
 | 
|---|
| 155 |     }
 | 
|---|
| 156 |   }
 | 
|---|
| 157 | }
 | 
|---|
| 158 | 
 | 
|---|