~launchpad-p-s/sofastatistics/main

« back to all changes in this revision

Viewing changes to projselect.py

  • Committer: Grant Paton-Simpson
  • Date: 2009-05-19 04:21:43 UTC
  • Revision ID: g@ubuntu-20090519042143-p561mbokz3inefvd
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import os
 
3
import sys
 
4
import wx
 
5
 
 
6
import getdata
 
7
import projects
 
8
import util
 
9
 
 
10
 
 
11
USER_PATH, LOCAL_PATH = util.get_user_paths()
 
12
 
 
13
class ProjSelectDlg(wx.Dialog):
 
14
    def __init__(self, parent, projs):
 
15
        wx.Dialog.__init__(self, parent=parent, title="Projects",
 
16
                           size=wx.DefaultSize, 
 
17
                           style=wx.RESIZE_BORDER|wx.CAPTION|wx.CLOSE_BOX|
 
18
                           wx.SYSTEM_MENU, pos=(100, 100))
 
19
        self.parent = parent
 
20
        self.panel = wx.Panel(self)
 
21
        self.projs = projs
 
22
        self.read_only=False
 
23
        self.szrMain = wx.BoxSizer(wx.VERTICAL)
 
24
        lblChoose = wx.StaticText(self.panel, -1, "Choose an existing project ...")
 
25
        self.dropProjs = wx.Choice(self.panel, -1, choices=self.projs)
 
26
        self.dropProjs.SetSelection(0)
 
27
        self.StoreProjName(self.projs[0])
 
28
        self.dropProjs.Bind(wx.EVT_CHOICE, self.OnProjSelect)
 
29
        self.btnEdit = wx.Button(self.panel, wx.ID_EDIT)
 
30
        self.btnEdit.Bind(wx.EVT_BUTTON, self.OnEdit)
 
31
        szrExistingTop = wx.BoxSizer(wx.HORIZONTAL)
 
32
        szrExistingTop.Add(self.dropProjs, 1, wx.GROW|wx.RIGHT, 10)
 
33
        szrExistingTop.Add(self.btnEdit, 0)
 
34
        self.GetNotes(fil_proj=self.projs[0])
 
35
        self.txtProjNotes = wx.TextCtrl(self.panel, -1, self.proj_notes,
 
36
                                        style=wx.TE_MULTILINE|wx.TE_READONLY, 
 
37
                                        size=(400, 90))
 
38
        bxExisting = wx.StaticBox(self.panel, -1, "Existing Projects")
 
39
        szrExisting = wx.StaticBoxSizer(bxExisting, wx.VERTICAL)
 
40
        
 
41
        szrExisting.Add(szrExistingTop, 0, wx.GROW|wx.ALL, 10)
 
42
        szrExisting.Add(self.txtProjNotes, 1, wx.GROW|wx.ALL, 10)
 
43
        bxNew = wx.StaticBox(self.panel, -1, "")
 
44
        szrNew = wx.StaticBoxSizer(bxNew, wx.HORIZONTAL)
 
45
        lblMakeNew = wx.StaticText(self.panel, -1, "... or make a new project")
 
46
        btnMakeNew = wx.Button(self.panel, wx.ID_NEW)
 
47
        btnMakeNew.Bind(wx.EVT_BUTTON, self.OnNewClick)
 
48
        szrNew.Add(lblMakeNew, 1, wx.GROW|wx.ALL, 10)
 
49
        szrNew.Add(btnMakeNew, 0, wx.ALL, 10)
 
50
        self.SetupButtons()
 
51
        self.szrMain.Add(lblChoose, 0, wx.ALL, 10)
 
52
        self.szrMain.Add(szrExisting, 1, wx.LEFT|wx.BOTTOM|wx.RIGHT|wx.GROW, 10)
 
53
        self.szrMain.Add(szrNew, 0, wx.GROW|wx.LEFT|wx.BOTTOM|wx.RIGHT, 10)
 
54
        self.szrMain.Add(self.szrButtons, 0, wx.ALL, 10)
 
55
        self.panel.SetSizer(self.szrMain)
 
56
        self.szrMain.SetSizeHints(self)
 
57
        self.Layout()
 
58
    
 
59
    def StoreProjName(self, proj_name):
 
60
        "NB must have .proj on end"
 
61
        #print proj_name # debug
 
62
        self.proj_name = proj_name
 
63
    
 
64
    def SetupButtons(self):
 
65
        """
 
66
        Must have ID of wx.ID_... to trigger validators (no event binding 
 
67
            needed) and for std dialog button layout.
 
68
        """
 
69
        btnCancel = wx.Button(self.panel, wx.ID_CANCEL) # 
 
70
        btnCancel.Bind(wx.EVT_BUTTON, self.OnCancel)
 
71
        btnOK = wx.Button(self.panel, wx.ID_OK)
 
72
        btnOK.Bind(wx.EVT_BUTTON, self.OnOK)
 
73
        btnOK.SetDefault()
 
74
        self.szrButtons = wx.StdDialogButtonSizer()
 
75
        self.szrButtons.AddButton(btnCancel)
 
76
        self.szrButtons.AddButton(btnOK)
 
77
        self.szrButtons.Realize()
 
78
        self.SetReadOnly()
 
79
    
 
80
    def GetNotes(self, fil_proj):
 
81
        f = open(os.path.join(LOCAL_PATH, "projs", fil_proj), "r")
 
82
        proj_dic = {}
 
83
        exec f in proj_dic
 
84
        f.close()
 
85
        # must always be stored, even if only ""
 
86
        self.proj_notes = proj_dic["proj_notes"]
 
87
    
 
88
    def OnProjSelect(self, event):
 
89
        proj_sel_id = self.dropProjs.GetSelection()
 
90
        self.SetNotes(proj_sel_id)
 
91
        self.SetReadOnly()
 
92
        event.Skip()
 
93
    
 
94
    def SetReadOnly(self):       
 
95
        proj_sel_id = self.dropProjs.GetSelection() 
 
96
        self.read_only = (self.projs[proj_sel_id] == \
 
97
                          projects.SOFA_DEFAULT_PROJ)
 
98
    
 
99
    def SetNotes(self, proj_sel_id):        
 
100
        self.GetNotes(self.projs[proj_sel_id])
 
101
        self.txtProjNotes.SetValue(self.proj_notes)
 
102
           
 
103
    def OnEdit(self,event):
 
104
        dlgProj = projects.ProjectDlg(parent=self, read_only=self.read_only,
 
105
                          fil_proj=self.projs[self.dropProjs.GetSelection()])
 
106
        # refresh projects list and display accordingly
 
107
        ret_val = dlgProj.ShowModal()
 
108
        if ret_val == wx.ID_DELETE:
 
109
            # redo and pick 1st
 
110
            self.projs = projects.GetProjs()
 
111
            self.dropProjs.SetItems(self.projs)
 
112
            self.dropProjs.SetSelection(0)
 
113
            self.SetNotes(0)
 
114
            self.SetReadOnly()
 
115
        elif ret_val == wx.ID_OK:
 
116
            self.SetToNameFromOK()
 
117
          
 
118
    def OnNewClick(self, event):
 
119
        dlgProj = projects.ProjectDlg(parent=self, read_only=self.read_only)
 
120
        ret_val = dlgProj.ShowModal()
 
121
        if ret_val == wx.ID_OK:
 
122
            self.SetToNameFromOK()
 
123
 
 
124
    def SetToNameFromOK(self):
 
125
        # redo choices and display record with new name
 
126
        self.projs = projects.GetProjs()
 
127
        self.dropProjs.SetItems(self.projs)
 
128
        # get index of new name
 
129
        # NB proj name should have been set by projects
 
130
        proj_sel_id = self.projs.index(self.proj_name)
 
131
        self.dropProjs.SetSelection(proj_sel_id) # may have changed name
 
132
        self.SetNotes(proj_sel_id)
 
133
        self.SetReadOnly()
 
134
 
 
135
    def OnCancel(self, event):
 
136
        self.Destroy()
 
137
    
 
138
    def OnOK(self, event):
 
139
        proj_sel_id = self.dropProjs.GetSelection()
 
140
        fil_proj = self.projs[proj_sel_id]
 
141
        try:
 
142
            proj_name = fil_proj[:-5]
 
143
            self.parent.SetProj(proj_name)
 
144
        except Exception:
 
145
            pass
 
146
        self.Destroy()
 
147
 
 
148
#"""
 
149
if __name__ == "__main__":
 
150
    app = wx.PySimpleApp()
 
151
    myframe = ProjSelectDlg(None, ["MOH.proj", "test.proj"])
 
152
    myframe.Show()
 
153
    app.MainLoop()
 
154
#"""