File: gui_Right.py

package info (click to toggle)
fontypython 0.4.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,152 kB
  • ctags: 559
  • sloc: python: 3,619; makefile: 10
file content (373 lines) | stat: -rw-r--r-- 12,560 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
##	Fonty Python Copyright (C) 2006, 2007, 2008, 2009 Donn.C.Ingle
##	Contact: donn.ingle@gmail.com - I hope this email lasts.
##
##	This file is part of Fonty Python.
##	Fonty Python is free software: you can redistribute it and/or modify
##	it under the terms of the GNU General Public License as published by
##	the Free Software Foundation, either version 3 of the License, or
##	(at your option) any later version.
##
##	Fonty Python is distributed in the hope that it will be useful,
##	but WITHOUT ANY WARRANTY; without even the implied warranty of
##	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##	GNU General Public License for more details.
##
##	You should have received a copy of the GNU General Public License
##	along with Fonty Python.  If not, see <http://www.gnu.org/licenses/>.

import wx

## June 25th 2016
## Remarking these two lines because they are causing a segfault:
##  ../src/common/stdpbase.cpp(62): assert "traits" failed in Get(): 
##  create wxApp before calling this
##  Segmentation fault (core dumped)
##
##  I do not know how to test or fix this, hence simply removing it.
##  AFAICT, stock buttons will be in the system language.
##
## Setup wxPython to access translations : enables the stock buttons.
##langid = wx.LANGUAGE_DEFAULT # Picks this up from $LANG
##mylocale = wx.Locale( langid )

from pubsub import *
from wxgui import ps

from gui_PogChooser import *

import fpsys # Global objects
import fontyfilter
import fontybugs

## Fetch the dialogue classes - used for zip dir dialog.
import dialogues

