summaryrefslogtreecommitdiff
path: root/cad/src/cnt/commands/EditNanotube/EditNanotube_PropertyManager.py
blob: e04b8de8c3d73bc81b6500025a018852b3ff2925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Copyright 2008 Nanorex, Inc.  See LICENSE file for details.
"""
EditNanotube_PropertyManager.py

@author: Ninad, Mark
@version: $Id$
@copyright: 2007-2008 Nanorex, Inc.  See LICENSE file for details.

TODO: as of 2008-01-18
See EditNanotube_EditCommand for details.
"""
from PyQt4.Qt import SIGNAL
from PM.PM_GroupBox      import PM_GroupBox

from command_support.DnaOrCnt_PropertyManager   import DnaOrCnt_PropertyManager

from PM.PM_Constants     import PM_DONE_BUTTON
from PM.PM_Constants     import PM_WHATS_THIS_BUTTON
from PM.PM_Constants     import PM_CANCEL_BUTTON

from PM.PM_SpinBox import PM_SpinBox
from PM.PM_DoubleSpinBox import PM_DoubleSpinBox
from PM.PM_LineEdit import PM_LineEdit

from geometry.VQT import V, vlen

from utilities.debug import print_compact_stack
from utilities.prefs_constants import editNanotubeEditCommand_cursorTextCheckBox_length_prefs_key
from utilities.prefs_constants import editNanotubeEditCommand_showCursorTextCheckBox_prefs_key
from widgets.prefs_widgets    import connect_checkbox_with_boolean_pref


_superclass = DnaOrCnt_PropertyManager

class EditNanotube_PropertyManager( DnaOrCnt_PropertyManager ):
    """
    The NanotubeSegmenta_PropertyManager class provides a Property Manager
    for the EditNanotube_EditCommand.

    @ivar title: The title that appears in the property manager header.
    @type title: str

    @ivar pmName: The name of this property manager. This is used to set
                  the name of the PM_Dialog object via setObjectName().
    @type name: str

    @ivar iconPath: The relative path to the PNG file that contains a
                    22 x 22 icon image that appears in the PM header.
    @type iconPath: str
    """

    title         =  "Nanotube Properties"
    pmName        =  title
    iconPath      =  "ui/actions/Command Toolbar/BuildNanotube/EditNanotube.png"

    def __init__( self,  command ):
        """
        Constructor for the Cnt Segment Properties property manager.
        """

        #For model changed signal
        self.previousSelectionParams = None

        #see self.connect_or_disconnect_signals for comment about this flag
        self.isAlreadyConnected = False
        self.isAlreadyDisconnected = False

        # Initialized here. Their values will be set in
        # _update_widgets_in_PM_before_show()
        self.endPoint1 = V(0, 0, 0)
        self.endPoint2 = V(0, 0, 0)

        _superclass.__init__( self, command)


        self.showTopRowButtons( PM_DONE_BUTTON | \
                                PM_CANCEL_BUTTON | \
                                PM_WHATS_THIS_BUTTON)

    def connect_or_disconnect_signals(self, isConnect):
        """
        Connect or disconnect widget signals sent to their slot methods.
        This can be overridden in subclasses. By default it does nothing.
        @param isConnect: If True the widget will send the signals to the slot
                          method.
        @type  isConnect: boolean
        """
        if isConnect:
            change_connect = self.win.connect
        else:
            change_connect = self.win.disconnect

        change_connect(self.nameLineEdit,
                       SIGNAL("editingFinished()"),
                       self._nameChanged)

        change_connect(self.showCursorTextCheckBox,
                       SIGNAL('stateChanged(int)'),
                       self._update_state_of_cursorTextGroupBox)
        return

    def show(self):
        """
        Show this property manager. Overrides EditCommand_PM.show()
        This method also retrives the name information from the
        command's structure for its name line edit field.
        @see: EditNanotube_EditCommand.getStructureName()
        @see: self.close()
        """
        _superclass.show(self)
        #if self.command is not None:
            #name = self.command.getStructureName()
            #if name is not None:
                #self.nameLineEdit.setText(name)

    def close(self):
        """
        Close this property manager.
        Also sets the name of the self.command's structure to the one
        displayed in the line edit field.
        @see self.show()
        @see: EditNanotube_EditCommand.setStructureName
        """
        if self.command is not None:
            name = str(self.nameLineEdit.text())
            self.command.setStructureName(name)
        _superclass.close(self)

    def _connect_showCursorTextCheckBox(self):
        """
        Connect the show cursor text checkbox with user prefs_key.
        Overrides
        DnaOrCnt_PropertyManager._connect_showCursorTextCheckBox
        """
        connect_checkbox_with_boolean_pref(
            self.showCursorTextCheckBox ,
            editNanotubeEditCommand_showCursorTextCheckBox_prefs_key)


    def _params_for_creating_cursorTextCheckBoxes(self):
        """
        Returns params needed to create various cursor text checkboxes connected
        to prefs_keys  that allow custom cursor texts.
        @return: A list containing tuples in the following format:
                ('checkBoxTextString' , preference_key). PM_PrefsCheckBoxes
                uses this data to create checkboxes with the the given names and
                connects them to the provided preference keys. (Note that
                PM_PrefsCheckBoxes puts thes within a GroupBox)
        @rtype: list
        @see: PM_PrefsCheckBoxes
        @see: self._loadDisplayOptionsGroupBox where this list is used.
        @see: Superclass method which is overridden here --
        DnaOrCnt_PropertyManager._params_for_creating_cursorTextCheckBoxes()
        """
        params = \
               [  #Format: (" checkbox text", prefs_key)

                  ("Nanotube length",
                   editNanotubeEditCommand_cursorTextCheckBox_length_prefs_key)
                 ]

        return params

    def setParameters(self, params):
        """
        This is called when entering "Nanotube Segment Properties
        (i.e. "Edit properties...") to retrieve and set parameters of the
        nanotube segment that might be modified during this command and
        are needed to regenerate the nanotube segment.

        @param params: The parameters of the nanotube segment.
                       These parameters are retreived via
                       L{NanotubeSegment.getProps()}, called from
                       L{EditNanotube_EditCommand.editStructure()}.

                       Parameters:
                       - n, m (chirality)
                       - type (i.e. carbon or boron nitride)
                       - endings (none, hydrogen, nitrogen)
                       - endpoints (endPoint1, endPoint2)
        @type params: list (n, m), type, endings, (endPoint1, endPoint2)

        @note: Any widgets in the property manager that display these
        parameters should be updated here.

        @see: L{NanotubeSegment.getProps()}

        TODO:
        - Make this a EditCommand_PM API method?
        - See also the routines GraphicsMode.setParams or object.setProps
        ..better to name them all in one style?
        """
        (self.n, self.m), self.type, self.endings,\
            (self.endPoint1, self.endPoint2) = params

        # This is needed to update the endpoints since the Nanotube segment
        # may have been moved (i.e. translated or rotated). In that case,
        # the endpoints are not updated, so we recompute them here.
        nanotubeChunk = self.command.struct.members[0]
        self.endPoint1, self.endPoint2, radius = \
            self.command.struct.nanotube.computeEndPointsFromChunk(nanotubeChunk)

        if 0:
            print "\n--------------"
            print "setParameters():"
            print "Struct=", self.command.struct
            print "N, M:", self.n, self.m
            print "type:", self.type
            print "endings:", self.endings
            print "pt1, pt2:", self.endPoint1, self.endPoint2

    def getParameters(self):
        """
        Get the parameters that the edit command will use to determine if
        any have changed. If any have, then the nanotube will be modified.
        """
        if 0:
            print "\n--------------"
            print "getParameters():"
            print "Struct=", self.command.struct
            print "N, M:", self.n, self.m
            print "type:", self.type
            print "endings:", self.endings
            print "pt1, pt2:", self.endPoint1, self.endPoint2

        return (self.n, self.m,
                self.type,
                self.endings,
                self.endPoint1, self.endPoint2)

    def _update_widgets_in_PM_before_show(self):
        """
        This is called only when user is editing an existing structure.
        Its different than self.update_widgets_in_pm_before_show. (that method
        is called just before showing the property manager)
        @see: EditNanotube_EditCommand.editStructure()

        """
        if self.command and self.command.hasValidStructure():

            self.nanotube = self.command.struct.nanotube

            self.n, self.m = self.nanotube.getChirality()
            self.type = self.nanotube.getType()
            self.endings = self.nanotube.getEndings()
            self.endPoint1, self.endPoint2 = self.nanotube.getEndPoints()
            pass

        # Note that _update_widgets_in_PM_before_show() is called in
        # self.show, before you connect the signals. So, for the
        # 'first show' we will need to manually set the value of any
        # widgets that need updated. But later, when a different
        # NanotubeSegment is clicked, (while still in
        # EditNanotube_EditCommand, the propMgr will already be connected
        # so any calls in that case is redundant.
        self.updateNameField()
        self.updateLength()
        self.updateNanotubeDiameter()
        self.updateChirality()

        return

    def _update_UI_do_updates(self):
        """
        Overrides superclass method.

        @see: Command_PropertyManager._update_UI_do_updates()
        """
        self._update_widgets_in_PM_before_show()

        if self.command.struct:
            msg = "Editing structure <b>%s</b>." % \
                self.command.getStructureName()
        else:
            msg = "Select a nanotube to edit."
        self.updateMessage(msg)
        return

    def _addGroupBoxes( self ):
        """
        Add the Property Manager group boxes.
        """

        self._pmGroupBox1 = PM_GroupBox( self, title = "Parameters" )
        self._loadGroupBox1( self._pmGroupBox1 )

        self._displayOptionsGroupBox = PM_GroupBox( self,
                                                    title = "Display Options" )
        self._loadDisplayOptionsGroupBox( self._displayOptionsGroupBox )

    def _loadGroupBox1(self, pmGroupBox):
        """
        Load widgets in group box 4.
        """

        self.nameLineEdit = PM_LineEdit( pmGroupBox,
                         label         =  "Name:",
                         text          =  "",
                         setAsDefault  =  False)

        # Nanotube Length
        self.ntLengthLineEdit  =  \
            PM_LineEdit( pmGroupBox,
                         label         =  "Length: ",
                         text          =  "0.0 Angstroms",
                         setAsDefault  =  False)

        self.ntLengthLineEdit.setDisabled(True)

        # Nanotube Radius
        self.ntDiameterLineEdit  =  \
            PM_LineEdit( pmGroupBox,
                         label         =  "Nanotube Diameter: ",
                         setAsDefault  =  False)

        self.ntDiameterLineEdit.setDisabled(True)

        # Nanotube chirality. These are disabled (read-only) for now. --Mark
        self.chiralityNSpinBox = \
            PM_SpinBox( pmGroupBox,
                        label        = "Chirality (n) :",
                        minimum      =  2,
                        maximum      =  100,
                        setAsDefault = True )
        self.chiralityNSpinBox.setDisabled(True)

        self.chiralityMSpinBox = \
            PM_SpinBox( pmGroupBox,
                        label        = "Chirality (m) :",
                        minimum      =  0,
                        maximum      =  100,
                        setAsDefault = True )
        self.chiralityMSpinBox.setDisabled(True)

    def _addWhatsThisText(self):
        """
        Add what's this text.
        """
        pass

    def _addToolTipText(self):
        """
        Add Tooltip text
        """
        pass

    def _nameChanged(self):
        """
        Slot for "Name" field.

        @TODO: Include a validator for the name field.
        """

        _name = str(self.nameLineEdit.text())

        if not _name: # Minimal test. Need to implement a validator.
            self.updateNameField()
            return

        self.command.setStructureName(_name)
        msg = "Editing structure <b>%s</b>." % _name
        self.updateMessage(msg)

        return

    def updateNameField(self):
        """
        Update the name field showing the name of the currently selected protein.
        clear the combobox list.
        """

        if self.command.hasValidStructure():
            self.nameLineEdit.setEnabled(True)
            self.nameLineEdit.setText(self.command.getStructureName())
        else:
            self.nameLineEdit.setDisabled(True)
            self.nameLineEdit.setText("")
        return

    def updateLength( self ):
        """
        Update the nanotube Length lineEdit widget.
        """
        if self.command.hasValidStructure():
            _nanotubeLength = vlen(self.endPoint1 - self.endPoint2)
            _lengthText = "%-7.4f Angstroms" % (_nanotubeLength)
        else:
            _lengthText = ""
        self.ntLengthLineEdit.setText(_lengthText)
        return

    def updateNanotubeDiameter(self):
        """
        Update the nanotube Diameter lineEdit widget.
        """
        if self.command.hasValidStructure():
            _diameterText = "%-7.4f Angstroms" %  (self.nanotube.getDiameter())
        else:
            _diameterText = ""
        self.ntDiameterLineEdit.setText(_diameterText)
        return

    def updateChirality( self ):
        """
        Update the nanotube chirality spinboxes (read-only).
        """
        if self.command.hasValidStructure():
            n, m = self.nanotube.getChirality()
        else:
            n = 0
            m = 0
        self.chiralityNSpinBox.setValue(n)
        self.chiralityMSpinBox.setValue(m)
        return

    pass # End of EditNanotube_PropertyManager class