Commits

Georg Brandl committed 90e232b

gui: add better load/save handling with current filename

Comments (0)

Files changed (2)

 
 """Main window for the standalone GUI."""
 
+import sys
+from os import path
 import cPickle as pickle
 
 from PyQt4.QtCore import pyqtSignature as qtsig, SIGNAL, QModelIndex
         self._loading = False
         self.current_panel = None
         self.panels = []
+        self.pristine = True  # nothing loaded so far
+        self.filename = None
 
         loadUi(self, 'main.ui')
 
               data.data_title,
               data.environment,
               '<br>'.join(data.sources)), panel))
-        self.datalistmodel.reset()
+        self.pristine = False
         if not self._loading:
+            self.datalistmodel.reset()
             self.datalist.setCurrentIndex(
                 self.datalistmodel.index(len(self.panels)-1, 0))
 
         self.canvas.plotter.lines = on
         # XXX replot
 
+    def check_save(self):
+        if self.pristine:  # nothing there to be saved
+            return True
+        resp = QMessageBox.question(self, 'ufit', 'Save current session?',
+            QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
+        if resp == QMessageBox.Yes:
+            return self.save_session()
+        elif resp == QMessageBox.No:
+            return True
+        return False
+
     @qtsig('')
     def on_actionLoad_triggered(self):
+        if not self.check_save():
+            return
+        if self.filename:
+            initialdir = path.dirname(self.filename)
+        else:
+            initialdir = ''
         filename = QFileDialog.getOpenFileName(
-            self, 'Select file name', '', 'ufit files (*.ufit)')
+            self, 'Select file name', initialdir, 'ufit files (*.ufit)')
         if filename == '':
             return
-        self.load_session(str(filename))
+        self.filename = unicode(filename).encode(sys.getfilesystemencoding())
+        try:
+            self.load_session(self.filename)
+        except Exception, err:
+            QMessageBox.warning(self, 'Error', 'Loading failed: %s' % err)
+        else:
+            self.setWindowTitle('ufit - %s' % self.filename)
 
     def load_session(self, filename):
         for panel in self.panels[1:]:
                 self.handle_new_data(data, model)
         finally:
             self._loading = False
+        self.datalistmodel.reset()
         self.datalist.setCurrentIndex(
             self.datalistmodel.index(len(self.panels)-1, 0))
 
     @qtsig('')
     def on_actionSave_triggered(self):
-        # XXX track self filename
-        self.on_actionSaveAs_triggered()
+        self.save_session()
 
     @qtsig('')
     def on_actionSaveAs_triggered(self):
+        self.save_session_as()
+
+    def save_session(self):
+        if self.filename is None:
+            return self.save_session_as()
+        try:
+            self.save_session_inner(self.filename)
+        except Exception, err:
+            QMessageBox.warning(self, 'Error', 'Saving failed: %s' % err)
+            return False
+        return True
+
+    def save_session_as(self):
+        if self.filename:
+            initialdir = path.dirname(self.filename)
+        else:
+            initialdir = ''
         filename = QFileDialog.getSaveFileName(
-            self, 'Select file name', '', 'ufit files (*.ufit)')
+            self, 'Select file name', initialdir, 'ufit files (*.ufit)')
         if filename == '':
-            return
-        self.save_session(str(filename))
+            return False
+        self.filename = unicode(filename).encode(sys.getfilesystemencoding())
+        try:
+            self.save_session_inner(self.filename)
+        except Exception, err:
+            QMessageBox.warning(self, 'Error', 'Saving failed: %s' % err)
+            return False
+        else:
+            self.setWindowTitle('ufit - %s' % self.filename)
+            return True
 
-    def save_session(self, filename):
+    def save_session_inner(self, filename):
         fp = open(filename, 'wb')
         info = {
             'panels': [(panel[1].data, panel[1].model) for panel in self.panels]
         pickle.dump(info, fp, protocol=pickle.HIGHEST_PROTOCOL)
 
     @qtsig('')
+    def on_actionRemoveData_triggered(self):
+        pass
+
+    @qtsig('')
+    def on_actionMergeData_triggered(self):
+        pass
+
+    @qtsig('')
+    def on_actionQuit_triggered(self):
+        self.close()
+
+    def closeEvent(self, event):
+        if not self.check_save():
+            event.ignore()
+        else:
+            event.accept()
+
+    @qtsig('')
     def on_actionAbout_triggered(self):
         QMessageBox.information(self, 'About',
                                 'ufit, written by Georg Brandl 2013.')
 
-    @qtsig('')
-    def on_actionQuit_triggered(self):
-        # XXX ask for saving
-        self.close()
-
 
 def main(args):
     import time
    </widget>
    <widget class="QMenu" name="menuHelp">
     <property name="title">
-     <string>Help</string>
+     <string>&amp;Help</string>
     </property>
     <addaction name="actionAbout"/>
    </widget>
    <widget class="QMenu" name="menuView">
     <property name="title">
-     <string>View</string>
+     <string>&amp;Data</string>
     </property>
+    <addaction name="actionRemoveData"/>
+    <addaction name="actionMergeData"/>
+    <addaction name="separator"/>
     <addaction name="actionConnectData"/>
    </widget>
    <addaction name="menuFile"/>
+   <addaction name="menuView"/>
    <addaction name="menuHelp"/>
-   <addaction name="menuView"/>
   </widget>
   <action name="actionLoad">
    <property name="text">
     <string>&amp;Load session...</string>
    </property>
+   <property name="shortcut">
+    <string>Ctrl+O</string>
+   </property>
   </action>
   <action name="actionSave">
    <property name="text">
     <string>&amp;Save session</string>
    </property>
+   <property name="shortcut">
+    <string>Ctrl+S</string>
+   </property>
   </action>
   <action name="actionAbout">
    <property name="text">
    <property name="text">
     <string>&amp;Quit</string>
    </property>
+   <property name="shortcut">
+    <string>Ctrl+Q</string>
+   </property>
   </action>
   <action name="actionSaveAs">
    <property name="text">
     <string>Save session as...</string>
    </property>
+   <property name="shortcut">
+    <string>Ctrl+Shift+S</string>
+   </property>
   </action>
   <action name="actionLoadData">
    <property name="text">
     <string>Load data</string>
    </property>
+   <property name="shortcut">
+    <string>Ctrl+L</string>
+   </property>
   </action>
   <action name="actionConnectData">
    <property name="checkable">
     <string>Connect datapoints</string>
    </property>
   </action>
+  <action name="actionRemoveData">
+   <property name="text">
+    <string>Remove selected dataset(s)</string>
+   </property>
+  </action>
+  <action name="actionMergeData">
+   <property name="text">
+    <string>Merge selected datasets</string>
+   </property>
+  </action>
  </widget>
  <customwidgets>
   <customwidget>