class TargetPogChooser(wx.Panel):
	"""
	Far right-hand side control. Chooses target pogs. Houses control buttons.
	"""
	def __init__(self, parent):
		wx.Panel.__init__(self, parent, id = -1)	
		
		self.icon = wx.StaticBitmap( self, -1, wx.Bitmap(fpsys.mythingsdir + 'icon_target_16x16.png', wx.BITMAP_TYPE_PNG) )
		self.textInfo = wx.StaticText(self, -1, _("Target Pogs"), style = wx.ALIGN_LEFT)
		self.textInfo.SetFont(wx.Font(10, fpsys.DFAM, wx.NORMAL, wx.FONTWEIGHT_BOLD))
		
		s = None
		if fpsys.state.targetpattern == "P":
			s = fpsys.state.targetobject.name
			
		## The actual list control
		self.pogTargetlist = PogChooser(self, whoami="TARGETPOG", select = s) 
		
		## Subscriptions:
		ps.sub(target_pog_has_been_selected, self.OnPogTargetClick) ##DND: class TargetPogChooser
		ps.sub(clear_targetpog_selection, self.SelectNoTargetPog) ##DND: class TargetPogChooser
		
		## The "no pog" button
		self.idnone = wx.NewId()
		self.buttNoPog = wx.Button(self, label = _("Clear selection"), id = self.idnone)

		self.buttNoPog.SetToolTipString(_("Deselects any chosen Pogs."))
		## The buttons under the pog list
		## Giving them all id numbers so my single handler can tell them apart.
		self.idnew = wx.NewId()
		self.idinstall = wx.NewId()
		self.iduninstall = wx.NewId()
		self.iddelete = wx.NewId()
		#Sept 2009
		self.idzip = wx.NewId()

		self.buttNew = wx.Button(self, label = _("New Pog"), id = self.idnew ) 
		self.buttNew.SetToolTipString(_("Creates a new, empty Pog"))
		
		self.buttInstall = wx.Button(self, label = _("Install Pog"), id = self.idinstall ) 
		self.buttInstall.SetToolTipString(_("Installs all selected Pogs.\nUse SHIFT/CTRL+Click on the list above."))
		
		self.buttUninstall = wx.Button(self, label = _("Uninstall Pog"), id = self.iduninstall ) 
		self.buttUninstall.SetToolTipString(_("Uninstalls all selected Pogs.\nUse SHIFT/CTRL+Click on the list above."))
		
		self.buttDelete = wx.Button(self, label = _("Delete Pog") , id = self.iddelete) 
		self.buttDelete.SetToolTipString(_("Deletes the selected Pog(s)"))

		self.buttZip = wx.Button(self, label = _("Zip Pog(s)") , id = self.idzip) 
		self.buttZip.SetToolTipString(_("Save a zip file of the selected Pogs"))
		
		
		self.sizer = wx.BoxSizer(wx.VERTICAL)		 
		self.iconandtext = wx.BoxSizer(wx.HORIZONTAL)
		self.iconandtext.Add(self.icon, 0, wx.TOP | wx.BOTTOM, border = 4)
		self.iconandtext.Add(self.textInfo, 1, wx.EXPAND | wx.ALL, border = 4)
		self.sizer.Add(self.iconandtext, 0, wx.EXPAND)
		self.sizer.Add(self.pogTargetlist, 1, wx.EXPAND)
		self.sizer.Add(self.buttNoPog, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, border=5)	  
		self.sizer.Add(self.buttInstall, 0, wx.EXPAND) 
		self.sizer.Add(self.buttUninstall, 0, wx.EXPAND) 
		self.sizer.Add(self.buttNew, 0, wx.EXPAND) 
		self.sizer.Add(self.buttDelete, 0, wx.EXPAND) 
		self.sizer.Add(self.buttZip, 0, wx.EXPAND) 
		self.SetSizer(self.sizer)
		
		## Bind the events:
		e=wx.EVT_BUTTON # was wx.EVT_LEFT_UP
		self.buttNoPog.Bind(e, self.multiClick)
		self.buttNew.Bind(e, self.multiClick)
		self.buttInstall.Bind(e, self.multiClick)
		self.buttUninstall.Bind(e, self.multiClick)
		self.buttDelete.Bind(e, self.multiClick)
		self.buttZip.Bind(e, self.multiClick)
		
		self.toggleButtons()
		
	## Catch all the button clicks on the control.
	def multiClick(self, e):
		## NEW
		if e.GetId() == self.idnew: 
			## New Pog button pressed
			dlg = wx.TextEntryDialog(
					self, _("Enter a name for the new Pog"),
					_("New Pog"), _("Fonty Python"))
			dlg.SetValue("")
			if dlg.ShowModal() == wx.ID_OK:
				nam = dlg.GetValue()
				if nam == "":
					ps.pub( show_message, _("A Pog with no name won't be created, however it was a good try!") )
				## Is it unique?
				elif fpsys.isPog(nam):
					## Nope, it's there already
					ps.pub(show_message, _("%s already exists.") % nam)
				else:
					## We have a winner.
					## Make a pog object and then write it,
					ipog = fontcontrol.Pog(nam)
					try:
						ipog.write()
					except fontybugs.PogWriteError, e:
						ps.pub(show_error_and_abort, unicode( e ))
					del ipog
					## Now put it into the list
					self.pogTargetlist.AddItem(nam)
					#ps.pub(add_item_to_notebook, nam)
					ps.pub( add_pog_item_to_source, nam )
					ps.pub( update_font_view )
			dlg.Destroy()
			return
		
		## A list of multiple-selected pog names:
		tl = self.pogTargetlist
		multipogs=[ tl.GetItemText(i) for i in xrange(tl.GetItemCount()) if tl.IsSelected(i)]

		## DELETE
		if e.GetId() == self.iddelete:
			## Selected Pog(s) to be deleted:
			tokill = multipogs
			iPogsToKill = []
			##Is any one of those installed?
			allok=True
			for p in tokill:
				## Michael Hoeft tested pog delete and noticed the bug. Thanks.
				## Fixed Oct 2009
				iPog = fontcontrol.Pog(p)
				if iPog.isInstalled():
					ps.pub( show_error, _("One or more selected Pogs is installed, fix your selection and try again.") )
					allok=False
					break
				iPogsToKill.append( iPog )

			if allok:
				## Build a string for reporting in the dialog
				if len(tokill) > 1: 
					pogname=""
					for f in tokill[:-1]:
						pogname += u"%s," % f
					pogname += tokill[-1] #We want "remove blah,bloop,zoom, are you sure?"
				else:
					pogname=tokill[0]

				dlg = wx.MessageDialog(self, _("Remove %s, are you sure?") % pogname,
									   _("Are you sure?"),
									   wx.YES_NO | wx.ICON_INFORMATION
									  )
				if dlg.ShowModal() == wx.ID_YES:
					## Let's do them in:
					for victim in iPogsToKill: #tokill:
						#fpsys.instantiateTargetPog(victim) #Makes the fpsys.state.targetobject
						## Now kill the file on disk:
						try:
							victim.delete()
							#fpsys.state.targetobject.delete()
						except fontybugs.PogCannotDelete, e:
							ps.pub(show_error, unicode( e ))
							return
						## Remove from the lists:
						self.pogTargetlist.RemoveItem(victim.name)
						ps.pub( remove_pog_item_from_source, victim.name)

						## What if it was ALSO our view object?
						if fpsys.state.viewobject.label() == victim.name:
							## It was! We must declare it Empty.
							fpsys.SetViewPogToEmpty()
					
					## Now re-draw things
					ps.pub(update_font_view)
					dlg.Destroy()

					## Select no pog.
					self.SelectNoTargetPog()
			return
		
		## NO POG pressed
		if e.GetId() == self.idnone:
			## Select No Pog button pressed
			if fpsys.state.targetobject is None: return #Already done.
			self.SelectNoTargetPog()
			ps.pub(update_font_view)
			return #No need to tell mainframe about this.
			


		## Prepare for Install/Uninstall POG

		## install or uninstall all selected pogs - caters for multiple pog selections
		## the instantiateTargetPog must be called on each pog-name in the list
		## to setup the globals in fpsys. This is new from my previous one-selection only
		## code which assumed that instantiateTargetPog had been called already (when pog
		## was selected by the mouse)

		## Install
		if e.GetId() == self.idinstall:
			wx.BeginBusyCursor()
			for p in multipogs: 
				fpsys.instantiateTargetPog(p) # sets up fpsys.state.targetobject
				
				ok=True
				try:
					fpsys.state.targetobject.install()
				except (fontybugs.PogSomeFontsDidNotInstall), er:
					## Show a warning, but continue.
					ps.pub( show_error, unicode(er) ) 
				except (fontybugs.PogEmpty, fontybugs.PogAllFontsFailedToInstall), er:
					## Either Pog is empty, or
					## not a single font in this pog actually installed.
					## It has already been flagged as NOT INSTALLED
					ps.pub( show_error, unicode(er) )
					ok=False
				except (fontybugs.NoFontsDir), er:
					ps.pub( show_error, unicode(er) )
					ok=False

				if ok:
					## Update GUI

					## July 2016
					## =========
					## Added this test to sync view and target
					## objects IF they happen to be the samePog
					if fpsys.state.samepogs:
						# We installed RHS, so need to set that in RHS
						# which is a sep object.
						fpsys.state.viewobject.setInstalledFlag(True)
					ps.pub( change_pog_icon )
					self.toggleButtons()
					ps.pub( update_font_view )

			wx.EndBusyCursor()
			
		## Uninstall
		if e.GetId() == self.iduninstall:
			wx.BeginBusyCursor()
			for p in multipogs:
				fpsys.instantiateTargetPog(p)

				ok=True
				try:
					fpsys.state.targetobject.uninstall()
				except (fontybugs.PogEmpty, 
								fontybugs.PogNotInstalled, 
								fontybugs.PogLinksRemain
							 ), er:
					## PogNotInstalled is prevented by buttons greying out in the gui.
					ps.pub( show_error, unicode(er) )
					ok=False
				if ok:
					## Update GUI

					## July 2016
					## =========
					## Added this test to sync view and target
					## objects IF they happen to be the samePog
					if fpsys.state.samepogs:
						# We uninstalled RHS, so need to set that in RHS
						# which is a sep object.
						fpsys.state.viewobject.setInstalledFlag(False)
					ps.pub( change_pog_icon )
					self.toggleButtons()
					ps.pub( update_font_view )

			wx.EndBusyCursor()	

		## Sep 2009 : ZIP POGS 
		if e.GetId() == self.idzip: 
			dlg = dialogues.LocateDirectory( self )
			ok=False
			if dlg.ShowModal() == wx.ID_OK:
				todir=dlg.GetPath()
				ok=True
			dlg.Destroy()
			if ok:
				wx.BeginBusyCursor()
				
				for p in multipogs:
					ipog = fontcontrol.Pog(p)
					bugs=ipog.zip( todir )

				wx.EndBusyCursor()	
				extra=""
				if bugs: extra=_("Some fonts were skipped, try purging the Pog(s) involved.")
				ps.pub(print_to_status_bar,_("Zip file(s) have been created.%s") % extra )
			else:
				ps.pub(print_to_status_bar,_("Zip cancelled."))


	def OnPogTargetClick(self, args):
		"""
		args[0] pogname
		args[1] is pognochange
		"""
		## Made it so a second click on a target pog will unselect it.
		if args[1]: #pognochange = True, so let's deselect this pog
			self.SelectNoTargetPog()
			ps.pub(update_font_view)
			return
		try:
			fpsys.instantiateTargetPog(args[0])
		except fontybugs.PogInvalid, e:
			ps.pub(show_error_and_abort, unicode( e ))
			return

		ps.pub(update_font_view)
		self.toggleButtons()
		
	def toggleButtons(self):
		## If this is a no target pog situation, hide 'em all.
		if fpsys.state.targetobject is None:
			self.buttDelete.Enable(False)
			self.buttInstall.Enable(False)
			self.buttUninstall.Enable(False)
			self.buttZip.Enable(False)
			return 
		installed = fpsys.state.targetobject.isInstalled()
		self.buttDelete.Enable(not(installed)) # DELETE button is inverse of installed status
		self.buttInstall.Enable(not(installed)) # INSTALL button is inverse 
		self.buttUninstall.Enable(installed) # UNINSTALL = True if pog is installed.
		self.buttZip.Enable(True)
		
	def SelectNoTargetPog(self):
		wx.BeginBusyCursor()
		## Go figure out what item gets what image
		self.pogTargetlist.SortOutTheDamnImages( pognochange = True )
		self.pogTargetlist.ClearSelection() #Select nothing.
		
		fpsys.SetTargetPogToNone()  # Tell fpsys that we have no pog target selected	
		self.toggleButtons() # Disable the buttons on the bottom right.		
		wx.EndBusyCursor()