PLCOpenEditor.py
changeset 213 4931959ea256
parent 212 e36ba4f15fc8
child 217 ddb5b2e499e2
equal deleted inserted replaced
212:e36ba4f15fc8 213:4931959ea256
   219         AppendMenu(parent, help='', id=wx.ID_SAVEAS,
   219         AppendMenu(parent, help='', id=wx.ID_SAVEAS,
   220               kind=wx.ITEM_NORMAL, text=u'Save As...\tCTRL+SHIFT+S')
   220               kind=wx.ITEM_NORMAL, text=u'Save As...\tCTRL+SHIFT+S')
   221         AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE,
   221         AppendMenu(parent, help='', id=ID_PLCOPENEDITORFILEMENUGENERATE,
   222               kind=wx.ITEM_NORMAL, text=u'Generate Program\tCTRL+G')
   222               kind=wx.ITEM_NORMAL, text=u'Generate Program\tCTRL+G')
   223         parent.AppendSeparator()
   223         parent.AppendSeparator()
       
   224         AppendMenu(parent, help='', id=wx.ID_PAGE_SETUP,
       
   225               kind=wx.ITEM_NORMAL, text=u'Page Setup')
       
   226         AppendMenu(parent, help='', id=wx.ID_PREVIEW,
       
   227               kind=wx.ITEM_NORMAL, text=u'Preview')
       
   228         AppendMenu(parent, help='', id=wx.ID_PRINT,
       
   229               kind=wx.ITEM_NORMAL, text=u'Print')
       
   230         parent.AppendSeparator()
   224         AppendMenu(parent, help='', id=wx.ID_PROPERTIES,
   231         AppendMenu(parent, help='', id=wx.ID_PROPERTIES,
   225               kind=wx.ITEM_NORMAL, text=u'Properties')
   232               kind=wx.ITEM_NORMAL, text=u'Properties')
   226         parent.AppendSeparator()
   233         parent.AppendSeparator()
   227         AppendMenu(parent, help='', id=wx.ID_EXIT,
   234         AppendMenu(parent, help='', id=wx.ID_EXIT,
   228               kind=wx.ITEM_NORMAL, text=u'Quit\tCTRL+Q')
   235               kind=wx.ITEM_NORMAL, text=u'Quit\tCTRL+Q')
   232         self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL)
   239         self.Bind(wx.EVT_MENU, self.OnCloseProjectMenu, id=wx.ID_CLOSE_ALL)
   233         self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE)
   240         self.Bind(wx.EVT_MENU, self.OnSaveProjectMenu, id=wx.ID_SAVE)
   234         self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS)
   241         self.Bind(wx.EVT_MENU, self.OnSaveProjectAsMenu, id=wx.ID_SAVEAS)
   235         self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu,
   242         self.Bind(wx.EVT_MENU, self.OnGenerateProgramMenu,
   236               id=ID_PLCOPENEDITORFILEMENUGENERATE)
   243               id=ID_PLCOPENEDITORFILEMENUGENERATE)
       
   244         self.Bind(wx.EVT_MENU, self.OnPageSetupMenu, id=wx.ID_PAGE_SETUP)
       
   245         self.Bind(wx.EVT_MENU, self.OnPreviewMenu, id=wx.ID_PREVIEW)
       
   246         self.Bind(wx.EVT_MENU, self.OnPrintMenu, id=wx.ID_PRINT)
   237         self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES)
   247         self.Bind(wx.EVT_MENU, self.OnPropertiesMenu, id=wx.ID_PROPERTIES)
   238         self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT)
   248         self.Bind(wx.EVT_MENU, self.OnQuitMenu, id=wx.ID_EXIT)
   239     
   249     
   240     def _init_coll_EditMenu_Items(self, parent):
   250     def _init_coll_EditMenu_Items(self, parent):
   241         AppendMenu(parent, help='', id=wx.ID_REFRESH,
   251         AppendMenu(parent, help='', id=wx.ID_REFRESH,
   475         self.CurrentLanguage = ""
   485         self.CurrentLanguage = ""
   476         self.SelectedItem = None
   486         self.SelectedItem = None
   477         self.DrawingMode = FREEDRAWING_MODE
   487         self.DrawingMode = FREEDRAWING_MODE
   478         #self.DrawingMode = DRIVENDRAWING_MODE
   488         #self.DrawingMode = DRIVENDRAWING_MODE
   479         
   489         
       
   490         self.PrintData = wx.PrintData()
       
   491         self.PrintData.SetPaperId(wx.PAPER_A4)
       
   492         self.PrintData.SetPrintMode(wx.PRINT_MODE_PRINTER)
       
   493         self.PageSetupData = wx.PageSetupDialogData(self.PrintData)
       
   494         self.PageSetupData.SetMarginTopLeft(wx.Point(10, 15))
       
   495         self.PageSetupData.SetMarginBottomRight(wx.Point(10, 20))
       
   496         
   480         if not self.ModeSolo or fileOpen is not None:
   497         if not self.ModeSolo or fileOpen is not None:
   481             self.RefreshProjectTree()
   498             self.RefreshProjectTree()
   482         
   499         
   483         self.RefreshFileMenu()
   500         self.RefreshFileMenu()
   484         self.RefreshEditMenu()
   501         self.RefreshEditMenu()
   650 #-------------------------------------------------------------------------------
   667 #-------------------------------------------------------------------------------
   651 
   668 
   652     def RefreshFileMenu(self):
   669     def RefreshFileMenu(self):
   653         if self.FileMenu:
   670         if self.FileMenu:
   654             if self.Controler.HasOpenedProject():
   671             if self.Controler.HasOpenedProject():
       
   672                 selected = self.GetPageSelection()
       
   673                 if selected >= 0:
       
   674                     graphic_viewer = isinstance(self.GetPage(selected), Viewer)
       
   675                 else:
       
   676                     graphic_viewer = False
   655                 if self.GetPageCount() > 0:
   677                 if self.GetPageCount() > 0:
   656                     self.FileMenu.Enable(wx.ID_CLOSE, True)
   678                     self.FileMenu.Enable(wx.ID_CLOSE, True)
       
   679                     if graphic_viewer:
       
   680                         self.FileMenu.Enable(wx.ID_PREVIEW, True)
       
   681                         self.FileMenu.Enable(wx.ID_PRINT, True)
       
   682                     else:
       
   683                         self.FileMenu.Enable(wx.ID_PREVIEW, False)
       
   684                         self.FileMenu.Enable(wx.ID_PRINT, False)
   657                 else:
   685                 else:
   658                     self.FileMenu.Enable(wx.ID_CLOSE, False)
   686                     self.FileMenu.Enable(wx.ID_CLOSE, False)
       
   687                     self.FileMenu.Enable(wx.ID_PREVIEW, False)
       
   688                     self.FileMenu.Enable(wx.ID_PRINT, False)
   659                 self.FileMenu.Enable(wx.ID_CLOSE_ALL, True)
   689                 self.FileMenu.Enable(wx.ID_CLOSE_ALL, True)
   660                 self.FileMenu.Enable(wx.ID_SAVE, True)
   690                 self.FileMenu.Enable(wx.ID_SAVE, True)
   661                 self.FileMenu.Enable(wx.ID_SAVEAS, True)
   691                 self.FileMenu.Enable(wx.ID_SAVEAS, True)
   662                 self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True)
   692                 self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, True)
       
   693                 self.FileMenu.Enable(wx.ID_PAGE_SETUP, True)
   663                 self.FileMenu.Enable(wx.ID_PROPERTIES, True)
   694                 self.FileMenu.Enable(wx.ID_PROPERTIES, True)
   664             else:
   695             else:
   665                 self.FileMenu.Enable(wx.ID_CLOSE, False)
   696                 self.FileMenu.Enable(wx.ID_CLOSE, False)
   666                 self.FileMenu.Enable(wx.ID_CLOSE_ALL, False)
   697                 self.FileMenu.Enable(wx.ID_CLOSE_ALL, False)
   667                 self.FileMenu.Enable(wx.ID_SAVE, False)
   698                 self.FileMenu.Enable(wx.ID_SAVE, False)
   668                 self.FileMenu.Enable(wx.ID_SAVEAS, False)
   699                 self.FileMenu.Enable(wx.ID_SAVEAS, False)
   669                 self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False)
   700                 self.FileMenu.Enable(ID_PLCOPENEDITORFILEMENUGENERATE, False)
       
   701                 self.FileMenu.Enable(wx.ID_PAGE_SETUP, False)
       
   702                 self.FileMenu.Enable(wx.ID_PREVIEW, False)
       
   703                 self.FileMenu.Enable(wx.ID_PRINT, False)
   670                 self.FileMenu.Enable(wx.ID_PROPERTIES, False)
   704                 self.FileMenu.Enable(wx.ID_PROPERTIES, False)
   671 
   705 
   672     def OnNewProjectMenu(self, event):
   706     def OnNewProjectMenu(self, event):
   673         dialog = ProjectDialog(self)
   707         dialog = ProjectDialog(self)
   674         if dialog.ShowModal() == wx.ID_OK:
   708         if dialog.ShowModal() == wx.ID_OK:
   803                 message = wx.MessageDialog(self, "%s is not a valid folder!"%os.path.dirname(filepath), "Error", wx.OK|wx.ICON_ERROR)
   837                 message = wx.MessageDialog(self, "%s is not a valid folder!"%os.path.dirname(filepath), "Error", wx.OK|wx.ICON_ERROR)
   804                 message.ShowModal()
   838                 message.ShowModal()
   805                 message.Destroy()
   839                 message.Destroy()
   806             self.RefreshTitle()
   840             self.RefreshTitle()
   807         dialog.Destroy()
   841         dialog.Destroy()
       
   842 
       
   843     def OnPageSetupMenu(self, event):
       
   844         dialog = wx.PageSetupDialog(self, self.PageSetupData)
       
   845         if dialog.ShowModal() == wx.ID_OK:
       
   846             self.PageSetupData = wx.PageSetupDialogData(dialog.GetPageSetupData())
       
   847             self.PrintData = wx.PrintData(self.PageSetupData.GetPrintData())
       
   848         dialog.Destroy()
       
   849         event.Skip()
       
   850 
       
   851     def OnPreviewMenu(self, event):
       
   852         selected = self.GetPageSelection()        
       
   853         if selected != -1:
       
   854             data = wx.PrintDialogData(self.PrintData)
       
   855             properties = self.Controler.GetProjectProperties()
       
   856             page_size = map(int, properties["pageSize"])
       
   857             margins = (self.PageSetupData.GetMarginTopLeft(), self.PageSetupData.GetMarginBottomRight())
       
   858             printout = GraphicPrintout(self.GetPage(selected), page_size, margins, True)
       
   859             printout2 = GraphicPrintout(self.GetPage(selected), page_size, margins, True)
       
   860             preview = wx.PrintPreview(printout, printout2, data)
       
   861 
       
   862             if preview.Ok():
       
   863                 preview_frame = wx.PreviewFrame(preview, frame, "Print preview")
       
   864 
       
   865                 preview_frame.Initialize()
       
   866                 
       
   867                 preview_frame.Show(True)
       
   868         event.Skip()
       
   869 
       
   870     def OnPrintMenu(self, event):
       
   871         selected = self.GetPageSelection()        
       
   872         if selected != -1:
       
   873             dialog_data = wx.PrintDialogData(self.PrintData)
       
   874             dialog_data.SetToPage(1)
       
   875             properties = self.Controler.GetProjectProperties()
       
   876             page_size = map(int, properties["pageSize"])
       
   877             margins = (self.PageSetupData.GetMarginTopLeft(), self.PageSetupData.GetMarginBottomRight())
       
   878             printer = wx.Printer(dialog_data)
       
   879             printout = GraphicPrintout(self.GetPage(selected), page_size, margins)
       
   880             
       
   881             if not printer.Print(self, printout, True):
       
   882                 wx.MessageBox("There was a problem printing.\nPerhaps your current printer is not set correctly?", "Printing", wx.OK)
       
   883             printout.Destroy()
       
   884         event.Skip()
   808 
   885 
   809     def OnPropertiesMenu(self, event):
   886     def OnPropertiesMenu(self, event):
   810         self.ShowProperties()
   887         self.ShowProperties()
   811         event.Skip()
   888         event.Skip()
   812 
   889 
  3802         if buffer:
  3879         if buffer:
  3803             self.Controler.BufferProject()
  3880             self.Controler.BufferProject()
  3804             self.ParentWindow.RefreshTitle()
  3881             self.ParentWindow.RefreshTitle()
  3805             self.ParentWindow.RefreshEditMenu()
  3882             self.ParentWindow.RefreshEditMenu()
  3806 
  3883 
       
  3884 UPPER_DIV = lambda x, y: (x / y) + {True : 0, False : 1}[(x % y) == 0]
       
  3885 
       
  3886 class GraphicPrintout(wx.Printout):
       
  3887     def __init__(self, viewer, page_size, margins, preview = False):
       
  3888         wx.Printout.__init__(self)
       
  3889         self.Viewer = viewer
       
  3890         self.PageSize = page_size
       
  3891         self.Preview = preview
       
  3892         self.Margins = margins
       
  3893         self.FontSize = 14
       
  3894         self.TextMargin = 5
       
  3895         
       
  3896         maxx, maxy = viewer.GetMaxSize()
       
  3897         self.PageGrid = (UPPER_DIV(maxx, page_size[0]), UPPER_DIV(maxy, page_size[1]))
       
  3898         
       
  3899     def GetPageNumber(self):
       
  3900         return self.PageGrid[0] * self.PageGrid[1]
       
  3901     
       
  3902     def HasPage(self, page):
       
  3903         return page <= self.GetPageNumber()
       
  3904         
       
  3905     def GetPageInfo(self):
       
  3906         page_number = self.GetPageNumber()
       
  3907         return (1, page_number, 1, page_number)
       
  3908 
       
  3909     def OnBeginDocument(self, startPage, endPage):
       
  3910         dc = self.GetDC()
       
  3911         if not self.Preview and isinstance(dc, wx.PostScriptDC):
       
  3912             dc.SetResolution(720)
       
  3913             self.FontSize = 140
       
  3914             self.TextMargin = 50
       
  3915         super(GraphicPrintout, self).OnBeginDocument(startPage, endPage)
       
  3916 
       
  3917     def OnPrintPage(self, page):
       
  3918         dc = self.GetDC()
       
  3919         dc.printing = not self.Preview
       
  3920         
       
  3921         # Get the size of the DC in pixels
       
  3922         ppiPrinterX, ppiPrinterY = self.GetPPIPrinter()
       
  3923         ppiScreenX, ppiScreenY = self.GetPPIScreen()
       
  3924         pw, ph = self.GetPageSizePixels()
       
  3925         dw, dh = dc.GetSizeTuple()
       
  3926         Xscale = (float(dw) * float(ppiPrinterX)) / (float(pw) * 25.4)
       
  3927         Yscale = (float(dh) * float(ppiPrinterY)) / (float(ph) * 25.4)
       
  3928         
       
  3929         margin_left = self.Margins[0].x * Xscale
       
  3930         margin_top = self.Margins[0].y * Yscale
       
  3931         area_width = dw - self.Margins[1].x * Xscale - margin_left
       
  3932         area_height = dh - self.Margins[1].y * Yscale - margin_top
       
  3933         
       
  3934         dc.SetPen(wx.BLACK_PEN)
       
  3935         dc.SetBrush(wx.TRANSPARENT_BRUSH)    
       
  3936         dc.DrawRectangle(margin_left, margin_top, area_width, area_height)
       
  3937         
       
  3938         dc.SetFont(wx.Font(self.FontSize, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
       
  3939         dc.SetTextForeground(wx.BLACK)
       
  3940         block_name = " - ".join(self.Viewer.GetTagName().split("::")[1:])
       
  3941         text_width, text_height = dc.GetTextExtent(block_name)
       
  3942         dc.DrawText(block_name, margin_left, margin_top - text_height - self.TextMargin)
       
  3943         dc.DrawText("Page: %d" % page, margin_left, margin_top + area_height + self.TextMargin)
       
  3944         
       
  3945         # Calculate the position on the DC for centering the graphic
       
  3946         posX = area_width * ((page - 1) % self.PageGrid[0])
       
  3947         posY = area_height * ((page - 1) / self.PageGrid[0])
       
  3948 
       
  3949         scaleX = float(area_width) / float(self.PageSize[0])
       
  3950         scaleY = float(area_height) / float(self.PageSize[1])
       
  3951         scale = min(scaleX, scaleY)
       
  3952 
       
  3953         # Set the scale and origin
       
  3954         dc.SetDeviceOrigin(-posX + margin_left, -posY + margin_top)
       
  3955         dc.SetUserScale(scale, scale)
       
  3956         dc.SetClippingRegion(posX, posY, self.PageSize[0], self.PageSize[1])
       
  3957         
       
  3958         #-------------------------------------------
       
  3959         
       
  3960         self.Viewer.DoDrawing(dc, True)
       
  3961         
       
  3962         return True
       
  3963 
  3807 #-------------------------------------------------------------------------------
  3964 #-------------------------------------------------------------------------------
  3808 #                               Exception Handler
  3965 #                               Exception Handler
  3809 #-------------------------------------------------------------------------------
  3966 #-------------------------------------------------------------------------------
  3810 
  3967 
  3811 Max_Traceback_List_Size = 20
  3968 Max_Traceback_List_Size = 20