Exemplo n.º 1
0
class MoleculeChooser:
    """presents user w/ a list of molecules currently loaded;
mode can be 'single', 'browse', 'multiple' or 'extended'.
Molecules can be selected from the list or by picking in the camera.

OK button returns a list of entries which have been selected.
Cancel returns an empty list. 
Typical usage is:
    ans = MoleculeChooser(self.vf).go()
    if ans !=[]:
        then get the value(s)
NB: this class doesn't grab the focus and binds picking w/
B1 to selecting the molecule in the MoleculeChooser. """
    def __init__(self, viewer, mode='single', title='Choose Molecule'):

        self.vf = viewer
        self.mode = mode
        self.ipf = InputFormDescr(title=title)

    def done_cb(self):
        self.ap.stop()
        self.ipf.form.destroy()
        self.ipf.form = None

    def go(self, modal=1, blocking=0, event="<ButtonRelease-1>"):
        """Start the form"""

        entries = []
        for i in range(len(self.vf.Mols)):
            mol = self.vf.Mols[i]
            molParser = mol.parser
            molStr = molParser.getMoleculeInformation()
            entries.append((mol.name, molStr))


##          self.ipf.insert(0,{'name': 'Molecule',
##                             'widgetType': 'ListChooser',
##                             'title' : 'select a molecule',
##                             'mode' : self.mode,
##                             'entries' : entries})

        self.ipf.insert(
            0, {
                'name': 'Molecule',
                'widgetType': ListChooser,
                'wcfg': {
                    'title': 'select a molecule',
                    'mode': self.mode,
                    'entries': entries
                },
                'gridcfg': {
                    'sticky': 'wens'
                }
            })

        if not (modal or blocking):
            self.ipf.append({
                'widgetType': Tkinter.Button,
                'wcfg': {
                    'text': 'Dismiss',
                    'command': self.done_cb
                },
                'gridcfg': {
                    'sticky': 'we'
                }
            })

        from Pmv.picker import AtomPicker
        self.ap = AtomPicker(self.vf,
                             None,
                             0,
                             callbacks=[self.onPick],
                             immediate=1)
        self.ap.go(modal=0)
        val = self.vf.getUserInput(self.ipf, modal=modal, blocking=blocking)
        if val:
            if modal or blocking:
                if len(val['Molecule']) == 1:
                    mols = self.vf.Mols.NodesFromName(val['Molecule'][0])
                    if self.mode == 'single': return mols[0]
                    return mols
                else:
                    molNames = ""
                    for m in val['Molecule']:
                        molNames = molNames + ',' + m
                    mols = self.vf.Mols.NodesFromName(molNames)
                    self.ap.stop()
                    if self.mode == 'single': return mols[0]
                    return mols
            else:
                self.form = val
                return val
        else:
            return val

    def getMolSet(self):
        """method to get currently selected molecules when the chooser is used
        in modal=0 and blocking=0 mode"""

        val = self.form.checkValues()
        if len(val['Molecule']) == 0:
            t = "Nothing selected!"
            self.vf.warningMsg(t, title="MoleculeChooser WARNING:")
            return None
        molNames = ""
        for m in val['Molecule']:
            # Create the list of names
            if molNames == "":
                molNames = m
            else:
                molNames = molNames + ";" + m
        mols = self.vf.Mols.NodesFromName(molNames)
        if self.mode == 'single': return mols[0]
        return mols

    def onPick(self, atoms):
        listChooser = self.ipf.entryByName['Molecule']['widget']
        tkListBox = listChooser.lb
        if atoms:
            pickedMol = atoms[0].top
            #then need to make pickedMol the selection in self.lc
            for i in range(len(listChooser.entries)):
                if pickedMol.name == listChooser.entries[i][0]:
                    self.pickedMolIndex = i
                    tkListBox.select_clear(0, 'end')
                    listChooser.select(i)
                    return
            print "error: %s not in mv.Mols" % pickedMol.name
