Package Gnumed :: Package timelinelib :: Module printing |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.printing

      1  # Copyright (C) 2009, 2010, 2011  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  import wx
     20
     21
     22 -class TimelinePrintout(wx.Printout):
     23      """
     24      This class has the functionality of printing out a Timeline document.
     25      Responds to calls such as OnPrintPage and HasPage.
     26      Instances of this class are passed to wx.Printer.Print() or a
     27      wx.PrintPreview object to initiate printing or previewing.
     28      """
     29
     30 -    def __init__(self, drawing_area, preview=False):
     31          wx.Printout.__init__(self)
     32          self.drawing_area = drawing_area
     33          self.preview = preview
     34
     35 -    def OnBeginDocument(self, start, end):
     36          return super(TimelinePrintout, self).OnBeginDocument(start, end)
     37
     38 -    def OnEndDocument(self):
     39          super(TimelinePrintout, self).OnEndDocument()
     40
     41 -    def OnBeginPrinting(self):
     42          super(TimelinePrintout, self).OnBeginPrinting()
     43
     44 -    def OnEndPrinting(self):
     45          super(TimelinePrintout, self).OnEndPrinting()
     46
     47 -    def OnPreparePrinting(self):
     48          super(TimelinePrintout, self).OnPreparePrinting()
     49
     50 -    def HasPage(self, page):
     51          if page <= 1:
     52              return True
     53          else:
     54              return False
     55
     56 -    def GetPageInfo(self):
     57          minPage  = 1
     58          maxPage  = 1
     59          pageFrom = 1
     60          pageTo   = 1
     61          return (minPage, maxPage, pageFrom, pageTo)
     62
     63 -    def OnPrintPage(self, page):
     64          def SetScaleAndDeviceOrigin(dc):
     65              (panel_width, panel_height) = self.drawing_area.GetSize()
     66              # Let's have at least 50 device units margin
     67              x_margin = 50
     68              y_margin = 50
     69              # Add the margin to the graphic size
     70              x_max = panel_width  + (2 * x_margin)
     71              y_max = panel_height + (2 * y_margin)
     72              # Get the size of the DC in pixels
     73              (dc_width, dc_heighth) = dc.GetSizeTuple()
     74              # Calculate a suitable scaling factor
     75              x_scale = float(dc_width) / x_max
     76              y_scale = float(dc_heighth) / y_max
     77              # Use x or y scaling factor, whichever fits on the DC
     78              scale = min(x_scale, y_scale)
     79              # Calculate the position on the DC for centering the graphic
     80              x_pos = (dc_width - (panel_width  * scale)) / 2.0
     81              y_pos = (dc_heighth - (panel_height * scale)) / 2.0
     82              dc.SetUserScale(scale, scale)
     83              dc.SetDeviceOrigin(int(x_pos), int(y_pos))
     84          dc = self.GetDC()
     85          SetScaleAndDeviceOrigin(dc)
     86          dc.BeginDrawing()
     87          dc.DrawBitmap(self.drawing_area.get_current_image(), 0, 0, True)
     88          dc.EndDrawing()
     89          return True
     90
     91
     92 -def print_timeline(main_frame):
     93      pdd = wx.PrintDialogData(main_frame.printData)
     94      pdd.SetToPage(1)
     95      printer = wx.Printer(pdd)
     96      printout = TimelinePrintout(main_frame.main_panel.drawing_area, False)
     97      frame = wx.GetApp().GetTopWindow()
     98      if not printer.Print(frame, printout, True):
     99          if printer.GetLastError() == wx.PRINTER_ERROR:
    100              wx.MessageBox(_("There was a problem printing.\nPerhaps your current printer is not set correctly?"), _("Printing"), wx.OK)
    101      else:
    102          main_frame.printData = wx.PrintData(printer.GetPrintDialogData().GetPrintData())
    103      printout.Destroy()
    104
    105
    106 -def print_preview(main_frame):
    107      data = wx.PrintDialogData(main_frame.printData)
    108      printout_preview  = TimelinePrintout(main_frame.main_panel.drawing_area, True)
    109      printout = TimelinePrintout(main_frame.main_panel.drawing_area, False)
    110      preview = wx.PrintPreview(printout_preview, printout, data)
    111      if not preview.Ok():
    112          return
    113      frame = wx.GetApp().GetTopWindow()
    114      pfrm = wx.PreviewFrame(preview, frame, _("Print preview"))
    115      pfrm.Initialize()
    116      pfrm.SetPosition(frame.GetPosition())
    117      pfrm.SetSize(frame.GetSize())
    118      pfrm.Show(True)
    119
    120
    121 -def print_setup(main_frame):
    122      psdd = wx.PageSetupDialogData(main_frame.printData)
    123      psdd.CalculatePaperSizeFromId()
    124      dlg = wx.PageSetupDialog(main_frame.main_panel.drawing_area, psdd)
    125      dlg.ShowModal()
    126      # this makes a copy of the wx.PrintData instead of just saving
    127      # a reference to the one inside the PrintDialogData that will
    128      # be destroyed when the dialog is destroyed
    129      main_frame.printData = wx.PrintData(dlg.GetPageSetupData().GetPrintData())
    130      dlg.Destroy()
    131