Index: src/UIElements/Views/Qt4/QtElementList.cpp
===================================================================
--- src/UIElements/Views/Qt4/QtElementList.cpp	(revision 9fb2137ff4183dfa58b9083f255c8037873fb5ba)
+++ src/UIElements/Views/Qt4/QtElementList.cpp	(revision 95f49f80844c3103ded5d5940874d5342ce246a6)
@@ -66,4 +66,31 @@
 	setHeaderLabels(header);
 
+	{
+    periodentafel *&periode = World::getInstance().getPeriode();
+
+    elementSelection.clear();
+
+    int i;
+
+    clear();
+    periodentafel::const_iterator iter;
+    for(iter = periode->begin(),i=0;
+        iter != periode->end();
+        ++i,++iter) {
+      const element *e = iter->second;
+      int count = 0;
+      count = const_cast<const World &>(World::getInstance()).
+          getAllAtoms(AtomByType(e)).size();
+
+      QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
+      treeItem->setText(NUMBER, QString::number(e->getAtomicNumber()));
+      treeItem->setText(NAME, QString(e->getName().c_str()));
+      treeItem->setText(SYMBOL, QString(e->getSymbol().c_str()));
+      treeItem->setText(MASS, QString::number(e->getMass()));
+      setOccurrence(*treeItem, count);
+      elementSelection.push_back(treeItem->isSelected());
+//      insertTopLevelItem(e->getAtomicNumber()-1, treeItem);
+    }
+	}
 	dirty = true;
 
@@ -74,5 +101,4 @@
 //  connect(this,SIGNAL(changed()),this,SLOT(update()));
 
-//  emit needsRefill();
 }
 
@@ -91,5 +117,4 @@
 {
   if (notification->getChannelNo() == atom::ElementChanged) {
-//    updateElement(static_cast<atom &>(*publisher));
     dirty = true;
 
@@ -101,24 +126,38 @@
 void QtElementList::updateElement(const atom &_atom)
 {
-  // this function has not been tested and is not used because it is
-  // incompatible with the current refill scheme
+  const atomicNumber_t newelement = _atom.getElementNo();
+  atomicNumber_t oldelement = -1;
+  QTreeWidgetItem *newtreeItem = topLevelItem(newelement-1);
+  ASSERT( newtreeItem != NULL,
+      "QtElementList::updateElement() - new element item not present.");
+//  if (newtreeItem == NULL) {
+//    // add new item
+//    const element& e = _atom.getElement();
+//    newtreeItem = new QTreeWidgetItem();
+//    newtreeItem->setText(NUMBER, QString::number(e.getAtomicNumber()));
+//    newtreeItem->setText(NAME, QString(e.getName().c_str()));
+//    newtreeItem->setText(SYMBOL, QString(e.getSymbol().c_str()));
+//    newtreeItem->setText(MASS, QString::number(e.getMass()));
+//    setOccurrence(*newtreeItem, 0);
+//    elementSelection.push_back(newtreeItem->isSelected());
+//    insertTopLevelItem(newelement-1, newtreeItem);
+//  }
   AtomElementMap_t::iterator iter = AtomElementMap.find(_atom.getId());
   if (iter == AtomElementMap.end()) {
     AtomElementMap.insert( std::make_pair(_atom.getId(), -1));
     iter = AtomElementMap.find(_atom.getId());
-  }
-  const atomicNumber_t oldelement = iter->second;
-  const atomicNumber_t newelement = _atom.getElementNo();
+  } else
+    oldelement = iter->second;
   iter->second = newelement;
-  QTreeWidgetItem *oldtreeItem = NULL;
-  if (oldelement != (atomicNumber_t)-1)
-    oldtreeItem = topLevelItem(oldelement-1);
-  QTreeWidgetItem *newtreeItem = topLevelItem(newelement-1);
-  ASSERT( (newtreeItem != NULL),
-      "QtElementList::updateElement() - missing new element item.");
+
+  // reduce old occurence
   if (oldelement != (atomicNumber_t)-1) {
+    QTreeWidgetItem *oldtreeItem = topLevelItem(oldelement-1);
+    ASSERT( oldtreeItem != NULL,
+        "QtElementList::updateElement() - old element item not present.");
     const int count_old = oldtreeItem->text(OCCURRENCE).toInt();
     setOccurrence(*oldtreeItem, count_old-1);
   }
+  // increase new occurence
   const int count_new = newtreeItem->text(OCCURRENCE).toInt();
   setOccurrence(*newtreeItem, count_new+1);
@@ -130,15 +169,12 @@
       "QtElementList::setOccurrence() - count for an elment < 0.");
 
-//    // something must be very wrong, rather refill
-//    dirty = true;
-//    refill();
-//  } else {
-    if (count > 0) {
-      _item.setText(OCCURRENCE, QString::number(count));
-    } else {
-      _item.setText(OCCURRENCE, "none");
-      _item.setDisabled(true);
-    }
-//  }
+  if (count > 0) {
+    _item.setText(OCCURRENCE, QString::number(count));
+    if (_item.isDisabled())
+      _item.setDisabled(false);
+  } else {
+    _item.setText(OCCURRENCE, "none");
+    _item.setDisabled(true);
+  }
 }
 
@@ -147,29 +183,4 @@
   refill_mutex.lock();
 
-//  periodentafel *&periode = World::getInstance().getPeriode();
-//
-//  elementSelection.clear();
-//
-//  int i;
-//
-//  clear();
-//  periodentafel::const_iterator iter;
-//  for(iter = periode->begin(),i=0;
-//      iter != periode->end();
-//      ++i,++iter) {
-//    const element *e = iter->second;
-//    int count = 0;
-//    count = const_cast<const World &>(World::getInstance()).
-//        getAllAtoms(AtomByType(e)).size();
-//
-//    QTreeWidgetItem *treeItem = new QTreeWidgetItem(this);
-//    treeItem->setText(NUMBER, QString::number(e->getAtomicNumber()));
-//    treeItem->setText(NAME, QString(e->getName().c_str()));
-//    treeItem->setText(SYMBOL, QString(e->getSymbol().c_str()));
-//    treeItem->setText(MASS, QString::number(e->getMass()));
-//    setOccurrence(*treeItem, count);
-//    elementSelection.push_back(treeItem->isSelected());
-//  }
-//  dirty = false
   const atom * const walker = const_cast<const World &>(World::getInstance()).
       getAtom(AtomById(_atomid));
@@ -178,9 +189,11 @@
   else {
     AtomElementMap_t::iterator iter = AtomElementMap.find(_atomid);
-    QTreeWidgetItem *oldtreeItem = topLevelItem(iter->second-1);
-    const int count_old = oldtreeItem->text(OCCURRENCE).toInt();
-    setOccurrence(*oldtreeItem, count_old-1);
-
-    AtomElementMap.erase(iter);
+    if (iter != AtomElementMap.end()) {
+      QTreeWidgetItem *oldtreeItem = topLevelItem(iter->second-1);
+      const int count_old = oldtreeItem->text(OCCURRENCE).toInt();
+      setOccurrence(*oldtreeItem, count_old-1);
+
+      AtomElementMap.erase(iter);
+    }
   }
 
