1 # common.py: global variables
2 # arch-tag: global variables
3 # author: Alberto Griggio <albgrig@tiscalinet.it>
7 import os
, sys
, locale
, Image
12 if os
.path
.expanduser('~') != '~':
13 bookmarks_file
= os
.path
.expanduser('~/.photoreider/bookmarks')
14 config_file
= os
.path
.expanduser('~/.photoreider/config')
15 confdir
= os
.path
.expanduser('~/.photoreider')
16 if not os
.path
.exists(confdir
):
17 try: os
.mkdir(confdir
)
18 except (IOError, OSError): pass # this is not fatal...
20 confdir
= os
.path
.dirname(sys
.argv
[0])
21 bookmarks_file
= os
.path
.join(confdir
, 'bookmarks')
22 config_file
= os
.path
.join(confdir
, 'config')
24 config
= None # ConfigParser instance used to load/store options
26 interpolations
= [ Image
.NEAREST
, Image
.BILINEAR
, Image
.BICUBIC
]
29 'GIF': ('file_gif.xpm', (208, 232, 208)),
30 'ICO': ('file_ico.xpm', (249, 240, 208)),
31 'JPEG': ('file_jpg.xpm', (224, 232, 192)),
32 'PCX': ('file_pcx.xpm', (216, 231, 216)),
33 'PNG': ('file_png.xpm', (224, 216, 208)),
34 'PNM': ('file_pnm.xpm', (218, 237, 192)),
35 'PSD': ('file_psd.xpm', (255, 255, 223)),
36 'TIF': ('file_tif.xpm', (200, 200, 213)),
37 'XBM': ('file_xbm.xpm', (224, 224, 224)),
38 'XCF': ('file_xcf.xpm', (191, 239, 233)),
39 'XPM': ('file_xpm.xpm', (222, 217, 234)),
40 'BMP': ('file_bmp.xpm', (229, 213, 213)),
42 default_icon_and_color
= ('file_image.xpm', (240, 240, 240))
43 unknown_icon_and_color
= ('file_unknown.xpm', (255, 255, 255))
55 def format_size_str(number
):
56 sf
= ['bytes', 'KB', 'MB', 'GB']
59 while number
> 1000 and i
< 4:
60 number
= number
/ 1024.0
62 return '%s %s' % (locale
.format('%.1f', number
), sf
[i
])
65 def get_mask(pil_image
, _mask_table
=[0]*128 + [255]*128):
67 If the image has some transparency, returns a wx.Mask object used to mask
68 the transparent pixels, otherwise returns None
69 The function should always be called with only one argument
71 if pil_image
.mode
== 'RGBA':
72 alpha
= pil_image
.split()[3]
73 mask
= wx
.EmptyImage(*alpha
.size
)
74 #mask.SetData(alpha.convert('1').convert('RGB').tostring())
75 mask
.SetData(alpha
.point(_mask_table
, '1').convert('RGB').tostring())
76 return wx
.Mask(wx
.BitmapFromImage(mask
, 1))
77 elif pil_image
.mode
== 'P':
78 # let's try to get the transparency value...
79 transparency
= pil_image
.info
.get('transparency')
82 palette
[transparency
*3 : transparency
*3 + 3] = [0, 0, 0]
83 pil_image
.putpalette(palette
)
84 mask
= wx
.EmptyImage(*pil_image
.size
)
85 mask
.SetData(pil_image
.convert('1').convert('RGB').tostring())
86 return wx
.Mask(wx
.BitmapFromImage(mask
, 1))
90 # custom event to update the menubar when the sorting of the PictureList
92 _SORT_CHANGED_EVENT
= wx
.NewEventType()
94 class _SortChangedEvent(wx
.PyEvent
):
96 wx
.PyEvent
.__init
__(self
)
97 self
.SetEventType(_SORT_CHANGED_EVENT
)
98 self
.sort_index
= sort_index
99 self
.reverse_sort
= reverse_sort
101 # end of class _SortChangedEvent
105 def EVT_SORT_CHANGED(win
, func
):
106 global _win_to_post
; _win_to_post
= win
107 win
.Connect(-1, -1, _SORT_CHANGED_EVENT
, func
)
109 def send_sort_changed_event():
110 wx
.PostEvent(_win_to_post
, _SortChangedEvent())
113 _exiting_lock
= threading
.RLock()
116 def exiting(val
=None):
118 _exiting_lock
.acquire()
119 if val
is not None: _is_exiting
= val
121 _exiting_lock
.release()
124 exit_app
= None # reference to a function called to exit the app nicely