Schedulator summary now predicts release dates based on your bounce goals,
[wvapps.git] / photoreider / common.py
blob5e9fe67be06891ccaaba4be43c4be6d86d990e58
1 # common.py: global variables
2 # arch-tag: global variables
3 # author: Alberto Griggio <albgrig@tiscalinet.it>
4 # license: GPL
6 import wx
7 import os, sys, locale, Image
8 import threading
10 __version__ = '0.3.4'
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...
19 else:
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 ]
28 icons_and_colors = {
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))
45 # sort indexes
46 SORT_NAME = 0
47 SORT_DATE = 1
48 SORT_SIZE = 2
49 SORT_TYPE = 3
51 sort_index = 0
52 reverse_sort = False
55 def format_size_str(number):
56 sf = ['bytes', 'KB', 'MB', 'GB']
57 i = 0
58 number = number
59 while number > 1000 and i < 4:
60 number = number / 1024.0
61 i += 1
62 return '%s %s' % (locale.format('%.1f', number), sf[i])
65 def get_mask(pil_image, _mask_table=[0]*128 + [255]*128):
66 """\
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
70 """
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')
80 if transparency:
81 palette = [255] * 768
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))
87 return None
90 # custom event to update the menubar when the sorting of the PictureList
91 # changes
92 _SORT_CHANGED_EVENT = wx.NewEventType()
94 class _SortChangedEvent(wx.PyEvent):
95 def __init__(self):
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
103 _win_to_post = None
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()
114 _is_exiting = False
116 def exiting(val=None):
117 global _is_exiting
118 _exiting_lock.acquire()
119 if val is not None: _is_exiting = val
120 retval = _is_exiting
121 _exiting_lock.release()
122 return retval
124 exit_app = None # reference to a function called to exit the app nicely