summaryrefslogtreecommitdiff
path: root/cad/src/outtakes/InsertNanotube/NanotubeGeneratorPropertyManager.py
blob: 97068024bc1bb7793eef7040c0b1743f8074b55b (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
# Copyright 2005-2007 Nanorex, Inc.  See LICENSE file for details.
"""
$Id$

History:

Mark 2007-05-18: Implemented Nanotube generator dialog using PropMgrBaseClass.
Mark 2007-08-06: Renamed NanotubeGeneratorDialog to NanotubeGeneratorPropertyManager.
"""

import math

from PyQt4 import QtCore, QtGui
from PyQt4.Qt import SIGNAL

from PM.PM_Dialog        import PM_Dialog
from PM.PM_GroupBox      import PM_GroupBox
from PM.PM_ComboBox      import PM_ComboBox
from PM.PM_DoubleSpinBox import PM_DoubleSpinBox
from PM.PM_SpinBox       import PM_SpinBox

from model.bonds import CC_GRAPHITIC_BONDLENGTH, BN_GRAPHITIC_BONDLENGTH
from utilities.debug import print_compact_traceback

ntBondLengths = [CC_GRAPHITIC_BONDLENGTH, BN_GRAPHITIC_BONDLENGTH]

class NanotubeGeneratorPropertyManager(PM_Dialog):
    """
    The NanotubeGeneratorPropertyManager class provides a Property Manager
    for the "Build > Nanotube" command.
    """
    # The title that appears in the property manager header.
    title = "Nanotube"
    # The name of this property manager. This will be set to
    # the name of the PropMgr (this) object via setObjectName().
    pmName = title
    # The relative path to PNG file that appears in the header.
    iconPath = "ui/actions/Tools/Build Structures/Nanotube.png"

    def __init__(self):
        """Construct the Graphene Property Manager.
        """
        PM_Dialog.__init__(self, self.pmName, self.iconPath, self.title)
        #@self.addGroupBoxes()
        #@self.add_whats_this_text()
        self.updateMessageGroupBox()

    def updateMessageGroupBox(self):
        msg = ""

        # A (4, 4) tube is stable, but a (3, 3) has not been seen in
        # isolation.  Circumference of a (4, 4) tube is about 6.93.
        xOffset = self.n + self.m * math.cos(math.pi/3.0)
        yOffset = self.m * math.sin(math.pi/3.0)
        circumference = math.sqrt(xOffset * xOffset + yOffset * yOffset)
        if (circumference < 6.5):
            msg = "Warning: Small diameter nanotubes may be unstable, \
            and may give unexpected results when minimized.<p>"

        msg = msg + "Edit the Nanotube parameters and select <b>Preview</b> to \
        preview the structure. Click <b>Done</b> to insert it into the model."

        # This causes the "Message" box to be displayed as well.
        # setAsDefault=True causes this message to be reset whenever
        # this PropMgr is (re)displayed via show(). Mark 2007-06-01.
        self.MessageGroupBox.insertHtmlMessage(msg, setAsDefault=True)


    def _addGroupBoxes(self):
        """
        Add the 3 group boxes to the Nanotube Property Manager dialog.
        """
        self.pmGroupBox1 = \
            PM_GroupBox( self,
                         title          = "Nanotube Parameters" )

        self.pmGroupBox2 = \
            PM_GroupBox( self,
                         title          = "Nanotube Distortion" )

        self.pmGroupBox3 = \
            PM_GroupBox( self,
                         title          = "Multi-Walled Nanotubes" )

        # Add group box widgets.
        self._loadGroupBox1(self.pmGroupBox1)
        self._loadGroupBox2(self.pmGroupBox2)
        self._loadGroupBox3(self.pmGroupBox3)

    def _loadGroupBox1(self, inPmGroupBox):
        """
        Load widgets in group box 1.
        """

        memberChoices = ["Carbon", "Boron Nitride"]
        self.typeComboBox= \
            PM_ComboBox( inPmGroupBox,
                         label        = "Type :",
                         choices      = memberChoices,
                         index        = 0,
                         setAsDefault = True,
                         spanWidth    = False )

        self.connect( self.typeComboBox,
                      SIGNAL("currentIndexChanged(int)"),
                      self.nt_type_changed)

        self.lengthField = \
            PM_DoubleSpinBox( inPmGroupBox,
                              label        = "Length :",
                              value        = 20.0,
                              setAsDefault = True,
                              minimum      = 1.0,
                              maximum      = 1000.0,
                              singleStep   = 1.0,
                              decimals     = 3,
                              suffix       = " Angstroms" )

        self.n = 5

        self.chiralityNSpinBox = \
            PM_SpinBox( inPmGroupBox,
                        label        = "Chirality (n) :",
                        value        = self.n,
                        setAsDefault = True )

        self.connect(self.chiralityNSpinBox,
                     SIGNAL("valueChanged(int)"),
                     self.chirality_fixup)

        self.m = 5

        self.chiralityMSpinBox = \
            PM_SpinBox( inPmGroupBox,
                        label        = "Chirality (m) :",
                        value        = self.m,
                        setAsDefault = True )

        self.connect(self.chiralityMSpinBox,
                     SIGNAL("valueChanged(int)"),
                     self.chirality_fixup)

        self.bondLengthField = \
            PM_DoubleSpinBox( inPmGroupBox,
                              label        = "Bond Length :",
                              value        = CC_GRAPHITIC_BONDLENGTH,
                              setAsDefault = True,
                              minimum      = 1.0,
                              maximum      = 3.0,
                              singleStep   = 0.1,
                              decimals     = 3,
                              suffix       = " Angstroms" )

        endingChoices = ["None", "Hydrogen", "Nitrogen"]

        self.endingsComboBox= \
            PM_ComboBox( inPmGroupBox,
                         label        = "Endings :",
                         choices      = endingChoices,
                         index        = 0,
                         setAsDefault = True,
                         spanWidth    = False )

    def _loadGroupBox2(self, inPmGroupBox):
        """
        Load widgets in group box 2.
        """

        self.zDistortionField = \
            PM_DoubleSpinBox( inPmGroupBox,
                              label        = "Z-distortion :",
                              value        = 0.0,
                              setAsDefault = True,
                              minimum      = 0.0,
                              maximum      = 10.0,
                              singleStep   = 0.1,
                              decimals     = 3,
                              suffix       = " Angstroms" )

        self.xyDistortionField = \
            PM_DoubleSpinBox( inPmGroupBox,
                              label        = "XY-distortion :",
                              value        = 0.0,
                              setAsDefault = True,
                              minimum      = 0.0,
                              maximum      = 2.0,
                              singleStep   = 0.1,
                              decimals     = 3,
                              suffix       = " Angstroms" )


        self.twistSpinBox = \
            PM_SpinBox( inPmGroupBox,
                        label        = "Twist :",
                        value        = 0,
                        setAsDefault = True,
                        minimum      = 0,
                        maximum      = 100, # What should maximum be?
                        suffix       = " deg/A" )

        self.bendSpinBox = \
            PM_SpinBox( inPmGroupBox,
                        label        = "Bend :",
                        value        = 0,
                        setAsDefault = True,
                        minimum      = 0,
                        maximum      = 360,
                        suffix       = " deg" )

    def _loadGroupBox3(self, inPmGroupBox):
        """
        Load widgets in group box 3.
        """

        # "Number of Nanotubes" SpinBox
        self.mwntCountSpinBox = \
            PM_SpinBox( inPmGroupBox,
                        label        = "Number :",
                        value        = 1,
                        setAsDefault = True,
                        minimum      = 1,
                        maximum      = 10,
                        suffix       = " nanotubes" )

        self.mwntCountSpinBox.setSpecialValueText("SWNT")

        # "Spacing" lineedit.
        self.mwntSpacingField = \
            PM_DoubleSpinBox( inPmGroupBox,
                              label        = "Spacing :",
                              value        = 2.46,
                              setAsDefault = True,
                              minimum      = 1.0,
                              maximum      = 10.0,
                              singleStep   = 0.1,
                              decimals     = 3,
                              suffix       = " Angstroms" )

    def _addWhatsThisText(self):
        """
        What's This text for widgets in this Property Manager.
        """
        from ne1_ui.WhatsThisText_for_PropertyManagers import whatsThis_NanotubeGeneratorPropertyManager
        whatsThis_NanotubeGeneratorPropertyManager(self)

    def _addToolTipText(self):
        """
        Tool Tip text for widgets in this Property Manager.
        """
        from ne1_ui.ToolTipText_for_PropertyManagers import ToolTip_NanotubeGeneratorPropertyManager
        ToolTip_NanotubeGeneratorPropertyManager(self)

    def chirality_fixup(self, spinBoxValueJunk = None):
        """
        Slot for several validators for different parameters.
        This gets called each time a user types anything into a widget or
        changes a spinbox.
        @param spinBoxValueJunk: This is the Spinbox value from the valueChanged
                                 signal. It is not used. We just want to know
                                 that the spinbox value has changed.
        @type  spinBoxValueJunk: double or None
        """

        if not hasattr(self, 'n'):
            print_compact_traceback("Bug: no attr 'n' ") # mark 2007-05-24
            return

        n_previous = int(self.n)
        m_previous = int(self.m)
        n = self.chiralityNSpinBox.value()
        m = self.chiralityMSpinBox.value()
        # Two restrictions to maintain
        # n >= 2
        # 0 <= m <= n
        if n < 2:
            n = 2
        if m != self.m:
            # The user changed m. If m became larger than n, make n bigger.
            if m > n:
                n = m
        elif n != self.n:
            # The user changed n. If n became smaller than m, make m smaller.
            if m > n:
                m = n
        self.chiralityNSpinBox.setValue(n)
        self.chiralityMSpinBox.setValue(m)
        self.m, self.n = m, n
        self.updateMessageGroupBox()

    def nt_type_changed(self, idx):
        """
        Slot for Nanotube Type combobox.
        Update the bond length field when the type changes.
        """
        self.bondLengthField.setValue(ntBondLengths[idx])