Advertisement
shh_algo_PY

Smart Notes with Tags

Apr 9th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.28 KB | None | 0 0
  1. from PyQt5.QtCore import Qt
  2. from PyQt5.QtWidgets import (
  3.     QApplication, QWidget, QPushButton,
  4.     QLabel, QListWidget, QLineEdit,
  5.     QTextEdit, QInputDialog, QHBoxLayout,
  6.     QVBoxLayout, QFormLayout)
  7.  
  8. import json
  9.  
  10. app = QApplication([])
  11.  
  12. '''Notes in json'''
  13. notes = {
  14.     "Welcome!" : {
  15.         "text" : "This is the best note taking app in the world!",
  16.         "tags" : ["good", "instructions"]
  17.     }
  18. }
  19.  
  20. # Comment out this section!
  21. #with open("notes_data.json", "w") as file:
  22.     #json.dump(notes, file, ensure_ascii=False)
  23.  
  24. '''Application interface'''
  25. # Application window parameters
  26. notes_win = QWidget()
  27. notes_win.setWindowTitle('Smart Notes')
  28. notes_win.resize(900, 600)
  29.  
  30. # Application window widgets
  31. # List widget: List of notes
  32. list_notes = QListWidget()
  33. list_notes_label = QLabel('List of notes')
  34.  
  35. # Buttons under the list of notes
  36. button_note_create = QPushButton('Create note')
  37. button_note_del = QPushButton('Delete note')
  38. button_note_save = QPushButton('Save note')
  39.  
  40. # Creates a field to insert tags
  41. field_tag = QLineEdit('') # Creates a blank field
  42. field_tag.setPlaceholderText('Enter tag...') # Text prompt inside the field
  43. field_text = QTextEdit() # Field for entering text
  44.  
  45. # Buttons for all the tags
  46. # CHANGE NAMES TO BE CLEARER
  47. button_add = QPushButton('Add to note')
  48. button_del = QPushButton('Untag from note')
  49. button_search = QPushButton('Search notes by tag')
  50.  
  51. # List widget: List of tags
  52. list_tags = QListWidget()
  53. list_tags_label = QLabel('List of tags')
  54.  
  55. # Arranging widgets by layout
  56. layout_notes = QHBoxLayout()
  57. col_1 = QVBoxLayout()
  58. col_1.addWidget(field_text)
  59.  
  60. col_2 = QVBoxLayout()
  61. col_2.addWidget(list_notes_label)
  62. col_2.addWidget(list_notes)
  63. row_1 = QHBoxLayout()
  64. row_1.addWidget(button_note_create)
  65. row_1.addWidget(button_note_del)
  66. row_2 = QHBoxLayout()
  67. row_2.addWidget(button_note_save)
  68. col_2.addLayout(row_1)
  69. col_2.addLayout(row_2)
  70.  
  71. col_2.addWidget(list_tags_label)
  72. col_2.addWidget(list_tags)
  73. col_2.addWidget(field_tag)
  74. row_3 = QHBoxLayout()
  75. row_3.addWidget(button_add)
  76. row_3.addWidget(button_del)
  77. row_4 = QHBoxLayout()
  78. row_4.addWidget(button_search)
  79.  
  80. col_2.addLayout(row_3)
  81. col_2.addLayout(row_4)
  82.  
  83. layout_notes.addLayout(col_1, stretch = 2)
  84. layout_notes.addLayout(col_2, stretch = 1)
  85. notes_win.setLayout(layout_notes)
  86.  
  87. '''Application functionality'''
  88.  
  89. def add_note():
  90.     note_name, ok = QInputDialog.getText(notes_win, "Add note", "Note name: ")
  91.     if ok and note_name != "":
  92.         notes[note_name] = {"text" : "", "tags" : []}
  93.         list_notes.addItem(note_name)
  94.         list_tags.addItems(notes[note_name]["tags"])
  95.         print(notes)
  96.  
  97. def show_note():
  98.     # Get the text from the note with the title highlighted and display it in the edit field
  99.     key = list_notes.selectedItems()[0].text()
  100.     print(key)
  101.     field_text.setText(notes[key]["text"])
  102.     list_tags.clear()
  103.     list_tags.addItems(notes[key]["tags"])
  104.  
  105. def save_note():
  106.     if list_notes.selectedItems():
  107.         key = list_notes.selectedItems()[0].text()
  108.         notes[key]["text"] = field_text.toPlainText()
  109.         with open("notes_data.json", "w") as file:
  110.             json.dump(notes, file, sort_keys=True, ensure_ascii=False)
  111.         print(notes)
  112.     else:
  113.         print("Note to save is not selected!")
  114.  
  115. def del_note():
  116.     if list_notes.selectedItems():
  117.         key = list_notes.selectedItems()[0].text()
  118.         del notes[key]
  119.         list_notes.clear()
  120.         list_tags.clear()
  121.         field_text.clear()
  122.         list_notes.addItems(notes)
  123.         with open("notes_data.json", "w") as file:
  124.             json.dump(notes, file, sort_keys=True, ensure_ascii=False)
  125.         print(notes)
  126.     else:
  127.         print("Note to delete is not selected!")
  128.  
  129. #============================================#
  130. #Working with note tags
  131. def add_tag():
  132.     if list_notes.selectedItems():
  133.         key = list_notes.selectedItems()[0].text()
  134.         tag = field_tag.text()
  135.         if not tag in notes[key]["tags"]:
  136.             notes[key]["tags"].append(tag)
  137.             list_tags.addItem(tag)
  138.             field_tag.clear()
  139.         with open("notes_data.json", "w") as file:
  140.             json.dump(notes, file, sort_keys=True, ensure_ascii=False)
  141.         print(notes)
  142.     else:
  143.         print("Note to add a tag is not selected!")
  144.  
  145. def del_tag():
  146.     if list_tags.selectedItems():
  147.         key = list_notes.selectedItems()[0].text()
  148.         tag = list_tags.selectedItems()[0].text()
  149.         notes[key]["tags"].remove(tag)
  150.         list_tags.clear()
  151.         list_tags.addItems(notes[key]["tags"])
  152.         with open("notes_data.json", "w") as file:
  153.             json.dump(notes, file, sort_keys=True, ensure_ascii=False)
  154.     else:
  155.         print("Tag to delete is not selected!")
  156.  
  157. def search_tag():
  158.     print(button_search.text())
  159.     tag = field_tag.text()
  160.     if button_search.text() == "Search notes by tag" and tag:
  161.         print(tag)
  162.         notes_filtered = {} #notes with the highlighted tag will be here
  163.         for note in notes:
  164.             if tag in notes[note]["tags"]:
  165.                 notes_filtered[note]=notes[note]
  166.         button_search.setText("Reset search")
  167.         list_notes.clear()
  168.         list_tags.clear()
  169.         list_notes.addItems(notes_filtered)
  170.         print(button_search.text())
  171.     elif button_search.text() == "Reset search":
  172.         field_tag.clear()
  173.         list_notes.clear()
  174.         list_tags.clear()
  175.         list_notes.addItems(notes)
  176.         button_search.setText("Search notes by tag")
  177.         print(button_search.text())
  178.     else:
  179.         pass
  180. #============================================#
  181. '''Run the application'''
  182. # Connecting event processing
  183. # When clicked, show the note
  184. # Change to show current button processing
  185.  
  186. button_note_create.clicked.connect(add_note)
  187. list_notes.itemClicked.connect(show_note)
  188. button_note_save.clicked.connect(save_note)
  189. button_note_del.clicked.connect(del_note)
  190.  
  191. #============================================#
  192. '''More button tags'''
  193. button_add.clicked.connect(add_tag)
  194. button_del.clicked.connect(del_tag)
  195. button_search.clicked.connect(search_tag)
  196. #============================================#
  197.  
  198. with open("notes_data.json", "r") as file:
  199.     notes = json.load(file)
  200. list_notes.addItems(notes)
  201.  
  202. # Run the application
  203. notes_win.show()
  204. app.exec_()
  205.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement