~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
import wx
import sys
import pprint

import getdata
import projects
import table_edit


class DataSelectDlg(wx.Dialog):
    def __init__(self, parent, proj_name):
        """
        NB self.dbe etc are all set during getdata.setupDataDropdowns
            because it calls getDbDetsObj to get the bits it needs
            and gets all the rest at the same time.
        No point getting them again in a more explicit way.
        """
        title = "Data in \"%s\" Project" % proj_name
        wx.Dialog.__init__(self, parent=parent, title=title,
                           size=(500, 300), 
                           style=wx.CAPTION|wx.CLOSE_BOX|
                           wx.SYSTEM_MENU, pos=(100, 100))
        self.parent = parent
        self.panel = wx.Panel(self)
        lblfont = wx.Font(11, wx.SWISS, wx.NORMAL, wx.BOLD)
        self.szrMain = wx.BoxSizer(wx.VERTICAL)
        lblChoose = wx.StaticText(self.panel, -1, 
                                  "Choose an existing data table ...")
        proj_dic = projects.GetProjSettingsDic(proj_name=proj_name)
        self.var_labels, self.var_notes, self.val_dics = \
            projects.GetLabels(proj_dic["fil_labels"])
        getdata.setupDataDropdowns(self, self.panel, proj_dic["default_dbe"], 
                             proj_dic["conn_dets"], proj_dic["default_dbs"], 
                             proj_dic["default_tbls"])
        self.dropDatabases.Bind(wx.EVT_CHOICE, self.OnDatabaseSel)
        self.dropTables.Bind(wx.EVT_CHOICE, self.OnTableSel)
        self.chkReadOnly = wx.CheckBox(self.panel, -1, "Read Only")
        self.chkReadOnly.SetValue(True)
        self.btnOpen = wx.Button(self.panel, wx.ID_OPEN)
        self.btnOpen.Bind(wx.EVT_BUTTON, self.OnOpen)
        szrExistingTop = wx.BoxSizer(wx.HORIZONTAL)
        lblDbs = wx.StaticText(self.panel, -1, "Databases:")
        lblDbs.SetFont(lblfont)
        szrExistingTop.Add(lblDbs, 0, wx.RIGHT, 5)
        szrExistingTop.Add(self.dropDatabases, 0)
        self.dropDatabases.Bind(wx.EVT_CHOICE, self.OnDatabaseSel)
        szrExistingBottom = wx.BoxSizer(wx.HORIZONTAL)
        lblTbls = wx.StaticText(self.panel, -1, "Data tables:")
        lblTbls.SetFont(lblfont)
        szrExistingBottom.Add(lblTbls, 0, wx.RIGHT, 5)
        szrExistingBottom.Add(self.dropTables, 1, wx.GROW|wx.RIGHT, 10)
        szrExistingBottom.Add(self.chkReadOnly, 0, wx.TOP|wx.LEFT, 5)
        szrExistingBottom.Add(self.btnOpen, 0)
        bxExisting = wx.StaticBox(self.panel, -1, "Existing data tables")
        szrExisting = wx.StaticBoxSizer(bxExisting, wx.VERTICAL)
        szrExisting.Add(szrExistingTop, 0, wx.GROW|wx.ALL, 10)
        szrExisting.Add(szrExistingBottom, 0, 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 data table")
        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.lblFeedback = wx.StaticText(self.panel, -1, "")
        self.szrButtons.Add(self.lblFeedback, 0, wx.LEFT|wx.TOP, 10)
        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 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()

    def AddFeedback(self, feedback):
        self.lblFeedback.SetLabel(feedback)
        wx.Yield()
        
    def OnDatabaseSel(self, event):
        getdata.ResetDataAfterDbSel(self)
    
    def OnTableSel(self, event):
        getdata.ResetDataAfterTblSel(self)
    
    def OnOpen(self, event):
        ""
        if not self.has_unique:
            wx.MessageBox("Table \"%s\" cannot be opened because it " % \
              self.tbl_name + \
              "lacks a unique index") # needed for caching even if read only
        else:
            read_only = self.chkReadOnly.IsChecked()
            dlg = table_edit.TblEditor(self, self.dbe, self.conn, 
                                       self.cur, self.db, self.tbl_name, 
                                       self.flds, self.var_labels, self.idxs,
                                       read_only)
            dlg.ShowModal()
        event.Skip()
    
    def OnCancel(self, event):
        self.Destroy()
    
    def OnOK(self, event):
        self.Destroy()
        

if __name__ == "__main__":
    app = wx.PySimpleApp()
    myframe = DataSelectDlg(None, "MOH.proj")
    myframe.Show()
    app.MainLoop()