changeset 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 2b93834e5ede
children 3db04b75c7c0
files libgui/graphics/Figure.cc libgui/graphics/Figure.h libgui/graphics/module.mk
diffstat 3 files changed, 78 insertions(+), 2 deletions(-) [+]
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 ();
--- a/libgui/graphics/Figure.h	Mon Feb 09 21:36:01 2015 +0100
+++ b/libgui/graphics/Figure.h	Mon Feb 09 16:06:02 2015 -0500
@@ -70,6 +70,9 @@
 
   static Figure* create (const graphics_object& go);
 
+  QString fileName (void);
+  void setFileName (const QString& name);
+
   MouseMode mouseMode (void);
 
   Container* innerContainer (void);
@@ -104,9 +107,13 @@
 
   static void updateBoundingBoxHelper (void*);
 
+  void save_figure_callback (const std::string& file);
+
 private slots:
   void setMouseMode (MouseMode mode);
   void fileNewFigure (void);
+  void fileSaveFigure (bool prompt = false);
+  void fileSaveFigureAs (void);
   void fileCloseFigure (void);
   void editCopy (void);
   void editCut (void);
--- a/libgui/graphics/module.mk	Mon Feb 09 21:36:01 2015 +0100
+++ b/libgui/graphics/module.mk	Mon Feb 09 16:06:02 2015 -0500
@@ -116,6 +116,7 @@
   @OCTGUI_DLL_DEFS@ \
   @QT_CPPFLAGS@ \
   -Igraphics -I$(srcdir)/graphics \
+  -Isrc -I$(srcdir)/src \
   -I$(top_srcdir)/liboctave/cruft/misc \
   -I$(top_srcdir)/liboctave/array \
   -I$(top_builddir)/liboctave/numeric -I$(top_srcdir)/liboctave/numeric \