~launchpad-p-s/sofastatistics/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import sys
import wx

import getdata
import projects
import util


USER_PATH, LOCAL_PATH = util.get_user_paths()

class ProjSelectDlg(wx.Dialog):
    def __init__(self, parent, projs):
        wx.Dialog.__init__(self, parent=parent, title="Projects",
                           size=wx.DefaultSize, 
                           style=wx.RESIZE_BORDER|wx.CAPTION|wx.CLOSE_BOX|
                           wx.SYSTEM_MENU, pos=(100, 100))
        self.parent = parent
        self.panel = wx.Panel(self)
        self.projs = projs
        self.read_only=False
        self.szrMain = wx.BoxSizer(wx.VERTICAL)
        lblChoose = wx.StaticText(self.panel, -1, "Choose an existing project ...")
        self.dropProjs = wx.Choice(self.panel, -1, choices=self.projs)
        self.dropProjs.SetSelection(0)
        self.StoreProjName(self.projs[0])
        self.dropProjs.Bind(wx.EVT_CHOICE, self.OnProjSelect)
        self.btnEdit = wx.Button(self.panel, wx.ID_EDIT)
        self.btnEdit.Bind(wx.EVT_BUTTON, self.OnEdit)
        szrExistingTop = wx.BoxSizer(wx.HORIZONTAL)
        szrExistingTop.Add(self.dropProjs, 1, wx.GROW|wx.RIGHT, 10)
        szrExistingTop.Add(self.btnEdit, 0)
        self.GetNotes(fil_proj=self.projs[0])
        self.txtProjNotes = wx.TextCtrl(self.panel, -1, self.proj_notes,
                                        style=wx.TE_MULTILINE|wx.TE_READONLY, 
                                        size=(400, 90))
        bxExisting = wx.StaticBox(self.panel, -1, "Existing Projects")
        szrExisting = wx.StaticBoxSizer(bxExisting, wx.VERTICAL)
        
        szrExisting.Add(szrExistingTop, 0, wx.GROW|wx.ALL, 10)
        szrExisting.Add(self.txtProjNotes, 1, wx.GROW|wx.ALL, 10)
        bxNew = wx.StaticBox(self.panel, -1, "")
        szrNew = wx.StaticBoxSizer(bxNew, wx.HORIZONTAL)
        lblMakeNew = wx.StaticText(self.panel, -1, "... or make a new project")
        btnMakeNew = wx.Button(self.panel, wx.ID_NEW)
        btnMakeNew.Bind(wx.EVT_BUTTON, self.OnNewClick)
        szrNew.Add(lblMakeNew, 1, wx.GROW|wx.ALL, 10)
        szrNew.Add(btnMakeNew, 0, wx.ALL, 10)
        self.SetupButtons()
        self.szrMain.Add(lblChoose, 0, wx.ALL, 10)
        self.szrMain.Add(szrExisting, 1, wx.LEFT|wx.BOTTOM|wx.RIGHT|wx.GROW, 10)
        self.szrMain.Add(szrNew, 0, wx.GROW|wx.LEFT|wx.BOTTOM|wx.RIGHT, 10)
        self.szrMain.Add(self.szrButtons, 0, wx.ALL, 10)
        self.panel.SetSizer(self.szrMain)
        self.szrMain.SetSizeHints(self)
        self.Layout()
    
    def StoreProjName(self, proj_name):
        "NB must have .proj on end"
        #print proj_name # debug
        self.proj_name = proj_name
    
    def SetupButtons(self):
        """
        Must have ID of wx.ID_... to trigger validators (no event binding 
            needed) and for std dialog button layout.
        """
        btnCancel = wx.Button(self.panel, wx.ID_CANCEL) # 
        btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
        btnOK = wx.Button(self.panel, wx.ID_OK)
        btnOK.Bind(wx.EVT_BUTTON, self.OnOK)
        btnOK.SetDefault()
        self.szrButtons = wx.StdDialogButtonSizer()
        self.szrButtons.AddButton(btnCancel)
        self.szrButtons.AddButton(btnOK)
        self.szrButtons.Realize()
        self.SetReadOnly()
    
    def GetNotes(self, fil_proj):
        f = open(os.path.join(LOCAL_PATH, "projs", fil_proj), "r")
        proj_dic = {}
        exec f in proj_dic
        f.close()
        # must always be stored, even if only ""
        self.proj_notes = proj_dic["proj_notes"]
    
    def OnProjSelect(self, event):
        proj_sel_id = self.dropProjs.GetSelection()
        self.SetNotes(proj_sel_id)
        self.SetReadOnly()
        event.Skip()
    
    def SetReadOnly(self):       
        proj_sel_id = self.dropProjs.GetSelection() 
        self.read_only = (self.projs[proj_sel_id] == \
                          projects.SOFA_DEFAULT_PROJ)
    
    def SetNotes(self, proj_sel_id):        
        self.GetNotes(self.projs[proj_sel_id])
        self.txtProjNotes.SetValue(self.proj_notes)
           
    def OnEdit(self,event):
        dlgProj = projects.ProjectDlg(parent=self, read_only=self.read_only,
                          fil_proj=self.projs[self.dropProjs.GetSelection()])
        # refresh projects list and display accordingly
        ret_val = dlgProj.ShowModal()
        if ret_val == wx.ID_DELETE:
            # redo and pick 1st
            self.projs = projects.GetProjs()
            self.dropProjs.SetItems(self.projs)
            self.dropProjs.SetSelection(0)
            self.SetNotes(0)
            self.SetReadOnly()
        elif ret_val == wx.ID_OK:
            self.SetToNameFromOK()
          
    def OnNewClick(self, event):
        dlgProj = projects.ProjectDlg(parent=self, read_only=self.read_only)
        ret_val = dlgProj.ShowModal()
        if ret_val == wx.ID_OK:
            self.SetToNameFromOK()

    def SetToNameFromOK(self):
        # redo choices and display record with new name
        self.projs = projects.GetProjs()
        self.dropProjs.SetItems(self.projs)
        # get index of new name
        # NB proj name should have been set by projects
        proj_sel_id = self.projs.index(self.proj_name)
        self.dropProjs.SetSelection(proj_sel_id) # may have changed name
        self.SetNotes(proj_sel_id)
        self.SetReadOnly()

    def OnCancel(self, event):
        self.Destroy()
    
    def OnOK(self, event):
        proj_sel_id = self.dropProjs.GetSelection()
        fil_proj = self.projs[proj_sel_id]
        try:
            proj_name = fil_proj[:-5]
            self.parent.SetProj(proj_name)
        except Exception:
            pass
        self.Destroy()

#"""
if __name__ == "__main__":
    app = wx.PySimpleApp()
    myframe = ProjSelectDlg(None, ["MOH.proj", "test.proj"])
    myframe.Show()
    app.MainLoop()
#"""