summaryrefslogtreecommitdiff
path: root/cad/src/widgets/GlobalDisplayStylesComboBox.py
blob: 7b63a6d68605dab394124261532b1bdbaa8ef121 (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
"""
GlobalDisplayStylesComboBox.py - Provides a combo box with all the global display
styles.

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

To do (for Mark):
- Add tooltip and What's This text.
- Change "Default Display Style" in the preferences dialog (Mode page) to
"Global Display Style at start up"

REVIEW: does this module belong in the ne1_ui package? [bruce 080711 comment]
"""

import os
from PyQt4.Qt import SIGNAL, QComboBox
import foundation.env as env
from utilities.constants import diDEFAULT ,diTrueCPK, diLINES
from utilities.constants import diBALL, diTUBES, diDNACYLINDER, diPROTEIN
from utilities.prefs_constants import startupGlobalDisplayStyle_prefs_key
from utilities.icon_utilities import geticon

# Should DNA Cylinder be a global display style? Selecting it makes everything
# except DNA invisible. If we leave it in the list, we must document this
# confusing behavior in the "What's This" text.
# --Mark 2008-03-16
# [note: this may have been fixed since then; not sure. [bruce 080711 comment]]

displayIndexes = [diLINES, diTUBES, diBALL, diTrueCPK, diDNACYLINDER, diPROTEIN]
displayNames   = ["Lines", "Tubes", "Ball and Stick", "CPK", "DNA Cylinder", "Protein"]
displayIcons   = ["Lines", "Tubes", "Ball_and_Stick", "CPK", "DNACylinder", "Protein"]

displayIconsDict = dict(zip(displayNames, displayIcons))
displayNamesDict = dict(zip(displayIndexes, displayNames))

class GlobalDisplayStylesComboBox(QComboBox):
    """
    The GlobalDisplayStylesComboBox widget provides a combobox with all
    the standard NE1 display styles.
    """

    def __init__(self, win):
        """
        Constructs a combobox with all the display styles.

        @param win: The NE1 mainwindow.
        @type  win: L{Ui_MainWindow}
        """
        QComboBox.__init__(self, win)
        self.win = win
        self._setup(disconnect = False)

    def _setup(self, display_style = diDEFAULT, disconnect = True):
        """
        Private method. Populates self and sets the current display style.
        """

        from utilities.debug_prefs import debug_pref, Choice_boolean_False

        # Add a new experimental Protein display style
        # if the Enable proteins debug pref is set to True.
        # piotr 080710
        from utilities.GlobalPreferences import ENABLE_PROTEINS

        if display_style == diDEFAULT:
            display_style = env.prefs[ startupGlobalDisplayStyle_prefs_key ]

        if disconnect:
            self.disconnect( self,
                             SIGNAL("currentIndexChanged(int)"),
                             self._setDisplayStyle )
        self.clear()

        ADD_DEFAULT_TEXT = False

        for displayName in displayNames:

            # Experimental display style for Proteins.
            if displayName == "Protein" and \
               not ENABLE_PROTEINS:
                # Skip the Proteins style.
                continue

            basename = displayIconsDict[displayName] + ".png"
            iconPath = os.path.join("ui/actions/View/Display/",
                                    basename)
            self.addItem(geticon(iconPath), displayName)

        self.setCurrentIndex(displayIndexes.index(display_style))

        self.connect( self,
                      SIGNAL("currentIndexChanged(int)"),
                      self._setDisplayStyle )

    def getDisplayStyleIndex(self):
        """
        Returns the current global display style.

        @return: the current global display style (i.e. diDEFAULT, diTUBES, etc)
        @rtype:  int
        """
        return displayIndexes[self.currentIndex()]

    def _setDisplayStyle(self, index):
        """
        Private slot method. Only called when self's index changes (i.e when
        the user selects a new global display style via self).

        @param index: the combobox index
        @type  index: int
        """
        assert index in range(self.count())

        glpane = self.win.assy.glpane
        glpane.setGlobalDisplayStyle(displayIndexes[index])
        glpane.gl_update()

    def setDisplayStyle(self, display_style):
        """
        Public method. Sets the display style of self to I{display_style}.

        @param display_style: display style code (i.e. diTUBES, diLINES, etc.)
        @type  display_style: int

        @note: does not directly call glpane.setGlobalDisplayStyle, but it's
               not documented whether it sometimes does this indirectly
               via a signal it causes to be sent from self.
        """
        assert display_style in displayIndexes

        # If self is already set to display_style, return.
        if self.currentIndex() == displayIndexes.index(display_style):
            return

        self.setCurrentIndex(displayIndexes.index(display_style)) # Generates signal!