| 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 |  * QtInfoBox.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: Mar 4, 2010
 | 
|---|
| 27 |  *      Author: crueger
 | 
|---|
| 28 |  */
 | 
|---|
| 29 | 
 | 
|---|
| 30 | // include config.h
 | 
|---|
| 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 32 | #include <config.h>
 | 
|---|
| 33 | #endif
 | 
|---|
| 34 | 
 | 
|---|
| 35 | #include "Views/Qt4/QtInfoBox.hpp"
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #include <iostream>
 | 
|---|
| 38 | #include <QAbstractItemView>
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "UIElements/Qt4/InstanceBoard/QtObservedInstanceBoard.hpp"
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #include <boost/thread/locks.hpp>
 | 
|---|
| 45 | 
 | 
|---|
| 46 | #include "Element/element.hpp"
 | 
|---|
| 47 | #include "Element/periodentafel.hpp"
 | 
|---|
| 48 | #include "World.hpp"
 | 
|---|
| 49 | 
 | 
|---|
| 50 | using namespace std;
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /***************** Basic structure for tab layout ***********/
 | 
|---|
| 53 | 
 | 
|---|
| 54 | QtInfoBox::QtInfoBox(QtObservedInstanceBoard *_board, QWidget *_parent) :
 | 
|---|
| 55 |     QTabWidget(_parent),
 | 
|---|
| 56 |     curAtomId(-1), nextAtomId(-1),
 | 
|---|
| 57 |     curMoleculeId(-1), nextMoleculeId(-1),
 | 
|---|
| 58 |     page_mol(NULL), page_atom(NULL),
 | 
|---|
| 59 |     board(_board),
 | 
|---|
| 60 |     currentPage(0),
 | 
|---|
| 61 |     periode(World::getInstance().getPeriode())
 | 
|---|
| 62 | {
 | 
|---|
| 63 |   timer = new QTimer(this);
 | 
|---|
| 64 |   timer->setSingleShot(true);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   setMinimumWidth(200);
 | 
|---|
| 67 |   setMinimumHeight(220);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |   connect(timer, SIGNAL(timeout()), this, SLOT(timerTimeout()));
 | 
|---|
| 70 | }
 | 
|---|
| 71 | 
 | 
|---|
| 72 | QtInfoBox::~QtInfoBox()
 | 
