-
Notifications
You must be signed in to change notification settings - Fork 1
/
messageeditor.py
100 lines (65 loc) · 2.54 KB
/
messageeditor.py
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
# vim:tw=120
# -*- coding: utf-8 -*-
## @package messageeditor
# item editor for message type items. Shows popups for adding and removing optional and repeated fields.
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import FD
## item editor for message type items. Shows popups for adding and removing optional and repeated fields.
class MessageEditor(QWidget):
## @var form
# the QFormLayout that holds the various popups.
## constructor.
def __init__(self, parent=None):
QWidget.__init__(self,parent)
vbox=QVBoxLayout(self)
self.setLayout(vbox)
label=QLabel(self)
label.setText("MessageEditor")
vbox.addWidget(label)
self.namelabel=QLabel(self)
vbox.addWidget(self.namelabel)
vbox.addWidget(QLabel("Create new child"))
self.form = QFormLayout()
vbox.addLayout(self.form)
self.repeatedpopup=QMenu(self)
self.repeatedmenubutton = QPushButton(self)
self.repeatedmenubutton.setMenu(self.repeatedpopup)
self.form.addRow("&Repeated", self.repeatedmenubutton)
QObject.connect(self.repeatedpopup, SIGNAL("triggered(QAction *)"),
self.add_child)
self.optionalpopup=QMenu(self)
self.optionalmenubutton = QPushButton(self)
self.optionalmenubutton.setMenu(self.optionalpopup)
self.form.addRow("&Optional",self.optionalmenubutton)
QObject.connect(self.optionalpopup, SIGNAL("triggered(QAction *)"),
self.add_child)
vbox.addStretch()
def add_child(self, action):
field_name = str(action.text())
self.widgetitem.add_child(field_name)
# remove this menu option if it was one of the optional fields
for action in self.optionalpopup.actions() :
if action.text() == field_name:
self.optionalpopup.removeAction( action)
self.widgetitem.treeWidget().emit_gpbupdate()
def set_optional_enums(self):
unfilled_optional_fields={}
filled_optional_fields={}
for name,fd in self.widgetitem.optional_fields.items():
if not self.widgetitem.find_child_by_name(name):
unfilled_optional_fields[name]=fd
self.set_popup_menu(self.optionalpopup, unfilled_optional_fields)
self.optionalmenubutton.setEnabled( len(unfilled_optional_fields))
def set_treewidget(self, widgetitem):
self.widgetitem=widgetitem
self.fd = widgetitem.field_desc
self.namelabel.setText(widgetitem.field_desc.name)
self.set_popup_menu(self.repeatedpopup, widgetitem.repeated_fields)
self.repeatedmenubutton.setEnabled( len(widgetitem.repeated_fields ))
self.set_optional_enums()
def set_popup_menu(self, menu, dict) :
menu.clear()
for name,fd in dict.items():
menu.addAction(name)