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

Nicolas Brisset nicolas.brisset at eurocopter.com
Wed Mar 7 23:47:59 UTC 2012


SVN commit 1284152 by brisset:

Add drag&drop feature:
- if dropping a .kst file, open it after checking what needed to be saved has been saved
- if dropping something else, open it in the datawizard

The prompt to save feature in case of change has been refactored along the way to make it more readable code-wise. I hope I haven't broken anything.
The reason why our previous attempt had not been conclusive is that we have to setAcceptDrops(false) all the way down the parent chain: _tabWidget *and* View.

Note: my tests exposed a bug: adding view items is not registered as a change. This should be fixed!

FEATURE: 272510


 M  +45 -19    mainwindow.cpp  
 M  +4 -1      mainwindow.h  
 M  +2 -0      scene.cpp  
 M  +2 -0      view.cpp  


--- branches/work/kst/portto4/kst/src/libkstapp/mainwindow.cpp #1284151:1284152
@@ -105,6 +105,7 @@
   _tabWidget->createView();
 
   setCentralWidget(_tabWidget);
+  _tabWidget->setAcceptDrops(false); // Force drops to be passed to parent
   connect(_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(currentViewChanged()));
   connect(_tabWidget, SIGNAL(currentViewModeChanged()), this, SLOT(currentViewModeChanged()));
   connect(PlotItemManager::self(), SIGNAL(tiedZoomRemoved()), this, SLOT(tiedZoomRemoved()));
@@ -116,6 +117,7 @@
   QTimer::singleShot(0, this, SLOT(performHeavyStartupActions()));
 
   updateRecentKstFiles();
+  setAcceptDrops(true);
 }
 
 
@@ -194,8 +196,12 @@
 }
 
 
-bool MainWindow::promptSave() {
-  int rc = QMessageBox::warning(this, tr("Kst: Save Promp"), tr("Your document has been modified.\nSave changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
+bool MainWindow::promptSaveDone() {
+  if (! _doc->isChanged()) {
+    return true; // No need to ask if there is no unsaved change -> we're done
+  }
+  else { // Changes registered: ask the user
+    int rc = QMessageBox::warning(this, tr("Kst: Save Prompt"), tr("Your document has been modified.\nSave changes?"), QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
   if (rc == QMessageBox::Save) {
     save();
   } else if (rc == QMessageBox::Cancel) {
@@ -203,10 +209,11 @@
   }
   return true;
 }
+}
 
 
 void MainWindow::closeEvent(QCloseEvent *e) {
-  if (_doc->isChanged() && !promptSave()) {
+  if (!promptSaveDone()) {
     e->ignore();
     return;
   }
@@ -258,11 +265,8 @@
   bool clearApproved = false;
   if (force) {
     clearApproved = true;
-  } else if (_doc->isChanged()) {
-    clearApproved = promptSave();
   } else {
-    int rc = QMessageBox::warning(this, tr("Kst"), tr("Delete everything?"), QMessageBox::Ok, QMessageBox::Cancel);
-    clearApproved = (rc == QMessageBox::Ok);
+    clearApproved = promptSaveDone();
   }
 
   if (clearApproved) {
@@ -271,16 +275,17 @@
     delete _doc;
     _doc = new Document(this);
     _scriptServer->setStore(_doc->objectStore());
+    tabWidget()->clear();
+    tabWidget()->createView();
+    return;
   } else {
     return;
   }
 
-  tabWidget()->clear();
-  tabWidget()->createView();
 }
 
 void MainWindow::open() {
-  if (_doc->isChanged() && !promptSave()) {
+  if (!promptSaveDone()) {
     return;
   }
   QSettings settings("Kst2");
@@ -425,12 +430,7 @@
   QDir::setCurrent(file.left(file.lastIndexOf('/')));
 
   QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
-  delete _dataManager;
-  _dataManager = 0;
-  delete _doc;
-  _doc = new Document(this);
-  _scriptServer->setStore(_doc->objectStore());
-
+  newDoc(true); // Does all the init stuff, but does not ask for override as it's supposed to be done elsewhere
   bool ok = _doc->open(file);
   QApplication::restoreOverrideCursor();
 
@@ -438,9 +438,6 @@
     QMessageBox::critical(this, tr("Kst"),
         tr("Error opening document '%1':\n%2\n"
            "Maybe it is a Kst 1 file which could not be read by Kst 2.").arg(file, _doc->lastError()));
-    delete _doc;
-    _doc = new Document(this);
-    _scriptServer->setStore(_doc->objectStore());
   }
 
   setWindowTitle("Kst - " + file);
@@ -1835,7 +1832,12 @@
   dataWizard->show();
 }
 
+void MainWindow::showDataWizard(const QString &dataFile) {
+  DataWizard *dataWizard = new DataWizard(this, dataFile);
+  dataWizard->show();
+}
 
+
 void MainWindow::openRecentDataFile()
 {
   QAction *action = qobject_cast<QAction *>(sender());
@@ -1909,6 +1911,30 @@
   }
 }
 
+void MainWindow::dragEnterEvent(QDragEnterEvent *event)
+{
+     if (event->mimeData()->hasUrls()) {
+       event->acceptProposedAction();
 }
+}
 
+
+void MainWindow::dropEvent(QDropEvent *event)
+{
+  QString path = event->mimeData()->urls().first().toLocalFile();
+  if (path.endsWith(QString(".kst"))) {
+     if (!promptSaveDone()) { // There are things to save => cancel
+       event->ignore();
+       return;
+     }
+     openFile(path);
+   }
+   else {
+     showDataWizard(path); // This is not destructive: it only add data, no need to ask for confirmation
+   }
+   event->accept();
+}
+
+}
+
 // vim: ts=2 sw=2 et
--- branches/work/kst/portto4/kst/src/libkstapp/mainwindow.h #1284151:1284152
@@ -79,6 +79,7 @@
     void showChangeDataSampleDialog();
     void showChangeFileDialog();
     void showDataWizard();
+    void showDataWizard(const QString &dataFile);
     void showBugReportWizard();
     void showPluginDialog(QString &pluginName);
 
@@ -166,11 +167,13 @@
 
     void readSettings();
     void writeSettings();
-    bool promptSave();
+    bool promptSaveDone();
 
     QAction* createRecentFileAction(const QString& filename, int idx, const QString& text, const char* openslot);
     void updateRecentFiles(const QString& key, QMenu *menu, QList<QAction*>& actions, QMenu* submenu, const QString& newfilename, const char* openslot);
     
+    void dragEnterEvent(QDragEnterEvent *event);
+    void dropEvent(QDropEvent *event);
 
   private:
     Document *_doc;
--- branches/work/kst/portto4/kst/src/libkstapp/scene.cpp #1284151:1284152
@@ -101,6 +101,8 @@
     //            " drop hot spot" << m->item->dropHotSpot << " topleft:" << m->item->rect().topLeft();
     m->item->moveTo(viewpos - m->item->rect().center() - m->item->dropHotSpot.toPoint());
     event->acceptProposedAction();
+  } else {
+    event->ignore();
   }
 }
 
--- branches/work/kst/portto4/kst/src/libkstapp/view.cpp #1284151:1284152
@@ -110,6 +110,8 @@
   connect(this, SIGNAL(viewModeChanged(View::ViewMode)), PlotItemManager::self(), SLOT(clearFocusedPlots()));
 
   applyDialogDefaultsFill();
+
+  setAcceptDrops(false); // Pass event to parent
 }
 
 


More information about the Kst mailing list