interface_gui.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from python_qt_binding import QtWidgets, QtCore
4 from python_qt_binding.QtWidgets import QWidget, QPushButton, QColorDialog
5 from python_qt_binding.QtCore import Slot
6 from python_qt_binding.QtGui import QColor
7 
8 from frame_editor.commands import *
9 
10 from frame_editor.interface import Interface
11 import rospkg
12 import os
13 
15 
16  def __init__(self, frame_editor):
17  self.editor = frame_editor
18  self.editor.observers.append(self)
19 
20  self.old_frame = None
21 
22  self.layout = QtWidgets.QGridLayout()
23  self.widget = QWidget()
24  self.widget.setLayout(self.layout)
25 
26  self.mesh_label = QtWidgets.QLineEdit("File:")
27  self.mesh_label.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed)
28  self.mesh_button = QtWidgets.QPushButton("Open")
29  self.mesh_button.clicked.connect(lambda: self.btn_open_mesh_clicked())
30 
31  self.diameter_label = QtWidgets.QLabel("Diameter:")
32  self.diameter_spinbox = QtWidgets.QDoubleSpinBox()
33  self.diameter_spinbox.editingFinished.connect(lambda: self.diameter_changed())
34 
35  self.length_label = QtWidgets.QLabel("Length:")
36  self.length_spinbox = QtWidgets.QDoubleSpinBox()
37  self.length_spinbox.editingFinished.connect(lambda: self.length_changed())
38 
39  self.width_label = QtWidgets.QLabel("Width:")
40  self.width_spinbox = QtWidgets.QDoubleSpinBox()
41  self.width_spinbox.editingFinished.connect(lambda: self.width_changed())
42 
43  self.height_label = QtWidgets.QLabel("Height:")
44  self.height_spinbox = QtWidgets.QDoubleSpinBox()
45  self.height_spinbox.editingFinished.connect(lambda: self.height_changed())
46 
47  self.color_label = QtWidgets.QLabel()
48  self.color_label.setAutoFillBackground(True)
49  self.update_color_label(None)
50  self.color_button = QtWidgets.QPushButton("Set Color")
51  self.color_button.clicked.connect(lambda: self.btn_color_clicked())
52 
53  self.layout.addWidget(self.mesh_label, 0, 0)
54  self.layout.addWidget(self.mesh_button, 0, 1)
55  self.layout.addWidget(self.diameter_label, 1, 0)
56  self.layout.addWidget(self.diameter_spinbox, 1, 1)
57  self.layout.addWidget(self.length_label, 2, 0)
58  self.layout.addWidget(self.length_spinbox, 2, 1)
59  self.layout.addWidget(self.width_label, 3, 0)
60  self.layout.addWidget(self.width_spinbox, 3, 1)
61  self.layout.addWidget(self.height_label, 4, 0)
62  self.layout.addWidget(self.height_spinbox, 4, 1)
63  self.layout.addWidget(self.color_label, 5, 0)
64  self.layout.addWidget(self.color_button, 5, 1)
65 
66  print("init")
67  self.update_widget(None)
68 
69  def get_widget(self):
70  return self.widget
71 
72  def update(self, editor, level, elements):
73 
74  if level & 2:
75  ## Check for change
76  if self.editor.active_frame is not self.old_frame:
77  self.update_widget(self.editor.active_frame)
78  self.update_values(self.editor.active_frame)
79  self.update_color_label(self.editor.active_frame)
80 
81  elif level & 4:
82  if self.editor.active_frame is not None:
83  self.update_values(self.editor.active_frame)
84  self.update_color_label(self.editor.active_frame)
85 
86  def update_widget(self, frame):
87  ## Clear layout
88  #while self.layout.count():
89  # child = self.layout.takeAt(0)
90  # child.widget().deleteLater()
91 
92  self.mesh_label.hide()
93  self.mesh_button.hide()
94  self.diameter_label.hide()
95  self.diameter_spinbox.hide()
96  self.length_label.hide()
97  self.length_spinbox.hide()
98  self.width_label.hide()
99  self.width_spinbox.hide()
100  self.height_label.hide()
101  self.height_spinbox.hide()
102 
103  if frame is None or frame.style == "none":
104  self.widget.setEnabled(False)
105  return
106 
107  if frame.style == "mesh":
108  self.mesh_label.show()
109  self.mesh_button.show()
110  elif frame.style == "sphere":
111  self.diameter_label.show()
112  self.diameter_spinbox.show()
113  else:
114  self.length_label.show()
115  self.length_spinbox.show()
116  self.width_label.show()
117  self.width_spinbox.show()
118  if frame.style == "cube":
119  self.height_label.show()
120  self.height_spinbox.show()
121 
122  self.widget.setEnabled(True)
123 
124  def update_values(self, frame):
125  if frame is None or frame.style == "none":
126  return
127 
128  if frame.style == "mesh":
129  self.mesh_label.setText(frame.path)
130  elif frame.style == "sphere":
131  self.diameter_spinbox.setValue(frame.diameter)
132  else:
133  self.length_spinbox.setValue(frame.length)
134  self.width_spinbox.setValue(frame.width)
135  if frame.style == "cube":
136  self.height_spinbox.setValue(frame.height)
137 
138  def update_color_label(self, frame):
139  if frame is None:
140  values = "{}, {}, {}, {}".format(200, 200, 200, 255)
141  else:
142  values = "{}, {}, {}, {}".format(frame.color[0]*255, frame.color[1]*255, frame.color[2]*255, frame.color[3]*255)
143  self.color_label.setStyleSheet("QLabel { background-color: rgba("+values+"); }")
144 
145  @Slot(float)
146  def diameter_changed(self):
147  if self.editor.active_frame.diameter != self.diameter_spinbox.value():
148  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "diameter", self.diameter_spinbox.value()))
149 
150  @Slot(float)
151  def length_changed(self):
152  if self.editor.active_frame.length != self.length_spinbox.value():
153  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "length", self.length_spinbox.value()))
154 
155  @Slot(float)
156  def width_changed(self):
157  if self.editor.active_frame.width != self.width_spinbox.value():
158  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "width", self.width_spinbox.value()))
159 
160  @Slot(float)
161  def height_changed(self):
162  if self.editor.active_frame.height != self.height_spinbox.value():
163  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "height", self.height_spinbox.value()))
164 
165  @Slot(bool)
167  path = QtWidgets.QFileDialog.getOpenFileName(None, 'Open Mesh', '/home', 'Mesh Files (*.stl)')[0]
168  try:
169  rospackage = rospkg.get_package_name(path)
170  if rospackage is None:
171  QtWidgets.QMessageBox.warning(self.widget, "Saving absolute path to mesh",
172  "Cannot find rospackage with selected mesh in it!\nSaving absolute path to mesh instead!")
173  #print("WARNING cannot find rospackage with mesh in it, saving absolute path")
174  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "package", ""))
175  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "path", path))
176  else:
177  rel_path = os.path.relpath(path , rospkg.RosPack().get_path(rospackage))
178  print("Saving: package: {} + relative path: {}".format(rospackage, rel_path))
179  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "package", rospackage))
180  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "path", rel_path))
181  except:
182  QtWidgets.QMessageBox.warning(self.widget, "Saving absolute path to mesh",
183  "The found rospackage with selected mesh in it is not sourced in your ROS workspace!\n"+
184  "Cannot resolve the packagepath\nSaving absolute path to mesh instead!")
185  #print("The package found is not sourced withing the current workspace, saving absolute path instead!")
186  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "package", ""))
187  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "path", path))
188 
189  @Slot(bool)
190  def btn_color_clicked(self):
191  frame = self.editor.active_frame
192  color = QtWidgets.QColorDialog.getColor(
193  QColor(frame.color[0]*255,
194  frame.color[1]*255,
195  frame.color[2]*255,
196  frame.color[3]*255),
197  None,
198  "Select Color",
199  options=QtWidgets.QColorDialog.ShowAlphaChannel)
200  self.editor.command(Command_SetStyleColor(self.editor, frame, color.getRgbF()))
201 
202 # eof
def update(self, editor, level, elements)


frame_editor
Author(s): ipa-lth , ipa-frn
autogenerated on Wed Oct 21 2020 03:36:00