project_plugin.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 from qt_gui.plugin import Plugin
4 
5 from python_qt_binding import QtWidgets, QtCore, QtGui
6 import os
7 import rospy
8 
10 
11  def __init__(self, context):
12  super(ProjectPlugin, self).__init__(context)
13 
14 
15  self.editor = self.create_editor(context)
16  self.editor.undo_stack.cleanChanged.connect(self.clean_changed)
17 
18 
19  self.widget = self.create_main_widget()
20  context.add_widget(self.widget)
21 
22 
23  self.create_menus()
24 
25 
26  self.settings = QtCore.QSettings('rqt_frame_editor', 'frame_editor')
27 
28 
29  self.load_file("") # loads empty.xml
31 
32  def create_editor(self, context):
33  raise NotImplementedError
34 
35  def create_main_widget(self):
36  raise NotImplementedError
37 
38  def create_menus(self):
39 
40 
42  newAction = QtWidgets.QAction("&New", self)
43  newAction.setShortcuts(QtGui.QKeySequence.New)
44  newAction.setStatusTip("Create a new file")
45  newAction.setIcon(QtGui.QIcon.fromTheme("document-new"))
46  newAction.triggered.connect(self.new_file)
47 
48  openAction = QtWidgets.QAction("&Open", self)
49  openAction.setShortcuts(QtGui.QKeySequence.Open)
50  openAction.setStatusTip("Open a file")
51  openAction.setIcon(QtGui.QIcon.fromTheme("document-open"))
52  openAction.triggered.connect(self.open)
53 
54  saveAction = QtWidgets.QAction("&Save", self)
55  saveAction.setShortcuts(QtGui.QKeySequence.Save)
56  saveAction.setStatusTip("Save file")
57  saveAction.setIcon(QtGui.QIcon.fromTheme("document-save"))
58  saveAction.triggered.connect(self.save)
59 
60  saveAsAction = QtWidgets.QAction("Save_&As", self)
61  saveAsAction.setShortcuts(QtGui.QKeySequence.SaveAs)
62  saveAsAction.setStatusTip("Save file as...")
63  saveAsAction.setIcon(QtGui.QIcon.fromTheme("document-save-as"))
64  saveAsAction.triggered.connect(self.save_as)
65 
66  undoAction = self.editor.undo_stack.createUndoAction(self, self.tr("&Undo"))
67  undoAction.setShortcuts(QtGui.QKeySequence.Undo)
68  undoAction.setIcon(QtGui.QIcon.fromTheme("edit-undo"))
69  redoAction = self.editor.undo_stack.createRedoAction(self, self.tr("&Redo"))
70  redoAction.setShortcuts(QtGui.QKeySequence.Redo)
71  redoAction.setIcon(QtGui.QIcon.fromTheme("edit-redo"))
72 
73 
74 
85 
86 
87  tool_bar = self.widget.mainToolBar
88 
89  undoButton = QtWidgets.QToolButton()
90  undoButton.setDefaultAction(undoAction)
91  redoButton = QtWidgets.QToolButton()
92  redoButton.setDefaultAction(redoAction)
93 
94  tool_bar.addAction(newAction)
95  tool_bar.addAction(openAction)
96  tool_bar.addAction(saveAction)
97  tool_bar.addAction(saveAsAction)
98  tool_bar.addSeparator()
99  tool_bar.addWidget(undoButton)
100  tool_bar.addWidget(redoButton)
101  #tool_bar.addSeparator()
102 
103 
104  def new_file(self):
105  if self.ok_to_continue():
106 
108  self.load_file("")
109 
110 
112 
113  def open(self):
114  if self.ok_to_continue():
115 
116  file_name, stuff = QtWidgets.QFileDialog.getOpenFileName(self.widget,
117  "Select a file to open", self.settings.value('last_folder', ''), self.file_type)
118 
119  if not file_name == "":
120  if self.editor.get_file_name() == "":
121  self.load_file(file_name)
122  else:
123  # Already some file loaded
124  # Ask to add or replace
125  rospy.logwarn("current filename '{}'".format(self.editor.get_file_name()))
126  choice = QtWidgets.QMessageBox.question(self.widget,
127  "Keep current frames?",
128  "Do you want to keep frames in your list, which are not in the currently loaded file?",
129  QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.Yes)
130  if choice == QtWidgets.QMessageBox.Yes:
131  self.load_file(file_name)
132  # trigger asterisk (content actually changed)
133  self.clean_changed(False)
134  else:
135  self.load_file("")
136  self.load_file(file_name)
137 
138 
139  def load_file(self, file_name):
140  if not self.editor.load_file(file_name):
141  rospy.logerr("ERROR LOADING FILE")
142  return False
143  else:
145 
146  if file_name:
147  self.settings.setValue('last_folder', os.path.dirname(file_name))
148  self.settings.sync()
149  return True
150 
151  def ok_to_continue(self):
152  """If the file has been modified, the user is asked, whether he wants to save it first or not.
153  Returns True when the file has been saved or was unmodified.
154  Returns False when the user reconsiders in case of an unmodified file.
155  """
156  if self.widget.isWindowModified():
157  reply = QtWidgets.QMessageBox.warning(self.widget, "frame editor",
158  "The file has been modified.\nDo you want to save your changes?",
159  QtWidgets.QMessageBox.Yes |
160  QtWidgets.QMessageBox.No |
161  QtWidgets.QMessageBox.Cancel |
162  QtWidgets.QMessageBox.Escape,
163  QtWidgets.QMessageBox.Yes)
164 
165  if reply == QtWidgets.QMessageBox.Yes:
166  return self.save()
167  elif reply == QtWidgets.QMessageBox.Cancel:
168  return False
169 
170  return True
171 
172  def save(self):
173  """Calls save_as or save_file
174  """
175  if self.editor.get_file_name() == "":
176  return self.save_as()
177  else:
178  return self.save_file(self.editor.get_full_file_path())
179 
180  def save_as(self):
181  #file_path = QtCore.QFileInfo(self.editor.get_file_name()).canonicalPath()
182  #file_name, stuff = QtWidgets.QFileDialog.getSaveFileName(None, "Save File", file_path, self.file_type)
183  file_name, stuff = QtWidgets.QFileDialog.getSaveFileName(None, "Save File", self.editor.get_full_file_path(), self.file_type)
184  if file_name == "":
185  return False
186  else:
187  if not file_name.endswith(".yaml"):
188  file_name += ".yaml"
189  return self.save_file(file_name)
190 
191  def save_file(self, file_name):
192  if not self.write_file(file_name):
193  rospy.logwarn("Saving canceled")
194  return False
195  else:
197  rospy.loginfo("File saved")
198  return True
199 
200  def write_file(self, file_name):
201  raise NotImplementedError
202 
203  def get_shown_name(self):
204  file_name = self.editor.get_file_name()
205 
206 
207  shown_name = "Untitled"
208  if not file_name == "":
209  shown_name = file_name
210 
211  return shown_name
212 
214 
215  self.editor.undo_stack.setClean()
216  self.widget.setWindowModified(False)
217 
218 
219  shown_name = self.get_shown_name()
220 
221  self.widget.lab_file_name.setText(self.tr('{} - {}'.format(shown_name, "frame editor")))
222 
223  def stripped_name(self, full_name):
224  return QtCore.QFileInfo(full_name).fileName()
225 
226  def clean_changed(self, is_clean):
227  self.widget.setWindowModified(not is_clean)
228 
229  # set file name label
230  modified_identifier = "*" if self.widget.isWindowModified else ""
231  shown_name = self.get_shown_name()
232  self.widget.lab_file_name.setText(self.tr('{}{} - {}'.format(shown_name, modified_identifier, "frame editor")))
233 
234 
235 
236 
238  def shutdown_plugin(self):
239  #self._update_thread.kill()
240 
241 
242  if self.widget.isWindowModified():
243  if self.editor.get_file_name() != "":
244  autosave_path = self.editor.get_full_file_path()+".autosave"
245  self.save_file(autosave_path)
246  autosaved = True
247  reply = QtWidgets.QMessageBox.warning(self.widget, "frame editor",
248  "The file has been modified.\nDo you want to save your changes before exiting (Save As...)?",
249  QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.Default,
250  QtWidgets.QMessageBox.No)
251 
252  if reply == QtWidgets.QMessageBox.Yes:
253  self.save_as()
254  if autosaved:
255  os.remove(autosave_path)
256  # unregister interfaces
257 
258 
259  def save_settings(self, plugin_settings, instance_settings):
260  # TODO save intrinsic configuration, usually using:
261  # instance_settings.set_value(k, v)
262  pass
263 
264  def restore_settings(self, plugin_settings, instance_settings):
265  # TODO restore intrinsic configuration, usually using:
266  # v = instance_settings.value(k)
267  pass
268 
269  #def trigger_configuration(self):
270  # Comment in to signal that the plugin has a way to configure
271  # This will enable a setting button (gear icon) in each dock widget title bar
272  # Usually used to open a modal configuration dialog
273 
274 # eof
frame_editor.project_plugin.ProjectPlugin.__init__
def __init__(self, context)
Definition: project_plugin.py:11
frame_editor.project_plugin.ProjectPlugin.create_menus
def create_menus(self)
Definition: project_plugin.py:38
qt_gui::plugin::Plugin
frame_editor.project_plugin.ProjectPlugin.create_main_widget
def create_main_widget(self)
Definition: project_plugin.py:35
frame_editor.project_plugin.ProjectPlugin.update_current_filename
def update_current_filename(self)
Definition: project_plugin.py:213
frame_editor.project_plugin.ProjectPlugin.save_as
def save_as(self)
Definition: project_plugin.py:180
frame_editor.project_plugin.ProjectPlugin.create_editor
def create_editor(self, context)
Definition: project_plugin.py:32
frame_editor.project_plugin.ProjectPlugin.save
def save(self)
Definition: project_plugin.py:172
frame_editor.project_plugin.ProjectPlugin.new_file
def new_file(self)
Definition: project_plugin.py:104
frame_editor.project_plugin.ProjectPlugin.editor
editor
Editor.
Definition: project_plugin.py:15
frame_editor.project_plugin.ProjectPlugin
Definition: project_plugin.py:9
frame_editor.project_plugin.ProjectPlugin.widget
widget
Main widget.
Definition: project_plugin.py:19
frame_editor.project_plugin.ProjectPlugin.clean_changed
def clean_changed(self, is_clean)
Definition: project_plugin.py:226
frame_editor.project_plugin.ProjectPlugin.load_file
def load_file(self, file_name)
Definition: project_plugin.py:139
frame_editor.project_plugin.ProjectPlugin.ok_to_continue
def ok_to_continue(self)
Definition: project_plugin.py:151
frame_editor.project_plugin.ProjectPlugin.shutdown_plugin
def shutdown_plugin(self)
PLUGIN ##.
Definition: project_plugin.py:238
frame_editor.project_plugin.ProjectPlugin.settings
settings
Menus.
Definition: project_plugin.py:26
frame_editor.project_plugin.ProjectPlugin.save_settings
def save_settings(self, plugin_settings, instance_settings)
Definition: project_plugin.py:259
frame_editor.project_plugin.ProjectPlugin.get_shown_name
def get_shown_name(self)
Definition: project_plugin.py:203
qt_gui::plugin
frame_editor.project_plugin.ProjectPlugin.open
def open(self)
Definition: project_plugin.py:113
frame_editor.project_plugin.ProjectPlugin.write_file
def write_file(self, file_name)
Definition: project_plugin.py:200
frame_editor.project_plugin.ProjectPlugin.stripped_name
def stripped_name(self, full_name)
Definition: project_plugin.py:223
frame_editor.project_plugin.ProjectPlugin.save_file
def save_file(self, file_name)
Definition: project_plugin.py:191
frame_editor.project_plugin.ProjectPlugin.restore_settings
def restore_settings(self, plugin_settings, instance_settings)
Definition: project_plugin.py:264


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