summaryrefslogtreecommitdiff
path: root/lib/taurus/qt/qtdesigner/tauruspluginplugin.py
blob: 2abb98f93bdf0d5f4efaa58c7677d8e5f345148d (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
#!/usr/bin/env python

#############################################################################
##
# This file is part of Taurus
##
# http://taurus-scada.org
##
# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
# Taurus is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
##
# Taurus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
##
# You should have received a copy of the GNU Lesser General Public License
# along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################

"""
tauruspluginplugin.py:
"""
from __future__ import absolute_import

from taurus.external.qt import QtDesigner


def build_qtdesigner_widget_plugin(klass):
    #from . import taurusplugin # - after futurize stage 1
    import taurusplugin.taurusplugin as taurusplugin # + after futurize stage 1

    class Plugin(taurusplugin.TaurusWidgetPlugin):
        WidgetClass = klass

    Plugin.__name__ = klass.__name__ + "QtDesignerPlugin"
    return Plugin

_SKIP = ["QLogo", "QGroupWidget", "TaurusGroupWidget"]

_plugins = {}


def main():
    from taurus.core.util.log import Logger
    from taurus.qt.qtgui.util import TaurusWidgetFactory
    Logger.setLogLevel(Logger.Debug)
    _log = Logger(__name__)

    try:
        wf = TaurusWidgetFactory()
        klasses = wf.getWidgetClasses()
        ok_nb, skipped_nb, e1_nb, e2_nb, e3_nb, e4_nb = 0, 0, 0, 0, 0, 0
        for widget_klass in klasses:
            name = widget_klass.__name__
            #_log.debug("Processing %s" % name)
            if name in _SKIP:
                #_log.debug("Skipped %s" % name)
                skipped_nb += 1
                continue
            # if getQtDesignerPluginInfo does not exist, returns None or raises
            # an exception: forget the widget
            cont = False
            try:
                qt_info = widget_klass.getQtDesignerPluginInfo()
                if qt_info is None:
                    #_log.debug("E1: Canceled %s (getQtDesignerPluginInfo)" % name)
                    e1_nb += 1
                    cont = True
            except AttributeError:
                #_log.debug("E2: Canceled %s (widget doesn't have getQtDesignerPluginInfo())" % name)
                e2_nb += 1
                cont = True
            except Exception as e:
                #_log.debug("E3: Canceled %s (%s)" % (name, str(e)))
                e3_nb += 1
                cont = True

            if cont:
                continue
            for k in ('module', ):
                if k not in qt_info:
                    #_log.debug("E4: Canceled %s (getQtDesignerPluginInfo doesn't have key %s)" % (name, k))
                    e4_nb += 1
                    cont = True
            if cont:
                continue

            plugin_klass = build_qtdesigner_widget_plugin(widget_klass)
            plugin_klass_name = plugin_klass.__name__
            globals()[plugin_klass_name] = plugin_klass
            _plugins[plugin_klass_name] = plugin_klass

            ok_nb += 1
            #_log.debug("DONE processing %s" % name)
        _log.info("Inpected %d widgets. %d (OK), %d (Skipped), %d (E1), %d (E2), %d (E3), %d(E4)" % (
            len(klasses), ok_nb, skipped_nb, e1_nb, e2_nb, e3_nb, e4_nb))
        _log.info("E1: getQtDesignerPluginInfo() returns None")
        _log.info("E2: widget doesn't implement getQtDesignerPluginInfo()")
        _log.info("E3: getQtDesignerPluginInfo() throws exception")
        _log.info(
            "E4: getQtDesignerPluginInfo() returns dictionary with missing key (probably 'module' key)")
    except Exception as e:
        import traceback
        traceback.print_exc()
        # print e


class TaurusWidgets(QtDesigner.QPyDesignerCustomWidgetCollectionPlugin):

    def __init__(self, parent=None):
        QtDesigner.QPyDesignerCustomWidgetCollectionPlugin.__init__(parent)
        self._widgets = None

    def customWidgets(self):
        if self._widgets is None:
            self._widgets = [w(self) for w in _plugins.values()]
        return self._widgets

if __name__ != "__main__":
    main()