summaryrefslogtreecommitdiff
path: root/cad/src/widgets/widget_helpers.py
blob: d4cf6740be40cdff2e3c4c713b704e75ed94483d (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
# Copyright 2004-2008 Nanorex, Inc.  See LICENSE file for details.
"""
widgets.py - helpers related to widgets, and some simple custom widgets.

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

TODO:

Probably should be split into a few modules (e.g. has several color-related
helpers).

[bruce 080203 has done some of this splitting but not all. I moved some of it
into undo_manager, some into a new module menu_helpers, and some into an
outtakes file "old_extrude_widgets.py".]

In the meantime we might rename it to widget_helpers.py.
"""

from PyQt4 import QtGui
from PyQt4.Qt import QDialog
from PyQt4.Qt import QVBoxLayout
from PyQt4.Qt import SIGNAL
from PyQt4.Qt import QString
from PyQt4.Qt import QValidator
from PyQt4.Qt import QColor
from PyQt4.Qt import QTextEdit
from PyQt4.Qt import QPushButton
from PyQt4.Qt import QSize
from PyQt4.Qt import QMessageBox


from utilities.qt4transition import qt4todo

# ==

def double_fixup(validator, text, prevtext):
    """
    Returns a string that represents a float which meets the requirements of validator.
    text is the input string to be checked, prevtext is returned if text is not valid.
    """
    r, c = validator.validate(QString(text), 0)

    if r == QValidator.Invalid:
        return prevtext
    elif r == QValidator.Intermediate:
        if len(text) == 0:
            return ""
        return prevtext
    else:
        return text

# ==

# bruce 050614 [comment revised 050805] found colorchoose as a method in MWsemantics.py, nowhere used,
# so I moved it here for possible renovation and use.
# See also some color utilities in debug_prefs.py and prefs_widgets.py.
# Maybe some of them should all go into a new file specifically for colors. #e

def colorchoose(self, r, g, b):
    """
    #doc -- note that the args r,g,b should be ints, but the retval
    is a 3-tuple of floats. (Sorry, that's how I found it.)
    """
    # r, g, b is the default color displayed in the QColorDialog window.
    from PyQt4.Qt import QColorDialog
    color = QColorDialog.getColor(QColor(r, g, b), self, "choose") #k what does "choose" mean?
    if color.isValid():
        return color.red()/255.0, color.green()/255.0, color.blue()/255.0
    else:
        return r/255.0, g/255.0, b/255.0 # returning None might be more useful, since it lets callers "not change anything"
    pass

def RGBf_to_QColor(fcolor): # by Mark 050730
    """
    Converts RGB float to QColor.
    """
    # moved here by bruce 050805 since it requires QColor and is only useful with Qt widgets
    r = int (fcolor[0]*255 + 0.5) # (same formula as in elementSelector.py)
    g = int (fcolor[1]*255 + 0.5)
    b = int (fcolor[2]*255 + 0.5)
    return QColor(r, g, b)

def QColor_to_RGBf(qcolor): # by Mark 050921
    """
    Converts QColor to RGB float.
    """
    return qcolor.red()/255.0, qcolor.green()/255.0, qcolor.blue()/255.0

def QColor_to_Hex(qcolor): # by Mark 050921
    """
    Converts QColor to a hex color string. For example, a QColor of
    blue (0, 0, 255) returns "0000FF".
    """
    return "%02X%02X%02X" % (qcolor.red(), qcolor.green(), qcolor.blue())

def get_widget_with_color_palette(frame, color):
    """
    Return the widget <frame> after setting its palette based on <color>
    (a QColor provided by the user).
    """
    #ninad070502: This is used in many dialogs which show a colored frame
    #that represents the current color of the object in the glpane.
    #Example, in Rotary motor prop dialog, you will find a colored frame
    #that shows the present color of the rotary motor.
    frame.setAutoFillBackground(True)
    plt = QtGui.QPalette()
    plt.setColor(QtGui.QPalette.Active,   QtGui.QPalette.Window, color)
    plt.setColor(QtGui.QPalette.Inactive, QtGui.QPalette.Window, color)
    plt.setColor(QtGui.QPalette.Disabled, QtGui.QPalette.Window, color)
    frame.setPalette(plt)
    return frame

# ==

class TextMessageBox(QDialog):
    """
    The TextMessageBox class provides a modal dialog with a textedit widget
    and a close button.  It is used as an option to QMessageBox when displaying
    a large amount of text.  It also has the benefit of allowing the user to copy and
    paste the text from the textedit widget.

    Call the setText() method to insert text into the textedit widget.
    """
    def __init__(self, parent = None, name = None, modal = 1, fl = 0):
        #QDialog.__init__(self,parent,name,modal,fl)
        QDialog.__init__(self,parent)
        self.setModal(modal)
        qt4todo("handle flags in TextMessageBox.__init__")

        if name is None: name = "TextMessageBox"
        self.setObjectName(name)
        self.setWindowTitle(name)

        TextMessageLayout = QVBoxLayout(self)
        TextMessageLayout.setMargin(5)
        TextMessageLayout.setSpacing(1)

        self.text_edit = QTextEdit(self)

        TextMessageLayout.addWidget(self.text_edit)

        self.close_button = QPushButton(self)
        self.close_button.setText("Close")
        TextMessageLayout.addWidget(self.close_button)

        self.resize(QSize(350, 300).expandedTo(self.minimumSizeHint()))
            # Width changed from 300 to 350. Now hscrollbar doesn't appear in
            # Help > Graphics Info textbox. mark 060322
        qt4todo('self.clearWState(Qt.WState_Polished)') # what is this?

        self.connect(self.close_button, SIGNAL("clicked()"),self.close)

    def setText(self, txt):
        """
        Sets the textedit's text to txt
        """
        self.text_edit.setPlainText(txt)

    pass

#==

def PleaseConfirmMsgBox(text = 'Please Confirm.'): # mark 060302.
    """
    Prompts the user to confirm/cancel by pressing a 'Confirm' or 'Cancel' button in a QMessageBox.
    <text> is the confirmation string to explain what the user is confirming.

    Returns:
        True - if the user pressed the Confirm button
        False - if the user pressed the Cancel button (or Enter, Return or Escape)
    """
    ret = QMessageBox.warning( None, "Please Confirm",
            str(text) + "\n",
            "Confirm",
            "Cancel",
            "",
            1,  # The "default" button, when user presses Enter or Return (1 = Cancel)
            1)  # Escape (1= Cancel)

    if ret == 0:
        return True # Confirmed
    else:
        return False # Cancelled

# end