#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# generated by wxGlade 0.6.3 on Mon Mar 12 21:35:52 2012

import wx
import wx.grid
import win32com.client 
import win32api


    
class Data:
    pass

pass

def Init(path=None):
    import win32com.client        
    Visum=win32com.client.Dispatch('Visum.Visum')
    if path!=None: Visum.LoadVersion(path)
    return Visum

class MyDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        Data.Paths=self.Get_Paths()
        # begin wxGlade: MyDialog.__init__
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE|wx.STAY_ON_TOP
        wx.Dialog.__init__(self, *args, **kwds)
        self.parametry_produkcja_staticbox = wx.StaticBox(self, -1, "Parametry Produkcja")
        self.parametry_atrakcja_staticbox = wx.StaticBox(self, -1, "Parametry Atrakcja")
        self.generacja_staticbox = wx.StaticBox(self, -1, "Parametry Generacji")
        self.sizer_grid_rozklad_staticbox = wx.StaticBox(self, -1, "Parametry rozkladu")
        self.wybor_atrybutow_staticbox = wx.StaticBox(self, -1, "Atrybuty")
        self.panel_1 = wx.Panel(self, -1)
        self.x1 = wx.Button(self, -1, "x1")
        self.x2 = wx.Button(self, -1, "x2")
        self.x3 = wx.Button(self, -1, "x3")
        self.x4 = wx.Button(self, -1, "x4")
        self.x5 = wx.Button(self, -1, "x5")
        self.x6 = wx.Button(self, -1, "x6")
        self.x7 = wx.Button(self, -1, "x7")
        self.x8 = wx.Button(self, -1, "x8")
        self.grid_produkcja = wx.grid.Grid(self, -1, size=(1, 1))
        self.grid_atrakcja = wx.grid.Grid(self, -1, size=(1, 1))
        self.grid_rozklad = wx.grid.Grid(self, -1, size=(1, 1))
        self.Import_btn = wx.Button(self, -1, "Import z Excela")
        self.Export_btn = wx.Button(self, -1, "Eksport do Excela")
        self.panel_2 = wx.Panel(self, -1)
        self.Licz_Generacje_btn_copy = wx.Button(self, -1, "Wprowadz parametry")
        self.HelpBtn = wx.Button(self, -1, "Help")
        self.logo = wx.StaticBitmap(self, -1, wx.Bitmap(Data.Paths["logo"], wx.BITMAP_TYPE_ANY))
        self.Execute_btn = wx.Button(self, -1, "Execute")
        self.CancelBtn = wx.Button(self, -1, "Cancel")

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x1)
        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x2)
        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x3)
        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x4)
        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x5)
        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x6)
        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x7)
        self.Bind(wx.EVT_BUTTON, self.btn_click, self.x8)
        self.Bind(wx.EVT_BUTTON, self.Import_Hndlr, self.Import_btn)
        self.Bind(wx.EVT_BUTTON, self.Export_Hndlr, self.Export_btn)
        self.Bind(wx.EVT_BUTTON, self.Generation_Hndlr, self.Licz_Generacje_btn_copy)
        self.Bind(wx.EVT_BUTTON, self.HelpClick, self.HelpBtn)
        self.Bind(wx.EVT_BUTTON, self.Execute_Hndlr, self.Execute_btn)
        self.Bind(wx.EVT_BUTTON, self.Cancel_Click, self.CancelBtn)
        # end wxGlade
        self.__wewnetrzny_init()
        
    def __set_properties(self):
        # begin wxGlade: MyDialog.__set_properties
        self.SetTitle("Generacja Rozklad")
        self.SetSize((630, 440))
        self.panel_1.SetMinSize((83, 20))
        self.x1.SetMinSize((100, 20))
        self.x2.SetMinSize((100, 20))
        self.x3.SetMinSize((100, 20))
        self.x4.SetMinSize((100, 20))
        self.x5.SetMinSize((100, 20))
        self.x6.SetMinSize((100, 20))
        self.x7.SetMinSize((100, 20))
        self.x8.SetMinSize((100, 20))
        self.grid_produkcja.CreateGrid(8, 4)
        self.grid_produkcja.SetRowLabelSize(20)
        self.grid_produkcja.SetColLabelSize(20)
        self.grid_produkcja.SetColLabelValue(0, "SO")
        self.grid_produkcja.SetColSize(0, 50)
        self.grid_produkcja.SetColLabelValue(1, "SD")
        self.grid_produkcja.SetColSize(1, 50)
        self.grid_produkcja.SetColLabelValue(2, "SC")
        self.grid_produkcja.SetColSize(2, 50)
        self.grid_produkcja.SetColLabelValue(3, "SCp")
        self.grid_produkcja.SetColSize(3, 50)
        self.grid_atrakcja.CreateGrid(8, 4)
        self.grid_atrakcja.SetRowLabelSize(20)
        self.grid_atrakcja.SetColLabelSize(20)
        self.grid_atrakcja.SetColLabelValue(0, "SO")
        self.grid_atrakcja.SetColSize(0, 50)
        self.grid_atrakcja.SetColLabelValue(1, "SD")
        self.grid_atrakcja.SetColSize(1, 50)
        self.grid_atrakcja.SetColLabelValue(2, "SC")
        self.grid_atrakcja.SetColSize(2, 50)
        self.grid_atrakcja.SetColLabelValue(3, "SCp")
        self.grid_atrakcja.SetColSize(3, 50)
        self.grid_rozklad.CreateGrid(3, 4)
        self.grid_rozklad.SetRowLabelSize(20)
        self.grid_rozklad.SetColLabelSize(20)
        self.grid_rozklad.SetColLabelValue(0, "SO")
        self.grid_rozklad.SetColSize(0, 50)
        self.grid_rozklad.SetColLabelValue(1, "SD")
        self.grid_rozklad.SetColSize(1, 50)
        self.grid_rozklad.SetColLabelValue(2, "SC")
        self.grid_rozklad.SetColSize(2, 50)
        self.grid_rozklad.SetColLabelValue(3, "SCp")
        self.grid_rozklad.SetColSize(3, 50)
        self.Import_btn.SetMinSize((150,-1))
        self.Export_btn.SetMinSize((150,-1))
        self.Licz_Generacje_btn_copy.SetMinSize((150, -1))
        self.HelpBtn.SetMinSize((87, -1))
        self.logo.SetMinSize((80, 20))
        self.Execute_btn.SetMinSize((70, -1))
        self.CancelBtn.SetMinSize((70, -1))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyDialog.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
        rozklad = wx.BoxSizer(wx.HORIZONTAL)
        grid_sizer_1 = wx.GridSizer(2, 2, 0, 0)
        sizer_grid_rozklad = wx.StaticBoxSizer(self.sizer_grid_rozklad_staticbox, wx.HORIZONTAL)
        generacja = wx.StaticBoxSizer(self.generacja_staticbox, wx.HORIZONTAL)
        parametry_atrakcja = wx.StaticBoxSizer(self.parametry_atrakcja_staticbox, wx.HORIZONTAL)
        parametry_produkcja = wx.StaticBoxSizer(self.parametry_produkcja_staticbox, wx.HORIZONTAL)
        wybor_atrybutow = wx.StaticBoxSizer(self.wybor_atrybutow_staticbox, wx.VERTICAL)
        wybor_atrybutow.Add(self.panel_1, 0, wx.EXPAND, 0)
        wybor_atrybutow.Add(self.x1, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        wybor_atrybutow.Add(self.x2, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        wybor_atrybutow.Add(self.x3, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        wybor_atrybutow.Add(self.x4, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        wybor_atrybutow.Add(self.x5, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        wybor_atrybutow.Add(self.x6, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        wybor_atrybutow.Add(self.x7, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        wybor_atrybutow.Add(self.x8, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
        generacja.Add(wybor_atrybutow, 1, wx.EXPAND, 0)
        parametry_produkcja.Add(self.grid_produkcja, 1, wx.EXPAND, 0)
        generacja.Add(parametry_produkcja, 2, wx.EXPAND, 0)
        parametry_atrakcja.Add(self.grid_atrakcja, 1, wx.EXPAND, 0)
        generacja.Add(parametry_atrakcja, 2, wx.EXPAND, 0)
        sizer_1.Add(generacja, 6, wx.EXPAND, 0)
        sizer_grid_rozklad.Add(self.grid_rozklad, 1, wx.EXPAND, 0)
        rozklad.Add(sizer_grid_rozklad, 2, wx.EXPAND, 0)
        grid_sizer_1.Add(self.Import_btn, 1, wx.LEFT|wx.ALIGN_BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 10)
        grid_sizer_1.Add(self.Export_btn, 1, wx.RIGHT|wx.ALIGN_RIGHT|wx.ALIGN_BOTTOM, 20)
        grid_sizer_1.Add(self.panel_2, 1, wx.EXPAND, 0)
        grid_sizer_1.Add(self.Licz_Generacje_btn_copy, 1, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 15)
        rozklad.Add(grid_sizer_1, 3, wx.EXPAND, 0)
        sizer_1.Add(rozklad, 3, wx.EXPAND, 0)
        sizer_5.Add(self.HelpBtn, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 10)
        sizer_5.Add(self.logo, 3, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 10)
        sizer_5.Add(self.Execute_btn, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 10)
        sizer_5.Add(self.CancelBtn, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 10)
        sizer_3.Add(sizer_5, 1, wx.ALL|wx.EXPAND, 10)
        sizer_1.Add(sizer_3, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        # end wxGlade

    def Get_Paths(self):
        '''
        Create paths to working folder, html and png file    
        '''
        
        Paths={}
        Paths["MainVisum"] = Visum.GetWorkingFolder()
        Paths["ScriptFolder"] = Paths["MainVisum"] + "\\AddIns\\Generacja_Rozklad"
        Paths["logo"]=Paths["ScriptFolder"] + "\\help\\i2_logo.png"
        Paths["GPA"]=Paths["ScriptFolder"] + "\\ShowFlow.gpa"
        Paths["Help"]=Paths["ScriptFolder"] + "\\help.htm"
        Paths["Report"]=Paths["ScriptFolder"] + "\\report.txt "
        Paths["xls"]=Paths["ScriptFolder"]+"\\input.xls" 
        Paths["proc"]=Paths["ScriptFolder"]+"\\proc.xml" 
        Paths["proc_backup"]=Paths["ScriptFolder"]+"\\proc_backup.xml" 
        return Paths           
                 
    def __wewnetrzny_init(self):
    
   
        def Connect_xls():            
            Excel=win32com.client.Dispatch("Excel.Application")   #uruchomienie Excela
            try:
                WorkBook=Excel.Workbooks.Open(Data.Paths["xls"])
            except:
                win32api.MessageBox(0, 'Najprawdopodobniej nie masz pliku input.xls w katalogu\n Visum -> Exe -> AddIns \n inna opcja: musisz zamknac i zapisac Excela',"Blad")
        
                
            return WorkBook,Excel
        
        
        self.grid_rozklad.SetRowLabelValue(0,"a")
        self.grid_rozklad.SetRowLabelValue(1,"b")
        self.grid_rozklad.SetRowLabelValue(2,"c")
        for i in range(8):
            self.grid_atrakcja.SetRowLabelValue(i,"x"+str(i+1))
            self.grid_produkcja.SetRowLabelValue(i,"x"+str(i+1))
        Data.Strata=["SO","SD","SC","SCp"]    
        Data.xls,Data.Excel=Connect_xls()
        Data.Buttons=[self.x1,
                     self.x2,
                     self.x3,
                     self.x4,
                     self.x5,
                     self.x6,
                     self.x7,
                     self.x8]
        
        for i in range(4):
            for j in range(8):
                self.grid_produkcja.SetCellValue( j, i, str(0.0) )
                self.grid_atrakcja.SetCellValue( j, i, str(0.0) )
        for i in range(4):
            for j in range(3):
                self.grid_rozklad.SetCellValue( j, i, str(0.0) )
    
    def btn_click(self, event): # wxGlade: MyDialog.<event_handler>
        button = event.GetEventObject()
        attr=Visum.Net.Zones.AskAttribute()
        if attr:
            try:
                button.SetLabel(str(attr))
            except:
                win32api.MessageBox(0,'Najprawdopodobniej w nazwie parametru sa polskie znaki - nie moze byc',"Blad")
        
    def HelpClick(self, event): # wxGlade: MyDialog.<event_handler>
        import os
        os.startfile(Data.Paths["Help"])

    def Cancel_Click(self, event): # wxGlade: MyDialog.<event_handler>
        self.Destroy()

    def Import_Hndlr(self, event): # wxGlade: MyDialog.<event_handler>
        
        def GetFromXLS():
            Data.Atrybuty=[]
            for i in range(8):
                Data.Atrybuty.append(Data.xls.ActiveSheet.Cells(i+4,3).Value)
            Data.ParamProdukcja=Data.xls.ActiveSheet.Range("D4:G11").Value
            Data.ParamAtrakcja=Data.xls.ActiveSheet.Range("J4:M11").Value
            Data.ParamRozklad=Data.xls.ActiveSheet.Range("D16:G18").Value
        
        def PasteIntoGrids():
            for i in range(4):
                for j in range(8):
                    self.grid_produkcja.SetCellValue( j, i, str(Data.ParamProdukcja[j][i]) )
                    self.grid_atrakcja.SetCellValue( j, i, str(Data.ParamAtrakcja[j][i]) )
            for i in range(4):
                for j in range(3):
                    self.grid_rozklad.SetCellValue( j, i, str(Data.ParamRozklad[j][i]) )
            i=0
            for button in Data.Buttons:
                if button.GetLabel()[0]=="x":
                    if Data.Atrybuty[i]!=None:
                        button.SetLabel(str(Data.Atrybuty[i]))
                i+=1
        
        Data.Excel.Visible=True
        GetFromXLS()
        PasteIntoGrids()
        
    def Export_Hndlr(self, event): # wxGlade: MyDialog.<event_handler>
        Data.Excel.Visible=True
        i=0
        for button in Data.Buttons:
            label=button.GetLabel()
            if label[0]!="x":
                Data.xls.ActiveSheet.Cells(i+4,3).Value=label  
            i+=1
        for i in range(4):
                for j in range(8):
                    if j<3:
                       Data.xls.ActiveSheet.Cells(j+16,i+4).Value=self.grid_rozklad.GetCellValue( j, i)
                    Data.xls.ActiveSheet.Cells(j+4,i+4).Value=self.grid_produkcja.GetCellValue( j, i)
                    Data.xls.ActiveSheet.Cells(j+4,i+10).Value=self.grid_atrakcja.GetCellValue( j, i)
        Data.xls.Save()
        
    def Generation_Hndlr(self, event): # wxGlade: MyDialog.<event_handler>
        def Backup_Procedures():             
            Visum.Procedures.Save(Data.Paths["proc_backup"])
            Visum.Procedures.OpenXml(Data.Paths["proc"])
            
        def Get_Attrs():
            Attrs=[]
            i=0
            for button in Data.Buttons:
                label=button.GetLabel()
                if label[0]!="x":
                    Attrs.append(label)  
                i+=1
            return Attrs

        def Generation():
            Generation_Procedures=[Visum.Procedures.Operations.ItemByKey(1),
                                   Visum.Procedures.Operations.ItemByKey(2),
                                   Visum.Procedures.Operations.ItemByKey(3),
                                   Visum.Procedures.Operations.ItemByKey(4)] 
            i=0
            for proc in Generation_Procedures:
                StrataLines=proc.TripGenerationParameters.TripGenerationDStratParameters(Data.Strata[i])
                i+=1
                j=2
                for attr in Data.Attrs:                
                    proc.TripGenerationParameters.AddLinCombAtt(attr)
                    StrataLines.SetProductionRate(j, self.grid_produkcja.GetCellValue( j-2, i-1))
                    StrataLines.SetAttractionRate(j, self.grid_atrakcja.GetCellValue( j-2, i-1))
                    j+=1
        
        def Distribution(): # wxGlade: MyDialog.<event_handler>
            Distribution_Procedures=[Visum.Procedures.Operations.ItemByKey(5),
                                   Visum.Procedures.Operations.ItemByKey(6),
                                   Visum.Procedures.Operations.ItemByKey(7),
                                   Visum.Procedures.Operations.ItemByKey(8)] 
            
            i=0
            for proc in Distribution_Procedures:
                StrataDist=proc.TripDistributionParameters.TripDistributionDStratParameters(Data.Strata[i])
                StrataDist.GravitationParameters.SetAttValue("Para_a",self.grid_rozklad.GetCellValue(0,i))
                StrataDist.GravitationParameters.SetAttValue("Para_b",self.grid_rozklad.GetCellValue(1,i))
                StrataDist.GravitationParameters.SetAttValue("Para_c",self.grid_rozklad.GetCellValue(2,i))
                i+=1
        

        Data.Attrs=Get_Attrs()
        Backup_Procedures() 
        Generation()
        Distribution()

    def Execute_Hndlr(self, event): # wxGlade: MyDialog.<event_handler>
        Visum.Procedures.Execute()

# end of class MyDialog

stand_alone=True
try:
    Visum
    stand_alone=0
except:
    Visum=Init("D:/TK.ver")
#Visum=Init()

if __name__ == "__main__":
    if stand_alone:
        app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    dialog_1 = MyDialog(None, -1, "")
    app.SetTopWindow(dialog_1)
    dialog_1.Show()
    if stand_alone:
        app.MainLoop()