Exemplo n.º 2
0
class MoleculeChooser:
    """presents user w/ a list of molecules currently loaded;
mode can be 'single', 'browse', 'multiple' or 'extended'.
Molecules can be selected from the list or by picking in the camera.

OK button returns a list of entries which have been selected.
Cancel returns an empty list. 
Typical usage is:
    ans = MoleculeChooser(self.vf).go()
    if ans !=[]:
        then get the value(s)
NB: this class doesn't grab the focus and binds picking w/
B1 to selecting the molecule in the MoleculeChooser. """ 


    def __init__(self, viewer, mode = 'single', title = 'Choose Molecule'):

        self.vf = viewer
        self.mode = mode
        self.ipf = InputFormDescr(title = title)


    def done_cb(self):
        self.ap.stop()
        self.ipf.form.destroy()
        self.ipf.form = None

    def go(self, modal=1, blocking=0, event = "<ButtonRelease-1>"):
        """Start the form"""

        entries = []
        for i in range(len(self.vf.Mols)):
            mol = self.vf.Mols[i]
            molParser = mol.parser
            molStr = molParser.getMoleculeInformation()
            entries.append((mol.name, molStr))

##          self.ipf.insert(0,{'name': 'Molecule',
##                             'widgetType': 'ListChooser',
##                             'title' : 'select a molecule',
##                             'mode' : self.mode,
##                             'entries' : entries})

        self.ipf.insert(0,{'name': 'Molecule',
                           'widgetType': ListChooser,
                           'wcfg':{
                               'title' : 'select a molecule',
                               'mode' : self.mode,
                               'entries' : entries},
                           'gridcfg':{'sticky':'wens'}})

        if not (modal or blocking):
            self.ipf.append({'widgetType':Tkinter.Button,
                             'wcfg':{'text':'Dismiss',
                                     'command': self.done_cb},
                             'gridcfg':{'sticky':'we'}})

        from Pmv.picker import AtomPicker
        self.ap = AtomPicker(self.vf, None, 0, callbacks=[self.onPick],
                             immediate=1)
        self.ap.go(modal=0)
        val = self.vf.getUserInput(self.ipf, modal=modal, blocking=blocking)
        if val:
            if modal or blocking:
                if len(val['Molecule'])==1:
                    mols = self.vf.Mols.NodesFromName( val['Molecule'][0] )
                    if self.mode=='single': return mols[0]
                    return mols
                else:
                    molNames = ""
                    for m in val['Molecule']: 
                        molNames = molNames+','+m
                    mols = self.vf.Mols.NodesFromName( molNames )
                    self.ap.stop()
                    if self.mode=='single': return mols[0]
                    return mols
            else:
                self.form = val
                return val
        else:
            return val

    def getMolSet(self):
        """method to get currently selected molecules when the chooser is used
        in modal=0 and blocking=0 mode"""

        val = self.form.checkValues()
        if len(val['Molecule'])==0:
            t = "Nothing selected!"
            self.vf.warningMsg(t, title="MoleculeChooser WARNING:")
            return None
        molNames = ""
        for m in val['Molecule']:
            # Create the list of names
            if molNames == "":
                molNames = m
            else:
                molNames = molNames + ";" + m
        mols = self.vf.Mols.NodesFromName( molNames )
        if self.mode=='single': return mols[0]
        return mols

    def onPick(self,atoms):
        listChooser = self.ipf.entryByName['Molecule']['widget']
        tkListBox = listChooser.lb
        if atoms:
            pickedMol = atoms[0].top
            #then need to make pickedMol the selection in self.lc
            for i in range(len(listChooser.entries)):
                if pickedMol.name == listChooser.entries[i][0]:
                    self.pickedMolIndex= i
                    tkListBox.select_clear(0,'end')
                    listChooser.select(i)
                    return
            print "error: %s not in mv.Mols" %pickedMol.name