|---|
| 73 | {
 | 
|---|
| 74 |   clearAtomTab();
 | 
|---|
| 75 |   clearMoleculeTab();
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | void QtInfoBox::atomHover(const atomId_t _id)
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   nextAtomId = _id;
 | 
|---|
| 81 |   timer->start(500);
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | void QtInfoBox::moleculeHover(const moleculeId_t _id)
 | 
|---|
| 85 | {
 | 
|---|
| 86 |   nextMoleculeId = _id;
 | 
|---|
| 87 |   timer->start(500);
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | void QtInfoBox::timerTimeout()
 | 
|---|
| 91 | {
 | 
|---|
| 92 |   if (nextAtomId != (atomId_t)-1)
 | 
|---|
| 93 |     showAtom(nextAtomId);
 | 
|---|
| 94 |   if (nextMoleculeId != (moleculeId_t)-1)
 | 
|---|
| 95 |     showMolecule(nextMoleculeId);
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | void QtInfoBox::clearAtomTab()
 | 
|---|
| 99 | {
 | 
|---|
| 100 |   boost::recursive_mutex::scoped_lock lock(tabs_mutex);
 | 
|---|
| 101 |   if (page_atom){
 | 
|---|
| 102 |     if (page_atom->getAtom())
 | 
|---|
| 103 |       disconnect(page_atom->getAtom().get(), SIGNAL(atomRemoved()), this, SLOT(clearAtomTab()));
 | 
|---|
| 104 |     //removeTab(indexOf(page_atom));
 | 
|---|
| 105 |     delete(page_atom);
 | 
|---|
| 106 |     page_atom = NULL;
 | 
|---|
| 107 |   }
 | 
|---|
| 108 | }
 | 
|---|
| 109 | 
 | 
|---|
| 110 | void QtInfoBox::clearMoleculeTab()
 | 
|---|
| 111 | {
 | 
|---|
| 112 |   boost::recursive_mutex::scoped_lock lock(tabs_mutex);
 | 
|---|
| 113 |   if (page_mol){
 | 
|---|
| 114 |     if (page_mol->getMolecule())
 | 
|---|
| 115 |       disconnect(page_mol->getMolecule().get(), SIGNAL(moleculeRemoved()), this, SLOT(clearMoleculeTab()));
 | 
|---|
| 116 |     //removeTab(indexOf(page_mol));
 | 
|---|
| 117 |     delete(page_mol);
 | 
|---|
| 118 |     page_mol = NULL;
 | 
|---|
| 119 |   }
 | 
|---|
| 120 | }
 | 
|---|
| 121 | 
 | 
|---|
| 122 | void QtInfoBox::showAtom(const atomId_t _id)
 | 
|---|
| 123 | {
 | 
|---|
| 124 |   currentPage = currentIndex();
 | 
|---|
| 125 | 
 | 
|---|
| 126 |   // Remove old tabs.
 | 
|---|
| 127 |   clearAtomTab();
 | 
|---|
| 128 |   clearMoleculeTab();
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   QtObservedAtom::ptr curAtom = board->getObservedAtom(_id);
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   // Show new tabs.
 | 
|---|
| 133 |   if (curAtom){
 | 
|---|
| 134 |     curAtomId = curAtom->getAtomIndex();
 | 
|---|
| 135 |     nextAtomId = -1;
 | 
|---|
| 136 |     nextMoleculeId = -1;
 | 
|---|
| 137 | 
 | 
|---|
| 138 |     boost::recursive_mutex::scoped_lock lock(tabs_mutex);
 | 
|---|
| 139 |     page_atom = new QtAtomInfoPage(curAtom, periode, this);
 | 
|---|
| 140 |     addTab(page_atom, "Atom");
 | 
|---|
| 141 |     connect(curAtom.get(), SIGNAL(atomRemoved()), this, SLOT(clearAtomTab()));
 | 
|---|
| 142 | 
 | 
|---|
| 143 |     if (curAtom->getMoleculeRef() != NULL) {
 | 
|---|
| 144 |       QtObservedMolecule::ptr curMolecule = curAtom->getMoleculeRef()->getRef();
 | 
|---|
| 145 |       if (curMolecule) {
 | 
|---|
| 146 |         page_mol = new QtMoleculeInfoPage(curMolecule, this);
 | 
|---|
| 147 |         addTab(page_mol, "Molecule");
 | 
|---|
| 148 |         connect(curMolecule.get(), SIGNAL(moleculeRemoved()), this, SLOT(clearMoleculeTab()));
 | 
|---|
| 149 | 
 | 
|---|
| 150 |         if (currentPage > 0)
 | 
|---|
| 151 |           setCurrentIndex(currentPage);
 | 
|---|
| 152 |       }
 | 
|---|
| 153 |     }
 | 
|---|
| 154 |   }
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | void QtInfoBox::showMolecule(const moleculeId_t _id)
 | 
|---|
| 158 | {
 | 
|---|
| 159 |   currentPage = currentIndex();
 | 
|---|
| 160 | 
 | 
|---|
| 161 |   // Remove old tabs.
 | 
|---|
| 162 |   clearAtomTab();
 | 
|---|
| 163 |   clearMoleculeTab();
 | 
|---|
| 164 | 
 | 
|---|
| 165 |   QtObservedMolecule::ptr curMolecule = board->getObservedMolecule(_id);
 | 
|---|
| 166 |   nextAtomId = -1;
 | 
|---|
| 167 |   nextMoleculeId = -1;
 | 
|---|
| 168 | 
 | 
|---|
| 169 |   // Show new tabs.
 | 
|---|
| 170 |   if (curMolecule){
 | 
|---|
| 171 |     boost::recursive_mutex::scoped_lock lock(tabs_mutex);
 | 
|---|
| 172 |     page_mol = new QtMoleculeInfoPage(curMolecule, this);
 | 
|---|
| 173 |     addTab(page_mol, "Molecule");
 | 
|---|
| 174 |     connect(curMolecule.get(), SIGNAL(moleculeRemoved()), this, SLOT(clearMoleculeTab()));
 | 
|---|
| 175 | 
 | 
|---|
| 176 |     if (currentPage > 0)
 | 
|---|
| 177 |       setCurrentIndex(currentPage);
 | 
|---|
| 178 |   }
 | 
|---|
| 179 | }
 | 
|---|
| 180 | 
 | 
|---|
| 181 | /************************ Tab for single Atoms ********************/
 | 
|---|
| 182 | 
 | 
|---|
| 183 | static void addInfo(QTreeWidget *info, const QString &key, const QString &value)
 | 
|---|
| 184 | {
 | 
|---|
| 185 |   QTreeWidgetItem *treeItem = new QTreeWidgetItem(info);
 | 
|---|
| 186 |   treeItem->setText(0, key);
 | 
|---|
| 187 |   treeItem->setText(1, value);
 | 
|---|
| 188 | }
 | 
|---|
| 189 | 
 | 
|---|
| 190 | QtAtomInfoPage::QtAtomInfoPage(
 | 
|---|
| 191 |     QtObservedAtom::ptr &_atom,
 | 
|---|
| 192 |     periodentafel *_periode,
 | 
|---|
| 193 |     QWidget *parent) :
 | 
|---|
| 194 |   QTreeWidget(parent),
 | 
|---|
| 195 |   atomRef(_atom),
 | 
|---|
| 196 |   periode(_periode)
 | 
|---|
| 197 | {
 | 
|---|
| 198 |   setColumnCount(2);
 | 
|---|
| 199 |   QStringList header;
 | 
|---|
| 200 |   header << "data";
 | 
|---|
| 201 |   header << "value";
 | 
|---|
| 202 |   setHeaderLabels(header);
 | 
|---|
| 203 | 
 | 
|---|
| 204 |   updatePage();
 | 
|---|
| 205 | }
 | 
|---|
| 206 | 
 | 
|---|
| 207 | void QtAtomInfoPage::updatePage()
 | 
|---|
| 208 | {
 | 
|---|
| 209 |   clear();
 | 
|---|
| 210 | 
 | 
|---|
| 211 |   if (atomRef == NULL)
 | 
|---|
| 212 |     return;
 | 
|---|
| 213 |   const element * const _element = periode->FindElement(atomRef->getAtomElement());
 | 
|---|
| 214 |   addInfo(this, "Name", QString(atomRef->getAtomName().c_str()));
 | 
|---|
| 215 |   addInfo(this, "Element", QString(_element->getName().c_str()));
 | 
|---|
| 216 |   addInfo(this, "Mass", QString("%1").arg(_element->getMass()));
 | 
|---|
| 217 |   addInfo(this, "Charge", QString("%1").arg(_element->getCharge()));
 | 
|---|
| 218 |   addInfo(this, "Bonds", QString("%1").arg(atomRef->getAtomBonds().size()));
 | 
|---|
| 219 |   addInfo(this, "Position x", QString(toString(atomRef->getAtomPosition()[0]).c_str()));
 | 
|---|
| 220 |   addInfo(this, "Position y", QString(toString(atomRef->getAtomPosition()[1]).c_str()));
 | 
|---|
| 221 |   addInfo(this, "Position z", QString(toString(atomRef->getAtomPosition()[2]).c_str()));
 | 
|---|
| 222 | }
 | 
|---|
| 223 | 
 | 
|---|
| 224 | QtAtomInfoPage::~QtAtomInfoPage()
 | 
|---|
| 225 | {}
 | 
|---|
| 226 | 
 | 
|---|
| 227 | /************************ Tab for single Molecules *****************/
 | 
|---|
| 228 | 
 | 
|---|
| 229 | QtMoleculeInfoPage::QtMoleculeInfoPage(
 | 
|---|
| 230 |     QtObservedMolecule::ptr &_mol,
 | 
|---|
| 231 |     QWidget *parent) :
 | 
|---|
| 232 |     QTreeWidget(parent),
 | 
|---|
| 233 |     mol(_mol)
 | 
|---|
| 234 | {
 | 
|---|
| 235 |   setColumnCount(2);
 | 
|---|
| 236 |   QStringList header;
 | 
|---|
| 237 |   header << "data";
 | 
|---|
| 238 |   header << "value";
 | 
|---|
| 239 |   setHeaderLabels(header);
 | 
|---|
| 240 | 
 | 
|---|
| 241 |   updatePage();
 | 
|---|
| 242 | }
 | 
|---|
| 243 | 
 | 
|---|
| 244 | void QtMoleculeInfoPage::updatePage()
 | 
|---|
| 245 | {
 | 
|---|
| 246 |   clear();
 | 
|---|
| 247 | 
 | 
|---|
| 248 |   if (mol == NULL)
 | 
|---|
| 249 |     return;
 | 
|---|
| 250 |   addInfo(this, "Name", QString(mol->getMolName().c_str()));
 | 
|---|
| 251 |   addInfo(this, "Formula", QString(mol->getMolFormula().c_str()));
 | 
|---|
| 252 |   addInfo(this, "Atoms", QString("%1").arg(mol->getAtomCount()));
 | 
|---|
| 253 |   addInfo(this, "NonHydrogens", QString("%1").arg(mol->getNonHydrogenCount()));
 | 
|---|
| 254 |   addInfo(this, "Bonds", QString("%1").arg(mol->getBondCount()));
 | 
|---|
| 255 |   const Vector molCenter = mol->getMolCenter();
 | 
|---|
| 256 |   addInfo(this, "Center x", QString("%1").arg(molCenter[0]));
 | 
|---|
| 257 |   addInfo(this, "Center y", QString("%1").arg(molCenter[1]));
 | 
|---|
| 258 |   addInfo(this, "Center z", QString("%1").arg(molCenter[2]));
 | 
|---|
| 259 | }
 | 
|---|
| 260 | 
 | 
|---|
| 261 | QtMoleculeInfoPage::~QtMoleculeInfoPage()
 | 
|---|
| 262 | {}
 | 
|---|