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