Go to the documentation of this file.00001
00002
00003 from qt_gui.plugin import Plugin
00004 from qt_gui_py_common.worker_thread import WorkerThread
00005
00006 from python_qt_binding import loadUi, QtGui, QtCore
00007 from python_qt_binding.QtGui import QWidget
00008 from python_qt_binding.QtCore import Signal, Slot
00009
00010
00011 class ProjectPlugin(Plugin):
00012
00013 def __init__(self, context):
00014 super(ProjectPlugin, self).__init__(context)
00015
00016
00017 self.editor = self.create_editor()
00018 self.editor.undo_stack.cleanChanged.connect(self.clean_changed)
00019
00020
00021 self.widget = self.create_main_widget()
00022 context.add_widget(self.widget)
00023
00024
00025 self.create_menus()
00026
00027
00028 self.file_name = ""
00029 self.set_current_file("")
00030 self.load_file("")
00031
00032
00033 def create_editor(self):
00034 raise NotImplementedError
00035
00036 def create_main_widget(self):
00037 raise NotImplementedError
00038
00039 def create_menus(self):
00040
00041
00042
00043 newAction = QtGui.QAction("&New", self)
00044 newAction.setShortcuts(QtGui.QKeySequence.New)
00045 newAction.setStatusTip("Create a new file")
00046 newAction.setIcon(QtGui.QIcon.fromTheme("document-new"))
00047 newAction.triggered.connect(self.new_file)
00048
00049 openAction = QtGui.QAction("&Open", self)
00050 openAction.setShortcuts(QtGui.QKeySequence.Open)
00051 openAction.setStatusTip("Open a file")
00052 openAction.setIcon(QtGui.QIcon.fromTheme("document-open"))
00053 openAction.triggered.connect(self.open)
00054
00055 saveAction = QtGui.QAction("&Save", self)
00056 saveAction.setShortcuts(QtGui.QKeySequence.Save)
00057 saveAction.setStatusTip("Save file")
00058 saveAction.setIcon(QtGui.QIcon.fromTheme("document-save"))
00059 saveAction.triggered.connect(self.save)
00060
00061 saveAsAction = QtGui.QAction("Save_&As", self)
00062 saveAsAction.setShortcuts(QtGui.QKeySequence.SaveAs)
00063 saveAsAction.setStatusTip("Save file as...")
00064 saveAsAction.setIcon(QtGui.QIcon.fromTheme("document-save-as"))
00065 saveAsAction.triggered.connect(self.save_as)
00066
00067 undoAction = self.editor.undo_stack.createUndoAction(self, self.tr("&Undo"))
00068 undoAction.setShortcuts(QtGui.QKeySequence.Undo)
00069 undoAction.setIcon(QtGui.QIcon.fromTheme("edit-undo"))
00070 redoAction = self.editor.undo_stack.createRedoAction(self, self.tr("&Redo"))
00071 redoAction.setShortcuts(QtGui.QKeySequence.Redo)
00072 redoAction.setIcon(QtGui.QIcon.fromTheme("edit-redo"))
00073
00074
00075
00076 file_menu = self.widget.menuBar.addMenu("&File")
00077 file_menu.addAction(newAction)
00078 file_menu.addAction(openAction)
00079 file_menu.addAction(saveAction)
00080 file_menu.addAction(saveAsAction)
00081
00082 edit_menu = self.widget.menuBar.addMenu("&Edit")
00083
00084 edit_menu.addAction(undoAction)
00085 edit_menu.addAction(redoAction)
00086
00087
00088 tool_bar = self.widget.mainToolBar
00089
00090 undoButton = QtGui.QToolButton()
00091 undoButton.setDefaultAction(undoAction)
00092 redoButton = QtGui.QToolButton()
00093 redoButton.setDefaultAction(redoAction)
00094
00095 tool_bar.addAction(newAction)
00096 tool_bar.addAction(openAction)
00097 tool_bar.addAction(saveAction)
00098 tool_bar.addAction(saveAsAction)
00099 tool_bar.addSeparator()
00100 tool_bar.addWidget(undoButton)
00101 tool_bar.addWidget(redoButton)
00102
00103
00104
00105 def new_file(self):
00106 if self.ok_to_continue():
00107
00108
00109 self.load_file("")
00110
00111
00112 self.set_current_file("")
00113
00114 def open(self):
00115 if self.ok_to_continue():
00116 file_name, stuff = QtGui.QFileDialog.getOpenFileName(self.widget,
00117 "Select a file to open", ".", self.file_type)
00118
00119 if not file_name == "":
00120 self.load_file(file_name)
00121
00122 def load_file(self, file_name):
00123 if not self.editor.load_file(file_name):
00124 print "ERROR LOADING FILE"
00125 return False
00126 else:
00127 self.set_current_file(file_name)
00128 return True
00129
00130 def ok_to_continue(self):
00131 """If the file has been modified, the user is asked, whether he wants to save it first or not.
00132 Returns True when the file has been saved or was unmodified.
00133 Returns False when the user reconsiders in case of an unmodified file.
00134 """
00135 if self.widget.isWindowModified():
00136 reply = QtWidgets.QMessageBox.warning(self.widget, "frame editor",
00137 "The file has been modified.\nDo you want to save your changes?",
00138 QtGui.QMessageBox.Yes | QtGui.QMessageBox.Default,
00139 QtGui.QMessageBox.No,
00140 QtGui.QMessageBox.Cancel | QtGui.QMessageBox.Escape)
00141
00142 if reply == QtGui.QMessageBox.Yes:
00143 return self.save()
00144 elif reply == QtGui.QMessageBox.Cancel:
00145 return False
00146
00147 return True
00148
00149 def save(self):
00150 """Calls save_as or save_file
00151 """
00152 if self.file_name == "":
00153 return self.save_as()
00154 else:
00155 return self.save_file(self.file_name)
00156
00157 def save_as(self):
00158 file_name, stuff = QtGui.QFileDialog.getSaveFileName(None, "Save File", ".", self.file_type)
00159 if file_name == "":
00160 return False
00161 else:
00162 return self.save_file(file_name)
00163
00164 def save_file(self, file_name):
00165 if not self.write_file(file_name):
00166 print "Saving canceled"
00167 return False
00168 else:
00169 self.set_current_file(file_name)
00170 print "File saved"
00171 return True
00172
00173 def write_file(self, file_name):
00174 raise NotImplementedError
00175
00176 def set_current_file(self, file_name):
00177
00178 self.editor.undo_stack.setClean()
00179 self.widget.setWindowModified(False)
00180
00181
00182 self.file_name = file_name
00183
00184
00185 shown_name = "Untitled"
00186 if not self.file_name == "":
00187 shown_name = self.stripped_name(file_name)
00188
00189 self.widget.setWindowTitle(self.tr('{} [*] - {}'.format(shown_name, "frame editor")))
00190
00191 def stripped_name(self, full_name):
00192 return QtCore.QFileInfo(full_name).fileName()
00193
00194 def clean_changed(self, is_clean):
00195 self.widget.setWindowModified(not is_clean)
00196
00197
00198
00199
00200
00201 def shutdown_plugin(self):
00202
00203
00204
00205 if self.widget.isWindowModified():
00206 reply = QtGui.QMessageBox.warning(self.widget, "frame editor",
00207 "The file has been modified.\nDo you want to save your changes before exiting (Save As...)?",
00208 QtGui.QMessageBox.Yes | QtGui.QMessageBox.Default,
00209 QtGui.QMessageBox.No)
00210
00211 if reply == QtGui.QMessageBox.Yes:
00212 self.save_as()
00213
00214
00215
00216 def save_settings(self, plugin_settings, instance_settings):
00217
00218
00219 pass
00220
00221 def restore_settings(self, plugin_settings, instance_settings):
00222
00223
00224 pass
00225
00226
00227
00228
00229
00230
00231