topicstab.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 from PyQt5.QtWidgets import QWidget, QPushButton, \
21  QVBoxLayout, QHBoxLayout, QLabel, \
22  QGroupBox, QScrollArea, QBoxLayout
23 from PyQt5.QtCore import Qt
24 from visualstates.configs.rosconfig import RosConfig
25 from visualstates.gui.dialogs.pubsubdialog import PubSubDialog
26 
27 
28 class TopicsTab(QWidget):
29  def __init__(self, config):
30  super(QWidget, self).__init__()
31  self.config = config
32  self.count = 0
33  self.topicRows = {}
34  self.topicUIs = {}
35 
36  layout = QVBoxLayout()
37  self.setLayout(layout)
38 
39  publishersBox = QGroupBox('Publishers')
40  pubVLayout = QVBoxLayout()
41  publishersBox.setLayout(pubVLayout)
42  layout.addWidget(publishersBox)
43 
44  addPublisherBtn = QPushButton('Add Publisher')
45  addPublisherBtn.setMaximumWidth(200)
46  addPublisherBtn.clicked.connect(self.addPublisher)
47  pubVLayout.addWidget(addPublisherBtn)
48 
49  pubRowLayout = QHBoxLayout()
50  pubVLayout.addLayout(pubRowLayout)
51 
52  # publisher headers
53  pubRowLayout.addSpacing(10)
54  titleLblStyleSheet = 'QLabel { font-weight: bold;}'
55  methodLbl = QLabel('Method Name')
56  methodLbl.setStyleSheet(titleLblStyleSheet)
57  methodLbl.setMinimumWidth(200)
58  pubRowLayout.addWidget(methodLbl)
59  topicLbl = QLabel('Topic')
60  topicLbl.setStyleSheet(titleLblStyleSheet)
61  topicLbl.setMinimumWidth(200)
62  pubRowLayout.addWidget(topicLbl)
63  typeLbl = QLabel('Type')
64  typeLbl.setStyleSheet(titleLblStyleSheet)
65  typeLbl.setMinimumWidth(200)
66  pubRowLayout.addWidget(typeLbl)
67  actionLbl = QLabel('Actions')
68  actionLbl.setStyleSheet(titleLblStyleSheet)
69  actionLbl.setMaximumWidth(200)
70  actionLbl.setMinimumWidth(200)
71  pubRowLayout.addWidget(actionLbl)
72  pubRowLayout.addSpacing(10)
73 
74  pubScrollArea = QScrollArea()
75  pubScrollArea.setWidgetResizable(True)
76  # pubScrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
77  pubScrollArea.setStyleSheet('QScrollArea {border: 0px;}')
78  self.pubScrollVlayout = QVBoxLayout()
79  self.pubScrollVlayout.setDirection(QBoxLayout.TopToBottom)
80  self.pubScrollVlayout.setAlignment(Qt.AlignTop)
81  dummyBox = QGroupBox()
82  dummyBox.setStyleSheet('QGroupBox {padding: 0px; margin: 0px;}')
83  dummyBox.setLayout(self.pubScrollVlayout)
84  pubScrollArea.setWidget(dummyBox)
85  pubVLayout.addWidget(pubScrollArea)
86 
87  subscribersBox = QGroupBox('Subscribers')
88  subsVLayout = QVBoxLayout()
89  subscribersBox.setLayout(subsVLayout)
90  layout.addWidget(subscribersBox)
91 
92  addSubscriberBtn = QPushButton('Add Subscriber')
93  addSubscriberBtn.setMaximumWidth(200)
94  addSubscriberBtn.clicked.connect(self.addSubscriber)
95  subsVLayout.addWidget(addSubscriberBtn)
96 
97  subsRowLayout = QHBoxLayout()
98  subsVLayout.addLayout(subsRowLayout)
99 
100  # publisher headers
101  subsRowLayout.addSpacing(10)
102  titleLblStyleSheet = 'QLabel {font-weight: bold;}'
103  varLbl = QLabel('Variable Name')
104  varLbl.setStyleSheet(titleLblStyleSheet)
105  varLbl.setMinimumWidth(200)
106  subsRowLayout.addWidget(varLbl)
107  topicLbl = QLabel('Topic')
108  topicLbl.setStyleSheet(titleLblStyleSheet)
109  topicLbl.setMinimumWidth(200)
110  subsRowLayout.addWidget(topicLbl)
111  typeLbl = QLabel('Type')
112  typeLbl.setStyleSheet(titleLblStyleSheet)
113  typeLbl.setMinimumWidth(200)
114  subsRowLayout.addWidget(typeLbl)
115  actionLbl = QLabel('Actions')
116  actionLbl.setStyleSheet(titleLblStyleSheet)
117  actionLbl.setMaximumWidth(200)
118  actionLbl.setMinimumWidth(200)
119  subsRowLayout.addWidget(actionLbl)
120  subsRowLayout.addSpacing(10)
121 
122  subsScrollArea = QScrollArea()
123  subsScrollArea.setWidgetResizable(True)
124  # subsScrollArea.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
125  subsScrollArea.setStyleSheet('QScrollArea {border: 0px;}')
126  self.subsScrollVlayout = QVBoxLayout()
127  self.subsScrollVlayout.setDirection(QBoxLayout.TopToBottom)
128  self.subsScrollVlayout.setAlignment(Qt.AlignTop)
129  dummyBox = QGroupBox()
130  dummyBox.setStyleSheet('QGroupBox {padding: 0px; margin: 0px;}')
131  dummyBox.setLayout(self.subsScrollVlayout)
132  subsScrollArea.setWidget(dummyBox)
133  subsVLayout.addWidget(subsScrollArea)
134 
135  # draw by the current config
136  for topic in self.config.topics:
137  self.addTopic(topic)
138 
139  def addPublisher(self):
140  dialog = PubSubDialog(config=self.config, isPublisher=True)
141  dialog.topicAdded.connect(self.topicAddedHandler)
142  dialog.exec_()
143 
144  def addSubscriber(self):
145  dialog = PubSubDialog(config=self.config, isPublisher=False)
146  dialog.topicAdded.connect(self.topicAddedHandler)
147  dialog.exec_()
148 
149  def addTopic(self, topic):
150  rowLayout = QHBoxLayout()
151  topicStyle = 'QLabel {border: 1px solid black;}'
152  nameLbl = QLabel()
153  nameLbl.setStyleSheet(topicStyle)
154  nameLbl.setMinimumWidth(200)
155  rowLayout.addWidget(nameLbl)
156  topicLbl = QLabel(topic['name'])
157  topicLbl.setStyleSheet(topicStyle)
158  topicLbl.setMinimumWidth(200)
159  rowLayout.addWidget(topicLbl)
160  typeLbl = QLabel(topic['type'])
161  typeLbl.setStyleSheet(topicStyle)
162  typeLbl.setMinimumWidth(200)
163  rowLayout.addWidget(typeLbl)
164 
165  editBtn = QPushButton('Edit')
166  editBtn.setMinimumWidth(96)
167  editBtn.setMaximumWidth(96)
168  editBtn.setObjectName(str(topic['id']))
169  editBtn.clicked.connect(self.editHandler)
170  rowLayout.addWidget(editBtn)
171  removeBtn = QPushButton('Remove')
172  removeBtn.setMinimumWidth(96)
173  removeBtn.setMaximumWidth(96)
174  removeBtn.setObjectName(str(topic['id']))
175  removeBtn.clicked.connect(self.removeHandler)
176  rowLayout.addWidget(removeBtn)
177  UIs = [nameLbl, topicLbl, typeLbl, editBtn, removeBtn, rowLayout]
178  self.topicUIs[topic['id']] = UIs
179  if topic['opType'] == RosConfig.PUBLISH:
180  nameLbl.setText(topic['methodname'])
181  self.pubScrollVlayout.addLayout(rowLayout)
182  elif topic['opType'] == RosConfig.SUBSCRIBE:
183  nameLbl.setText(topic['variablename'])
184  self.subsScrollVlayout.addLayout(rowLayout)
185 
186  def topicAddedHandler(self):
187  addedTopic = self.config.topics[len(self.config.topics)-1]
188  self.addTopic(addedTopic)
189 
190  def editHandler(self):
191  editID = int(self.sender().objectName())
192  editTopic = None
193  dialog = None
194  for topic in self.config.topics:
195  if topic['id'] == editID:
196  editTopic = topic
197  break
198  if editTopic is not None:
199  if editTopic['opType'] == RosConfig.PUBLISH:
200  dialog = PubSubDialog(topic=editTopic, config=self.config, isPublisher=True)
201  elif editTopic['opType'] == RosConfig.SUBSCRIBE:
202  dialog = PubSubDialog(topic=editTopic, config=self.config, isPublisher=False)
203  dialog.topicUpdated.connect(self.topicUpdatedHandler)
204  dialog.exec_()
205 
206  def topicUpdatedHandler(self, updatedID):
207  updatedTopic = None
208  for topic in self.config.topics:
209  if topic['id'] == updatedID:
210  updatedTopic = topic
211  break
212 
213  if updatedTopic is not None:
214  UIs = self.topicUIs[updatedID]
215  if updatedTopic['opType'] == RosConfig.PUBLISH:
216  UIs[0].setText(updatedTopic['methodname'])
217  elif updatedTopic['opType'] == RosConfig.SUBSCRIBE:
218  UIs[0].setText(updatedTopic['variablename'])
219 
220  UIs[1].setText(updatedTopic['name'])
221  UIs[2].setText(updatedTopic['type'])
222 
223  def removeHandler(self):
224  removeID = int(self.sender().objectName())
225  removeItem = None
226  for topic in self.config.topics:
227  if topic['id'] == removeID:
228  removeItem = topic
229  break
230  if removeItem is not None:
231  self.config.topics.remove(removeItem)
232  UIs = self.topicUIs[removeID]
233  for uiItem in UIs:
234  uiItem.deleteLater()


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20