summaryrefslogtreecommitdiff
path: root/lib/taurus/qt/qtdesigner/containerplugin.py
blob: b2b4dc88752a1cdc0d3bc1d6f3365d35804b088f (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
#!/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/>.
#
# ###########################################################################

"""
Every TaurusWidget should have the following Qt Designer extended
capabilities:

  - Task menu:
    it means when you right click on the widget in the designer, it will have
    the following additional items:

    - 'Edit model...': opens a customized dialog for editing the widget model

  - Property Sheet:
    it means that in the Qt Designer property sheet it will have the following
    properties customized:

    - 'model': will have a '...' button that will open a customized dialog for
      editing the widget model (same has 'Edit model...' task menu item
"""


from taurus.external.qt import QtDesigner
from taurus.qt.qtgui.container.qcontainer import QGroupWidget


Q_TYPEID = {
    "QPyDesignerContainerExtension": "com.trolltech.Qt.Designer.Container",
    "QPyDesignerPropertySheetExtension": "com.trolltech.Qt.Designer.PropertySheet",  # noqa
    "QPyDesignerTaskMenuExtension": "com.trolltech.Qt.Designer.TaskMenu",
    "QPyDesignerMemberSheetExtension": "com.trolltech.Qt.Designer.MemberSheet",
}


class QGroupWidgetContainerExtension(QtDesigner.QPyDesignerContainerExtension):
    def __init__(self, widget, parent=None):
        super(QGroupWidgetContainerExtension, self).__init__(parent)
        self._widget = widget
        self._page_widget = None

    def addWidget(self, widget):
        if self.count() > 0:
            raise Exception("Can only have at maximum one child")
        self._layout().addWidget(widget)
        self._page_widget = widget

    def _content(self):
        return self._widget.content()

    def _layout(self):
        return self._content().layout()

    def count(self):
        return self._layout().count()

    def currentIndex(self):
        if self.count() > 0:
            return 0
        return -1

    def insertWidget(self, index, widget):
        self.addWidget(widget)

    def remove(self, index):
        self._layout().removeWidget(self.widget(index))

    def setCurrentIndex(self, index):
        pass

    def widget(self, index):
        return self._page_widget


class QGroupWidgetExtensionFactory(QtDesigner.QExtensionFactory):
    def __init__(self, parent=None):
        super(QGroupWidgetExtensionFactory, self).__init__(parent)

    def createExtension(self, obj, iid, parent):
        if iid != Q_TYPEID["QPyDesignerContainerExtension"]:
            return None
        if isinstance(obj, QGroupWidget):
            return QGroupWidgetContainerExtension(obj, parent)
        return None


def create_plugin():
    from taurus.qt.qtdesigner.taurusplugin.taurusplugin import (
        TaurusWidgetPlugin,
    )

    class QGroupWidgetPlugin(TaurusWidgetPlugin):

        WidgetClass = QGroupWidget

        def initialize(self, formEditor):
            if self.isInitialized():
                return

            manager = formEditor.extensionManager()
            if manager:
                self.factory = QGroupWidgetExtensionFactory(manager)
                manager.registerExtensions(
                    self.factory, Q_TYPEID["QPyDesignerContainerExtension"]
                )
            self.initialized = True

    return QGroupWidgetPlugin


QGroupWidgetPlugin = create_plugin()