PLCOpenEditor.py
changeset 838 06db7d4edbe6
parent 815 e4f24593a758
child 967 8a339cd61cb4
equal deleted inserted replaced
837:fb0b66e9b4dd 838:06db7d4edbe6
   379                     self.ShowErrorMessage(_("Can't save project to file %s!")%filepath)
   379                     self.ShowErrorMessage(_("Can't save project to file %s!")%filepath)
   380             else:
   380             else:
   381                 self.ShowErrorMessage(_("\"%s\" is not a valid folder!")%os.path.dirname(filepath))
   381                 self.ShowErrorMessage(_("\"%s\" is not a valid folder!")%os.path.dirname(filepath))
   382             self._Refresh(TITLE, FILEMENU, PAGETITLES)
   382             self._Refresh(TITLE, FILEMENU, PAGETITLES)
   383         dialog.Destroy()
   383         dialog.Destroy()
   384 
       
   385 #-------------------------------------------------------------------------------
       
   386 #                            Debug Variables Panel
       
   387 #-------------------------------------------------------------------------------
       
   388 
       
   389 #-------------------------------------------------------------------------------
       
   390 #                               Viewer Printout
       
   391 #-------------------------------------------------------------------------------
       
   392 
       
   393 UPPER_DIV = lambda x, y: (x / y) + {True : 0, False : 1}[(x % y) == 0]
       
   394 
       
   395 class GraphicPrintout(wx.Printout):
       
   396     def __init__(self, viewer, page_size, margins, preview = False):
       
   397         wx.Printout.__init__(self)
       
   398         self.Viewer = viewer
       
   399         self.PageSize = page_size
       
   400         if self.PageSize[0] == 0 or self.PageSize[1] == 0:
       
   401             self.PageSize = (1050, 1485)
       
   402         self.Preview = preview
       
   403         self.Margins = margins
       
   404         self.FontSize = 5
       
   405         self.TextMargin = 3
       
   406         
       
   407         maxx, maxy = viewer.GetMaxSize()
       
   408         self.PageGrid = (UPPER_DIV(maxx, self.PageSize[0]), 
       
   409                          UPPER_DIV(maxy, self.PageSize[1]))
       
   410         
       
   411     def GetPageNumber(self):
       
   412         return self.PageGrid[0] * self.PageGrid[1]
       
   413     
       
   414     def HasPage(self, page):
       
   415         return page <= self.GetPageNumber()
       
   416         
       
   417     def GetPageInfo(self):
       
   418         page_number = self.GetPageNumber()
       
   419         return (1, page_number, 1, page_number)
       
   420 
       
   421     def OnBeginDocument(self, startPage, endPage):
       
   422         dc = self.GetDC()
       
   423         if not self.Preview and isinstance(dc, wx.PostScriptDC):
       
   424             dc.SetResolution(720)
       
   425         super(GraphicPrintout, self).OnBeginDocument(startPage, endPage)
       
   426 
       
   427     def OnPrintPage(self, page):
       
   428         dc = self.GetDC()
       
   429         dc.SetUserScale(1.0, 1.0)
       
   430         dc.SetDeviceOrigin(0, 0)
       
   431         dc.printing = not self.Preview
       
   432         
       
   433         # Get the size of the DC in pixels
       
   434         ppiPrinterX, ppiPrinterY = self.GetPPIPrinter()
       
   435         ppiScreenX, ppiScreenY = self.GetPPIScreen()
       
   436         pw, ph = self.GetPageSizePixels()
       
   437         dw, dh = dc.GetSizeTuple()
       
   438         Xscale = (float(dw) * float(ppiPrinterX)) / (float(pw) * 25.4)
       
   439         Yscale = (float(dh) * float(ppiPrinterY)) / (float(ph) * 25.4)
       
   440         
       
   441         fontsize = self.FontSize * Yscale
       
   442         text_margin = self.TextMargin * Yscale
       
   443         
       
   444         margin_left = self.Margins[0].x * Xscale
       
   445         margin_top = self.Margins[0].y * Yscale
       
   446         area_width = dw - self.Margins[1].x * Xscale - margin_left
       
   447         area_height = dh - self.Margins[1].y * Yscale - margin_top
       
   448         
       
   449         dc.SetPen(MiterPen(wx.BLACK))
       
   450         dc.SetBrush(wx.TRANSPARENT_BRUSH)    
       
   451         dc.DrawRectangle(margin_left, margin_top, area_width, area_height)
       
   452         
       
   453         dc.SetFont(wx.Font(fontsize, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
       
   454         dc.SetTextForeground(wx.BLACK)
       
   455         block_name = " - ".join(self.Viewer.GetTagName().split("::")[1:])
       
   456         text_width, text_height = dc.GetTextExtent(block_name)
       
   457         dc.DrawText(block_name, margin_left, margin_top - text_height - self.TextMargin)
       
   458         dc.DrawText(_("Page: %d") % page, margin_left, margin_top + area_height + self.TextMargin)
       
   459         
       
   460         # Calculate the position on the DC for centering the graphic
       
   461         posX = area_width * ((page - 1) % self.PageGrid[0])
       
   462         posY = area_height * ((page - 1) / self.PageGrid[0])
       
   463 
       
   464         scaleX = float(area_width) / float(self.PageSize[0])
       
   465         scaleY = float(area_height) / float(self.PageSize[1])
       
   466         scale = min(scaleX, scaleY)
       
   467 
       
   468         # Set the scale and origin
       
   469         dc.SetDeviceOrigin(-posX + margin_left, -posY + margin_top)
       
   470         dc.SetClippingRegion(posX, posY, self.PageSize[0] * scale, self.PageSize[1] * scale)
       
   471         dc.SetUserScale(scale, scale)
       
   472         
       
   473         #-------------------------------------------
       
   474         
       
   475         self.Viewer.DoDrawing(dc, True)
       
   476         
       
   477         return True
       
   478 
   384 
   479 #-------------------------------------------------------------------------------
   385 #-------------------------------------------------------------------------------
   480 #                               Exception Handler
   386 #                               Exception Handler
   481 #-------------------------------------------------------------------------------
   387 #-------------------------------------------------------------------------------
   482 
   388