Ignore:
Timestamp:
Apr 18, 2016, 10:33:41 PM (9 years ago)
Author:
Frederik Heber <heber@…>
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)
Message:

Removed all mutexes and updateDirtyStates() in QtMoleculeList, replaced by simpler slots.

  • thanks to events we don't need this humonguous system of lists, update timers, and mutexes. We simply need to take action whenever an event comes in.
Location:
src/UIElements/Views/Qt4/MoleculeList
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.cpp

    r24eb6b r5771e6  
    6666using namespace std;
    6767
    68 const unsigned int QtMoleculeList::update_times_per_second = 20;
    69 
    7068QtMoleculeList::QtMoleculeList(
    7169    QtObservedInstanceBoard *_board,
    7270    QObject *_parent) :
    7371  QStandardItemModel(_parent),
    74   update_timer(NULL),
    75   board(_board)
     72  board(_board),
     73  observer(_board)
    7674{
    7775  setColumnCount(QtMoleculeItemFactory::COLUMNCOUNT);
    7876
    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)));
    8589  connect(board, SIGNAL(moleculeIndexChanged(const moleculeId_t,const moleculeId_t)),
    8690      this, SLOT(moleculeIndexChanged(const moleculeId_t, const moleculeId_t)));
     
    103107void QtMoleculeList::moleculeInserted(QtObservedMolecule::ptr _mol)
    104108{
    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));
    108114}
    109115
    110116void QtMoleculeList::moleculeRemoved(const moleculeId_t _id)
    111117{
    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    }
    121133  }
    122134}
     
    183195    MoleculeItemBiMap.left.erase(iter);
    184196  }
    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);
    195197}
    196198
    197199bool QtMoleculeList::isMoleculeItemPresent(const moleculeId_t _molid) const
    198200{
    199   boost::recursive_mutex::scoped_lock lock(map_mutex);
    200201  MoleculeItemBiMap_t::left_const_iterator iter =
    201202      MoleculeItemBiMap.left.find(_molid);
     
    205206QtMoleculeItem * QtMoleculeList::MoleculeIdToItem(const moleculeId_t _molid) const
    206207{
    207   boost::recursive_mutex::scoped_lock lock(map_mutex);
    208208  MoleculeItemBiMap_t::left_const_iterator iter =
    209209      MoleculeItemBiMap.left.find(_molid);
     
    216216const moleculeId_t QtMoleculeList::ItemToMoleculeId(const QtMoleculeItem * const _item) const
    217217{
    218   boost::recursive_mutex::scoped_lock lock(map_mutex);
    219218  const MoleculeItemBiMap_t::right_const_iterator iter =
    220219      MoleculeItemBiMap.right.find(const_cast<QtMoleculeItem * const>(_item));
     
    237236bool QtMoleculeList::isGroupItemPresent(const std::string &_formula) const
    238237{
    239   boost::recursive_mutex::scoped_lock lock(map_mutex);
    240238  FormulaTreeItemBiMap_t::left_const_iterator iter =
    241239      FormulaItemBiMap.left.find(_formula);
     
    245243QStandardItem * QtMoleculeList::FormulaToGroupItem(const std::string &_formula) const
    246244{
    247   boost::recursive_mutex::scoped_lock lock(map_mutex);
    248245  FormulaTreeItemBiMap_t::left_const_iterator iter =
    249246      FormulaItemBiMap.left.find(_formula);
     
    256253const std::string& QtMoleculeList::GroupItemToFormula(const QStandardItem * const _item) const
    257254{
    258   boost::recursive_mutex::scoped_lock lock(map_mutex);
    259255  static std::string emptystring;
    260256  const FormulaTreeItemBiMap_t::right_const_iterator iter =
     
    275271const QModelIndex QtMoleculeList::MoleculeIdToIndex(const moleculeId_t _id) const
    276272{
    277   boost::recursive_mutex::scoped_lock lock(refill_mutex);
    278273  QtMoleculeItem * const item = MoleculeIdToItem(_id);
    279274  ASSERT(item != NULL,
     
    285280const moleculeId_t QtMoleculeList::IndexToMoleculeId(const QModelIndex &_index) const
    286281{
    287   boost::recursive_mutex::scoped_lock lock(refill_mutex);
    288282  QtMoleculeItem * const item = dynamic_cast<QtMoleculeItem *>(itemFromIndex(_index));
    289283  if (item == NULL)
     
    300294      QtMoleculeItemFactory::getInstance().createGroupItems(_molecule_formula);
    301295  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) );
    306297  invisibleRootItem()->appendRow(groupItems);
    307298}
     
    366357void QtMoleculeList::removeMoleculeItem(QtMoleculeItem * const _item)
    367358{
    368   boost::recursive_mutex::scoped_lock lock(refill_mutex);
    369359  const QModelIndex mol_index = indexFromItem(_item);
    370360  QStandardItem *groupitem = _item->parent();
    371361  const QModelIndex group_index = groupitem->index();
    372362  {
    373     boost::recursive_mutex::scoped_lock lock(map_mutex);
    374363    MoleculeItemBiMap_t::right_iterator removeiter =
    375364        MoleculeItemBiMap.right.find(_item);
     
    392381}
    393382
    394 void QtMoleculeList::resetUpdateTimer()
    395 {
    396   // check timer's presence
    397   if (update_timer == NULL) {
    398     update_timer = new QTimer(this);
    399     connect( update_timer, SIGNAL(timeout()), this, SLOT(checkState()));
    400   } else
    401     update_timer->stop();
    402 
    403   // activate timer
    404   update_timer->start(1000/update_times_per_second);
    405 }
    406 
    407 bool QtMoleculeList::areAnyItemsDirty()
    408 {
    409   // get whether any items are dirty
    410   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 required
    426   if (dirty)
    427     updateItemStates();
    428 }
    429 
    430383void QtMoleculeList::checkForVisibilityChange(QStandardItem* _item)
    431384{
    432385//  qDebug() << "Item changed called.";
    433386
    434   boost::recursive_mutex::scoped_lock lock(refill_mutex);
    435387  if (_item->index().column() == QtMoleculeItem::VISIBILITY) {
    436388//    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    }
    445396  }
    446397}
     
    448399void QtMoleculeList::setVisibilityForMoleculeItem(QtMoleculeItem* _item)
    449400{
    450   if (ChangingChildrensVisibility)
    451     return;
    452 
    453   boost::recursive_mutex::scoped_lock lock(refill_mutex);
    454401  const bool visible = _item->checkState();
    455402  const moleculeId_t molid = _item->getMoleculeId();
    456403  std::string molecule_formula("illegal");
    457404  {
    458     boost::recursive_mutex::scoped_lock lock(map_mutex);
    459     MoleculeFormulaMap_t::const_iterator formulaiter =
     405    const MoleculeFormulaMap_t::const_iterator formulaiter =
    460406        MoleculeFormulaMap.find(molid);
    461407    ASSERT( formulaiter != MoleculeFormulaMap.end(),
     
    476422  // check whether we have to set the group item
    477423
    478   ChangingChildrensVisibility = true;
    479424  if (visible) {
    480425    ++(FormulaVisibilityCountMap[molecule_formula]);
     
    489434      visgroupItem->setCheckState(Qt::Unchecked);
    490435  }
    491   ChangingChildrensVisibility = false;
    492436
    493437  emit moleculesVisibilityChanged(_item->getMoleculeId(), visible);
     
    496440void QtMoleculeList::setVisibilityForGroupItem(QStandardItem* _item)
    497441{
    498   if (ChangingChildrensVisibility)
    499     return;
    500 
    501   ChangingChildrensVisibility = true;
    502 
    503   boost::recursive_mutex::scoped_lock lock(refill_mutex);
    504442  // go through all children, but don't enter for groupItem once more
    505443  const bool visible = _item->checkState();
    506444  QStandardItem *groupitem = getSpecificGroupItem(_item, QtMoleculeItem::NAME);
    507445  for (int i=0;i<groupitem->rowCount();++i) {
    508     QtMoleculeItem *molItem = dynamic_cast<QtMoleculeItem *>(
     446    QtMoleculeItem * const molItem = dynamic_cast<QtMoleculeItem *>(
    509447        groupitem->child(i, QtMoleculeItem::VISIBILITY));
    510448    if (molItem->checkState() != visible) {
     
    524462      +" have no entry in visibility count map?");
    525463  countiter->second = visible ? groupitem->rowCount() : 0;
    526 
    527   ChangingChildrensVisibility = false;
    528464}
    529465
     
    559495void QtMoleculeList::moleculeNameChanged(QStandardItem* item)
    560496{
    561   boost::recursive_mutex::scoped_lock lock(refill_mutex);
    562497  // obtain molecule id
    563498  if ( item->index().column() == QtMoleculeItem::NAME) {
     
    568503    const moleculeId_t molid = iter->second;
    569504    // change the name
    570     molecule * const mol = World::getInstance().getMolecule(MoleculeById(molid));
    571505    std::string cellValue = item->text().toStdString();
    572     if ((mol->getName() != cellValue) && (!cellValue.empty())) {
     506    if (!cellValue.empty()) {
    573507      // create actions such that we may undo
    574508      static MoleCuilder::ActionSequence sequence;
     
    578512      AQ.registerAction(makroaction);
    579513      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();
    582516    }
    583 }
    584 }
    585 
     517  }
     518}
    586519
    587520int QtMoleculeList::setOccurrence(QStandardItem * const _groupitem)
    588521{
    589   boost::recursive_mutex::scoped_lock lock(refill_mutex);
     522  ASSERT( _groupitem != NULL,
     523      "QtMoleculeList::setOccurrence() - group item at "+toString(_groupitem)
     524      +" is NULL");
    590525  QModelIndex modelindex = _groupitem->index();
    591526  ASSERT( modelindex.isValid(),
     
    604539  if (count == 0) {
    605540    // we have to remove the group item completely
    606     boost::recursive_mutex::scoped_lock lock(map_mutex);
    607541    const std::string molecule_formula = _groupitem->text().toStdString();
    608542    FormulaItemBiMap.left.erase(molecule_formula);
     
    615549}
    616550
    617 std::string QtMoleculeList::readdItem(QtMoleculeItem *_molitem)
    618 {
    619   boost::recursive_mutex::scoped_lock lock(refill_mutex);
     551void QtMoleculeList::moveItem(
     552    QtMoleculeItem *_molitem,
     553    const std::string &_new_formula)
     554{
    620555  // use takeRows of molecule ..
    621556  QStandardItem *groupitem = _molitem->parent();
     
    626561  QStandardItem *formulaitem =
    627562      _molitem->parent()->child(_molitem->index().row(), QtMoleculeItem::FORMULA);
    628   const std::string molecule_formula = formulaitem->text().toStdString();
    629563  QList<QStandardItem *> mol_row = _molitem->parent()->takeRow(_molitem->index().row());
    630564  // ..  and re-add where new formula fits
    631   if (!isGroupItemPresent(molecule_formula)) {
     565  if (!isGroupItemPresent(_new_formula)) {
    632566    // add new group item and formula entry
    633      addGroupItem(groupitem, molecule_formula);
     567     addGroupItem(groupitem, _new_formula);
    634568  } else {
    635     groupitem = FormulaToGroupItem(molecule_formula);
     569    groupitem = FormulaToGroupItem(_new_formula);
    636570  }
    637571  ASSERT( groupitem != NULL,
     
    639573  // finally add again
    640574  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
     578void 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);
    724596    // 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.");
    726598    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());
    748605    }
    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
     616void 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}
  • TabularUnified src/UIElements/Views/Qt4/MoleculeList/QtMoleculeList.hpp

    r24eb6b r5771e6  
    2626
    2727#include "UIElements/Views/Qt4/MoleculeList/QtMoleculeItem.hpp"
     28#include "UIElements/Views/Qt4/MoleculeList/QtObservedMoleculeObserver.hpp"
    2829#include "UIElements/Qt4/InstanceBoard/QtObservedMolecule.hpp"
    2930
     
    4748
    4849private slots:
     50  void formulaChanged(const QtObservedMolecule::ptr _mol);
     51  void nameChanged(const QtObservedMolecule::ptr _mol);
     52
    4953  void moleculeNameChanged(QStandardItem*);
    5054  void checkForVisibilityChange(QStandardItem*);
    51   void checkState();
    52 
    53   friend class QtMoleculeItem;
    54 
    55   std::string readdItem(QtMoleculeItem *_molitem);
    5655
    5756signals:
    58   void moleculeSelected(molecule*);
    59   void moleculeUnSelected(molecule*);
     57  void moleculeSelected(const QtObservedMolecule::ptr _mol);
     58  void moleculeUnSelected(const QtObservedMolecule::ptr _mol);
    6059  void moleculesVisibilityChanged(const moleculeId_t, bool);
    6160
    6261private:
    63 
    64   bool ChangingChildrensVisibility;
    6562
    6663private slots:
     
    7067
    7168private:
    72   mutable boost::recursive_mutex listAccessing_mutex;
    73   mutable boost::recursive_mutex refill_mutex;
    74   mutable boost::recursive_mutex map_mutex;
    75 
    76   //!> informs the view in regular intervals about updates
    77   QTimer *update_timer;
    78 
    79   //!> how often per second update is signalled to view
    80   static const unsigned int update_times_per_second;
    81 
    8269  friend class QtMoleculeListView;
    8370
     
    10794  void removeMoleculeItem(QtMoleculeItem * const _item);
    10895  int setOccurrence(QStandardItem * const _groupitem);
    109   bool areAnyItemsDirty();
    11096  void setVisibilityForMoleculeItem(QtMoleculeItem* _item);
    11197  void setVisibilityForGroupItem(QStandardItem* _item);
     98  void moveItem(QtMoleculeItem *_molitem, const std::string &_new_formula);
    11299
    113   void updateItemStates();
     100//  void updateItemStates();
    114101
    115102  //!> reference to InstanceBoard
     
    130117  MoleculeItemBiMap_t MoleculeItemBiMap;
    131118
    132   typedef std::map<moleculeId_t, unsigned int> KilledItemsPerMolecule_t;
    133   //!> takes note of how many items have already been killed for a specific row/molecule
    134   KilledItemsPerMolecule_t KilledItemsPerMolecule;
    135 
    136119  typedef std::set< moleculeId_t > list_of_molecules_t;
    137120  typedef std::set< std::pair<moleculeId_t, QtMoleculeItem::COLUMNTYPES> > list_of_molecule_items_t;
    138121  typedef std::set< std::pair<std::string, QtMoleculeItem::COLUMNTYPES> > list_of_group_items_t;
    139122
    140   //!> list of molecule items that need an update
    141   list_of_molecule_items_t dirtyMolItems;
    142   //!> list of molecule items that need an update of visibility
    143   list_of_molecules_t visibilityMolItems;
    144   //!> list of group items that need an update
    145   list_of_group_items_t dirtyGroupItems;
    146   //!> list of group items that need an update of visibility
    147   list_of_group_items_t visibilityGroupItems;
    148   //!> list of new molecules which need to be added
    149   std::vector<QtObservedMolecule::ptr> newMolecules;
    150   //!> list of molecules that have been removed
    151   std::vector<moleculeId_t> removedMolecules;
    152   //!> list of molecule items to molecule's whose formulas has changed and need to be moved
    153   list_of_molecules_t toBeMovedItems;
     123  //!> listens to all QtObservedMolecule and relays important events to us
     124  QtObservedMoleculeObserver observer;
    154125};
    155126
Note: See TracChangeset for help on using the changeset viewer.