taurus-org / taurus

Moved to https://gitlab.com/taurus-org/taurus
http://taurus-scada.org
43 stars 46 forks source link

Removing designMode from widget constructor #13

Open sf-migrator-bot opened 10 years ago

sf-migrator-bot commented 10 years ago

Using designMode as constructor parameter is not very nice. If you create a widget by composition and use it with QtDesigner (*) , the children of the widget will not be in designerMode and it will create a mess.

(*) for example by importing a .ui file. But in pure python code, providing designerMode everywhere it is needed is also a mess.


One way to fix that, the one i use with the help of Tiago is to check the modules:

designMode = 'PyQt4.QtDesigner' in sys.modules

It does not support all the cases, but it can be improved


Another way is to inject the context to the class when we create the QtDesignerPlugin

klass.QTDESIGNER_CONTEXT = True

And using something like that on the class:

designMode = hasattr(self, "QTDESIGNER_CONTEXT")

Reported by: vallsv ( http://sf.net/u/valentinvalls )

sf-migrator-bot commented 10 years ago

I like the idea! I prefer something like the first option. The weak point is if somebody does this kind of program:

import PyQt4.QtDesigner
import taurus.qt.QtGui

# ...

I propose here what I believe is a safer implementation:

I think we could have in taurus.qt.QtGui an API to check/set if in QtDesigner:

# taurus/qt/QtGui/__init__.py

__designerMode = False

def isDesignerMode():
    global __designerMode
    return __designerMode

def setDesignerMode(yesno):
    global __designerMode
    __designerMode = yesno

When QtDesigner starts, it imports taurus.qt.qtdesigner.tauruspluginplugin. The taurus.qt.qtdesigner.tauruspluginplugin could set the designer mode to True:

# taurus/qt/qtdesigner/tauruspluginplugin.py

from taurus.qt.QtGui import setDesignerMode

def main()
    setDesignerMode(True)
    # ...

The widgets could check if they are in QtDesigner if needed by doing:

# mywidget.py

from taurus.qt.QtGui import isDesignerMode

class MyWidget(...):

    def __init__(self, parent=None):
        # ...

        designerMode = isDesignerMode()
        if designerMode:
            # ...
        else:
            # ...

        # ...

Original comment by: tiagocoutinho (http://sourceforge.net/u/tiagocoutinho)

sf-migrator-bot commented 10 years ago

I like that!

Original comment by: cpascual (http://sourceforge.net/u/cpascual)

sf-migrator-bot commented 9 years ago

Original comment by: cpascual (http://sourceforge.net/u/cpascual)