summaryrefslogtreecommitdiff
path: root/cad/src/prototype/example_expr_command.py
blob: ce2837b16e00e51d4d560e117ae0a9910979557b (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
# Copyright 2007-2009 Nanorex, Inc.  See LICENSE file for details. 
"""
example_expr_command.py -- example of how to use an interactive graphics
expr in a command (unfinished, so partly scratch code); command and PM
are each variants of ExampleCommand1's command and PM classes
 
@author: Bruce
@version: $Id$
@copyright: 2007-2009 Nanorex, Inc.  See LICENSE file for details.

History:

070830 bruce split this out of test_commands.py and test_command_PMs.py,
in which it was called ExampleCommand2E
"""

# == PM class

from prototype.test_command_PMs import ExampleCommand1_PM

from PM.PM_LineEdit      import PM_LineEdit
from PM.PM_GroupBox      import PM_GroupBox

_superclass_PM = ExampleCommand1_PM

class ExampleCommand2E_PM( ExampleCommand1_PM ):
    # NOTE: used to use ExampleCommand2_PM which uses GBC; UNTESTED with this superclass [bruce 080910]
    """
    Property Manager for Example Command 2E
    """
    title = "Example Command 2E"
    prefix = "Thing2E" # for names created by GBC
    
    def _addGroupBoxes(self):
        """
        Add group boxes to this Property Manager.
        """
        _superclass_PM._addGroupBoxes(self)
        
        self.pmGroupBox2 = PM_GroupBox( self, title =  "group box 2" )
        self._loadGroupBox2(self.pmGroupBox2)
        
        return
    
    def _loadGroupBox2(self, groupbox):
        """
        Load widgets into group box 2.
        """
        self.someLineEdit  =  \
            PM_LineEdit( groupbox,
                         label        = "Text:",
                         text         = "initial text (pm)",
                         setAsDefault = True,
                         spanWidth    = False )
        ### TODO: self.someLineEdit.connectWithState( ... some text state ...)
        # and then connect the TextState text to the same state
        # (or just use that state? no, it needs an address outside that complicated expr)
        return

    pass

# == command class

# these imports are not needed in a minimal example like ExampleCommand1:

from graphics.drawing.CS_draw_primitives import drawline
from utilities.constants import red
from geometry.VQT import V
from exprs.instance_helpers import get_glpane_InstanceHolder
from exprs.draggable import DraggablyBoxed

from exprs.instance_helpers import InstanceMacro
from exprs.attr_decl_macros import State
from exprs.TextRect import TextRect

class TextState(InstanceMacro): # rename?
    text = State(str, "initial text", doc = "text")
    _value = TextRect(text) # need size?
    pass

from prototype.test_commands import ExampleCommand1 # NOTE: was ExampleCommand2, this revision UNTESTED [bruce 080910]


##from commands.SelectAtoms.SelectAtoms_Command import SelectAtoms_Command # used indirectly via ExampleCommand1
from commands.SelectAtoms.SelectAtoms_GraphicsMode import SelectAtoms_GraphicsMode



##class ExampleCommand2E_GM( ExampleCommand1.GraphicsMode_class): #bruce 071014 split out _GM class; works, except highlighting
class ExampleCommand2E_GM(SelectAtoms_GraphicsMode):
    def Draw_other(self):
        """
        Do some custom drawing (in the model's abs coordsys),
        as well as whatever the superclass does.
        """
        #print "start ExampleCommand2E Draw_other"
        super(ExampleCommand2E_GM, self).Draw_other()
        drawline(red, V(1,0,1), V(1,1,1), width = 2)
        self.command._expr_instance.draw()
        #print "end ExampleCommand2E Draw_other"
    pass

##class ExampleCommand2E_GM_KLUGED( ExampleCommand1.GraphicsMode_class,
##                            SelectAtoms_Command #### KLUGE, will it work? trying to use it just for its GM aspects...
##                           ): #bruce 071014 split out _GM class
##    # status, 071022: works except for highlighting (tho it looked like it did something on mouseover;
##    #  i forget if this eg had a good HL color change on that resizer), and on drag on that resizer i got
##    #  a region selection rubberband lasso/window. Until now it also had an exception in leftUp then,
##    ## AttributeError: 'ExampleCommand2E_GM_KLUGED' object has no attribute 'ignore_next_leftUp_event'
##    ##  [GLPane.py:1845] [selectAtomsMode.py:494]
##    # but setting this in __init__ works around that (see comment there).
#@ATTENTION:
#UPDATE 2008-08-22: Above comment is obsolete since SelectAtomsMode class has 
#been deprecated . This commented out code needs to be revised if its ever used.
#[-- Ninad comment]
##    
##    command = None # defeat the property in selectAtomsMode #k needed?
##    
##    def __init__(self, command):
##        ExampleCommand1.GraphicsMode_class.__init__(self, command) # includes self.command = command
##        SelectAtoms_Command.__init__(self, self.glpane) ##k??
##        self.ignore_next_leftUp_event = True
##            # kluge, since we didn't run the GM part of SelectAtoms_Command.Enter,
##            # which normally does this.
##        return
##
##    def Draw_other(self):
##        """
##        Do some custom drawing (in the model's abs coordsys)...
##        """
##        glpane = self.glpane
##        super(ExampleCommand2E_GM_KLUGED, self).Draw_other()
##        drawline(red, V(1,0,1), V(1,1,1), width = 2)
##        self.command._expr_instance.draw()
##    pass

##KLUGE_USE_SELATOMS_AS_GM = True ####
##
##if KLUGE_USE_SELATOMS_AS_GM:
##    ExampleCommand2E_GM = ExampleCommand2E_GM_KLUGED ##### improve

class ExampleCommand2E( ExampleCommand1, object):
    """
    Add things not needed in a minimal example, to try them out.
    (Uses a PM which is the same as ExampleCommand1 except for title.)
    """
    # Note: object superclass is only needed to permit super(ExampleCommand2E, self) to work.
    # object superclass should not come first, or it overrides __new__
    # (maybe could fix using def __init__ -- not tried, since object coming last works ok)

    commandName = 'ExampleCommand2E-commandName'
    PM_class = ExampleCommand2E_PM
    featurename = "Prototype: ExampleCommand2E"

    GraphicsMode_class = ExampleCommand2E_GM

    def __init__(self, commandSequencer):
        """
        create an expr instance, to draw in addition to the model
        """
        super(ExampleCommand2E, self).__init__(commandSequencer)
        glpane = commandSequencer.assy.glpane
        expr1 = TextState()
        expr2 = DraggablyBoxed(expr1, resizable = True)
            ###BUG: resizing is projecting mouseray in the wrong way, when plane is tilted!
            # I vaguely recall that the Boxed resizable option was only coded for use in 2D widgets,
            # whereas some other constrained drag code is correct for 3D but not yet directly usable in Boxed.
            # So this is just an example interactive expr, not the best way to do resizing in 3D. (Though it could be fixed.)

        # note: this code is similar to _expr_instance_for_imagename in confirmation_corner.py
        ih = get_glpane_InstanceHolder(glpane)
        expr = expr2
        index = (id(self),) # WARNING: needs to be unique, we're sharing this InstanceHolder with everything else in NE1
        self._expr_instance = ih.Instance( expr, index, skip_expr_compare = True)

        return
    
    pass # end of class ExampleCommand2E

# end