diff libgui/graphics/Figure.cc @ 19719:7335cc071ab0

make save and saveas menu items work in Qt figure window See also bug #44177. * Figure.h, Figure.cc (Figure::fileName, Figure::setFileName, Figure::save_figure_callback): New functions. (Figure::createFigureToolBarAndMenuBar): Enable Save and "Save As" items and connect them to new fileSaveFigure and fileSaveFigureAS functions.
author John W. Eaton <jwe@octave.org>
date Mon, 09 Feb 2015 16:06:02 -0500
parents 35bca657d74d
children 31d5d251f010
line wrap: on
line diff
--- a/libgui/graphics/Figure.cc	Mon Feb 09 21:36:01 2015 +0100
+++ b/libgui/graphics/Figure.cc	Mon Feb 09 16:06:02 2015 -0500
@@ -29,6 +29,8 @@
 #include <QActionGroup>
 #include <QApplication>
 #include <QEvent>
+#include <QFileDialog>
+#include <QFileInfo>
 #include <QFrame>
 #include <QMainWindow>
 #include <QMenu>
@@ -45,6 +47,10 @@
 #include "MouseModeActionGroup.h"
 #include "QtHandlesUtils.h"
 
+#include "octave-qt-link.h"
+
+#include "builtin-defun-decls.h"
+
 namespace QtHandles
 {
 
@@ -210,6 +216,26 @@
     return NoMode;
 }
 
+QString Figure::fileName (void)
+{
+  gh_manager::auto_lock lock;
+
+  const figure::properties& fp = properties<figure> ();
+
+  std::string name = fp.get_filename ();
+
+  return QString::fromStdString (name);
+}
+
+void Figure::setFileName (const QString& name)
+{
+  gh_manager::auto_lock lock;
+
+  figure::properties& fp = properties<figure> ();
+
+  fp.set_filename (name.toStdString ());
+}
+
 MouseMode Figure::mouseMode (void)
 {
   gh_manager::auto_lock lock;
@@ -250,8 +276,8 @@
   fileMenu->addAction (tr ("&New Figure"), this, SLOT (fileNewFigure (void)));
   fileMenu->addAction (tr ("&Open..."))->setEnabled (false);
   fileMenu->addSeparator ();
-  fileMenu->addAction (tr ("&Save"))->setEnabled (false);
-  fileMenu->addAction (tr ("Save &As"))->setEnabled (false);
+  fileMenu->addAction (tr ("&Save"), this, SLOT (fileSaveFigure (bool)));
+  fileMenu->addAction (tr ("Save &As"), this, SLOT (fileSaveFigureAs (void)));
   fileMenu->addSeparator ();
   fileMenu->addAction (tr ("&Close Figure"), this,
                        SLOT (fileCloseFigure (void)), Qt::CTRL|Qt::Key_W);
@@ -699,6 +725,48 @@
 {
 }
 
+void Figure::fileSaveFigure (bool prompt)
+{
+  QString file = fileName ();
+
+  if (file.isEmpty ())
+    {
+      prompt = true;
+
+      file = "untitled.eps";
+    }
+
+  if (prompt || file.isEmpty ())
+    {
+      QFileInfo finfo (file);
+
+      file = QFileDialog::getSaveFileName (qWidget<FigureWindow> (),
+                                           tr ("Save Figure As"),
+                                           finfo.absoluteFilePath (), 0, 0,
+                                           QFileDialog::DontUseNativeDialog);
+    }
+
+  if (! file.isEmpty ())
+    {
+      QFileInfo finfo (file);
+
+      setFileName (finfo.absoluteFilePath ());
+
+      octave_link::post_event (this, &Figure::save_figure_callback,
+                               file.toStdString ());
+    }
+}
+
+void Figure::save_figure_callback (const std::string& file)
+{
+  Ffeval (ovl ("print", file));
+}
+  
+void Figure::fileSaveFigureAs (void)
+{
+  fileSaveFigure (true);
+}
+
 void Figure::fileCloseFigure (void)
 {
   qWidget<QMainWindow> ()->close ();