- Timestamp:
- Apr 18, 2016, 10:33:41 PM (9 years ago)
- Branches:
- Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
- Children:
- ea2663
- Parents:
- 24eb6b
- git-author:
- Frederik Heber <heber@…> (03/18/16 10:17:01)
- git-committer:
- Frederik Heber <heber@…> (04/18/16 22:33:41)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp
r24eb6b r5771e6 66 66 using namespace std; 67 67 68 const unsigned int QtMoleculeList::update_times_per_second = 20;69 70 68 QtMoleculeList::QtMoleculeList( 71 69 QtObservedInstanceBoard *_board, 72 70 QObject *_parent) : 73 71 QStandardItemModel(_parent), 74 update_timer(NULL),75 board(_board)72 board(_board), 73 observer(_board) 76 74 { 77 75 setColumnCount(QtMoleculeItemFactory::COLUMNCOUNT); 78 76 79 resetUpdateTimer(); 80 81 connect(this,SIGNAL(itemChanged(QStandardItem*)),this,SLOT(moleculeNameChanged(QStandardItem*))); 82 connect(this, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(checkForVisibilityChange(QStandardItem*))); 83 connect(board, SIGNAL(moleculeInserted(QtObservedMolecule::ptr)), this, SLOT(moleculeInserted(QtObservedMolecule::ptr))); 84 connect(board, SIGNAL(moleculeRemoved(const moleculeId_t)), this, SLOT(moleculeRemoved(const moleculeId_t))); 77 connect(this,SIGNAL(itemChanged(QStandardItem*)), 78 this,SLOT(moleculeNameChanged(QStandardItem*))); 79 connect(this, SIGNAL(itemChanged(QStandardItem*)), 80 this, SLOT(checkForVisibilityChange(QStandardItem*))); 81 connect(&observer, SIGNAL(MoleculeInserted(QtObservedMolecule::ptr)), 82 this, SLOT(moleculeInserted(QtObservedMolecule::ptr))); 83 connect(&observer, SIGNAL(MoleculeRemoved(const moleculeId_t)), 84 this, SLOT(moleculeRemoved(const moleculeId_t))); 85 connect(&observer, SIGNAL(FormulaChanged(const QtObservedMolecule::ptr)), 86 this, SLOT(formulaChanged(const QtObservedMolecule::ptr))); 87 connect(&observer, SIGNAL(NameChanged(const QtObservedMolecule::ptr)), 88 this, SLOT(nameChanged(const QtObservedMolecule::ptr))); 85 89 connect(board, SIGNAL(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)), 86 90 this, SLOT(moleculeIndexChanged(const moleculeId_t, const moleculeId_t))); … … 103 107 void QtMoleculeList::moleculeInserted(QtObservedMolecule::ptr _mol) 104 108 { 105 // get ObservedMolecule from board 106 boost::recursive_mutex::scoped_lock lock(listAccessing_mutex); 107 newMolecules.push_back( _mol ); 109 LOG(1, "Adding molecule " << _mol->getMolName()); 110 // check that World knows the molecule still 111 const std::string formula = addMolecule(_mol); 112 LOG(1, "Adding " << formula << " to toBeSetOccurrence."); 113 setOccurrence(FormulaToGroupItem(formula)); 108 114 } 109 115 110 116 void QtMoleculeList::moleculeRemoved(const moleculeId_t _id) 111 117 { 112 boost::recursive_mutex::scoped_lock lock(map_mutex); 113 KilledItemsPerMolecule_t::iterator iter = KilledItemsPerMolecule.find(_id); 114 if (iter == KilledItemsPerMolecule.end()) 115 KilledItemsPerMolecule.insert( std::make_pair(_id, 1)); 116 else 117 ++(iter->second); 118 if (iter->second == QtMoleculeItem::COLUMNTYPES_MAX) { 119 boost::recursive_mutex::scoped_lock lock(listAccessing_mutex); 120 removedMolecules.push_back( _id ); 118 LOG(1, "Removing molecule " << _id); 119 if (!isMoleculeItemPresent(_id)) { 120 ELOG(1, "QtMoleculeItem to id " << _id << " has disappeared before removal."); 121 return; 122 } 123 QtMoleculeItem *item = MoleculeIdToItem(_id); 124 if (item != NULL) { 125 const std::string formula = item->parent()->text().toStdString(); 126 LOG(1, "Adding " << formula << " to toBeSetOccurrence."); 127 const int removeindex = setOccurrence(FormulaToGroupItem(formula)); 128 removeMoleculeItem(item); 129 if (removeindex != -1) { 130 LOG(1, "Removing row of group item to " << formula); 131 removeRows(removeindex, 1, invisibleRootItem()->index()); 132 } 121 133 } 122 134 } … … 183 195 MoleculeItemBiMap.left.erase(iter); 184 196 } 185 exchangeKeys(KilledItemsPerMolecule, _oldid, _newid);186 exchangeKeysOverAllColumns(dirtyMolItems, _oldid, _newid);187 exchangeKeysInSet(visibilityMolItems, _oldid, _newid);188 {189 std::vector<moleculeId_t>::iterator iter =190 std::find(removedMolecules.begin(), removedMolecules.end(), _oldid);191 removedMolecules.erase(iter);192 removedMolecules.push_back(_newid);193 }194 exchangeKeysInSet(toBeMovedItems, _oldid, _newid);195 197 } 196 198 197 199 bool QtMoleculeList::isMoleculeItemPresent(const moleculeId_t _molid) const 198 200 { 199 boost::recursive_mutex::scoped_lock lock(map_mutex);200 201 MoleculeItemBiMap_t::left_const_iterator iter = 201 202 MoleculeItemBiMap.left.find(_molid); … … 205 206 QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(const moleculeId_t _molid) const 206 207 { 207 boost::recursive_mutex::scoped_lock lock(map_mutex);208 208 MoleculeItemBiMap_t::left_const_iterator iter = 209 209 MoleculeItemBiMap.left.find(_molid); … … 216 216 const moleculeId_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const 217 217 { 218 boost::recursive_mutex::scoped_lock lock(map_mutex);219 218 const MoleculeItemBiMap_t::right_const_iterator iter = 220 219 MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item)); … … 237 236 bool QtMoleculeList::isGroupItemPresent(const std::string &_formula) const 238 237 { 239 boost::recursive_mutex::scoped_lock lock(map_mutex);240 238 FormulaTreeItemBiMap_t::left_const_iterator iter = 241 239 FormulaItemBiMap.left.find(_formula); … … 245 243 QStandardItem * QtMoleculeList::FormulaToGroupItem(const std::string &_formula) const 246 244 { 247 boost::recursive_mutex::scoped_lock lock(map_mutex);248 245 FormulaTreeItemBiMap_t::left_const_iterator iter = 249 246 FormulaItemBiMap.left.find(_formula); … … 256 253 const std::string& QtMoleculeList::GroupItemToFormula(const QStandardItem * const _item) const 257 254 { 258 boost::recursive_mutex::scoped_lock lock(map_mutex);259 255 static std::string emptystring; 260 256 const FormulaTreeItemBiMap_t::right_const_iterator iter = … … 275 271 const QModelIndex QtMoleculeList::MoleculeIdToIndex(const moleculeId_t _id) const 276 272 { 277 boost::recursive_mutex::scoped_lock lock(refill_mutex);278 273 QtMoleculeItem * const item = MoleculeIdToItem(_id); 279 274 ASSERT(item != NULL, … … 285 280 const moleculeId_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const 286 281 { 287 boost::recursive_mutex::scoped_lock lock(refill_mutex);288 282 QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index)); 289 283 if (item == NULL) … … 300 294 QtMoleculeItemFactory::getInstance().createGroupItems(_molecule_formula); 301 295 mainitem = groupItems.front(); 302 { 303 boost::recursive_mutex::scoped_lock lock(map_mutex); 304 FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) ); 305 } 296 FormulaItemBiMap.left.insert( std::make_pair(_molecule_formula, mainitem) ); 306 297 invisibleRootItem()->appendRow(groupItems); 307 298 } … … 366 357 void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item) 367 358 { 368 boost::recursive_mutex::scoped_lock lock(refill_mutex);369 359 const QModelIndex mol_index = indexFromItem(_item); 370 360 QStandardItem *groupitem = _item->parent(); 371 361 const QModelIndex group_index = groupitem->index(); 372 362 { 373 boost::recursive_mutex::scoped_lock lock(map_mutex);374 363 MoleculeItemBiMap_t::right_iterator removeiter = 375 364 MoleculeItemBiMap.right.find(_item); … … 392 381 } 393 382 394 void QtMoleculeList::resetUpdateTimer()395 {396 // check timer's presence397 if (update_timer == NULL) {398 update_timer = new QTimer(this);399 connect( update_timer, SIGNAL(timeout()), this, SLOT(checkState()));400 } else401 update_timer->stop();402 403 // activate timer404 update_timer->start(1000/update_times_per_second);405 }406 407 bool QtMoleculeList::areAnyItemsDirty()408 {409 // get whether any items are dirty410 boost::recursive_mutex::scoped_lock lock(listAccessing_mutex);411 bool dirty = false;412 dirty |= !dirtyMolItems.empty();413 dirty |= !visibilityMolItems.empty();414 dirty |= !dirtyGroupItems.empty();415 dirty |= !visibilityGroupItems.empty();416 dirty |= !newMolecules.empty();417 dirty |= !removedMolecules.empty();418 dirty |= !toBeMovedItems.empty();419 return dirty;420 }421 422 void QtMoleculeList::checkState()423 {424 const bool dirty = areAnyItemsDirty();425 // update if required426 if (dirty)427 updateItemStates();428 }429 430 383 void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item) 431 384 { 432 385 // qDebug() << "Item changed called."; 433 386 434 boost::recursive_mutex::scoped_lock lock(refill_mutex);435 387 if (_item->index().column() == QtMoleculeItem::VISIBILITY) { 436 388 // qDebug() << "visibilityItem changed: " << (_item->checkState() ? "checked" : "unchecked"); 437 boost::recursive_mutex::scoped_lock lock(listAccessing_mutex); 438 if ((_item->parent() == NULL) || (_item->parent() == invisibleRootItem())) 439 visibilityGroupItems.insert( std::make_pair( 440 GroupItemToFormula(_item->parent()), QtMoleculeItem::VISIBILITY) ); 441 else 442 visibilityMolItems.insert( 443 static_cast<QtMoleculeItem *>(_item)->getMoleculeId() 444 ); 389 if ((_item->parent() == NULL) || (_item->parent() == invisibleRootItem())) { 390 LOG(1, "Updating visibility of group item " << _item); 391 setVisibilityForGroupItem(_item); 392 } else { 393 LOG(1, "Updating visibility of item " << _item); 394 setVisibilityForMoleculeItem( assert_cast<QtMoleculeItem *>(_item) ); 395 } 445 396 } 446 397 } … … 448 399 void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item) 449 400 { 450 if (ChangingChildrensVisibility)451 return;452 453 boost::recursive_mutex::scoped_lock lock(refill_mutex);454 401 const bool visible = _item->checkState(); 455 402 const moleculeId_t molid = _item->getMoleculeId(); 456 403 std::string molecule_formula("illegal"); 457 404 { 458 boost::recursive_mutex::scoped_lock lock(map_mutex); 459 MoleculeFormulaMap_t::const_iterator formulaiter = 405 const MoleculeFormulaMap_t::const_iterator formulaiter = 460 406 MoleculeFormulaMap.find(molid); 461 407 ASSERT( formulaiter != MoleculeFormulaMap.end(), … … 476 422 // check whether we have to set the group item 477 423 478 ChangingChildrensVisibility = true;479 424 if (visible) { 480 425 ++(FormulaVisibilityCountMap[molecule_formula]); … … 489 434 visgroupItem->setCheckState(Qt::Unchecked); 490 435 } 491 ChangingChildrensVisibility = false;492 436 493 437 emit moleculesVisibilityChanged(_item->getMoleculeId(), visible); … … 496 440 void QtMoleculeList::setVisibilityForGroupItem(QStandardItem* _item) 497 441 { 498 if (ChangingChildrensVisibility)499 return;500 501 ChangingChildrensVisibility = true;502 503 boost::recursive_mutex::scoped_lock lock(refill_mutex);504 442 // go through all children, but don't enter for groupItem once more 505 443 const bool visible = _item->checkState(); 506 444 QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME); 507 445 for (int i=0;i<groupitem->rowCount();++i) { 508 QtMoleculeItem * molItem = dynamic_cast<QtMoleculeItem *>(446 QtMoleculeItem * const molItem = dynamic_cast<QtMoleculeItem *>( 509 447 groupitem->child(i, QtMoleculeItem::VISIBILITY)); 510 448 if (molItem->checkState() != visible) { … … 524 462 +" have no entry in visibility count map?"); 525 463 countiter->second = visible ? groupitem->rowCount() : 0; 526 527 ChangingChildrensVisibility = false;528 464 } 529 465 … … 559 495 void QtMoleculeList::moleculeNameChanged(QStandardItem* item) 560 496 { 561 boost::recursive_mutex::scoped_lock lock(refill_mutex);562 497 // obtain molecule id 563 498 if ( item->index().column() == QtMoleculeItem::NAME) { … … 568 503 const moleculeId_t molid = iter->second; 569 504 // change the name 570 molecule * const mol = World::getInstance().getMolecule(MoleculeById(molid));571 505 std::string cellValue = item->text().toStdString(); 572 if ( (mol->getName() != cellValue) && (!cellValue.empty())) {506 if (!cellValue.empty()) { 573 507 // create actions such that we may undo 574 508 static MoleCuilder::ActionSequence sequence; … … 578 512 AQ.registerAction(makroaction); 579 513 AQ.queueAction("change-single-molecule-name", MoleCuilder::Action::NonInteractive); 580 } else if(cellValue==""){581 item->setText(QString(mol->getName().c_str()));514 } else { 515 assert_cast<QtMoleculeItem *>(item)->updateState(); 582 516 } 583 } 584 } 585 517 } 518 } 586 519 587 520 int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem) 588 521 { 589 boost::recursive_mutex::scoped_lock lock(refill_mutex); 522 ASSERT( _groupitem != NULL, 523 "QtMoleculeList::setOccurrence() - group item at "+toString(_groupitem) 524 +" is NULL"); 590 525 QModelIndex modelindex = _groupitem->index(); 591 526 ASSERT( modelindex.isValid(), … … 604 539 if (count == 0) { 605 540 // we have to remove the group item completely 606 boost::recursive_mutex::scoped_lock lock(map_mutex);607 541 const std::string molecule_formula = _groupitem->text().toStdString(); 608 542 FormulaItemBiMap.left.erase(molecule_formula); … … 615 549 } 616 550 617 std::string QtMoleculeList::readdItem(QtMoleculeItem *_molitem) 618 { 619 boost::recursive_mutex::scoped_lock lock(refill_mutex); 551 void QtMoleculeList::moveItem( 552 QtMoleculeItem *_molitem, 553 const std::string &_new_formula) 554 { 620 555 // use takeRows of molecule .. 621 556 QStandardItem *groupitem = _molitem->parent(); … … 626 561 QStandardItem *formulaitem = 627 562 _molitem->parent()->child(_molitem->index().row(), QtMoleculeItem::FORMULA); 628 const std::string molecule_formula = formulaitem->text().toStdString();629 563 QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row()); 630 564 // .. and re-add where new formula fits 631 if (!isGroupItemPresent( molecule_formula)) {565 if (!isGroupItemPresent(_new_formula)) { 632 566 // add new group item and formula entry 633 addGroupItem(groupitem, molecule_formula);567 addGroupItem(groupitem, _new_formula); 634 568 } else { 635 groupitem = FormulaToGroupItem( molecule_formula);569 groupitem = FormulaToGroupItem(_new_formula); 636 570 } 637 571 ASSERT( groupitem != NULL, … … 639 573 // finally add again 640 574 groupitem->appendRow(mol_row); 641 642 return molecule_formula; 643 } 644 645 void QtMoleculeList::updateItemStates() 646 { 647 /// copy lists such that new signals for dirty/.. may come in right away 648 // TODO: if we had move semantics ... 649 listAccessing_mutex.lock(); 650 list_of_molecule_items_t dirtyMolItems_copy = dirtyMolItems; 651 dirtyMolItems.clear(); 652 list_of_molecules_t visibilityMolItems_copy = visibilityMolItems; 653 visibilityMolItems.clear(); 654 list_of_group_items_t dirtyGroupItems_copy = dirtyGroupItems; 655 dirtyGroupItems.clear(); 656 list_of_group_items_t visibilityGroupItems_copy = visibilityGroupItems; 657 visibilityGroupItems.clear(); 658 std::vector<QtObservedMolecule::ptr> newMolecules_copy = newMolecules; 659 newMolecules.clear(); 660 std::vector<moleculeId_t> removedMolecules_copy = removedMolecules; 661 removedMolecules.clear(); 662 list_of_molecules_t toBeMovedItems_copy = toBeMovedItems; 663 toBeMovedItems.clear(); 664 listAccessing_mutex.unlock(); 665 666 // wait till initial refill has been executed 667 boost::recursive_mutex::scoped_lock lock(refill_mutex); 668 669 // LOG(1, "Starting update."); 670 671 // remove removedMolecules from other lists. 672 for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin(); 673 removeiter != removedMolecules_copy.end(); ++removeiter) { 674 for (unsigned int i=0;i< QtMoleculeItem::COLUMNTYPES_MAX; ++i) 675 dirtyMolItems_copy.erase( std::make_pair(*removeiter,(QtMoleculeItem::COLUMNTYPES)i) ); 676 toBeMovedItems_copy.erase(*removeiter); 677 visibilityMolItems_copy.erase(*removeiter); 678 } 679 680 /// 1a. do the update for each dirty item 681 for (list_of_molecule_items_t::const_iterator dirtyiter = dirtyMolItems_copy.begin(); 682 dirtyiter != dirtyMolItems_copy.end(); ++dirtyiter) { 683 if (!isMoleculeItemPresent(dirtyiter->first)) 684 continue; 685 QtMoleculeItem * const mol_item = 686 getSpecificMoleculeItem( 687 MoleculeIdToItem(dirtyiter->first), 688 dirtyiter->second); 689 // LOG(1, "Updating item " << mol_item); 690 mol_item->updateState(); 691 } 692 693 /// 1b. do the visibility update for each dirty item 694 for (list_of_molecules_t::const_iterator visiter = visibilityMolItems_copy.begin(); 695 visiter != visibilityMolItems_copy.end(); ++visiter) { 696 if (!isMoleculeItemPresent(*visiter)) 697 continue; 698 QtMoleculeItem * const visitem = 699 getSpecificMoleculeItem( 700 MoleculeIdToItem(*visiter), 701 QtMoleculeItem::VISIBILITY ); 702 // LOG(1, "Updating visibility of item " << visitem); 703 setVisibilityForMoleculeItem(visitem); 704 } 705 706 /// 2. move all items that need to be moved 707 typedef std::set<std::string> formulas_t; 708 formulas_t toBeSetOccurrence; 709 for (list_of_molecules_t::const_iterator moveiter = toBeMovedItems_copy.begin(); 710 moveiter != toBeMovedItems_copy.end(); ++moveiter) { 711 boost::recursive_mutex::scoped_lock lock(map_mutex); 712 // LOG(1, "Moving item " << molitem); 713 MoleculeFormulaMap_t::iterator formulaiter = 714 MoleculeFormulaMap.find(*moveiter); 715 ASSERT( formulaiter != MoleculeFormulaMap.end(), 716 "QtMoleculeList::updateItemStates() - formula of molecule " 717 +toString(*moveiter)+" unknown."); 718 // LOG(1, "Adding " << formulaiter->second << " to toBeSetOccurrence."); 719 toBeSetOccurrence.insert( formulaiter->second ); 720 if (!isMoleculeItemPresent(*moveiter)) 721 continue; 722 QtMoleculeItem *const molitem = MoleculeIdToItem(*moveiter); 723 LOG(1, "Moving item " << molitem); 575 } 576 577 578 void QtMoleculeList::formulaChanged(const QtObservedMolecule::ptr _mol) 579 { 580 // we need the id as identifier to the items 581 const moleculeId_t molid = _mol->getMolIndex(); 582 LOG(3, "DEBUG: Moving item to id " << molid); 583 const MoleculeFormulaMap_t::iterator formulaiter = 584 MoleculeFormulaMap.find(molid); 585 ASSERT( formulaiter != MoleculeFormulaMap.end(), 586 "QtMoleculeList::updateItemStates() - formula of molecule " 587 +toString(molid)+" unknown."); 588 // we get old formula from stored map and new formula from the ObservedMolecule 589 const std::string old_formula = formulaiter->second; 590 const std::string new_formula = _mol->getMolFormula(); 591 592 // then we move the item if necessary 593 if (old_formula != new_formula) { 594 QtMoleculeItem *const molitem = MoleculeIdToItem(molid); 595 LOG(3, "DEBUG: Moving item " << molitem); 724 596 // remove from formula<->molecule bimap with old formula 725 LOG( 1, "Removing " << formulaiter->second<< " for " << formulaiter->first << " from MoleculeFormulaMap.");597 LOG(4, "DEBUG: Removing " << old_formula << " for " << formulaiter->first << " from MoleculeFormulaMap."); 726 598 MoleculeFormulaMap.erase( formulaiter ); 727 const std::string formula = readdItem(molitem); 728 // and add to formula<->molecule bimap with updated formula 729 LOG(1, "Adding " << formula << " for " << *moveiter << " to MoleculeFormulaMap."); 730 MoleculeFormulaMap.insert( std::make_pair(*moveiter, formula) ); 731 // LOG(1, "Adding " << formula << " to toBeSetOccurrence."); 732 toBeSetOccurrence.insert( formula ); 733 } 734 735 /// 3. remove all items whose molecules have been removed 736 for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin(); 737 removeiter != removedMolecules_copy.end(); ++removeiter) { 738 // LOG(1, "Removing molecule " << *removeiter); 739 if (!isMoleculeItemPresent(*removeiter)) 740 continue; 741 QtMoleculeItem *item = MoleculeIdToItem(*removeiter); 742 if (item != NULL) { 743 const std::string formula = item->parent()->text().toStdString(); 744 // LOG(1, "Adding " << formula << " to toBeSetOccurrence."); 745 toBeSetOccurrence.insert( formula ); 746 removeMoleculeItem(item); 747 KilledItemsPerMolecule.erase( *removeiter ); 599 moveItem(molitem, new_formula); 600 // changing occurrences for old_formula with possible removal 601 const int removeindex = setOccurrence( FormulaToGroupItem(old_formula) ); 602 if (removeindex != -1) { 603 LOG(3, "DEBUG: Removing row of group item to " << old_formula); 604 removeRows(removeindex, 1, invisibleRootItem()->index()); 748 605 } 749 } 750 751 // throw out items that we added by an update() while we are in this function 752 listAccessing_mutex.lock(); 753 for (std::vector<moleculeId_t>::const_iterator removeiter = removedMolecules_copy.begin(); 754 removeiter != removedMolecules_copy.end(); ++removeiter) { 755 for (unsigned int i=0;i< QtMoleculeItem::COLUMNTYPES_MAX; ++i) 756 dirtyMolItems.erase( std::make_pair(*removeiter,(QtMoleculeItem::COLUMNTYPES)i) ); 757 toBeMovedItems.erase(*removeiter); 758 visibilityMolItems.erase(*removeiter); 759 } 760 listAccessing_mutex.unlock(); 761 // after that it is not a problem as items have been removed (hence signOff() was called) 762 763 /// 4. instantiate all new items 764 for (std::vector<QtObservedMolecule::ptr>::iterator moliter = newMolecules_copy.begin(); 765 moliter != newMolecules_copy.end(); ++moliter) { 766 // LOG(1, "Adding molecule " << ObservedMolecule->getMoleculeName()); 767 // check that World knows the molecule still 768 const std::string formula = addMolecule(*moliter); 769 // LOG(1, "Adding " << formula << " to toBeSetOccurrence."); 770 toBeSetOccurrence.insert( formula ); 771 } 772 773 /// 5a. update the group item's occurrence and visibility 774 std::set<int> RowsToRemove; 775 for (std::set<std::string>::const_iterator groupiter = toBeSetOccurrence.begin(); 776 groupiter != toBeSetOccurrence.end(); ++groupiter) { 777 // LOG(1, "Updating group item's occurence " << *groupiter); 778 QStandardItem *groupitem = FormulaToGroupItem(*groupiter); 779 const int index = setOccurrence(groupitem); 780 if (index != -1) { 781 // LOG(1, "Removing row of group item " << groupitem); 782 RowsToRemove.insert(index); 783 } 784 } 785 toBeSetOccurrence.clear(); 786 787 // remove all visibility updates whose row is removed 788 for (list_of_group_items_t::iterator visiter = visibilityGroupItems_copy.begin(); 789 visiter != visibilityGroupItems_copy.end(); ) { 790 QStandardItem * const groupitem = FormulaToGroupItem(visiter->first); 791 if (RowsToRemove.count(groupitem->index().row()) != 0) { 792 // LOG(1, "Removing vis item " << *visiter << " because of removed group item."); 793 visibilityGroupItems_copy.erase(visiter++); 794 } else 795 ++visiter; 796 } 797 798 // update visibility of all group items 799 for (list_of_group_items_t::iterator visiter = visibilityGroupItems_copy.begin(); 800 visiter != visibilityGroupItems_copy.end(); ++visiter) { 801 // LOG(1, "Updating visibility of item " << *visiter); 802 QStandardItem * const groupitem = 803 getSpecificGroupItem(FormulaToGroupItem(visiter->first), 804 visiter->second); 805 setVisibilityForGroupItem(groupitem); 806 } 807 808 /// 5b. remove all rows with 0 occurrence starting from last 809 for (std::set<int>::reverse_iterator riter = RowsToRemove.rbegin(); 810 riter != RowsToRemove.rend(); ++riter) { 811 // LOG(1, "Removing group item at row " << *riter); 812 removeRows(*riter, 1, invisibleRootItem()->index()); 813 } 814 815 // and done 816 // LOG(1, "Done with update."); 817 } 606 // and add to formula<->molecule bimap with updated new_formula 607 LOG(4, "DEBUG: Adding " << new_formula << " for " << molid << " to MoleculeFormulaMap."); 608 MoleculeFormulaMap.insert( std::make_pair(molid, new_formula) ); 609 const int addindex = setOccurrence( FormulaToGroupItem(new_formula) ); 610 ASSERT( addindex == -1, 611 "QtMoleculeList::formulaChanged() - add mol to new formula "+ 612 toString(new_formula)+" made the row to be removed?"); 613 } 614 } 615 616 void QtMoleculeList::nameChanged(const QtObservedMolecule::ptr _mol) 617 { 618 // nothing to do, item updates itself 619 LOG(3, "DEBUG: QtMoleculeList got nameChangd for id " << _mol->getMolIndex()); 620 }
Note:
See TracChangeset
for help on using the changeset viewer.