forked from tweecode/twine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatisticsdialog.py
97 lines (72 loc) · 3.67 KB
/
statisticsdialog.py
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
#!/usr/bin/env python
#
# StatisticsDialog
# A StatisticsDialog displays the number of characters, words,
# passages, links, and broken links in a StoryPanel.
#
# This is not a live count.
#
import wx, re, locale
import metrics
class StatisticsDialog (wx.Dialog):
def __init__ (self, parent, storyPanel, app, id = wx.ID_ANY):
wx.Dialog.__init__(self, parent, id, title = 'Story Statistics')
self.storyPanel = storyPanel
# layout
panel = wx.Panel(parent = self)
panelSizer = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(panelSizer)
# count controls
countPanel = wx.Panel(parent = panel)
countPanelSizer = wx.FlexGridSizer(5, 2, metrics.size('relatedControls'), metrics.size('relatedControls'))
countPanel.SetSizer(countPanelSizer)
self.characters = wx.StaticText(countPanel)
countPanelSizer.Add(self.characters, flag = wx.ALIGN_RIGHT)
countPanelSizer.Add(wx.StaticText(countPanel, label = 'Characters'))
self.words = wx.StaticText(countPanel)
countPanelSizer.Add(self.words, flag = wx.ALIGN_RIGHT)
countPanelSizer.Add(wx.StaticText(countPanel, label = 'Words'))
self.passages = wx.StaticText(countPanel)
countPanelSizer.Add(self.passages, flag = wx.ALIGN_RIGHT)
countPanelSizer.Add(wx.StaticText(countPanel, label = 'Passages'))
self.links = wx.StaticText(countPanel)
countPanelSizer.Add(self.links, flag = wx.ALIGN_RIGHT)
countPanelSizer.Add(wx.StaticText(countPanel, label = 'Links'))
self.brokenLinks = wx.StaticText(countPanel)
countPanelSizer.Add(self.brokenLinks, flag = wx.ALIGN_RIGHT)
countPanelSizer.Add(wx.StaticText(countPanel, label = 'Broken Links'))
panelSizer.Add(countPanel, flag = wx.ALL | wx.ALIGN_CENTER, border = metrics.size('relatedControls'))
okButton = wx.Button(parent = panel, label = 'OK')
okButton.Bind(wx.EVT_BUTTON, lambda e: self.Close())
panelSizer.Add(okButton, flag = wx.ALL | wx.ALIGN_CENTER, border = metrics.size('relatedControls'))
panelSizer.Fit(self)
size = self.GetSize()
if size.width < StatisticsDialog.MIN_WIDTH:
size.width = StatisticsDialog.MIN_WIDTH
self.SetSize(size)
self.count()
panelSizer.Layout()
self.SetIcon(app.icon)
self.Show()
def count (self):
"""
Sets values for the various counts.
"""
# have to do some trickery here because Python doesn't do
# closures the way JavaScript does
counts = { 'words': 0, 'chars': 0, 'passages': 0, 'links': 0, 'brokenLinks': 0 }
def count (widget, counts):
counts['chars'] += len(widget.passage.text)
counts['words'] += len(widget.passage.text.split(None))
counts['passages'] += 1
counts['links'] += len(widget.passage.links())
counts['brokenLinks'] += len(widget.getBrokenLinks())
self.storyPanel.eachWidget(lambda w: count(w, counts))
for key in counts:
counts[key] = locale.format('%d', counts[key], grouping = True)
self.characters.SetLabel(str(counts['chars']))
self.words.SetLabel(str(counts['words']))
self.passages.SetLabel(str(counts['passages']))
self.links.SetLabel(str(counts['links']))
self.brokenLinks.SetLabel(str(counts['brokenLinks']))
MIN_WIDTH = 200 # total guesstimate