| [0e6156] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010-2012 University of Bonn. All rights reserved. | 
|---|
| [94d5ac6] | 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/>. | 
|---|
| [0e6156] | 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" | 
|---|
| [59a28e] | 48 | #include "Descriptors/AtomTypeDescriptor.hpp" | 
|---|
|  | 49 | #include <QAbstractItemView> | 
|---|
| [1de61e] | 50 | #include "Actions/SelectionAction/Atoms/AtomByElementAction.hpp" | 
|---|
|  | 51 | #include "Actions/SelectionAction/Atoms/NotAtomByElementAction.hpp" | 
|---|
| [0e6156] | 52 |  | 
|---|
|  | 53 | using namespace std; | 
|---|
|  | 54 |  | 
|---|
|  | 55 | const int QtElementList::COLUMNCOUNT = COLUMNTYPES_MAX; | 
|---|
| [59a28e] | 56 | const char *QtElementList::COLUMNNAMES[QtElementList::COLUMNCOUNT]={"Number","Name","Symbol","Mass","Occurrence"}; | 
|---|
| [0e6156] | 57 |  | 
|---|
|  | 58 | QtElementList::QtElementList(QWidget * _parent) : | 
|---|
| [f20fa5] | 59 | QTreeWidget (_parent), | 
|---|
| [0e6156] | 60 | Observer("QtElementList") | 
|---|
|  | 61 | { | 
|---|
| [f20fa5] | 62 | setColumnCount(COLUMNCOUNT); | 
|---|
| [59a28e] | 63 | setSelectionMode(QAbstractItemView::MultiSelection); | 
|---|
| [0e6156] | 64 |  | 
|---|
| [f20fa5] | 65 | QStringList header; | 
|---|
|  | 66 | for(int i=0; i<COLUMNCOUNT;++i) | 
|---|
|  | 67 | header << COLUMNNAMES[i]; | 
|---|
|  | 68 | setHeaderLabels(header); | 
|---|
| [0e6156] | 69 |  | 
|---|
| [25b3559] | 70 | dirty = true; | 
|---|
|  | 71 | refill(); | 
|---|
| [818201] | 72 |  | 
|---|
| [0e6156] | 73 | molecules = World::getInstance().getMolecules(); | 
|---|
|  | 74 | molecules->signOn(this); | 
|---|
|  | 75 |  | 
|---|
| [25b3559] | 76 | connect(this,SIGNAL(itemSelectionChanged()),this,SLOT(rowSelected())); | 
|---|
|  | 77 | connect(this,SIGNAL(changed()),this,SLOT(update())); | 
|---|
| [59a28e] | 78 | } | 
|---|
|  | 79 |  | 
|---|
|  | 80 | QtElementList::~QtElementList() | 
|---|
|  | 81 | { | 
|---|
|  | 82 | molecules->signOff(this); | 
|---|
|  | 83 | } | 
|---|
|  | 84 |  | 
|---|
|  | 85 | void QtElementList::update(Observable *publisher) { | 
|---|
|  | 86 |  | 
|---|
| [25b3559] | 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 |  | 
|---|
| [0e6156] | 96 | periodentafel *&periode = World::getInstance().getPeriode(); | 
|---|
| [f20fa5] | 97 |  | 
|---|
| [1de61e] | 98 | elementSelection.clear(); | 
|---|
|  | 99 |  | 
|---|
| [f20fa5] | 100 | int i; | 
|---|
| [59a28e] | 101 |  | 
|---|
|  | 102 | clear(); | 
|---|
| [f20fa5] | 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; | 
|---|
| [59a28e] | 108 | int count = 0; | 
|---|
|  | 109 | count = World::getInstance().getAllAtoms(AtomByType(e)).size(); | 
|---|
| [f20fa5] | 110 |  | 
|---|
|  | 111 | QTreeWidgetItem *treeItem = new QTreeWidgetItem(this); | 
|---|
| [1de61e] | 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())); | 
|---|
| [59a28e] | 116 | if (count > 0){ | 
|---|
| [1de61e] | 117 | treeItem->setText(OCCURRENCE, QString::number(count)); | 
|---|
| [59a28e] | 118 | }else{ | 
|---|
| [1de61e] | 119 | treeItem->setText(OCCURRENCE, "none"); | 
|---|
| [59a28e] | 120 | treeItem->setDisabled(true); | 
|---|
|  | 121 | } | 
|---|
| [1de61e] | 122 | elementSelection.push_back(treeItem->isSelected()); | 
|---|
| [0e6156] | 123 | } | 
|---|
| [25b3559] | 124 | dirty = false; | 
|---|
|  | 125 | } | 
|---|
|  | 126 |  | 
|---|
|  | 127 | void QtElementList::paintEvent(QPaintEvent * event) | 
|---|
|  | 128 | { | 
|---|
|  | 129 | if (dirty) | 
|---|
|  | 130 | refill(); | 
|---|
|  | 131 | QTreeWidget::paintEvent(event); | 
|---|
| [0e6156] | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 | void QtElementList::subjectKilled(Observable *publisher) { | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 |  | 
|---|
| [1de61e] | 138 | void QtElementList::rowSelected() | 
|---|
| [0e6156] | 139 | { | 
|---|
| [1de61e] | 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 | } | 
|---|
| [0e6156] | 157 | } | 
|---|
|  | 158 |  | 
|---|