pubsubdialog.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 import sys
21 from PyQt5.QtWidgets import QDialog, QFormLayout, QLabel, QLineEdit, QComboBox, \
22  QPushButton, QApplication, QHBoxLayout, QMessageBox
23 from PyQt5.QtCore import pyqtSignal
24 from visualstates.configs.rosconfig import RosConfig
25 from visualstates.configs.rospackage import getAllTypes
26 
27 
28 class PubSubDialog(QDialog):
29 
30  topicAdded = pyqtSignal()
31  topicUpdated = pyqtSignal(int)
32 
33  def __init__(self, topic=None, config=None, isPublisher=True):
34  super(QDialog, self).__init__()
35  self.isPublisher = isPublisher
36  # set dialog title
37  titleText = ''
38  if topic is None:
39  titleText += 'Add '
40  else:
41  titleText += 'Edit '
42  if self.isPublisher:
43  titleText += 'Publisher'
44  else:
45  titleText += 'Subscriber'
46  self.setWindowTitle(titleText)
47 
48  self.topic = topic
49  self.config = config
50 
51  layout = QFormLayout()
52  self.setLayout(layout)
53 
54  # create gui elements
55  methodNameLbl = None
56  if self.isPublisher:
57  methodNameLbl = QLabel('Method Name')
58  else:
59  methodNameLbl = QLabel('Variable Name')
60  topicLbl = QLabel('Topic')
61  typeLbl = QLabel('Type')
62 
63  self.methodNameEdit = QLineEdit()
64  self.methodNameEdit.setMinimumWidth(200)
65  self.topicEdit = QLineEdit()
66  self.topicEdit.setMinimumWidth(200)
67  self.typeCb = QComboBox()
68  self.typeCb.setEditable(True)
69  self.typeCb.setMinimumWidth(200)
70 
71  layout.addRow(methodNameLbl, self.methodNameEdit)
72  layout.addRow(topicLbl, self.topicEdit)
73  layout.addRow(typeLbl, self.typeCb)
74 
75  buttonLayout = QHBoxLayout()
76  cancelBtn = QPushButton('Cancel')
77  cancelBtn.clicked.connect(self.cancelClicked)
78  buttonLayout.addWidget(cancelBtn)
79  saveBtn = QPushButton('Add')
80  saveBtn.clicked.connect(self.saveClicked)
81  buttonLayout.addWidget(saveBtn)
82  layout.addRow(None, buttonLayout)
83 
84  for type in getAllTypes():
85  self.typeCb.addItem(type, type)
86 
87  # if topic is not None activate the edit mode
88  if self.topic is not None:
89  saveBtn.setText('Update')
90  if self.isPublisher:
91  self.methodNameEdit.setText(self.topic['methodname'])
92  else:
93  self.methodNameEdit.setText(self.topic['variablename'])
94  self.topicEdit.setText(self.topic['name'])
95  # find and set the combobox to the index
96  selectedTypeIndex = -1
97  for i in range(self.typeCb.count()):
98  if self.typeCb.itemText(i) == topic['type']:
99  selectedTypeIndex = i
100  break
101  if selectedTypeIndex == -1:
102  self.typeCb.addItem(topic['type'], topic['type'])
103  self.typeCb.setCurrentIndex(self.typeCb.count()-1)
104  else:
105  self.typeCb.setCurrentIndex(selectedTypeIndex)
106 
107  def cancelClicked(self):
108  self.close()
109 
110  def saveClicked(self):
111  newTopic = False
112  if self.topic is None:
113  newTopic = True
114 
115  methodname = self.methodNameEdit.text()
116  name = self.topicEdit.text()
117  type = self.typeCb.currentText()
118  if methodname == "" or name == "" or type == "" :
119  QMessageBox.warning(self, "Fields empty",
120  "One or more fields are empty and are required")
121  return
122 
123  for topic in self.config.topics:
124  if not newTopic:
125  if self.topic['id'] == topic['id']:
126  continue
127  if topic['opType'] == RosConfig.PUBLISH:
128  if self.isPublisher:
129  if methodname == topic['methodname']:
130  QMessageBox.information(self, "Method name present",
131  "Method name is already present in the publishers list")
132  return
133  else:
134  if methodname == topic['methodname']:
135  QMessageBox.information(self, "Variable name present",
136  "Variable name is already present in the publishers list as a method name")
137  return
138  else:
139  if self.isPublisher:
140  if methodname == topic['variablename']:
141  QMessageBox.information(self, "Method name present",
142  "Method name is already present in the subscribers list as a variable name")
143  return
144  else:
145  if methodname == topic['variablename']:
146  QMessageBox.information(self, "Variable name present",
147  "Variable name is already present in the subscribers list")
148  return
149 
150  if newTopic:
151  self.topic = {}
152 
153  if self.isPublisher:
154  self.topic['methodname'] = methodname
155  self.topic['opType'] = RosConfig.PUBLISH
156  else:
157  self.topic['variablename'] = methodname
158  self.topic['opType'] = RosConfig.SUBSCRIBE
159  self.topic['name'] = name
160  self.topic['type'] = type
161 
162  if newTopic:
163  self.topic['id'] = self.config.getTopicID()
164  self.config.topics.append(self.topic)
165  self.topicAdded.emit()
166  else:
167  self.topicUpdated.emit(self.topic['id'])
168 
169  self.close()
170 
171 
172 if __name__ == '__main__':
173  app = QApplication(sys.argv)
174  dialog = PubSubDialog()
175 
176  dialog.exec_()
def __init__(self, topic=None, config=None, isPublisher=True)
Definition: pubsubdialog.py:33


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