-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathqtdesigner.py
More file actions
176 lines (133 loc) · 4.02 KB
/
qtdesigner.py
File metadata and controls
176 lines (133 loc) · 4.02 KB
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
# -*- coding: utf-8 -*-
#
# Licensed under the terms of the BSD 3-Clause
# (see plotpy/LICENSE for details)
"""
plotpy.widgets.qtdesigner
-----------------------------
The `qtdesigner` module provides QtDesigner helper functions for :mod:`plotpy`:
* :py:func:`.qtdesigner.loadui`
* :py:func:`.qtdesigner.compileui`
* :py:func:`.qtdesigner.create_qtdesigner_plugins`
Reference
~~~~~~~~~
.. autofunction:: loadui
.. autofunction:: compileui
.. autofunction:: create_qtdesigner_plugin
"""
import io
from guidata.configtools import get_icon
from qtpy import QtGui as QG
from qtpy import uic
from qtpy.QtDesigner import QPyDesignerCustomWidgetPlugin
def loadui(fname, replace_class="QwtPlot"):
"""
Return Widget or Window class from QtDesigner ui file 'fname'
The loadUiType function (PyQt5.uic) doesn't work correctly with plotpy
QtDesigner plugins because they don't inheritate from a PyQt5.QtGui
object.
"""
uifile_text = open(fname).read().replace(replace_class, "QFrame")
ui, base_class = uic.loadUiType(io.StringIO(uifile_text))
class Form(base_class, ui):
""" """
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
return Form
def compileui(fname, replace_class="QwtPlot"):
"""
:param fname:
:param replace_class:
"""
uifile_text = open(fname).read().replace("QwtPlot", "QFrame")
uic.compileUi(
io.StringIO(uifile_text),
open(fname.replace(".ui", "_ui.py"), "w"),
pyqt3_wrapper=True,
)
def create_qtdesigner_plugin(
group,
module_name,
class_name,
plot_options={},
icon=None,
tooltip="",
whatsthis="",
):
"""Return a custom QtDesigner plugin class
Example:
create_qtdesigner_plugin(group = "plotpy", module_name = "plotpy.baseplot",
class_name = "PlotWidget",
plot_options=PlotOptions(type="image")),
icon = "image.png", tooltip = "", whatsthis = ""):
"""
Widget = getattr(__import__(module_name, fromlist=[class_name]), class_name)
class CustomWidgetPlugin(QPyDesignerCustomWidgetPlugin):
""" """
def __init__(self, parent=None):
QPyDesignerCustomWidgetPlugin.__init__(self)
self.initialized = False
def initialize(self, core):
"""
:param core:
:return:
"""
if self.initialized:
return
self.initialized = True
def isInitialized(self):
"""
:return:
"""
return self.initialized
def createWidget(self, parent):
"""
:param parent:
:return:
"""
return Widget(parent, options=plot_options)
def name(self):
"""
:return:
"""
return class_name
def group(self):
"""
:return:
"""
return group
def icon(self):
"""
:return:
"""
if icon is not None:
return get_icon(icon)
else:
return QG.QIcon()
def toolTip(self):
"""
:return:
"""
return tooltip
def whatsThis(self):
"""
:return:
"""
return whatsthis
def isContainer(self):
"""
:return:
"""
return False
def domXml(self):
"""
:return:
"""
return f'<widget class="{class_name}" name="{class_name.lower()}" />\n'
def includeFile(self):
"""
:return:
"""
return module_name
return CustomWidgetPlugin