[Kst] branches/work/kst/portto4/kst/src/libkstapp

Adam Treat treat at kde.org
Mon Jul 16 20:20:42 CEST 2007


SVN commit 688735 by treat:

* A little saving/loading workup.


 M  +18 -6     document.cpp  
 M  +3 -0      document.h  
 M  +14 -7     mainwindow.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/document.cpp #688734:688735
@@ -24,12 +24,13 @@
 
 #include <QDebug>
 #include <QFile>
+#include <QMessageBox>
 #include <QXmlStreamReader>
 
 namespace Kst {
 
 Document::Document(MainWindow *window)
-: _win(window), _session(new SessionModel), _dirty(false), _isOpen(false) {
+: _win(window), _session(new SessionModel), _dirty(false), _isOpen(false), _fileName(QString::null) {
 }
 
 
@@ -44,21 +45,28 @@
 }
 
 
+QString Document::fileName() const {
+  return _fileName;
+}
+
+
 bool Document::save(const QString& to) {
   // TODO:
   // - KSaveFile-ish behavior
   // - only save if changed
-  // - prompt overwrite
   // - only setChanged(false) if save was successful
   setChanged(false);
 
-  QString fn = to;
+  QString fn = !to.isEmpty() ? to : _fileName;
   QFile f(fn);
   if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
-    // QMessageBox::critical
+    QMessageBox::critical(_win, QObject::tr("Kst: Critical"),
+                          QObject::tr("File '%1' could not be opened for writing.").arg(fn));
     return false;
   }
 
+  _fileName = fn;
+
   QXmlStreamWriter xml;
   xml.setDevice(&f);
   xml.setAutoFormatting(true);
@@ -119,10 +127,13 @@
   _isOpen = false;
   QFile f(file);
   if (!f.open(QIODevice::ReadOnly)) {
-    // QMessageBox::critical
+    QMessageBox::critical(_win, QObject::tr("Kst: Critical"),
+                          QObject::tr("File '%1' could not be opened for reading.").arg(file));
     return false;
   }
 
+  _fileName = file;
+
   // If we move this into the <graphics> block then we could, if desired, open
   // .kst files that contained only data and basically "merge" that data into
   // the current session
@@ -230,7 +241,8 @@
 #undef malformed
 
   if (xml.hasError()) {
-    // QMessageBox::critical
+    QMessageBox::critical(_win, QObject::tr("Kst: Critical"),
+                          QObject::tr("File '%1' is malformed and encountered an error when opening.").arg(file));
     return false;
   }
 
--- branches/work/kst/portto4/kst/src/libkstapp/document.h #688734:688735
@@ -27,6 +27,8 @@
 
     SessionModel* session() const;
 
+    QString fileName() const;
+
     bool open(const QString& file);
     bool save(const QString& to = QString::null);
 
@@ -41,6 +43,7 @@
     SessionModel *_session;
     bool _dirty;
     bool _isOpen;
+    QString _fileName;
 };
 
 }
--- branches/work/kst/portto4/kst/src/libkstapp/mainwindow.cpp #688734:688735
@@ -82,10 +82,11 @@
 
 void MainWindow::cleanup() {
   KST::dataObjectList.lock().writeLock();
-  KstDataObjectList dol = KST::dataObjectList;
+  KstDataObjectList dol = KST::dataObjectList; //FIXME What is going on here?
   KST::dataObjectList.clear();
   KST::dataObjectList.lock().unlock();
-  dol.clear();
+  dol.clear(); //and here?
+
   KST::dataSourceList.lock().writeLock();
   KST::dataSourceList.clear();
   KST::dataSourceList.lock().unlock();
@@ -142,14 +143,18 @@
 void MainWindow::save() {
   if (_doc->isOpen()) {
     _doc->save();
+  } else {
+    saveAs();
   }
 }
 
 
 void MainWindow::saveAs() {
-  if (_doc->isOpen()) {
-    _doc->save();
+  QString fn = QFileDialog::getSaveFileName(this, tr("Kst: Save File"), _doc->fileName(), tr("Kst Sessions (*.kst)"));
+  if (fn.isEmpty()) {
+    return;
   }
+  _doc->save(fn);
 }
 
 
@@ -157,10 +162,12 @@
   if (_doc->isChanged() && !promptSave()) {
     return;
   }
-  QString fn = QFileDialog::getOpenFileName(this, tr("Kst: Open File"), QString(), tr("Kst Sessions (*.kst)"));
-  if (!fn.isEmpty()) {
-    delete _doc;
+  QString fn = QFileDialog::getOpenFileName(this, tr("Kst: Open File"), _doc->fileName(), tr("Kst Sessions (*.kst)"));
+  if (fn.isEmpty()) {
+    return;
   }
+
+  delete _doc;
   _doc = new Document(this);
   bool ok = _doc->open(fn);
   if (!ok) {


More information about the Kst mailing list