-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
269 lines (218 loc) · 9.68 KB
/
main.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
# Import the libraries
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
from db import DB
from note import Note
class MainWindow(Gtk.Window):
connection = DB()
text_input = None
note_list = None
container = None
# Main window constructor event, which initializaes everything inside
# the window.
def __init__(self):
Gtk.Window.__init__(self, title="My Notes")
self.set_border_width(5)
self.connect("destroy", self.exit_window)
self.add_elements()
self.set_default_size(400, 400)
# show the about dialog and add the default
# action for quitting the dialog.
def show_about(self, widget):
# create about dialog and add the widgets.
dialog = Gtk.Dialog(title="About - My Notes", transient_for=self, flags=0)
dialog.add_buttons(Gtk.STOCK_OK, Gtk.ResponseType.OK)
dialog.set_default_size(200, 200)
label = Gtk.Label(label="My Notes is a little application created in Python using the GTK GUI toolkit as practice with the Python scripting language.\nCreated by xman40100, see license on GitHub for additional details.", halign=Gtk.Align.START)
label.set_justify(Gtk.Justification.LEFT)
# get the content area of the dialog (Gtk.Box)
content_area = dialog.get_content_area()
content_area.pack_start(label, True, True, 0)
# show dialog and destroy on response.
dialog.show_all()
dialog.run()
dialog.destroy()
# Method that gets the current notes stored.
def get_notes(self):
notes = self.get_db_notes()
self.note_list = Gtk.Grid(column_spacing=10, row_spacing=10)
self.note_list.set_column_homogeneous(True)
self.note_list.set_hexpand(True)
self.note_list.set_vexpand(True)
note_quantity = len(notes)
# table columns.
columns = [
"Note",
"Added on",
"Actions"
]
# get the amount of columns and add them in the grid.
col_amount = len(columns)
for i in range(0, col_amount):
label = Gtk.Label(label=columns[i])
label.set_justify(Gtk.Justification.CENTER)
self.note_list.attach(label, i, 0, 1, 1)
# check if there are no notes available, if so, show the no notes registered
# label.
if (note_quantity == 0):
label_no_items = Gtk.Label(label="No notes registered.")
label.set_justify(Gtk.Justification.CENTER)
self.note_list.attach(label_no_items, 0, 1, col_amount, 1)
return self.note_list
for i in range(0, note_quantity):
item = notes[i]
# get the first 15 chars of the string, and replacing the new line special char.
note_text = item.get_text()
note_text = note_text.replace("\n", " ")
note_text = note_text[0:15] + "..."
# add the labels and buttons
label_text = Gtk.Label(label=note_text)
label_text.set_justify(Gtk.Justification.CENTER)
label_time = Gtk.Label(label=item.get_time_localtime())
label_time.set_justify(Gtk.Justification.CENTER)
# create the action buttons
action_grid = Gtk.Grid(column_spacing=10)
action_grid.set_column_homogeneous(True)
action_grid.set_hexpand(True)
delete_button = Gtk.Button(label="Delete")
delete_button.connect("clicked", self.delete_note, item.get_id())
view_button = Gtk.Button(label="View")
view_button.connect("clicked", self.view_note, item.get_id())
action_grid.attach(view_button, 1, 0, 1, 1)
action_grid.attach(delete_button, 2, 0, 1, 1)
# attach to the note_list table.
self.note_list.attach(label_text, 0, i + 1, 1, 1)
self.note_list.attach(label_time, 1, i + 1, 1, 1)
self.note_list.attach(action_grid, 2, i + 1, 1, 1)
return self.note_list
# This adds the elements into the GUI.
def add_elements(self):
# add scroll view
scrollview = Gtk.ScrolledWindow()
scrollview.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
# add the grid container.
self.container = Gtk.Grid(column_spacing=10, row_spacing=10)
# add button to add new note.
button = Gtk.Button(label="Add note")
button.connect("clicked", self.insert_note)
self.container.attach(button, 1, 0, 1, 1)
button2 = Gtk.Button(label="About")
button2.connect("clicked", self.show_about)
self.container.attach(button2, 12, 0, 1, 1)
label = Gtk.Label(label="Type your new note:")
label.set_justify(Gtk.Justification.LEFT)
self.container.attach(label, 1, 1, 1, 1)
# add a text counter
text = "Character count: 0/%d" % (Note.MAX_LENGTH)
self.text_counter = Gtk.Label(label=text)
label.set_justify(Gtk.Justification.CENTER)
self.container.attach(self.text_counter, 1, 2, 12, 1)
# add an text_input for typing
self.text_input = Gtk.TextView()
self.text_input.set_hexpand(False)
self.text_input.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
buffer = self.text_input.get_buffer()
buffer.connect("changed", self.update_charcount)
self.container.attach(self.text_input, 1, 3, 12, 4)
# get the stored notes and add them into the GUI
notes = self.get_notes()
self.container.attach(notes, 1, 7, 12, 1)
# add them to main GUI
scrollview.add(self.container)
self.add(scrollview)
# Method that gets the current notes in the database
def get_db_notes(self):
connection = self.connection.get_instance()
notes = Note.get_all(connection)
return notes
# exit window
def exit_window(self, widget):
self.connection.destroy_instance()
Gtk.main_quit()
# This method allows to insert a new note into the database.
def insert_note(self, widget):
# get the text and check if it's not empty.
text = self.get_text_input()
text_buffer = self.text_input.get_buffer()
if len(text) == 0:
self.create_error_dialog("Notes can't be empty!", None, "You cannot write an empty note.")
return
# insert on the database using the connection instance.
inserted = Note.insert(self.connection.get_instance(), text)
if not (inserted):
self.create_error_dialog("An error has occured!", None, "An error has occured while trying to insert a new note.")
return
self.reload_notes()
text_buffer.set_text("")
# This method allows to view a note.
def view_note(self, widget, note_id):
Note.view(self.connection.get_instance(), note_id, self)
# This method allows to delete a note.
def delete_note(self, widget, note_id):
# confirmation message to check if the note is going to be deleted
dialog = Gtk.MessageDialog(
transient_for=self,
flags=0,
message_type=Gtk.MessageType.QUESTION,
buttons=Gtk.ButtonsType.YES_NO,
text="Confirm delete",
title="Confirmation required",
secondary_text="Are you sure you want to delete this note? This operation cannot be undone."
)
# run the dialog, taking control, then destroy it after it receives a response.
response = dialog.run()
dialog.destroy()
if (response == Gtk.ResponseType.NO):
return
# check if the note was deleted.
deleted = Note.delete(self.connection.get_instance(), note_id)
if not (deleted):
self.create_error_dialog("An error has occured!", None, "An error has occured while trying to delete the selected note.")
return
# reload the notes widget.
self.reload_notes()
# This method allows to create a quick error dialog.
def create_error_dialog(self, dialog_title, dialog_subtitle, dialog_text):
dialog = Gtk.MessageDialog(
transient_for=self,
flags=0,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
text=dialog_subtitle,
title=dialog_title,
secondary_text=dialog_text
)
dialog.run()
dialog.destroy()
# This method allows to update the current character count.
def update_charcount(self, buffer):
start, end = buffer.get_bounds()
text = buffer.get_text(start, end, True)
length = len(text)
if length > Note.MAX_LENGTH:
# if note is larger than the database says, we'll reset the text
# so the user can't set more characters.
text_reset = text[0:Note.MAX_LENGTH]
buffer.set_text(text_reset)
length = length - 1
self.text_counter.set_text("Character count: %d/%d" % (length, Note.MAX_LENGTH))
return length
# This method allows to reload the notes widget.
def reload_notes(self):
# reload the notes widget.
self.container.remove_row(7)
# get the notes and set text to null.
notes = self.get_notes()
self.container.attach(notes, 1, 7, 12, 1)
self.container.show_all()
# This method returns the text from the input to write a note.
def get_text_input(self):
text_buffer = self.text_input.get_buffer()
start, end = text_buffer.get_bounds()
text = text_buffer.get_text(start, end, True)
return text
# instantiate new window, show it, and use the main GTk loop.
window = MainWindow()
window.show_all()
Gtk.main()