Advertisement
shh_algo_PY

Smart Notes, Day 2, Part 1

Apr 2nd, 2022
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 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. #=================================================#
  21. '''Comment out this section'''
  22. #with open("notes_data.json", "w") as file:
  23.     #json.dump(notes, file, ensure_ascii=False)
  24. #=================================================#
  25.  
  26. '''Application interface'''
  27. # Application window parameters
  28. notes_win = QWidget()
  29. notes_win.setWindowTitle('Smart Notes')
  30. notes_win.resize(900, 600)
  31.  
  32. # Application window widgets
  33. # List widget: List of notes
  34. list_notes = QListWidget()
  35. list_notes_label = QLabel('List of notes')
  36.  
  37. # Buttons under the list of notes
  38. button_note_create = QPushButton('Create note')
  39. button_note_del = QPushButton('Delete note')
  40. button_note_save = QPushButton('Save note')
  41.  
  42. # Creates a field to insert tags
  43. field_tag = QLineEdit('') # Creates a blank field
  44. field_tag.setPlaceholderText('Enter tag...') # Text prompt inside the field
  45. field_text = QTextEdit() # Field for entering text
  46.  
  47. # Buttons for all the tags
  48. button_add = QPushButton('Add to note')
  49. button_del = QPushButton('Untag from note')
  50. button_search = QPushButton('Search notes by tag')
  51.  
  52. # List widget: List of tags
  53. list_tags = QListWidget()
  54. list_tags_label = QLabel('List of tags')
  55.  
  56. # Arranging widgets by layout
  57. layout_notes = QHBoxLayout()
  58. col_1 = QVBoxLayout()
  59. col_1.addWidget(field_text)
  60.  
  61. col_2 = QVBoxLayout()
  62. col_2.addWidget(list_notes_label)
  63. col_2.addWidget(list_notes)
  64. row_1 = QHBoxLayout()
  65. row_1.addWidget(button_note_create)
  66. row_1.addWidget(button_note_del)
  67. row_2 = QHBoxLayout()
  68. row_2.addWidget(button_note_save)
  69. col_2.addLayout(row_1)
  70. col_2.addLayout(row_2)
  71.  
  72. col_2.addWidget(list_tags_label)
  73. col_2.addWidget(list_tags)
  74. col_2.addWidget(field_tag)
  75. row_3 = QHBoxLayout()
  76. row_3.addWidget(button_add)
  77. row_3.addWidget(button_del)
  78. row_4 = QHBoxLayout()
  79. row_4.addWidget(button_search)
  80.  
  81. col_2.addLayout(row_3)
  82. col_2.addLayout(row_4)
  83.  
  84. layout_notes.addLayout(col_1, stretch = 2)
  85. layout_notes.addLayout(col_2, stretch = 1)
  86. notes_win.setLayout(layout_notes)
  87.  
  88. '''Application functionality'''
  89.  
  90. #============================================#
  91. def add_note():
  92.     note_name, ok = QInputDialog.getText(notes_win, "Add note", "Note name: ")
  93.     if ok and note_name != "":
  94.         notes[note_name] = {"text" : "", "tags" : []}
  95.         list_notes.addItem(note_name)
  96.         list_tags.addItems(notes[note_name]["tags"])
  97.         print(notes)
  98. #============================================#
  99.  
  100. def show_note():
  101.     # Get the text from the note with the title highlighted and display it in the edit field
  102.     key = list_notes.selectedItems()[0].text()
  103.     print(key)
  104.     field_text.setText(notes[key]["text"])
  105.     list_tags.clear()
  106.     list_tags.addItems(notes[key]["tags"])
  107.  
  108. #============================================#
  109. def save_note():
  110.     if list_notes.selectedItems():
  111.         key = list_notes.selectedItems()[0].text()
  112.         notes[key]["text"] = field_text.toPlainText()
  113.         with open("notes_data.json", "w") as file:
  114.             json.dump(notes, file, sort_keys=True, ensure_ascii=False)
  115.         print(notes)
  116.     else:
  117.         print("Note to save is not selected!")
  118.  
  119. def del_note():
  120.     if list_notes.selectedItems():
  121.         key = list_notes.selectedItems()[0].text()
  122.         del notes[key]
  123.         list_notes.clear()
  124.         list_tags.clear()
  125.         field_text.clear()
  126.         list_notes.addItems(notes)
  127.         with open("notes_data.json", "w") as file:
  128.             json.dump(notes, file, sort_keys=True, ensure_ascii=False)
  129.         print(notes)
  130.     else:
  131.         print("Note to delete is not selected!")
  132.  
  133. #============================================#
  134.  
  135. '''Run the application'''
  136. # Connecting event processing
  137. # When clicked, show the note
  138. # Change to show current button processing
  139.  
  140. button_note_create.clicked.connect(add_note)
  141. list_notes.itemClicked.connect(show_note)
  142. button_note_save.clicked.connect(save_note)
  143. button_note_del.clicked.connect(del_note)
  144.  
  145. #============================================#
  146. with open("notes_data.json", "r") as file:
  147.     notes = json.load(file)
  148. list_notes.addItems(notes)
  149.  
  150. # Run the application
  151. notes_win.show()
  152. app.exec_()
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement