-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoDo-App.py
442 lines (365 loc) · 13.7 KB
/
toDo-App.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
# ################################################
# todo application
# > Enter your Tasks
# > Save your Tasks
# > Delete your Tasks
# > Load your saved Tasks
# -------------------------------By: Anupam Sharma
# ################################################
# Importing modules required in this project
# using --- PyQt6 ---
import sys
import time
import os
from PyQt6.QtCore import QThread, QSize, Qt, pyqtSignal
from PyQt6.QtWidgets import QApplication, QListWidget, QAbstractItemView, QListWidgetItem, QWidget, QLineEdit, QHBoxLayout, QVBoxLayout, QLabel, QPushButton
from PyQt6.QtGui import QIcon, QCursor
# --------Thread class---------------#
class WorkerThread(QThread):
status = pyqtSignal(str)
def run(self):
time.sleep(2)
self.status.emit('...')
print('...')
class todo(QWidget):
def __init__(self):
super().__init__()
# adding logo of the window
self.setWindowIcon(QIcon('icons/icon.png'))
# setting the title of the window
self.setWindowTitle('ToDo List')
# fixed size window
self.setFixedSize(350, 480)
# defining layouts
horizontalLayout = QHBoxLayout()
horizontalLayoutforbutton = QHBoxLayout()
verticalLayout = QVBoxLayout()
# label widget
label = QLabel('To Do''\'s')
label.setObjectName('header')
status = QLabel('Status:')
status.setObjectName('status')
self.toast = QLabel('...')
#self.toast.setAlignment(Qt.Alignment.AlignCenter)
self.toast.setObjectName('toast')
# listwidget
self.list = QListWidget()
#self.list.setAlternatingRowColors(True)
self.list.setAutoScroll(True)
self.list.setWordWrap(True)
# setting selection mode property
self.list.setSelectionMode(QAbstractItemView.SelectionMode.MultiSelection)
self.list.itemClicked.connect(self.item)
# Textbox widget
self.textbox = QLineEdit(self)
self.textbox.setPlaceholderText('Take a note!')
self.textbox.setFocus()
self.textbox.setCursor(QCursor(Qt.CursorShape.IBeamCursor))
# Add button widget
btn_addTask = QPushButton('Add Task')
btn_addTask.clicked.connect(self.addTask)
#self.btn_addTask.setIcon(QIcon('icons/plus.png'))
btn_addTask.setObjectName('addButton')
btn_addTask.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# Delete button widget
btn_deleteTask = QPushButton()
btn_deleteTask.clicked.connect(self.deleteT)
btn_deleteTask.setIcon(QIcon('icons/trash-regular.png'))
btn_deleteTask.setIconSize(QSize(20, 20))
btn_deleteTask.setToolTip('Delete selected task')
btn_deleteTask.setObjectName('deleteButton')
btn_deleteTask.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# Save button widget
btn_saveTask = QPushButton()
btn_saveTask.clicked.connect(self.saveT)
btn_saveTask.setIcon(QIcon('icons/save-regular.png'))
btn_saveTask.setIconSize(QSize(20, 20))
btn_saveTask.setToolTip('Save Your List')
btn_saveTask.setObjectName('saveButton')
btn_saveTask.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# Load button widget
btn_loadTask = QPushButton()
btn_loadTask.clicked.connect(self.loadTask)
btn_loadTask.setIcon(QIcon('icons/spreadsheet-regular.png'))
btn_loadTask.setIconSize(QSize(20, 20))
btn_loadTask.setToolTip('Load previously saved List')
btn_loadTask.setObjectName('loadButton')
btn_loadTask.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# ClearAll button widget
btn_clearAll = QPushButton('Clear All')
btn_clearAll.clicked.connect(self.clearAllTask)
btn_clearAll.setObjectName('clearAllButton')
btn_clearAll.setCursor(QCursor(Qt.CursorShape.PointingHandCursor))
# setting Layouts
verticalLayout.addWidget(label)
horizontalLayout.addWidget(status)
horizontalLayout.addWidget(self.toast, 1)
horizontalLayout.addWidget(btn_loadTask)
horizontalLayout.addWidget(btn_saveTask)
horizontalLayout.addWidget(btn_deleteTask)
verticalLayout.addLayout(horizontalLayout)
verticalLayout.addWidget(self.list)
verticalLayout.addWidget(self.textbox)
horizontalLayoutforbutton.addWidget(btn_clearAll)
horizontalLayoutforbutton.addWidget(btn_addTask)
verticalLayout.addLayout(horizontalLayoutforbutton)
self.setLayout(verticalLayout)
# Set Toast when item is selected from the list
def item(self):
if self.list.selectedItems():
self.toast.setText('Item Selected')
else:
self.toast.setText('...')
# Event handling method when any key is pressed
def keyPressEvent(self, event):
#print(event.key())
self.textbox.setFocus()
# when user press enter, it automatically add func() to add task in the list
if event.key() == 16777220 or event.key() == 16777221:
self.addTask()
# clear listwidget and linedit
def clearAllTask(self):
self.list.clear()
self.textbox.clear()
self.toast.setText('Reset')
self.ThreadClass()
# Adding listItem into the listwidget
def addTask(self):
task = self.textbox.text()
#print(len(task))
if task != "":
self.list.addItem(task)
self.textbox.clear()
self.toast.setText('Item Added')
self.ThreadClass()
else:
self.toast.setText('Add Item first')
self.toast.setStyleSheet('''color: #d9534f; ''')
self.ThreadClass()
# Delete the selected listItem
def deleteT(self):
if(self.list.selectedItems()):
for i in self.list.selectedItems():
task_index = self.list.row(i)
print(self.list.item(task_index).text())
self.list.takeItem(task_index)
self.toast.setText('Item deleted.')
self.ThreadClass()
else:
self.toast.setText('Select! item')
self.toast.setStyleSheet('''color: #d9534f;''')
self.ThreadClass()
# Save the selected listItem
def saveT(self):
if(self.list.selectedItems()):
with open('load.txt', 'a+') as file:
# getting the current time
current_time = time.asctime(time.localtime(time.time()))
print(self.list.count())
for i in reversed(self.list.selectedItems()):
timestamp = '\n------------ {}'.format(current_time)
task_index = self.list.row(i)
strr = self.list.item(task_index).text()
# concatinate time after the list item
strr = strr[0:] + timestamp
# replacing '\n' (new line) with symbol '$$' in the list item
strr = strr.replace('\n', '$$')
file.write(strr + '\n')
self.toast.setText('Task saved')
self.ThreadClass()
else:
self.toast.setText('Select! item')
self.toast.setStyleSheet('''color: #d9534f;''')
self.ThreadClass()
# Load the Saved task from the file named: load.txt
def loadTask(self):
try:
# check the file size or file not exist throws an exception
if os.stat('load.txt').st_size != 0:
self.list.clear()
# reading the item one by one
with open('load.txt', 'r') as file:
for i in file.readlines():
# replacing '$$' with '\n' in the list item
i = i.replace('$$', '\n')
self.list.addItem(i)
self.toast.setText('Task Loaded')
self.ThreadClass()
else:
self.toast.setText('Task List empty!')
self.toast.setStyleSheet('''color: #d9534f;''')
self.ThreadClass()
except:
file = open('load.txt', 'a+')
print('file created !')
self.loadTask()
# Default Toast Message
def toast_msg(self, msg):
self.toast.setText(msg)
self.toast.setStyleSheet('''color: #1a73e8;''')
# Thread function calling
def ThreadClass(self):
self.worker = WorkerThread()
self.worker.start()
self.worker.status.connect(self.toast_msg)
#------------------Main---------------------#
app = QApplication(sys.argv)
app.setStyleSheet('''
*{
background-color: #ffffff;
font-family: Century Gothic;
}
#header{
font-size: 30px;
/*font-family: Mistral;*/
color: #787c80;
margin: 0px 5px;
}
#toast, #status{
font-size: 12px;
color: #1a73e8;
font-weight: bold;
margin-left: 5px;
}
QToolTip {
color: #ffffff;
background-color: #1a73e8;
padding: 10px;
outline: 0;
border: 0;
/*opacity: 200;*/
}
QListWidget{
background-color: rgba(241, 243, 244, 1);
border: 1px solid rgba(241, 243, 244,1);
border-radius: 8px;
padding: 0.8em 0.5em 0.8em 0.5em ;
outline: 0;
font-size: 15px;
margin: 2px 5px 8px 5px;
}
QListWidget::item {
background-color: #ffffff;
/*background: qlineargradient( x1:0 y1:0, x2:1 y2:0, stop:0 #cccccc, stop:1 #fcfcfc);*/
border-radius: 8px;
margin-top: 0.4em;
padding: 0.5em;
}
QListWidget::item:selected {
background-color: #d2e3fc;
border : none;
color: #0275d8;
}
#deleteButton{
border-radius: 5px;
max-width: 2em;
padding: 0.2em;
margin-left: 0;
margin-right: 5px;
}
#deleteButton::hover, #loadButton::hover, #saveButton::hover{
background-color: #f0f0f0;
}
#deleteButton::pressed, #loadButton::pressed, #saveButton::pressed{
background-color: #e0f0fd;
}
#loadButton{
border-radius: 5px;
max-width: 2em;
padding: 0.2em;
}
#saveButton{
border-radius: 5px;
/*background-color: #0275d8;*/
max-width: 2em;
margin-left: 0;
padding: 0.2em;
}
#addButton, #clearAllButton{
padding: 1em;
background-color: #1a73e8;
margin-bottom: 5px;
margin-top: 3px;
}
#clearAllButton{
margin-left: 5px;
}
#addButton{
margin-right: 5px;
}
QPushButton{
font-size: 15px;
border-radius: 8px;
color: #ffffff;
font-weight: bold;
}
QPushButton::hover, #addButton::hover, #clearAllButton::hover{
background-color: #1967d2;
}
QPushButton::pressed, #addButton::pressed, #clearAllButton::pressed{
background-color: #185abc;
}
QLineEdit{
background-color: rgba(241, 243, 244, 1);
border: 1px solid rgba(241, 243, 244,1);
border-radius: 8px;
min-height: 2em;
padding: 0.5em;
font-size: 16px;
margin: 5px 5px 0px 5px;
}
QScrollBar{
border: none;
background-color: rgba(241, 243, 244, 1);
}
QScrollBar:vertical {
width: 18px;
}
QScrollBar:horizontal {
height: 10px;
}
QScrollBar::handle:vertical{
background : #CFD1D0;
border-radius: 5px;
margin-left: 0.5em;
}
QScrollBar::handle:horizontal{
background : #CFD1D0;
border-radius: 5px;
}
QScrollBar::add-line:horizontal {
border: none;
background: none;
width: 20px;
subcontrol-position: right;
subcontrol-origin: margin;
}
QScrollBar::add-line:vertical {
border: none;
background: none;
height: 20px;
margin-left: 5px;
subcontrol-position: top;
subcontrol-origin: margin;
}
QScrollBar::sub-line:horizontal {
border: none;
background: none;
width: 20px;
subcontrol-position: left;
subcontrol-origin: margin;
}
QScrollBar::sub-line:vertical {
border: none;
background: none;
height: 20px;
subcontrol-position: bottom;
subcontrol-origin: margin;
}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal, QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
background: none;
}
''')
window = todo()
window.show()
sys.exit(app.exec())