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.size_label = QtWidgets.QLabel("Size:")
32  self.size_spinbox = QtWidgets.QDoubleSpinBox()
33  self.size_spinbox.editingFinished.connect(lambda: self.size_changed())
34  self.size_spinbox.setDecimals(4)
35  self.size_spinbox.setMinimum(0.0)
36 
37  self.diameter_label = QtWidgets.QLabel("Diameter:")
38  self.diameter_spinbox = QtWidgets.QDoubleSpinBox()
39  self.diameter_spinbox.editingFinished.connect(lambda: self.diameter_changed())
40 
41  self.length_label = QtWidgets.QLabel("Length:")
42  self.length_spinbox = QtWidgets.QDoubleSpinBox()
43  self.length_spinbox.editingFinished.connect(lambda: self.length_changed())
44 
45  self.width_label = QtWidgets.QLabel("Width:")
46  self.width_spinbox = QtWidgets.QDoubleSpinBox()
47  self.width_spinbox.editingFinished.connect(lambda: self.width_changed())
48 
49  self.height_label = QtWidgets.QLabel("Height:")
50  self.height_spinbox = QtWidgets.QDoubleSpinBox()
51  self.height_spinbox.editingFinished.connect(lambda: self.height_changed())
52 
53  self.color_label = QtWidgets.QLabel()
54  self.color_label.setAutoFillBackground(True)
55  self.update_color_label(None)
56  self.color_button = QtWidgets.QPushButton("Set Color")
57  self.color_button.clicked.connect(lambda: self.btn_color_clicked())
58 
59  self.layout.addWidget(self.mesh_label, 0, 0)
60  self.layout.addWidget(self.mesh_button, 0, 1)
61  self.layout.addWidget(self.diameter_label, 1, 0)
62  self.layout.addWidget(self.diameter_spinbox, 1, 1)
63  self.layout.addWidget(self.length_label, 2, 0)
64  self.layout.addWidget(self.length_spinbox, 2, 1)
65  self.layout.addWidget(self.width_label, 3, 0)
66  self.layout.addWidget(self.width_spinbox, 3, 1)
67  self.layout.addWidget(self.height_label, 4, 0)
68  self.layout.addWidget(self.height_spinbox, 4, 1)
69  self.layout.addWidget(self.color_label, 5, 0)
70  self.layout.addWidget(self.color_button, 5, 1)
71  self.layout.addWidget(self.size_label, 6, 0)
72  self.layout.addWidget(self.size_spinbox, 6, 1)
73 
74 
75  self.update_widget(None)
76 
77  def get_widget(self):
78  return self.widget
79 
80  def update(self, editor, level, elements):
81 
82  if level & 2:
83 
84  if self.editor.active_frame is not self.old_frame:
85  self.update_widget(self.editor.active_frame)
86  self.update_values(self.editor.active_frame)
87  self.update_color_label(self.editor.active_frame)
88 
89  elif level & 4:
90  if self.editor.active_frame is not None:
91  self.update_values(self.editor.active_frame)
92  self.update_color_label(self.editor.active_frame)
93 
94  def update_widget(self, frame):
95 
99 
100  self.mesh_label.hide()
101  self.mesh_button.hide()
102  self.diameter_label.hide()
103  self.diameter_spinbox.hide()
104  self.length_label.hide()
105  self.length_spinbox.hide()
106  self.width_label.hide()
107  self.width_spinbox.hide()
108  self.height_label.hide()
109  self.height_spinbox.hide()
110  self.size_label.hide()
111  self.size_spinbox.hide()
112 
113  if frame is None or frame.style == "none":
114  self.widget.setEnabled(False)
115  return
116 
117  if frame.style == "mesh":
118  self.mesh_label.show()
119  self.mesh_button.show()
120  self.size_label.show()
121  self.size_spinbox.show()
122 
123  elif frame.style == "sphere":
124  self.diameter_label.show()
125  self.diameter_spinbox.show()
126  else:
127  self.length_label.show()
128  self.length_spinbox.show()
129  self.width_label.show()
130  self.width_spinbox.show()
131  if frame.style == "cube":
132  self.height_label.show()
133  self.height_spinbox.show()
134 
135  self.widget.setEnabled(True)
136 
137  def update_values(self, frame):
138  if frame is None or frame.style == "none":
139  return
140 
141  if frame.style == "mesh":
142  self.mesh_label.setText(frame.path)
143  self.size_spinbox.setValue(frame.scale)
144  elif frame.style == "sphere":
145  self.diameter_spinbox.setValue(frame.diameter)
146  else:
147  self.length_spinbox.setValue(frame.length)
148  self.width_spinbox.setValue(frame.width)
149  if frame.style == "cube":
150  self.height_spinbox.setValue(frame.height)
151 
152  def update_color_label(self, frame):
153  if frame is None:
154  values = "{}, {}, {}, {}".format(200, 200, 200, 255)
155  else:
156  values = "{}, {}, {}, {}".format(frame.color[0]*255, frame.color[1]*255, frame.color[2]*255, frame.color[3]*255)
157  self.color_label.setStyleSheet("QLabel { background-color: rgba("+values+"); }")
158 
159  @Slot(float)
160  def diameter_changed(self):
161  if self.editor.active_frame.diameter != self.diameter_spinbox.value():
162  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "diameter", self.diameter_spinbox.value()))
163 
164  @Slot(float)
165  def size_changed(self):
166  if self.editor.active_frame.scale != self.diameter_spinbox.value():
167  self.editor.command(Command_SetSize(self.editor, self.editor.active_frame, self.size_spinbox.value()))
168 
169 
170  @Slot(float)
171  def length_changed(self):
172  if self.editor.active_frame.length != self.length_spinbox.value():
173  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "length", self.length_spinbox.value()))
174 
175  @Slot(float)
176  def width_changed(self):
177  if self.editor.active_frame.width != self.width_spinbox.value():
178  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "width", self.width_spinbox.value()))
179 
180  @Slot(float)
181  def height_changed(self):
182  if self.editor.active_frame.height != self.height_spinbox.value():
183  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "height", self.height_spinbox.value()))
184 
185  @Slot(bool)
187  path = QtWidgets.QFileDialog.getOpenFileName(None, 'Open Mesh', '/home', 'Mesh Files (*.stl)')[0]
188  try:
189  rospackage = rospkg.get_package_name(path)
190  if rospackage is None:
191  QtWidgets.QMessageBox.warning(self.widget, "Saving absolute path to mesh",
192  "Cannot find rospackage with selected mesh in it!\nSaving absolute path to mesh instead!")
193  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "package", ""))
194  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "path", path))
195  else:
196  rel_path = os.path.relpath(path , rospkg.RosPack().get_path(rospackage))
197  rospy.loginfo("Saving: package: {} + relative path: {}".format(rospackage, rel_path))
198  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "package", rospackage))
199  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "path", rel_path))
200  except:
201  QtWidgets.QMessageBox.warning(self.widget, "Saving absolute path to mesh",
202  "The found rospackage with selected mesh in it is not sourced in your ROS workspace!\n"+
203  "Cannot resolve the packagepath\nSaving absolute path to mesh instead!")
204  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "package", ""))
205  self.editor.command(Command_SetGeometry(self.editor, self.editor.active_frame, "path", path))
206 
207  @Slot(bool)
208  def btn_color_clicked(self):
209  frame = self.editor.active_frame
210  color = QtWidgets.QColorDialog.getColor(
211  QColor(frame.color[0]*255,
212  frame.color[1]*255,
213  frame.color[2]*255,
214  frame.color[3]*255),
215  None,
216  "Select Color",
217  options=QtWidgets.QColorDialog.ShowAlphaChannel)
218  self.editor.command(Command_SetStyleColor(self.editor, frame, color.getRgbF()))
219 
220 # eof
frame_editor.interface_gui.FrameEditor_StyleWidget.height_changed
def height_changed(self)
Definition: interface_gui.py:181
frame_editor.interface_gui.FrameEditor_StyleWidget.color_button
color_button
Definition: interface_gui.py:56
frame_editor.interface_gui.FrameEditor_StyleWidget.update_color_label
def update_color_label(self, frame)
Definition: interface_gui.py:152
frame_editor.interface_gui.FrameEditor_StyleWidget.update
def update(self, editor, level, elements)
Definition: interface_gui.py:80
frame_editor.interface_gui.FrameEditor_StyleWidget.height_spinbox
height_spinbox
Definition: interface_gui.py:50
frame_editor.interface_gui.FrameEditor_StyleWidget.width_label
width_label
Definition: interface_gui.py:45
frame_editor.interface
Definition: interface.py:1
frame_editor.interface_gui.FrameEditor_StyleWidget.mesh_button
mesh_button
Definition: interface_gui.py:28
frame_editor.interface_gui.FrameEditor_StyleWidget.color_label
color_label
Definition: interface_gui.py:53
frame_editor.interface_gui.FrameEditor_StyleWidget
Definition: interface_gui.py:14
frame_editor.interface_gui.FrameEditor_StyleWidget.length_spinbox
length_spinbox
Definition: interface_gui.py:42
frame_editor.interface_gui.FrameEditor_StyleWidget.editor
editor
Definition: interface_gui.py:17
frame_editor.commands.Command_SetGeometry
Definition: commands.py:465
frame_editor.interface_gui.FrameEditor_StyleWidget.length_label
length_label
Definition: interface_gui.py:41
frame_editor.interface_gui.FrameEditor_StyleWidget.__init__
def __init__(self, frame_editor)
Definition: interface_gui.py:16
frame_editor.interface_gui.FrameEditor_StyleWidget.btn_color_clicked
def btn_color_clicked(self)
Definition: interface_gui.py:208
frame_editor.interface_gui.FrameEditor_StyleWidget.width_changed
def width_changed(self)
Definition: interface_gui.py:176
frame_editor.interface_gui.FrameEditor_StyleWidget.diameter_spinbox
diameter_spinbox
Definition: interface_gui.py:38
frame_editor.interface_gui.FrameEditor_StyleWidget.update_values
def update_values(self, frame)
Definition: interface_gui.py:137
frame_editor.interface_gui.FrameEditor_StyleWidget.mesh_label
mesh_label
Definition: interface_gui.py:26
frame_editor.commands
Definition: commands.py:1
frame_editor.commands.Command_SetSize
Definition: commands.py:445
frame_editor.interface_gui.FrameEditor_StyleWidget.height_label
height_label
Definition: interface_gui.py:49
frame_editor.interface_gui.FrameEditor_StyleWidget.size_label
size_label
Definition: interface_gui.py:31
frame_editor.interface_gui.FrameEditor_StyleWidget.update_widget
def update_widget(self, frame)
Definition: interface_gui.py:94
frame_editor.interface_gui.FrameEditor_StyleWidget.size_changed
def size_changed(self)
Definition: interface_gui.py:165
frame_editor.interface_gui.FrameEditor_StyleWidget.btn_open_mesh_clicked
def btn_open_mesh_clicked(self)
Definition: interface_gui.py:186
frame_editor.interface_gui.FrameEditor_StyleWidget.width_spinbox
width_spinbox
Definition: interface_gui.py:46
frame_editor.interface_gui.FrameEditor_StyleWidget.old_frame
old_frame
Definition: interface_gui.py:20
frame_editor.interface_gui.FrameEditor_StyleWidget.widget
widget
Definition: interface_gui.py:23
frame_editor.interface_gui.FrameEditor_StyleWidget.get_widget
def get_widget(self)
Definition: interface_gui.py:77
frame_editor.interface_gui.FrameEditor_StyleWidget.diameter_label
diameter_label
Definition: interface_gui.py:37
frame_editor.commands.Command_SetStyleColor
Definition: commands.py:426
frame_editor.interface.Interface
Definition: interface.py:5
frame_editor.interface_gui.FrameEditor_StyleWidget.diameter_changed
def diameter_changed(self)
Definition: interface_gui.py:160
frame_editor.interface_gui.FrameEditor_StyleWidget.layout
layout
Definition: interface_gui.py:22
frame_editor.interface_gui.FrameEditor_StyleWidget.size_spinbox
size_spinbox
Definition: interface_gui.py:32
frame_editor.interface_gui.FrameEditor_StyleWidget.length_changed
def length_changed(self)
Definition: interface_gui.py:171


frame_editor
Author(s): ipa-lth , ipa-frn
autogenerated on Thu May 15 2025 02:17:25