# HG changeset patch
# User Oleksii Kizitskyi <oleksii.kizitskyi@ukr.net>
# Date 1529793622 -10800
#      Sun Jun 24 01:40:22 2018 +0300
# Node ID 9c379a3aececc0dcbba4f7120ccd90e0e2f0cf25
# Parent  b7db401e1a9976e77591b0608dd6932758d10894
added print and print preview functions (qt print functionality)  to the figure window

* Figure.cc, Figure.h added print, print preview methods (that use qt print functionality) and ui elements to call them.

diff -r b7db401e1a99 -r 9c379a3aecec libgui/graphics/Figure.cc
--- a/libgui/graphics/Figure.cc	Tue Jun 19 09:18:44 2018 -0700
+++ b/libgui/graphics/Figure.cc	Sun Jun 24 01:40:22 2018 +0300
@@ -41,6 +41,9 @@ along with Octave; see the file COPYING.
 #include <QtDebug>
 #include <QTimer>
 #include <QToolBar>
+#include <QPainter>
+#include <QPrintDialog>
+#include <QPrintPreviewDialog>
 
 #include "Canvas.h"
 #include "Container.h"
@@ -126,7 +129,7 @@ namespace QtHandles
   Figure::Figure (const graphics_object& go, FigureWindow *win)
     : Object (go, win), m_blockUpdates (false), m_figureToolBar (nullptr),
       m_menuBar (nullptr), m_innerRect (), m_outerRect (),
-      m_mouseModeGroup (nullptr)
+      m_mouseModeGroup (nullptr), m_printer (QPrinter::HighResolution)
   {
     m_container = new Container (win);
     win->setCentralWidget (m_container);
@@ -305,6 +308,10 @@ namespace QtHandles
              SLOT (setMouseMode (MouseMode)));
     m_figureToolBar->addActions (m_mouseModeGroup->actions ());
 
+    QAction *print_button = m_figureToolBar->addAction (tr ("Print"));
+    connect (print_button, SIGNAL (triggered (void)),
+             this, SLOT (printFigure (void)));
+
     QAction *toggle_axes = m_figureToolBar->addAction (tr ("Axes"));
     connect (toggle_axes, SIGNAL (triggered (void)),
              this, SLOT (toggleAxes (void)));
@@ -325,6 +332,10 @@ namespace QtHandles
     fileMenu->addAction (tr ("&Save"), this, SLOT (fileSaveFigure (bool)));
     fileMenu->addAction (tr ("Save &As"), this, SLOT (fileSaveFigureAs (void)));
     fileMenu->addSeparator ();
+    fileMenu->addAction (tr ("P&rint Preview"), this, SLOT (printPreview (void)));
+    fileMenu->addAction (tr ("&Print"), this,
+                         SLOT (printFigure (void)), Qt::CTRL | Qt::Key_P);
+    fileMenu->addSeparator ();
     fileMenu->addAction (tr ("&Close Figure"), this,
                          SLOT (fileCloseFigure (void)), Qt::CTRL | Qt::Key_W);
 
@@ -1079,6 +1090,63 @@ namespace QtHandles
   }
 
   void
+  Figure::printFigure ()
+  {
+    QPrintDialog dialog(&m_printer, m_container);
+    dialog.setWindowTitle (tr ("Print Figure"));
+    if (dialog.exec () != QDialog::Accepted)
+    {
+        return;
+    }
+    else
+    {
+        print (&m_printer);
+    }
+  }
+
+  void
+  Figure::printPreview ()
+  {
+    QPrintPreviewDialog preview (&m_printer, m_container);
+    preview.setWindowFlags (Qt::Window);
+    preview.setMinimumHeight (m_container->height());
+    preview.setMinimumWidth (m_container->width());
+    connect (&preview, SIGNAL(paintRequested(QPrinter* )),this, SLOT(print(QPrinter* )));
+    preview.exec ();
+  }
+
+  void
+  Figure::print (QPrinter *printer)
+  {
+    QPainter painter;
+    painter.begin (printer);
+
+        double xscale, height;
+        double yscale, width;
+    if(printer->pageLayout ().orientation() == QPageLayout::Landscape)
+    {
+        width = m_container->height ();
+        height = m_container->width ();
+    }
+    else
+    {
+        height = m_container->height ();
+        width = m_container->width ();
+    }
+    xscale = printer->pageRect ().width()/width;
+    yscale = printer->pageRect ().height()/height;
+
+    double scale = qMin (xscale, yscale);
+    painter.translate (printer->paperRect ().x () + printer->pageRect ().width ()/2,
+                           printer->paperRect ().y () + printer->pageRect ().height ()/2);
+    painter.scale (scale, scale);
+    painter.translate (-m_container->width ()/2, -m_container->height ()/2);
+    painter.setRenderHints (QPainter::Antialiasing |
+                    QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, true);
+    m_container->render (&painter);
+  }
+
+  void
   Figure::enableMouseTracking (void)
   {
     // Enable mouse tracking on every widgets
diff -r b7db401e1a99 -r 9c379a3aecec libgui/graphics/Figure.h
--- a/libgui/graphics/Figure.h	Tue Jun 19 09:18:44 2018 -0700
+++ b/libgui/graphics/Figure.h	Sun Jun 24 01:40:22 2018 +0300
@@ -25,6 +25,7 @@ along with Octave; see the file COPYING.
 
 #include <QRect>
 #include <QStatusBar>
+#include <QPrinter>
 
 #include "GenericEventNotify.h"
 #include "MenuContainer.h"
@@ -128,6 +129,9 @@ namespace QtHandles
     void toggleAxes (void);
     void toggleGrid (void);
     void autoAxes (void);
+    void printFigure (void);
+    void printPreview (void);
+    void print (QPrinter* printer);
 
   public slots:
     uint8NDArray slotGetPixels (void);
@@ -145,6 +149,7 @@ namespace QtHandles
     QRect m_outerRect;
     MouseModeActionGroup *m_mouseModeGroup;
     int m_previousHeight;
+    QPrinter m_printer;
   };
 
 }
