Skip to content

Commit 841cca0

Browse files
committed
Added HelpDialog and HelpInfoBase to manage Help funtion in applications
1 parent 6cde0c3 commit 841cca0

File tree

3 files changed

+104
-73
lines changed

3 files changed

+104
-73
lines changed

manatools/basehelpinfo.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# vim: set fileencoding=utf-8 :
2+
# vim: set et ts=4 sw=4:
3+
'''
4+
Python manatools.basehelpinfo contains all the Help information base class
5+
that should be use for help dialog
6+
7+
License: LGPLv2+
8+
9+
Author: Angelo Naselli <[email protected]>
10+
11+
@package manatools.basehelpinfo
12+
'''
13+
14+
class HelpInfoBase:
15+
def __init__(self):
16+
pass
17+
18+
def show(self, info_to_show):
19+
'''
20+
super class must implement show to return the right string to show
21+
into dialog
22+
@param info_to_show: a kind of index of what to show, it depends on implementation
23+
'''
24+
raise NotImplementedError("show is not implemented")
25+
26+
def home(self):
27+
'''
28+
super class must implement show to return the very first info to show
29+
into dialog, such as index for instance. Than index could be anchored and anchors passed
30+
to show() to display related content
31+
'''
32+
raise NotImplementedError("home is not implemented")

manatools/ui/helpdialog.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# vim: set fileencoding=utf-8 :
2+
# vim: set et ts=4 sw=4:
3+
'''
4+
Python manatools.ui.helpdialog contains all the HelpDialog class
5+
that should be use into a manatools application
6+
7+
License: LGPLv2+
8+
9+
Author: Angelo Naselli <[email protected]>
10+
11+
@package manatools.ui.helpdialog
12+
'''
13+
import webbrowser
14+
15+
import manatools.ui.basedialog as basedialog
16+
import manatools.basehelpinfo as helpdata
17+
import yui
18+
import gettext
19+
# https://pymotw.com/3/gettext/#module-localization
20+
t = gettext.translation(
21+
'python-manatools',
22+
'/usr/share/locale',
23+
fallback=True,
24+
)
25+
_ = t.gettext
26+
ngettext = t.ngettext
27+
28+
class HelpDialog(basedialog.BaseDialog):
29+
def __init__(self, info, title=_("Help dialog"), icon="", minWidth=80, minHeight=20):
30+
basedialog.BaseDialog.__init__(self, title, icon, basedialog.DialogType.POPUP, minWidth, minHeight)
31+
'''
32+
HelpDialog constructor
33+
@param title dialog title
34+
@param icon dialog icon
35+
@param minWidth > 0 mim width size, see libYui createMinSize
36+
@param minHeight > 0 mim height size, see libYui createMinSize
37+
'''
38+
if not isinstance(info, helpdata.HelpInfoBase):
39+
raise TypeError("info must be a HelpInfoBase instance")
40+
self.info = info
41+
42+
def UIlayout(self, layout):
43+
'''
44+
layout implementation called in base class to setup UI
45+
'''
46+
# URL events are sent as YMenuEvent by libyui
47+
self.eventManager.addMenuEvent(None, self.onURLEvent, False)
48+
self.text = self.factory.createRichText(layout, "", False)
49+
self.text.setValue(self.info.home())
50+
align = self.factory.createRight(layout)
51+
self.quitButton = self.factory.createPushButton(align, _("&Quit"))
52+
self.eventManager.addWidgetEvent(self.quitButton, self.onQuitEvent)
53+
54+
def onQuitEvent(self) :
55+
# BaseDialog needs to force to exit the handle event loop
56+
self.ExitLoop()
57+
58+
def onURLEvent(self, mEvent):
59+
url = mEvent.id()
60+
if url:
61+
text = self.info.show(url)
62+
if text:
63+
self.text.setValue(text)
64+
else:
65+
print("onURLEvent: running webbrowser", url)
66+
webbrowser.open(url, 2)

