Exemplo n.º 1
0
int GeometryList::add_geometry(const char *geomcatname, const int *molids,
    const int *atomids, const int *cells, float k, int toggle) {

  // check that geometry category name is valid
  int geomcat = geom_list_index(geomcatname);
  GeometryMol *g = NULL;
  MoleculeList *mlist = app->moleculeList;
  if (geomcat == geom_list_index("Atoms")) 
    g = new GeometryAtom(*molids, *atomids, cells, mlist, app->commandQueue, this);
  else if (geomcat == geom_list_index("Bonds"))
    g = new GeometryBond((int *)molids, (int *)atomids, cells, mlist, app->commandQueue, this);
  else if (geomcat == geom_list_index("Angles"))
    g = new GeometryAngle((int *)molids, (int *)atomids, cells, mlist, app->commandQueue, this);
  else if (geomcat == geom_list_index("Dihedrals"))
    g = new GeometryDihedral((int *)molids, (int *)atomids, cells, mlist, app->commandQueue, this);
  else if (geomcat == geom_list_index("Springs"))
    g = new GeometrySpring((int *)molids, (int *)atomids, mlist, app->commandQueue, k, this);
  else {
    msgErr << "Unknown geometry category '" << geomcatname << "'." << sendmsg;
    return -1;
  }
  if(g && g->ok()) {
    GeomListPtr glist = geom_list(geomcat);

    // if there is already an identical label in the list,
    // do not add this one, instead toggle the displayed
    // status of the old one and return the index.
    for(int i=(glist->num() - 1); i >= 0; i--) {
      GeometryMol *g2 = (*glist)[i];
      if(!strcmp(g2->unique_name(), g->unique_name())) {
        // name matches
        if (toggle) {
          if (g2->displayed())
            g2->off();
          else
            g2->on();
        }
      
        delete g;
        return i;
      }
    }

    // spam the console
    msgInfo << "Added new " << geomcatname << " label " << g->name();
    if (g->has_value())
      msgInfo << " = " << g->calculate();
    msgInfo << sendmsg;

    // add the geometry object
    glist->append(g);
    
    // calculate the value for the first time
    g->calculate();

    // set the color, which also causes the display list to be generated
    g->set_color(geomLists.data(geomcat)->curColor);
    g->set_text_size(labelsize);
    g->set_text_thickness(labelthickness);

    // set the pick variables
    g->set_pick();

    // indicate we were successful; return index of this object
    return glist->num() - 1;
  }
  
  // if here, something did not work
  return -1;
}