-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes_main.py
More file actions
227 lines (201 loc) · 6.59 KB
/
notes_main.py
File metadata and controls
227 lines (201 loc) · 6.59 KB
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
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QListWidget, QHBoxLayout, QVBoxLayout, QLabel, \
QPushButton, QLineEdit, QInputDialog, QMessageBox
import json
def read_data():
try:
with open('f.json', 'r', encoding='utf-8') as file:
data = json.load(file)
return data
except:
print('Error add dict!')
def write_data(new_data):
try:
with open('f.json', 'w', encoding='utf-8') as file:
json.dump(new_data, file, ensure_ascii=False, sort_keys=True)
except:
print('Error add dict!')
def check_json():
notes = {'Добро пожаловать!':
{'текст':'Это самое лучшее приложение для заметок в мире.',
'теги':['добро', 'пожаловать']}
}
try:
with open('f.json', 'r+', encoding='utf-8') as file:
data = file.read().strip()
if data == '':
json.dump(notes, file, ensure_ascii=False, sort_keys=True)
file.seek(0)
data = json.load(file)
# print(type(data), data)
return data
except:
print('Ошибка работы с json')
def show_results():
key = list_text1.selectedItems()[0].text()
data = read_data()
text_field.setText(data[key]['текст'])
list_text2.clear()
for j in data[key]['теги']:
list_text2.addItem(j)
#Creat notice
def add_note_json(d):
data = read_data()
new_d = {**data, **d}
write_data(new_d)
def add_note():
notes_name, ok = QInputDialog.getText(window, 'Добавить заметку', 'Название заметки:')
notes = {}
if ok and notes_name != '':
notes[notes_name] = {'текст': '', 'теги': []}
list_text1.addItem(notes_name)
add_note_json(notes)
def delete_note():
if list_text1.selectedItems():
key = list_text1.selectedItems()[0].text()
data = read_data()
del data[key]
write_data(data)
list_text1.clear()
list_text2.clear()
text_field.clear()
list_text1.addItems(data)
else:
print('Заметка для удаления не выбрана.')
def save_note():
if list_text1.selectedItems():
key = list_text1.selectedItems()[0].text()
text = text_field.toPlainText()
data = read_data()
data[key]['текст'] = text
write_data(data)
def add_teg():
global key, data
if list_text1.selectedItems():
key = list_text1.selectedItems()[0].text()
text = lineEdit.text()
data = read_data()
if text not in data[key]['теги']:
data[key]['теги'].append(text)
write_data(data)
list_text2.clear()
for j in data[key]['теги']:
list_text2.addItem(j)
else:
print('Заметка для сохранения не выбрана.')
def del_teg():
if list_text2.selectedItems():
key = list_text1.selectedItems()[0].text()
text = list_text2.selectedItems()[0].text()
data = read_data()
if text in data[key]['теги']:
data[key]['теги'].remove(text)
write_data(data)
list_text2.clear()
for j in data[key]['теги']:
list_text2.addItem(j)
else:
print('Заметка для сохранения не выбрана.')
def find_btn():
global data
result = {}
searching = lineEdit.text().strip()
if searching and bnt6.text() == 'Искать заметки по тегу':
data = read_data()
try:
for i in data:
print(i)
if searching in data[i]['теги']:
result[i] = data[i]
data = result
bnt6.setText('Сбросить поиск')
except:
print('Error again')
list_text1.clear()
list_text2.clear()
text_field.clear()
list_text1.addItems(data)
elif bnt6.text() == 'Сбросить поиск':
try:
data = read_data()
bnt6.setText('Искать заметки по тегу')
except:
print('Error again')
list_text1.clear()
list_text2.clear()
text_field.clear()
list_text1.addItems(data)
else:
pass
app = QApplication([])
window = QWidget()
window.setWindowTitle('Умные заметки')
window.resize(800, 600)
window.show()
text_field = QTextEdit()
text_field.setText('')
list_text1 = QListWidget()
list_text1_label = QLabel('Список заметок')
list_text2 = QListWidget()
list_text2_label = QLabel('Список тегов')
lineEdit = QLineEdit()
lineEdit.setPlaceholderText('Введите тег...')
# выводим существующие заметки
data = check_json()
for i in data.keys():
list_text1.addItem(i)
list_text1.itemClicked.connect(show_results)
# widgets buttons
bnt_add = QPushButton('Создать заметку')
bnt_delete = QPushButton('Удалить заметку')
bnt3 = QPushButton('Сохранить заметку')
bnt4 = QPushButton('Добавить к заметке')
bnt5 = QPushButton('Открепить от заметки')
bnt6 = QPushButton('Искать заметки по тегу')
bnt_addTeg = QPushButton('Добавить к заметки')
bnt_delTeg = QPushButton('Открепить от заметки')
h_main = QHBoxLayout()
v1_layout = QVBoxLayout()
v2_layout = QVBoxLayout()
h2_1 = QHBoxLayout()
h2_2 = QHBoxLayout()
h2_3 = QHBoxLayout()
h2_4 = QHBoxLayout()
h2_5 = QHBoxLayout()
h2_6 = QHBoxLayout()
h2_7 = QHBoxLayout()
h2_8 = QHBoxLayout()
h2_9 = QHBoxLayout()
# first big textarea
v1_layout.addWidget(text_field)
# second zone
h2_1.addWidget(list_text1_label)
h2_2.addWidget(list_text1)
h2_3.addWidget(bnt_add)
h2_3.addWidget(bnt_delete)
h2_4.addWidget(bnt3)
h2_5.addWidget(list_text2_label)
h2_6.addWidget(list_text2)
h2_7.addWidget(lineEdit)
h2_8.addWidget(bnt6)
h2_9.addWidget(bnt_addTeg)
h2_9.addWidget((bnt_delTeg))
v2_layout.addLayout(h2_1)
v2_layout.addLayout(h2_2)
v2_layout.addLayout(h2_3)
v2_layout.addLayout(h2_4)
v2_layout.addLayout(h2_5)
v2_layout.addLayout(h2_6)
v2_layout.addLayout(h2_7)
v2_layout.addLayout(h2_9)
v2_layout.addLayout(h2_8)
h_main.addLayout(v1_layout)
h_main.addLayout(v2_layout)
window.setLayout(h_main)
bnt_add.clicked.connect(add_note)
bnt_delete.clicked.connect(delete_note)
bnt3.clicked.connect(save_note)
bnt_addTeg.clicked.connect(add_teg)
bnt_delTeg.clicked.connect(del_teg)
bnt6.clicked.connect(find_btn)
app.exec_()