-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventer.py
1245 lines (1077 loc) · 43.5 KB
/
eventer.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
from math import floor
import json
import datetime as dt
import base64 as b64
from PyQt5.QtGui import QValidator, QRegExpValidator, QIntValidator, \
QImage, QPixmap, QIcon, QFontMetrics, \
QCloseEvent, QResizeEvent
from PyQt5.QtCore import Qt, QCoreApplication, QRegExp, QByteArray, QSize, \
QTimer
from PyQt5.QtWidgets import QApplication, QSystemTrayIcon, QWidget, \
QHBoxLayout, QVBoxLayout, QGridLayout, \
QLabel, QLineEdit, QTextEdit, QPushButton, \
QComboBox, QMenu, QSpacerItem, QScrollArea, \
QMessageBox, QSizePolicy
from lang import RU, EN
import icons
""" Language. """
langs = {'RU': RU, 'EN': EN} # avaliable languages
def langSelect(config, lang):
""" Change selected language. """
config.lang = langs[lang]
reload()
""" Timedelta formatting. """
def getFormattedStrTime(time, t_, td, tf, ts):
""" Format 'time' to string with t_, td, tf and ts. """
if time == 0:
return 0
t_dec, t_uni = int(time / 10) % 10, time % 10
if t_dec == 0 and t_uni == 1:
frmtd = '{} {}'.format(time, t_) # 1
elif t_dec > 1 and t_uni == 1:
frmtd = '{} {}'.format(time, td) # 21, 31, ...
elif t_uni > 1 and t_uni < 5 and t_dec != 1:
frmtd = '{} {}'.format(time, tf) # 2, 3, 4, 22, 23, 24, 32, 33, ...
else:
frmtd = '{} {}'.format(time, ts) # 5 to 20, 25 to 30, 35 to 40, ...
return frmtd
def parseSeconds(num, dic=False):
""" Format seconds to string includes days, hours, minutes and seconds. """
days = floor(num / 86400)
hours = floor(num % 86400 / 3600)
minutes = floor(num % 86400 % 3600 / 60)
seconds = floor(num % 86400 % 3600 % 60)
if dic:
return {'days': days, 'hours': hours,
'minutes': minutes, 'seconds': seconds}
days = getFormattedStrTime(days, conf.lang.DAY_, conf.lang.DAYD,
conf.lang.DAYF, conf.lang.DAYS)
hours = getFormattedStrTime(hours, conf.lang.HOUR_, conf.lang.HOURD,
conf.lang.HOURF, conf.lang.HOURS)
minutes = getFormattedStrTime(minutes, conf.lang.MINUTE_, conf.lang.MINUTED,
conf.lang.MINUTEF, conf.lang.MINUTES)
seconds = getFormattedStrTime(seconds, conf.lang.SECOND_, conf.lang.SECONDD,
conf.lang.SECONDF, conf.lang.SECONDS)
text = ''
text += days if days != 0 else ''
text += ', ' if (days != 0 and hours != 0 and minutes != 0
and seconds != 0) else ''
text += hours if hours != 0 else ''
text += ', ' if (hours != 0 and minutes != 0 and seconds != 0) else ''
text += minutes if minutes != 0 else ''
text += ', ' if (minutes != 0 and seconds != 0) else ''
text += seconds if seconds != 0 else ''
if text == '':
text = '{} {}'.format(seconds, conf.lang.SECONDS)
return text
""" Icons. """
class Icon():
""" Icon.
Parameters
----------
byte : base64-encoded byte string
Set attribute 'base64' to 'byte'.
icon : QIcon
Set attribute 'icon' to 'icon'.
Attributes
----------
base64 : base64-encoded byte string
Encoded pixmap of icon.
icon : QIcon
Icon itself.
"""
def __init__(self, byte=b'', icon=None):
self.base64 = byte
self.icon = icon
def setBytes(self, byte):
""" Set attribute 'base64' to 'byte'. """
self.base64 = byte
return self
def setIcon(self, icon):
""" Set attribute 'icon' to 'icon'. """
self.icon = icon
return self
def convertToIcon(self):
""" Set 'icon' by converting 'base64' to QIcon. """
byte = QByteArray().fromBase64(self.base64)
image = QImage().fromData(byte, "ico");
self.icon = QIcon(QPixmap().fromImage(image))
return self
def getIcon(self):
""" Return 'icon'. """
return self.icon
def getBytes(self):
""" Return 'base64'. """
return self.base64
def convertToBytes(self):
""" Set 'base64' by converting 'icon' to bytes. """
pass # work in progress
""" Config class. """
class Config:
""" Config element.
Parameters and attributes
-------------------------
lang : Language object
Defines language of application.
tdelta : int
Defines time delta for repeating tasks, seconds.
filter: bool
If True filter in EditWindow will be shown.
backup : int
Timeout of backup timer.
mesout : int
Timeout of message timer.
Attribute
---------
supported : tuple
Defines appropriate input parameters.
"""
supported = ('lang', 'tdelta', 'filter', 'backup', 'mesout')
def __init__(self, lang=RU, tdelta=600, filter=True, backup=300,
mesout=60):
self.lang = lang
self.tdelta = tdelta
self.filter = filter
self.backup = backup
self.mesout = mesout
def __str__(self):
""" Represent current configuration. """
dic = {}
for i in self.supported:
dic[i] = str(getattr(self, i)) if \
hasattr(getattr(self, i), '__dict__') else \
getattr(self, i)
return json.dumps(dic)
def load(self, dic):
""" Load attributes from given dictionary. """
for i in dic:
if i not in self.supported:
pass
else:
setattr(self, i, dic[i])
def dump(self, f):
""" Write current configuration to config file. """
try:
with open(f, 'w') as f_:
f_.write(str(self))
except IOError:
errors.append(['{} "{}"'.format(self.lang['CANT_OPEN_FILE'], f),
'IOError: Can\'t open file!'])
conf = Config() # Config object
""" Preliminary definitions. """
errors = [] # see line 1235
# select directory where 'calendar.txt' will be placed
if sys.platform == 'win32': # if platform is windows, choose %appdata%
if os.getenv('APPDATA') is not None:
directory = '{}\\eventer'.format(os.getenv('APPDATA'))
filename = '{}{}calendar.txt'.format(directory, '\\')
config = '{}{}config.txt'.format(directory, '\\')
backup = '{}{}backup.txt'.format(directory, '\\')
else:
errors.append(['{} %appdata%'.format(conf.lang.CANT_FIND_DIR),
'IOError: Can\'t find directory'])
elif sys.platform == 'linux' or sys.platform == 'linux2':
# if platform is linux, choose '~/.eventer'
if os.getenv('HOME') is not None:
directory = '{}/.eventer'.format(os.getenv('HOME'))
filename = '{}{}calendar.txt'.format(directory, '/')
config = '{}{}config.txt'.format(directory, '/')
backup = '{}{}backup.txt'.format(directory, '/')
else:
errors.append(['{} /home/user'.format(conf.lang.CANT_FIND_DIR),
'IOError: Can\'t find directory'])
else:
errors.append(['{}'.format(conf.lang.UNSUPPORT_SYS),
'OSError: Unsupported system'])
if len(errors) == 0: # if there're no errors, create directory
if not os.path.exists(directory):
try:
os.makedirs(directory)
except:
errors.append(['{} "{}"'.
format(conf.lang.CANT_CREATE_DIR, directory),
'PermissionError: Can\'t create directory!'])
tasks = [] # array for reminders
""" Some common global definitions. """
def rewrite():
""" Update tasks file. """
try:
global tasks
tasks = sorted(tasks, key=lambda x: strToDate('{} {}'.format(x.date,
x.time)))
with open(filename, 'w') as f:
f.write('\n'.join([str(x) for x in tasks]))
except IOError:
errors.append(['{} "{}"'.format(conf.lang.CANT_OPEN_FILE, filename),
'IOError: Can\'t open file!'])
def rewriteConfig():
""" Update config file. """
try:
conf.dump(config)
except IOError:
errors.append(['{} "{}"'.format(conf.lang.CANT_OPEN_FILE, config),
'IOError: Can\'t open file!'])
def dateToStr(datetime):
"""Convert datetime object to strings and return a dictionary"""
time = datetime.strftime('%H{d}%M{d}%S'.format(d=conf.lang.TIME_DELIM))
date = datetime.strftime('%d{d}%m{d}%Y'.format(d=conf.lang.DATE_DELIM))
return {'time': time, 'date': date}
def strToDate(string):
"""Convert string to datetime object and return it"""
datetime = dt.datetime.strptime(string, '%d.%m.%Y %H:%M:%S')
return datetime
def error(widget, text, textconsole):
"""Show error message"""
QMessageBox.critical(widget, conf.lang.ERROR, text, QMessageBox.Ok,
QMessageBox.Ok)
# Here "raise Exception(textconsole)" can be used to close
# the application immediately after the first error.
# Also, logging to text file can be used here.
print(textconsole)
def clearLayout(layout):
""" Removes all widgets from specific layout. """
for i in reversed(range(layout.count())):
item = layout.itemAt(i)
if not isinstance(item, QSpacerItem):
item.widget().close()
layout.removeItem(item)
class Entry:
""" Task entry.
Parameters and attributes
-------------------------
date : string
Date of task, dd.mm.yy.
time : string
Time of task, hh:mm:ss.
text : string
Text of task.
"""
def __init__(self, date, time, text):
self.date = date
self.time = time
self.text = text
def __str__(self):
"""Return string, constits of date, time and text of entry,
separated by '|'.
The returned entry's text is base64-encoded.
"""
return '{}|{}|{}'.format(self.date, self.time,
b64.b64encode(self.text.encode('utf-8')))
def getDateTime(self):
"""Return datetime object that stores entry's date and time"""
return strToDate('{} {}'.format(self.date, self.time))
""" QT class definitions. """
class MainWindow(QWidget):
""" Main window of application. It is represented by icon in
taskbar.
Useful attributes
-----------------
addWindow : AddWindow object
addActive : boolean
If user wants to add new task, 'addWindow' becomes the QWidget for
it, and while it is active 'addActive' remains True.
editWindow : EditWindow object
editActive : boolean
If user wants to edit tasks, 'editWindow' becomes the QWidget for
it, and while it is active 'editActive' remains True.
tray : QSystemTrayIcon object
Tray icon and all its attributes like context menu and
activated action.
timer : QTimer object
Timer that fires every second. If one reminder's time is up,
shows message.
backupTimer : QTimer object
Timer that fires every 5 minutes. Saves current state of
tasks file to backup file.
"""
def __init__(self):
""" Init GUI and all required things. """
super().__init__()
iconAdd = Icon(byte=icons.add).convertToIcon().getIcon()
self.tray = QSystemTrayIcon(iconAdd, self)
menu = QMenu()
menu.addAction(conf.lang.ADD_ACTION,
lambda: self.showWindow(QSystemTrayIcon.Trigger))
menu.addAction(conf.lang.EDIT_ACTION,
lambda: self.showWindow('editAction'))
menu.addSeparator()
menu.addAction(conf.lang.OPT_ACTION,
lambda: self.showWindow('optAction'))
menu.addAction(conf.lang.RESTORE, self.restore)
menu.addAction(conf.lang.QUIT, self.quit)
self.tray.setContextMenu(menu)
self.tray.activated.connect(self.showWindow)
self.tray.show()
self.addWindow = None
self.addActive = False
self.editWindow = None
self.editActive = False
self.optWindow = None
self.optActive = False
self.timer = QTimer()
self.timer.timeout.connect(self.timerTick)
self.timer.start(1000)
self.backup()
self.backupTimer = QTimer()
self.backupTimer.timeout.connect(self.backup)
self.backupTimer.start(conf.backup * 1000)
def timerTick(self):
""" Checks tasks entry's time if it is up. Shows message with
entry's text if its time's up.
"""
global tasks
global rewrite
for entry in tasks:
date = entry.getDateTime()
today = dt.datetime.today()
if (date - today).days < 0:
tasks.remove(entry)
class EvMessageBox(QMessageBox):
""" QMessageBox with timer.
Parameters
----------
text : string
Text of message.
title : string
Title of message window.
wicon : QIcon object
Icon of message window.
icon : QMessageBox.Icon int
Icon of message body.
timeout : int
Time for message has being shown.
Useful attributes
-----------------
timer : QTimer object
Timer attached to message.
"""
def __init__(self, text, title, wicon, icon, timeout):
super().__init__()
self.timeout = timeout
self.setText(text)
self.setWindowTitle(title)
self.setWindowIcon(wicon)
self.setIcon(icon)
self.addButton(QPushButton(conf.lang.REPEAT.format(
parseSeconds(conf.tdelta))),
QMessageBox.YesRole)
self.addButton(QPushButton(conf.lang.CLOSE),
QMessageBox.NoRole)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
# self.setTextFormat(Qt.RichText)
self.timer = QTimer()
self.timer.timeout.connect(self.timerTick)
def showEvent(self, event):
""" Start timer on message showEvent. """
self.currentTime = 0
self.timer.start(1000)
def timerTick(self):
""" Done message on timeout. """
self.currentTime += 1
if self.currentTime >= self.timeout:
self.timer.stop()
self.done(-1)
msgBox = EvMessageBox(
entry.text,
'{} {}'.format(conf.lang.TASK, date),
Icon(byte=icons.alert).convertToIcon().getIcon(),
QMessageBox.Information,
conf.mesout)
reply = msgBox.exec_()
msgBox.raise_()
if reply != 1:
td = conf.tdelta if reply == 0 else 300 - conf.mesout
date = dateToStr(dt.datetime.now() + dt.timedelta(0, td))
date['date'] = date['date'].replace('/', '.')
date['time'] = date['time'].replace('.', ':')
tasks.append(Entry(date['date'], date['time'], entry.text))
rewrite()
if self.editActive:
self.editWindow.filterApply()
def showWindow(self, event):
""" Show child windows.
If event is QSystemTrayIcon.Trigger then it checks if
all windows are not open and show addWindow.
If event is 'addAction' it means that user from editWindow
want to edit reminder, so it opens addWindow.
Then it checks if addAction or editAction is True, and alert
appropriate window.
Then if event is 'editAction' then it opens editWindow.
"""
if event == 'editAction':
if self.editActive:
QApplication.alert(self.editWindow)
else:
self.editWindow = EditWindow(self)
self.editWindow.show()
self.editWindow.setFocus(True)
self.editWindow.activateWindow()
return self.editWindow
elif event == 'addAction':
self.addWindow = AddWindow(self)
self.addWindow.show()
self.addWindow.setFocus(True)
self.addWindow.activateWindow()
return self.addWindow
elif event == QSystemTrayIcon.Trigger:
if self.addActive:
QApplication.alert(self.addWindow)
else:
self.addWindow = AddWindow(self)
self.addWindow.show()
self.addWindow.setFocus(True)
self.addWindow.activateWindow()
return self.addWindow
elif event == 'optAction':
if self.addActive:
self.addWindow.hide()
if self.editActive:
self.editWindow.hide()
self.optWindow = OptionsWindow(self)
self.optWindow.show()
self.optWindow.setFocus(True)
self.optWindow.activateWindow()
def backup(self):
""" Copies content of tasks file to backup file. """
with open(backup, 'w') as to_, open(filename, 'r') as from_:
to_.write(from_.read())
def restore(self):
""" Restores content of tasks file from backup file after
user confirmation.
"""
global tasks
shure = QMessageBox.question(self, conf.lang.RESTORE,
conf.lang.RESTORE_TEXT,
QMessageBox.No | QMessageBox.Yes,
QMessageBox.No)
if shure == QMessageBox.No:
pass
else:
temp = open(backup).read() # don't forget to read backup
self.backup()
with open(filename, 'w') as to_:
to_.write(temp)
tasks = []
readTasks()
if self.editActive:
self.editWindow.filterApply()
def quit(self, really=True):
""" Quits application. Hides tray icon firstly.
If really is not True, do not quits the application.
It is used to re-launch the application.
"""
self.tray.hide()
self.timer.stop()
self.backupTimer.stop()
if really:
QCoreApplication.instance().quit()
class AddWindow(QWidget):
""" Window for adding reminders.
Useful attributes
-----------------
parentWindow : MainWindow (or EditWindow) object
Parent of the window.
(date|time)Edit : QLineEdit object
textEdit : QTextEdit object
(save|close)Btn : QPushButton object
Widgets in the window.
"""
def __init__(self, parent=None):
super().__init__()
self.index = None
self.initUI()
self.parentWindow = parent
self.parentWindow.addActive = True
def initUI(self):
""" Init user interface. """
self.setWindowTitle(conf.lang.ADD_TITLE)
iconAdd = Icon(byte=icons.add).convertToIcon().getIcon()
self.setWindowIcon(iconAdd)
self.resize(350, 350)
self.move(QApplication.desktop().screen().rect().center() -
self.rect().center()) # center window on screen
grid = QGridLayout()
self.setLayout(grid)
dateLbl = QLabel(conf.lang.DATE)
timeLbl = QLabel(conf.lang.TIME)
self.dateEdit = QLineEdit()
self.timeEdit = QLineEdit()
self.textEdit = QTextEdit()
self.saveBtn = QPushButton(conf.lang.SAVE)
self.closeBtn = QPushButton(conf.lang.CLOSE)
dateLbl.setAlignment(Qt.AlignCenter)
timeLbl.setAlignment(Qt.AlignCenter)
dateToolTipText = conf.lang.DATE_TOOLTIP_TEXT
timeToolTipText = conf.lang.TIME_TOOLTIP_TEXT
self.dateEdit.setToolTip(dateToolTipText)
self.timeEdit.setToolTip(timeToolTipText)
dateLbl.setToolTip(dateToolTipText)
timeLbl.setToolTip(timeToolTipText)
self.textEdit.setToolTip(conf.lang.TEXT_TOOLTIP_TEXT)
self.saveBtn.setToolTip(conf.lang.SAVE_TOOLTIP_TEXT)
self.closeBtn.setToolTip(conf.lang.CLOSE_TOOLTIP_TEXT)
grid.addWidget(dateLbl, 0, 0)
grid.addWidget(timeLbl, 0, 1)
grid.addWidget(self.dateEdit, 1, 0)
grid.addWidget(self.timeEdit, 1, 1)
grid.addWidget(self.textEdit, 2, 0, 14, 2)
grid.addWidget(self.saveBtn, 16, 0)
grid.addWidget(self.closeBtn, 16, 1)
""" regexp for date:
31 days in january, march, may, july, august, october and december;
30 days in april, june, september and november;
29 days in february (year validate is in save function);
2 or 4 decimal year (no 0000 year);
/ or . as delimiter.
"""
dateRegExp = QRegExp(
'^(((0[1-9]|[1-9])|[1-2]\d|3[01])(/|\.)' \
'((0)?1|(0)?3|(0)?5|(0)?7|(0)?8|10|12)|' \
'((0[1-9]|[1-9])|[1-2]\d)(/|\.)(0)?2|' \
'((0[1-9]|[1-9])|[1-2]\d|30)(/|\.)' \
'((0)?4|(0)?6|(0)?9|11))' \
'((/|\.)\d\d|(/|\.)' \
'(\d\d\d[1-9]|\d\d[1-9]\d|\d[1-9]\d\d|[1-9]\d\d\d))?$'
)
""" regexp for time:
23 hours, 59 minutes, 59 seconds;
: or . as delimiter.
"""
timeRegExp = QRegExp(
'^([0-9]|[01]\d|2[0-3])(:|\.)(\d|[0-5]\d)((:|\.)(\d|[0-5]\d))?$'
)
dateValid = QRegExpValidator(dateRegExp)
timeValid = QRegExpValidator(timeRegExp)
self.dateEdit.validator = dateValid
# if there's no text in lineEdits, pressing 'Return' button will
# insert today's date and time in them, else it will return 0
self.dateEdit.returnPressed.connect(lambda: \
self.dateEdit.setText(dateToStr(dt.datetime.today())['date']) if
self.dateEdit.text() is '' else 0)
self.dateEdit.textChanged.connect(self.checkState)
self.dateEdit.textChanged.emit(self.timeEdit.text())
self.timeEdit.validator = timeValid
self.timeEdit.returnPressed.connect(lambda: \
self.timeEdit.setText(dateToStr(dt.datetime.today())['time']) if
self.timeEdit.text() is '' else 0)
self.timeEdit.textChanged.connect(self.checkState)
self.timeEdit.textChanged.emit(self.timeEdit.text())
self.dateEdit.valid = False
self.timeEdit.valid = False
self.closeBtn.clicked.connect(lambda: self.closeEvent(QCloseEvent()))
self.saveBtn.clicked.connect(self.save)
# disable saveBtn
self.saveBtn.setEnabled(False)
def checkState(self, sender):
""" Checks if date and time are in appropriate forms.
Colors background of (date|time)Edit depending of their text.
"""
sender = self.sender()
validator = sender.validator
state = validator.validate(sender.text(), 0)[0]
if state == QValidator.Acceptable:
color = '#5df375' # green
self.sender().valid = True
elif state == QValidator.Intermediate:
color = '#fffa76' # yellow
self.sender().valid = False
else:
color = '#ff8d93' # red
self.sender().valid = False
sender.setStyleSheet('QLineEdit { background-color: %s }' % color)
# If date and time are in appropriate forms, enable saveBtn
self.saveBtn.setEnabled(self.dateEdit.valid and self.timeEdit.valid)
def save(self):
""" Saves reminder. """
date = self.dateEdit.text().replace('/', '.')
try:
d, m, y = date.split('.')
except ValueError:
d, m = date.split('.')
y = ''
if y == '':
y = dt.datetime.today().year
elif len(y) < 4:
y = floor(dt.datetime.today().year / 100) * 100 + int(y)
else:
y = int(y)
d, m = map(int, [d, m])
if y % 4 != 0 and m == 2 and d > 28:
d = 28 # if date if 29.02 but year is not leap
y = '{:04d}'.format(y)
m = '{:02d}'.format(m)
d = '{:02d}'.format(d)
date = '.'.join([d, m, y])
time = self.timeEdit.text().replace('.', ':')
try:
h, m, s = time.split(':')
except ValueError:
h, m = time.split(':')
s = ''
if s == '':
s = '00'
h, m, s = map(int, [h, m, s])
h = '{:02d}'.format(h)
m = '{:02d}'.format(m)
s = '{:02d}'.format(s)
time = ':'.join([h, m, s])
if self.index != None:
entry = tasks[self.index]
tasks.remove(entry)
text = self.textEdit.toPlainText()
entry = Entry(date, time, text)
tasks.append(entry)
rewrite()
self.closeEvent(QCloseEvent())
def closeEvent(self, event):
""" Closes window. """
event.ignore()
if self.index != None:
self.parentWindow.parentWindow.addActive = False
self.parentWindow.show()
self.parentWindow.filterApply()
else:
if self.parentWindow.editActive:
self.parentWindow.editWindow.filterApply()
self.parentWindow.addActive = False
self.hide()
class EditWindow(QWidget):
""" Window for adding reminders.
Useful attributes
-----------------
parentWindow : MainWindow object
Parent of the window.
taskArea : QScrollArea object
Scroll area of window.
rows : list of dictionaries
Element of rows is a dictionary:
{'date', 'text', 'edit', 'del'}.
'date' and 'text' are QLabels,
'edit' and 'del' are QPushButtons.
Such dictionary represents one element from tasks list.
grid : QGridLayout object
Layout of objects in QScrollArea.
activeTasks : list
List of visible tasks in grid.
filter : QHBoxLayout object
Layout of filter widgets.
(date|text)Field : QLineEdit object
Filter widgets.
"""
def __init__(self, parent=None):
super().__init__()
self.parentWindow = parent
self.parentWindow.editActive = True
self.activeTasks = tasks
self.dateFieldText = ''
self.textFieldText = ''
self.initUI()
def initUI(self):
""" Init user interface. """
# use QVBoxLayout to store two layouts vertically
self.vl = QVBoxLayout()
# widget for scrollArea
self.topWidget = QWidget()
self.grid = QGridLayout()
self.topWidget.setLayout(self.grid)
self.vl.addWidget(self.topWidget)
# layout for filter
self.filter = QHBoxLayout()
# draw filter widgets
self.drawFilter()
spacer = QSpacerItem(10, 0, vPolicy=QSizePolicy.MinimumExpanding)
self.vl.addItem(spacer)
self.vl.addLayout(self.filter)
self.setLayout(self.vl)
self.rows = []
self.taskArea = QScrollArea(self)
self.taskArea.setWidget(self.topWidget)
self.taskArea.setWidgetResizable(True)
self.taskArea.resize(500, 350)
self.resize(500, 395)
self.setMinimumSize(460, 90)
self.filterApply()
self.show()
self.move(QApplication.desktop().screen().rect().center() -
self.rect().center())
self.setWindowTitle(conf.lang.EDIT_TITLE)
iconEdit = Icon(byte=icons.edit).convertToIcon().getIcon()
self.setWindowIcon(iconEdit)
def drawFilter(self):
""" Draws filter widgets. """
clearLayout(self.filter)
self.hideBtn = QPushButton()
if conf.filter:
self.hideBtn.setText(conf.lang.VISIBLE_F)
self.hideBtn.setFixedSize(23, 23)
self.vl.setContentsMargins(11, 11, 11, 11)
else:
self.hideBtn.setText(conf.lang.HIDDEN_F)
self.hideBtn.setFixedSize(23, 15)
self.vl.setContentsMargins(0, 0, 0, 0)
self.hideBtn.clicked.connect(self.inverseFilter)
self.filter.addWidget(self.hideBtn)
if conf.filter:
filterLbl = QLabel(conf.lang.FILTER)
filterLbl.setStyleSheet('QLabel {' \
'border-width: 0 1px 0 0;' \
'border-style: solid;' \
'border-color: black;' \
'margin: 0 5px 0 0;' \
'}')
self.filter.addWidget(filterLbl)
dateLbl = QLabel(conf.lang.DATE_F)
self.filter.addWidget(dateLbl)
self.dateField = QLineEdit()
self.dateField.setText(self.dateFieldText)
self.filter.addWidget(self.dateField)
textLbl = QLabel(conf.lang.TEXT_F)
self.filter.addWidget(textLbl)
self.textField = QLineEdit()
self.textField.setText(self.textFieldText)
self.filter.addWidget(self.textField)
self.dateField.textChanged.connect(self.filterApply)
self.textField.textChanged.connect(self.filterApply)
def inverseFilter(self):
""" Change state of filter. """
conf.filter = not conf.filter
rewriteConfig()
if not conf.filter:
self.dateFieldText = self.dateField.text()
self.textFieldText = self.textField.text()
self.drawFilter()
self.resizeEvent(QResizeEvent(QSize(self.width(), self.height()),
QSize(self.width(), self.height())))
def fill(self):
""" Fill self.taskArea by items that represens tasks. """
# delete items from self.grid
clearLayout(self.grid)
aTasks = self.activeTasks
self.rows = [{} for i in range(len(aTasks))]
if len(aTasks) == 0:
noLbl = QLabel(conf.lang.NO_TASKS)
noLbl.setAlignment(Qt.AlignCenter)
self.grid.addWidget(noLbl, 0, 0, 1, 5)
else:
for i in range(len(aTasks)):
datetime = ' '.join((dateToStr(aTasks[i].getDateTime())['date'],
dateToStr(aTasks[i].getDateTime())['time']))
# replace newlines and tabs in text
text = aTasks[i].text.replace('\n', ' ').replace('\t', ' ')
row = {}
row['date'] = QLabel(datetime)
row['text'] = QLabel()
# change label's resizeEvent
# with passing QLabel and text
row['text'].resizeEvent = \
lambda evt, lbl=row['text'], txt=text: \
self.labelResize(lbl, evt, txt)
row['edit'] = QPushButton(conf.lang.EDIT)
row['del'] = QPushButton(conf.lang.DELETE)
row['edit'].setToolTip(conf.lang.EDIT_TOOLTIP_TEXT)
row['del'].setToolTip(conf.lang.DELETE_TOOLTIP_TEXT)
# pass k=i to lambda function to save value of i
row['edit'].clicked.connect(lambda checked, k=i: self.edit(k))
row['del'].clicked.connect(lambda checked, k=i: self.delete(k))
self.rows[i] = row
self.grid.addWidget(self.rows[i]['date'], i, 0)
self.grid.addWidget(self.rows[i]['text'], i, 1, 1, 2)
self.grid.addWidget(self.rows[i]['edit'], i, 3)
self.grid.addWidget(self.rows[i]['del'], i, 4)
# add spacer to pin rows to window's top side
spacer = QSpacerItem(10, 0, vPolicy=QSizePolicy.MinimumExpanding)
self.grid.addItem(spacer, len(aTasks), 0, 1, 5)
self.grid.setColumnMinimumWidth(0, 100)
self.grid.setColumnMinimumWidth(3, 80)
self.grid.setColumnMinimumWidth(4, 80)
for i in range(self.grid.columnCount()):
self.grid.setColumnStretch(i, 0)
self.grid.setColumnStretch(1, 1)
def labelResize(self, label, event, text):
""" Change label's text due to its size. """
width = self.taskArea.width() - 320
# if get width from event: width = event.size().width()
# resizing will stop when full text is shown in label
metrics = QFontMetrics(label.font())
ellipsis = metrics.elidedText(text, Qt.ElideRight, width)
label.setText(ellipsis)
def filterApply(self):
""" Selects tasks to be shown. """
if conf.filter:
date = self.dateField.text()
text = self.textField.text().replace('\n', ' ').replace('\t', ' ')
text = text.lower()
else:
date = ''
text = ''
aTasks = []
for i in tasks:
datetime = ' '.join((dateToStr(i.getDateTime())['date'],
dateToStr(i.getDateTime())['time']))
tasktext = i.text.replace('\n', ' ').replace('\t', ' ')
tasktext = tasktext.lower()
if datetime.find(date) > -1 and tasktext.find(text) > -1:
aTasks.append(i)
self.activeTasks = aTasks
self.fill()
def edit(self, index):
""" Open addWindow with selected task. """
self.parentWindow.addActive = True
self.parentWindow.showWindow('addAction')
addWindow = self.parentWindow.addWindow
addWindow.dateEdit.setText(dateToStr(tasks[index].
getDateTime())['date'])
addWindow.timeEdit.setText(dateToStr(tasks[index].
getDateTime())['time'])
addWindow.textEdit.setText(tasks[index].text)
addWindow.parentWindow = self
# pass index parameter to notice it in addWindow
addWindow.index = index
self.hide()
def delete(self, index):
""" Delete selected reminder from tasks list. """
entry = tasks[index]
tasks.remove(entry)
rewrite()
self.filterApply()
def resizeEvent(self, event):
""" Resize taskArea due to size of window. """
width = event.size().width()
height = self.size().height()
if conf.filter:
self.taskArea.resize(width, height - 45)
else:
self.taskArea.resize(width, height - 15)
def closeEvent(self, event):
""" Closes window. """
self.parentWindow.editActive = False
event.ignore()
self.hide()
class OptionsWindow(QWidget):
""" Window for changing settings.
Useful attributes
-----------------
parentWindow : MainWindow object
Parent of the window.
langCombo : QComboBox object
Language selector.
tdelta(Days|Hours|Mins|Secs)Edit : QLineEdit object
Widgets for time delta change.
mesoutSecsEdit : QLineEdit object
Widget for message timeout change.
backupMinsEdit : QLineEdit object
Widget for backup timeout change.