File: VolumeEditor.py

package info (click to toggle)
cain 1.10%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,848 kB
  • sloc: cpp: 49,612; python: 14,988; xml: 11,654; ansic: 3,644; makefile: 129; sh: 2
file content (132 lines) | stat: -rw-r--r-- 4,090 bytes parent folder | download | duplicates (4)
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
"""Implements the volume editor."""

import wx

class VolumeDialog(wx.Dialog):
    """Get the new volume."""
    
    def __init__(self, parent):
        """Construct."""
        wx.Dialog.__init__(self, parent, -1, 'Edit volume.')

        # Buttons.
        okButton = wx.Button(self, wx.ID_OK, 'OK')
        okButton.SetDefault()
        cancelButton = wx.Button(self, wx.ID_CANCEL, 'Cancel')
        buttons = wx.BoxSizer(wx.HORIZONTAL)
        buttons.Add(okButton, 0, wx.ALIGN_RIGHT, 5)
        buttons.Add(cancelButton, 0, wx.ALIGN_RIGHT, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        # Volume.
        sizer.Add(wx.StaticText(self, -1, 'Enter the new volume in liters.'),
                  0, wx.ALL, border=5)
        self.volume = wx.TextCtrl(self)
        sizer.Add(self.volume, 0, wx.ALL, border=5)
        # Update mass action propensities.
        self.update = wx.CheckBox(self, -1, 'Update mass action propensities.')
        sizer.Add(self.update, 0, wx.ALL, border=5)
        sizer.Add(buttons, 0, wx.ALIGN_RIGHT | wx.ALIGN_BOTTOM, 5)

        self.SetSizer(sizer)
        self.Layout()
        self.Fit()

class VolumeEditor(wx.Panel):
    """The volume editor."""
    
    def __init__(self, parent, application):
        """."""
        wx.Panel.__init__(self, parent)
        self.application = application

        button = wx.Button(self, label='Volume')
        self.volume = wx.StaticText(self, -1, '')
       
        # Layout with sizers.
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(button, 0, wx.ALL, 5)
        sizer.Add(self.volume, 0, wx.ALL, 5)
        # Add a little extra space to the volume string is not clipped.
        sizer.Add(wx.StaticText(self, -1, ' '), 0, wx.ALL, 5)
        
        self.Bind(wx.EVT_BUTTON, self.onEdit, button)

        self.SetSizer(sizer)
        sizer.Fit(self)

    def setModel(self, model):
        self.model = model
        if self.model:
            self.volume.SetLabel(str(self.model.volume))
        else:
            self.volume.SetLabel('')

    def onEdit(self, event):
        # Make sure that a model is selected.
        if not self.model:
            wx.MessageBox('Error: No model is selected.', 'Error')
            return
        # Volume dialog.
        dialog = VolumeDialog(self)
        if dialog.ShowModal() != wx.ID_OK:
            return
        volumeString = dialog.volume.GetValue()
        doUpdate = dialog.update.IsChecked()
        dialog.Destroy()
        # Check the volume.
        try:
            volume = float(volumeString)
        except:
            wx.MessageBox(volumeString + ' is not a floating-point number.',
                          'Error!', style=wx.OK|wx.ICON_EXCLAMATION)
            return
        if volume <= 0:
            wx.MessageBox(volumeString + ' is not a positive volume.',
                          'Error!', style=wx.OK|wx.ICON_EXCLAMATION)
            return
        # If we are updating the mass action propensities we need to parse
        # the model first.
        if doUpdate:
            self.application.syncModel(self.model.id)
            self.application.parseModel(self.model.id)
        # Set the volume.
        self.model.setVolume(volume, doUpdate)
        # Update the display.
        self.volume.SetLabel(str(self.model.volume))
        self.application.updateVolume(self.model.id)

# Test code.

class VolumeEditorModel:
    def __init__(self):
        self.id = None
        self.volume = 1.

    def setVolume(self, volume, doUpdate):
        self.volume = volume

class VolumeEditorFrame(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent, title='Volume Editor',
                          size=(200,100))
        editor = VolumeEditor(self, self)
        editor.setModel(VolumeEditorModel())

    def updateVolume(self, id):
        pass

    def syncModel(self, id):
        pass

    def parseModel(self, id):
        pass

def main():
    app = wx.PySimpleApp()
    frame = VolumeEditorFrame()
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()