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


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