summaryrefslogtreecommitdiff
path: root/cad/src/temporary_commands/TemporaryCommand.py
blob: f0f13ae7afcdea7c6d837c32fff2c5d3e650f9af (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
# Copyright 2007-2009 Nanorex, Inc.  See LICENSE file for details.
"""
TemporaryCommand.py -- provides several kinds of TemporaryCommand superclasses
(so far, just TemporaryCommand_Overdrawing, used for Zoom/Pan/Rotate).

@author:    Mark, Bruce
@version:   $Id$
@copyright: 2007-2009 Nanorex, Inc.  See LICENSE file for details.
@license:   GPL
"""

from PyQt4.Qt import Qt

from command_support.Command import Command, commonCommand

from command_support.GraphicsMode import GraphicsMode, commonGraphicsMode

# == useful pieces -- Command

class TemporaryCommand_preMixin(commonCommand):
    """
    A pre-Mixin class for Command subclasses
    which want to act as temporary commands.
    """
    command_should_resume_prevMode = True #bruce 071011, to be revised (replaces need for customized Done method)
    #See Command.anyCommand for detailed explanation of the following flag
    command_has_its_own_PM = False
    pass


# == useful pieces -- GraphicsMode

class ESC_to_exit_GraphicsMode_preMixin(commonGraphicsMode):
    """
    A pre-mixin class for GraphicsModes which overrides their keyPress method
    to call self.command.command_Done() when the ESC key is pressed
    if self.command.should_exit_when_ESC_key_pressed() returns true,
    or to call assy.selectNone otherwise,
    but which delegates all other keypresses to the superclass.
    """
    def keyPress(self, key):
        # ESC - Exit our command, if it permits exit due to ESC key.
        if key == Qt.Key_Escape:
            if self.command.should_exit_when_ESC_key_pressed():
                self.command.command_Done()
            else:
                self.glpane.assy.selectNone()
        else:
            #bruce 071012 bugfix: add 'else' to prevent letting superclass
            # also handle Key_Escape and do assy.selectNone.
            # I think that was a bug, since you might want to pan or zoom etc
            # in order to increase the selection. Other ways of changing
            # the viewpoint (eg trackball) don't deselect everything.
            super(ESC_to_exit_GraphicsMode_preMixin, self).keyPress(key) # Fixes bug 1172 (F1 key). mark 060321
        return
    pass


class Overdrawing_GraphicsMode_preMixin(commonGraphicsMode):
    """
    A pre-mixin class for GraphicsModes which want to override their
    Draw_* methods to do the parent command's drawing
    (perhaps in addition to their own, if they subclass this
     and further extend its Draw_* methods, or if they do incremental
     OpenGL drawing in event handler methods).

    (If there is no parent command, which I think never happens
     given how this is used as of 071012, this will raise an exception
     when any of its Draw_* methods are called.)

    @see: related class Delegating_GraphicsMode
    """
    #bruce 090310 revised this
    def Draw_preparation(self):
        self.parentGraphicsMode.Draw_preparation()

    def Draw_axes(self):
        self.parentGraphicsMode.Draw_axes()

    def Draw_other_before_model(self):
        self.parentGraphicsMode.Draw_other_before_model()

    def Draw_model(self):
        self.parentGraphicsMode.Draw_model()

    def Draw_other(self):
        # doing this fixes the bug in which Pan etc doesn't show the right things
        # for BuildCrystal or Extrude modes (e.g. bond-offset spheres in Extrude)
        self.parentGraphicsMode.Draw_other()

    def Draw_after_highlighting(self, pickCheckOnly = False):
        #bruce 090310 new feature (or bugfix) -- delegate this too
        self.parentGraphicsMode.Draw_after_highlighting(pickCheckOnly = pickCheckOnly)

    pass

# ==

class _TemporaryCommand_Overdrawing_GM( ESC_to_exit_GraphicsMode_preMixin,
                                        Overdrawing_GraphicsMode_preMixin,
                                        GraphicsMode ):
    """
    GraphicsMode component of TemporaryCommand_Overdrawing.

    Note: to inherit this, get it from
    TemporaryCommand_Overdrawing.GraphicsMode_class
    rather than inheriting it directly by name.
    """
    pass

class TemporaryCommand_Overdrawing( TemporaryCommand_preMixin,
                                    Command):
    """
    Common superclass for temporary view-change commands
    such as the Pan, Rotate, and Zoom tools.

    Provides the declarations that make a command temporary,
    a binding from the Escape key to the Done method,
    and a Draw method which delegates to the Draw method
    of the saved parentCommand.

    In other respects, inherits behavior from Command
    and GraphicsMode.
    """
    GraphicsMode_class = _TemporaryCommand_Overdrawing_GM
    __abstract_command_class = True
    featurename = "Undocumented Temporary Command"
        # (I don't know if this featurename is ever user-visible;
        #  if it is, it's probably wrong -- consider overriding
        #  self.get_featurename() to return the value from the
        #  prior command.
        #  The default implementation returns this constant
        #  or (if it's not overridden in subclasses) something
        #  derived from it. [bruce 071227])
    pass


# keep this around for awhile, to show how to set it up when we want the
# same thing while converting larger old modes:
#
### == temporary compatibility code (as is the use of commonXXX in the mixins above)
##
##from modes import basicMode
##
### now combine all the classes used to make TemporaryCommand_Overdrawing and its _GM class:
##
##class basicTemporaryCommand_Overdrawing( TemporaryCommand_preMixin,
##                       ESC_to_exit_GraphicsMode_preMixin,
##                       Overdrawing_GraphicsMode_preMixin,
##                       basicMode ):
##    pass

# end