Esempio n. 1
0
def connect_checkbox_with_boolean_pref_OLD(
    qcheckbox, prefs_key
):  #bruce 050810, slightly revised 070814, DEPRECATED since being replaced
    """
    Cause the checkbox to track the value of the given boolean preference,
    and cause changes to the checkbox to change the preference.
    (Use of the word "with" in the function name, rather than "to" or "from",
     is meant to indicate that this connection is two-way.)
    First remove any prior connection of the same type on the same checkbox.
    Legal for more than one checkbox to track and control the same pref [but that might be untested].
    """
    # first destroy any prior connection trying to control the same thing
    widget_destroyConnectionWithState(qcheckbox)

    # make a one-way connection from prefs value to checkbox, using Formula (active as soon as made)
    setter = qcheckbox.setChecked  #e or we might prefer a setter which wraps this with .blockSignals(True)/(False)
    conn1 = Formula(lambda: env.prefs.get(prefs_key), setter)

    # this calls the setter now and whenever the lambda-value changes, until it's destroyed
    # or until there's any exception in either arg that it calls.

    # make a one-way connection from Qt checkbox to preference (active as soon as made)
    def prefsetter(val):
        #e should we assert val is boolean? nah, just coerce it:
        val = not not val
        env.prefs[prefs_key] = val

    conn2 = destroyable_Qt_connection(qcheckbox, SIGNAL("toggled(bool)"),
                                      prefsetter)

    # package up both connections as a single destroyable object, and store it
    conn = list_of_destroyables(conn1, conn2)
    widget_setConnectionWithState(qcheckbox, conn)
    return
Esempio n. 2
0
 def connect_the_other_way(self):
     self.conn1 = Formula(self.usage_tracked_getter,
                          self.careful_widget_setter,
                          debug=self.debug)
     if self.debug:
         print "\n_changes__debug_print: %r connected from %r to %r using %r with %r" % \
               ( self, self.stateref, self.widget, self.conn1, self.usage_tracked_getter )
Esempio n. 3
0
def connect_colorpref_to_colorframe(
        prefs_key,
        colorframe):  #bruce 050805; revised 070425/070430 in Qt4 branch
    """
    Cause the bgcolor of the given Qt "color frame" to be set to
    each new legal color value stored in the given pref.
    """
    # first destroy any prior connection trying to control the same colorframe widget
    widget_destroyConnectionWithState(colorframe)

    # For Qt4, to fix bug 2320, we need to give the colorframe a unique palette, in which we can modify the background color.
    # To get this to work, it was necessary to make a new palette each time the color changes, modify it, and re-save into colorframe
    # (done below). This probably relates to "implicit sharing" of QPalette (see Qt 4.2 online docs).
    # [bruce 070425]
    def colorframe_bgcolor_setter(color):
        #e no convenient/clean way for Formula API to permit but not require this function to receive the formula,
        # unless we store it temporarily in env._formula (which we might as well do if this feature is ever needed)
        try:
            # make sure errors here don't stop the formula from running:
            # (Need to protect against certain kinds of erroneous color values? RGBf_to_QColor does it well enough.)
            ## Qt3 code used: colorframe.setPaletteBackgroundColor(RGBf_to_QColor(color))
            qcolor = RGBf_to_QColor(color)
            palette = QPalette(
            )  # QPalette(qcolor) would have window color set from qcolor, but that doesn't help us here
            qcolorrole = QPalette.Window
            ## http://doc.trolltech.com/4.2/qpalette.html#ColorRole-enum says:
            ##   QPalette.Window    10    A general background color.
            palette.setColor(QPalette.Active, qcolorrole,
                             qcolor)  # used when window is in fg and has focus
            palette.setColor(
                QPalette.Inactive, qcolorrole,
                qcolor)  # used when window is in bg or does not have focus
            palette.setColor(QPalette.Disabled, qcolorrole,
                             qcolor)  # used when widget is disabled
            colorframe.setPalette(palette)
            colorframe.setAutoFillBackground(True)
            # [Note: the above scheme was revised again by bruce 070430, for improved appearance
            #  (now has thin black border around color patch), based on Ninad's change in UserPrefs.py.]
            ## no longer needed: set color for qcolorrole = QPalette.ColorRole(role) for role in range(14)
            ## no longer needed: colorframe.setLineWidth(500) # width of outline of frame (at least half max possible size)
        except:
            print "data for following exception: ",
            print "colorframe %r has palette %r" % (colorframe,
                                                    colorframe.palette())
            # fyi: in Qt4, like in Qt3, colorframe is a QFrame
            print_compact_traceback(
                "bug (ignored): exception in formula-setter: "
            )  #e include formula obj in this msg?
        pass

    conn = Formula(lambda: env.prefs.get(prefs_key), colorframe_bgcolor_setter)
    # this calls the setter now and whenever the lambda-value changes, until it's destroyed
    # or until there's any exception in either arg that it calls.

    widget_setConnectionWithState(colorframe, conn)
    return