test/testHelpDialog.py

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
@package manatools
1212
'''
1313

14-
import manatools.ui.basedialog as basedialog
14+
import manatools.basehelpinfo as helpdata
15+
import manatools.ui.helpdialog as helpdialog
16+
1517
import yui
1618
import time
1719

@@ -21,29 +23,11 @@
2123
##
2224
######################################################################
2325

24-
class HelpInfoBase:
25-
def __init__(self):
26-
pass
2726

28-
def show(self, info_to_show):
29-
'''
30-
super class must implement show to return the right string to show
31-
into dialog
32-
@param info_to_show: a kind of index of what to show, it depends on implementation
33-
'''
34-
raise NotImplementedError("show is not implemented")
3527

36-
def home(self):
37-
'''
38-
super class must implement show to return the very first info to show
39-
into dialog, such as index for instance. Than index could be anchored and anchors passed
40-
to show() to display related content
41-
'''
42-
raise NotImplementedError("home is not implemented")
43-
44-
class HelpInfo(HelpInfoBase):
28+
class HelpInfo(helpdata.HelpInfoBase):
4529
def __init__(self):
46-
HelpInfoBase.__init__(self)
30+
helpdata.HelpInfoBase.__init__(self)
4731
index1 = '<b>%s</b>'%self._formatLink("Title 1", 'title1')
4832
index2 = '<b>%s</b>'%self._formatLink("Title 2", 'titleindex2')
4933
self.text = { 'home': "This text explain how to use manatools Help Dialog. <br><br>%s <br>%s"%(index1, index2),
@@ -76,61 +60,10 @@ def home(self):
7660
return self.text['home']
7761

7862

79-
class HelpDialog(basedialog.BaseDialog):
80-
def __init__(self, info):
81-
basedialog.BaseDialog.__init__(self, "Help dialog", "", basedialog.DialogType.POPUP, 80, 10)
82-
#### TODO check instance of HelpInfoBase
83-
self.info = info
84-
85-
def UIlayout(self, layout):
86-
'''
87-
layout implementation called in base class to setup UI
88-
'''
89-
90-
# Let's test a Menu widget
91-
menu = self.factory.createMenuButton(self.factory.createLeft(layout), "Test &menu")
92-
qm = yui.YMenuItem("&Quit")
93-
menu.addItem(qm)
94-
menu.rebuildMenuTree()
95-
sendObjOnEvent=True
96-
self.eventManager.addMenuEvent(qm, self.onQuitEvent, sendObjOnEvent)
97-
# URL events are sent as YMenuEvent by libyui
98-
self.eventManager.addMenuEvent(None, self.onURLEvent, False)
99-
100-
self.text = self.factory.createRichText(layout, "", False)
101-
self.text.setValue(self.info.home())
102-
103-
#let's test some buttons
104-
align = self.factory.createRight(layout)
105-
106-
# Let's test a quitbutton (same handle as Quit menu)
107-
self.quitButton = self.factory.createPushButton(align, "&Quit")
108-
self.eventManager.addWidgetEvent(self.quitButton, self.onQuitEvent, sendObjOnEvent)
109-
110-
# Let's test a cancel event
111-
self.eventManager.addCancelEvent(self.onCancelEvent)
112-
113-
def onCancelEvent(self) :
114-
print ("Got a cancel event")
115-
116-
def onQuitEvent(self, obj) :
117-
if isinstance(obj, yui.YItem):
118-
print ("Quit menu pressed")
119-
else:
120-
print ("Quit button pressed")
121-
# BaseDialog needs to force to exit the handle event loop
122-
self.ExitLoop()
123-
124-
def onURLEvent(self, mEvent):
125-
print("onURLEvent")
126-
url = mEvent.id()
127-
if url:
128-
self.text.setValue(self.info.show(url))
129-
13063
if __name__ == '__main__':
13164

13265
info = HelpInfo()
133-
td = HelpDialog(info)
66+
td = helpdialog.HelpDialog(info)
13467
td.run()
13568

13669

0 commit comments

Comments
 (0)