Ran update_copyright.py, updating all the copyright blurbs and adding AUTHORS.
[hooke.git] / hooke / ui / gui / hookeresults.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 import sys
20 import wx
21 from wx.lib.mixins.listctrl import CheckListCtrlMixin
22
23 import prettyformat
24
25 class CheckListCtrl(wx.ListCtrl, CheckListCtrlMixin):
26     def __init__(self, parent):
27         wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT)
28         CheckListCtrlMixin.__init__(self)
29         self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
30
31     def OnItemActivated(self, evt):
32         self.ToggleItem(evt.m_itemIndex)
33
34
35 class Results(wx.Panel):
36     def __init__(self, parent):
37         wx.Panel.__init__(self, parent, -1)
38         self.results_list = CheckListCtrl(self)
39         sizer = wx.BoxSizer()
40         sizer.Add(self.results_list, 1, wx.EXPAND)
41         self.SetSizer(sizer)
42         self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.results_list)
43         self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.results_list)
44         #self.results_list.OnCheckItem = self.OnCheckItem
45
46     def _GetWidthInPixels(self, text):
47         #TODO:
48         #Returns the width of a string in pixels
49         #Unfortunately, it does not work terribly well (although it should).
50         #Thus, we have to add a bit afterwards.
51         #Annoys the heck out of me.
52         font = self.results_list.GetFont()
53         dc = wx.WindowDC(self.results_list)
54         dc.SetFont(font)
55         width, height = dc.GetTextExtent(text)
56         return width
57
58     def ClearResults(self):
59         self.results_list.ClearAll()
60
61     def DisplayResults(self, results):
62         self.ClearResults()
63         header = results.header_as_list()
64         self.results_list.InsertColumn(0, 'Show')
65         for index, column in enumerate(header):
66             self.results_list.InsertColumn(index + 1, column, wx.LIST_FORMAT_RIGHT)
67
68         for result in results.results:
69             done = False
70             for index, column in enumerate(results.columns):
71                 value_str = results.get_pretty_value(column, result.result[column])
72                 if not done:
73                     index_col = self.results_list.InsertStringItem(sys.maxint, '')
74                     done = True
75                 column_width = len(self.results_list.GetColumn(index + 1).GetText())
76                 value_str = value_str.center(column_width)
77                 self.results_list.SetStringItem(index_col, index + 1, value_str)
78                 #self.results_list.SetItemData(index, result)
79
80         for index, result in enumerate(results.results):
81             if result.visible:
82                 #if we use 'CheckItem' then 'UpdatePlot' is called (ie repeated updates)
83                 self.results_list.SetItemImage(index, 1)
84                 #self.results_list.CheckItem(index)
85         for index in range(self.results_list.GetColumnCount()):
86             column_text = self.results_list.GetColumn(index).GetText()
87             column_width = self._GetWidthInPixels(column_text)
88             self.results_list.SetColumnWidth(index, column_width + 15)
89
90     def OnItemSelected(self, evt):
91         pass
92
93     def OnItemDeselected(self, evt):
94         pass