forked from arifwn/pyWRFChemEmiss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommons.py
executable file
·507 lines (443 loc) · 15.5 KB
/
commons.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""
Berisi widget custom yang sering dipakai
Arif Widi Nugroho
"""
import wx
import wx.lib.newevent
#import thread
import threading
from wx.lib.wordwrap import wordwrap
(ThreadProgressUpdateEvent, EVT_UPDATE_THREAD_PROGRESS) = wx.lib.newevent.NewEvent()
(ThreadProgressDoneEvent, EVT_THREAD_DONE) = wx.lib.newevent.NewEvent()
class ThreadProgressDialog(wx.Dialog):
def __init__(self, thread_proc, parent=None, id=-1, title="Processing"):
self.thread_proc = thread_proc
wx.Dialog.__init__(self, parent, id, title, style=wx.CAPTION)
self.panel = ThreadProgressPanel(self, thread_proc)
self.SetSize((350,150))
sizer = wx.BoxSizer()
self.SetSizer(sizer)
sizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)
def show_and_start(self):
self.Center()
self.Show()
self.panel.start()
class ThreadProgressPanel(wx.Panel):
def __init__(self, parent, thread_proc, id=-1):
wx.Panel.__init__(self, parent, id)
self.thread_proc = thread_proc
self.thread_proc.receiver = self
self.thread_id = None
main_sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(main_sizer)
self.gauge = wx.Gauge(self, -1, 50, (110, 95), (250, 25))
main_sizer.Add(self.gauge, 0, wx.ALL | wx.EXPAND, 5)
self.info = wx.StaticText(self, -1, "")
main_sizer.Add(self.info, 0, wx.ALL | wx.EXPAND, 5)
self.btn_abort = wx.Button(self, -1, "Cancel")
abort_sizer = wx.BoxSizer(wx.HORIZONTAL)
abort_sizer.Add(wx.BoxSizer(), 1, wx.ALL | wx.EXPAND, 5)
abort_sizer.Add(self.btn_abort, 0, wx.ALL | wx.EXPAND, 5)
main_sizer.Add(wx.BoxSizer(wx.VERTICAL), 1, wx.ALL | wx.EXPAND)
main_sizer.Add(wx.StaticLine(self, -1), 0, wx.ALL | wx.EXPAND, 5)
main_sizer.Add(abort_sizer, 0, wx.ALL | wx.EXPAND, 0)
self.Bind(EVT_UPDATE_THREAD_PROGRESS, self.OnUpdateProgress)
self.Bind(EVT_THREAD_DONE, self.OnDone)
self.Bind(wx.EVT_BUTTON, self.OnAbort, self.btn_abort)
def start(self):
try:
top = self.GetTopLevelParent().Parent.GetTopLevelParent()
top.Disable()
except:
print 'Disable fail'
# self.thread_id = thread.start_new_thread(self.thread_proc.run, ())
self.thread_proc.abort = False
self.thread_proc.queue = 0
self.thread_proc.start()
def OnUpdateProgress(self, event):
self.thread_proc.queue = self.thread_proc.queue - 1
self.gauge.Pulse()
wrp = wordwrap(event.info_str, self.GetSize()[0], wx.ClientDC(self))
self.info.SetLabel(wrp)
def OnDone(self, event):
"""tutup parent dialog"""
self.thread_proc.queue = self.thread_proc.queue - 1
try:
top = self.GetTopLevelParent().Parent.GetTopLevelParent()
top.Enable()
except:
print 'Disable fail'
try:
self.Parent.Show(False)
except:
print "close fail"
if self.thread_proc.abort:
wx.MessageBox(self.thread_proc.abort_msg, 'Failed!', wx.OK | wx.ICON_ERROR)
else:
wx.MessageBox(self.thread_proc.done_msg, 'Done!', wx.OK | wx.ICON_INFORMATION)
def OnAbort(self, event):
if self.thread_proc.isAlive() == False:
print "Error! Thread is KIA!"
self.thread_proc.abort = True
self.thread_proc.abort_msg = "Unexpected error occur!"
self.OnDone(None)
self.thread_proc.abort = True
self.btn_abort.SetLabel("Canceling...")
self.btn_abort.Disable()
class BaseThreadProc(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.receiver = None #set win sebagai receiver event
self.evt_progress = ThreadProgressUpdateEvent()
self.evt_done = ThreadProgressDoneEvent()
self.abort = False
self.done_msg = "Operation succeed!"
self.abort_msg = "Operation canceled!"
self.queue = 0
def progress(self, info_str):
if self.queue < 20:
pg = ThreadProgressUpdateEvent()
pg.info_str = info_str
wx.PostEvent(self.receiver, pg)
self.queue = self.queue + 1
# self.evt_progress.info_str = info_str
# wx.PostEvent(self.receiver, self.evt_progress)
def done(self):
dn = ThreadProgressDoneEvent()
wx.PostEvent(self.receiver, dn)
self.queue = self.queue + 1
exit()
# wx.PostEvent(self.receiver, self.evt_done)
def run(self):
pass
class SampleThreadProc(BaseThreadProc):
def __init__(self):
BaseThreadProc.__init__(self)
def run(self):
import time
for i in range(100):
time.sleep(0.1)
self.progress("Tick: {0}".format(i))
print 'tick', i
if self.abort:
print "Aborting..."
break
self.done()
# Textbox custom
class GrayTextCtrl(wx.TextCtrl):
def __init__(self, parent, id=-1, default_value="",
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.TE_RICH2, validator=None):
if validator==None:
validator = wx.DefaultValidator
wx.TextCtrl.__init__(self, parent, id, default_value,
pos, size, style=style,
validator=validator )
self.default_value = default_value
ln = len(self.default_value)
self.SetStyle(0,ln,wx.TextAttr("GRAY"))
self.SetToolTipString(default_value)
self.Bind(wx.EVT_LEFT_DOWN, self.EvtOnClick)
self.Bind(wx.EVT_RIGHT_DOWN, self.EvtOnClick)
self.Bind(wx.EVT_KILL_FOCUS, self.EvtKillFocus)
self.Bind(wx.EVT_SET_FOCUS, self.EvtFocus)
def set_num_value(self, num):
self.SetValue(num.__str__())
def color_reset(self):
self.SetBackgroundColour("white")
def color_pink(self):
self.SetBackgroundColour("pink")
def reset(self):
self.Value = self.default_value
ln = len(self.default_value)
self.SetStyle(0,ln,wx.TextAttr("GRAY"))
def EvtOnClick(self, event):
if self.Value == self.default_value:
self.Clear()
event.Skip()
def EvtFocus(self, event):
if self.Value == self.default_value:
ln = len(self.default_value)
self.SetStyle(0,ln,wx.TextAttr("BLACK"))
event.Skip()
def EvtKillFocus(self, event):
if self.Value == "":
self.Value = self.default_value
if self.Value == self.default_value:
ln = len(self.default_value)
self.SetStyle(0,ln,wx.TextAttr("GRAY"))
event.Skip()
def Validate(self):
return self.Validator.Validate(self.Parent)
def ValidateSilent(self):
return self.Validator.ValidateSilent(self.Parent)
def get_result(self):
txt = self.GetValue()
if txt == self.default_value:
return ""
else:
return txt
def get_result_int(self):
txt = self.GetValue()
if txt == self.default_value:
return None
else:
try:
res = int(txt)
return res
except:
return None
def get_result_float(self):
txt = self.GetValue()
if txt == self.default_value:
return None
else:
try:
res = float(txt)
return res
except:
return None
def get_eval_result_int(self):
txt = self.GetValue()
if txt == self.default_value:
return None
else:
try:
res_eval = eval(txt)
res = int(res_eval)
return res
except:
return None
def get_eval_result_float(self):
txt = self.GetValue()
if txt == self.default_value:
return None
else:
try:
res_eval = eval(txt)
res = float(res_eval)
return res
except:
return None
class EvalNumberTextValidator(wx.PyValidator):
def __init__(self, optional=False):
wx.PyValidator.__init__(self)
self.optional = optional
def Clone(self):
return EvalNumberTextValidator(self.optional)
def Validate(self, win):
tc = self.GetWindow()
val = tc.GetValue()
valid = True
try:
res_eval = eval(val)
float(res_eval)
except:
valid = False
if valid:
tc.SetBackgroundColour("white")
tc.Refresh()
return True
else:
if self.optional:
txt = ""
try:
txt = tc.default_value
except:
pass
if val==txt:
tc.SetBackgroundColour("white")
tc.Refresh()
return True
tc.SetBackgroundColour("pink")
tc.Refresh()
return False
def ValidateSilent(self, win):
tc = self.GetWindow()
val = tc.GetValue()
valid = True
try:
res_eval = eval(val)
float(res_eval)
except:
valid = False
if valid:
return True
else:
if self.optional:
txt = ""
try:
txt = tc.default_value
except:
pass
if val==txt:
return True
return False
class NumberTextValidator(wx.PyValidator):
def __init__(self, optional=False):
wx.PyValidator.__init__(self)
self.optional = optional
def Clone(self):
return NumberTextValidator(self.optional)
def Validate(self, win):
tc = self.GetWindow()
val = tc.GetValue()
valid = True
try:
float(val)
except:
valid = False
if valid:
tc.SetBackgroundColour("white")
tc.Refresh()
return True
else:
if self.optional:
txt = ""
try:
txt = tc.default_value
except:
pass
if val==txt:
tc.SetBackgroundColour("white")
tc.Refresh()
return True
tc.SetBackgroundColour("pink")
tc.Refresh()
return False
def ValidateSilent(self, win):
tc = self.GetWindow()
val = tc.GetValue()
valid = True
try:
float(val)
except:
valid = False
if valid:
return True
else:
if self.optional:
txt = ""
try:
txt = tc.default_value
except:
pass
if val==txt:
return True
return False
class CoordinateTextValidator(wx.PyValidator):
def __init__(self, optional=False):
wx.PyValidator.__init__(self)
self.optional = optional
def Clone(self):
return CoordinateTextValidator(self.optional)
def Validate(self, win):
tc = self.GetWindow()
val = tc.GetValue()
valid = True
try:
dat = val.split(',')
lat = float(dat[0])
lon = float(dat[1])
except:
valid = False
if valid:
tc.SetBackgroundColour("white")
tc.Refresh()
return True
else:
if self.optional:
txt = ""
try:
txt = tc.default_value
except:
pass
if val==txt:
tc.SetBackgroundColour("white")
tc.Refresh()
return True
tc.SetBackgroundColour("pink")
tc.Refresh()
return False
def ValidateSilent(self, win):
tc = self.GetWindow()
val = tc.GetValue()
valid = True
try:
dat = val.split(',')
lat = float(dat[0])
lon = float(dat[1])
except:
valid = False
if valid:
return True
else:
if self.optional:
txt = ""
try:
txt = tc.default_value
except:
pass
if val==txt:
return True
return False
class AlphaTextValidator(wx.PyValidator):
def __init__(self, default_text=""):
wx.PyValidator.__init__(self)
self.default_text = default_text
def Clone(self):
return AlphaTextValidator(self.default_text)
def Validate(self, win):
tc = self.GetWindow()
val = tc.GetValue()
valid = True
txt = self.default_text
if txt=="":
try:
txt=tc.default_value
except:
txt=""
if val==txt:
valid = False
elif len(val.strip())==0:
valid = False
if valid:
tc.SetBackgroundColour("white")
tc.Refresh()
return True
else:
tc.SetBackgroundColour("pink")
tc.Refresh()
return False
class ValidateError(Exception):
"""Exception yang diakibatkan oleh hasil validasi yang salah"""
pass
class BitmapPanel(wx.Panel):
"""
Panel untuk merender bitmap yang besar
Agar gambar tidak blank, taruh panel ini di dalam sizer yang
exclusive (hanya berisi panel ini saja)
------------------------------------------
self.panel_new = commons.BitmapPanel(self, -1, bmp, size=(bmp.GetWidth(), bmp.GetHeight()))
new_sizer = wx.BoxSizer(wx.HORIZONTAL)
new_sizer.SetMinSize((bmp.GetWidth(),bmp.GetHeight()))
new_sizer.Add(self.panel_new, 1, wx.ALL | wx.EXPAND, 0)
"""
def __init__(self, parent, id=-1, bitmap = None, size=wx.DefaultSize):
wx.Panel.__init__(self, parent, id, size=size)
self.bitmap = bitmap
self.draw_from_x = 0
self.draw_from_y = 0
self.brush_bg = wx.Brush("WHITE")
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.os_win = False
if 'wxMSW' in wx.PlatformInfo:
self.os_win = True
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.SetBackground(self.brush_bg)
dc.SetBackgroundMode(wx.TRANSPARENT)
if self.os_win==False:
dc.Clear()
dc.DrawBitmap(self.bitmap, self.draw_from_x, self.draw_from_y, True)