00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #include <math.h>
00028 #include "PickModeAddBond.h"
00029 #include "Pickable.h"
00030 #include "DisplayDevice.h"
00031 #include "Inform.h"
00032 #include "DrawMolecule.h"
00033 #include "CommandQueue.h"
00034 #include "TextEvent.h"
00035 #include "VMDApp.h"
00036 #include "utilities.h"
00037 
00038 PickModeAddBond::PickModeAddBond(VMDApp *vmdapp) 
00039 : app(vmdapp) {
00040 
00041   needName = FALSE;
00042 
00043   
00044   haveItems = 0;
00045 }
00046 
00047 void PickModeAddBond::pick_molecule_start(DrawMolecule *mol, DisplayDevice *d,
00048                         int, int tag, const int *  , int dim, const float *pos) {
00049   
00050   atom = tag;
00051   memcpy(pPos, pos, dim*sizeof(float));
00052   needName = TRUE;
00053   
00054   int shift_pressed = d->shift_state() & DisplayDevice::SHIFT;
00055   app->commandQueue->runcommand(new PickAtomEvent(mol->id(), tag, 
00056     shift_pressed));
00057 }
00058 
00059 void PickModeAddBond::pick_molecule_end(DrawMolecule *m, DisplayDevice *) {
00060 
00061   if(needName) {
00062     
00063 
00064     int id = m->id();
00065     molids[haveItems] = id; 
00066     atmids[haveItems] = atom;
00067 
00068     
00069     app->label_add("Atoms", 1, &id, &atom, NULL, 0, 1);
00070 
00071     
00072     haveItems++;
00073 
00074     
00075     if(haveItems >= 2) {
00076       if (molids[0] == molids[1]) {
00077         int id1 = atmids[0], id2 = atmids[1];
00078         MolAtom *atom1 = m->atom(id1), *atom2 = m->atom(id2);
00079         if (atom1->bonded(id2)) {
00080           int i;
00081 
00082           
00083           for (i=0; i<atom1->bonds; i++) {
00084             if (atom1->bondTo[i] == id2) {
00085               for (int j=i; j<MAXATOMBONDS-1; j++)
00086                 atom1->bondTo[j] = atom1->bondTo[j+1];
00087               atom1->bonds--;
00088               break;
00089             }
00090           }
00091 
00092           for (i=0; i<atom2->bonds; i++) {
00093             if (atom2->bondTo[i] == id1) {
00094               for (int j=i; j<MAXATOMBONDS-1; j++)
00095                 atom2->bondTo[j] = atom2->bondTo[j+1];
00096               atom2->bonds--;
00097               break;
00098             }
00099           }
00100         } else {
00101           
00102           if (atom1->bonds >= MAXATOMBONDS ||
00103               atom2->bonds >= MAXATOMBONDS) {
00104             msgErr << "Unable to add bond: one or both atoms already has the maximum number." << sendmsg;
00105           } else {
00106             m->add_bond(id1, id2, 1, ATOMNORMAL);
00107           } 
00108         } 
00109         m->force_recalc(DrawMolItem::MOL_REGEN); 
00110       } else {
00111         msgErr << "Cannot add bond between two molecules." << sendmsg;
00112       } 
00113       
00114       haveItems = 0;
00115 
00116       
00117       
00118       
00119       m->set_dataset_flag(BaseMolecule::BONDS);
00120     } 
00121   }
00122   needName = FALSE;
00123 }
00124