diff options
Diffstat (limited to 'edit_entry_form.py')
-rw-r--r-- | edit_entry_form.py | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/edit_entry_form.py b/edit_entry_form.py new file mode 100644 index 0000000..7af95dd --- /dev/null +++ b/edit_entry_form.py @@ -0,0 +1,96 @@ +import sys +from PyQt5.QtWidgets import QApplication, QDateTimeEdit, QDialog, QFormLayout, QHBoxLayout, QLabel, QLineEdit, QMessageBox, QPushButton +from PyQt5.QtGui import QFont +from PyQt5.QtCore import Qt + +Globals = __import__("globals") +from entry import Entry +DB = __import__("db_sqlite") + +class editEntryForm(QDialog): + """ + Form to edit/update an entry + """ + def __init__(self, id): + self.id = id + super().__init__() + self.initializeUI() + + def initializeUI(self): + self.resize(400, 1) + self.setWindowTitle("Edit Entry") + self.displayWidgets() + self.exec() + + def displayWidgets(self): + entry_form_layout = QFormLayout() + entry = list(filter(lambda e: e.id == self.id, Globals.entries))[0] + + title = QLabel("Edit Entry") + title.setFont(QFont("Arial", 18)) + title.setAlignment(Qt.AlignCenter) + entry_form_layout.addRow(title) + + self.entry_desc = QLineEdit() + self.entry_desc.setText(entry.desc) + entry_form_layout.addRow("Description:", self.entry_desc) + + self.entry_due = QDateTimeEdit() + self.entry_due.setDisplayFormat("MM/dd/yyyy") + self.entry_due.setDate(entry.due) + entry_form_layout.addRow("Due Date:", self.entry_due) + + self.entry_due_alt = QLineEdit() + self.entry_due_alt.setText(entry.due_alt) + entry_form_layout.addRow("Due Date (Alt):", self.entry_due_alt) + + self.entry_link = QLineEdit() # TODO see if there is a widget specifically for URLs + self.entry_link.setText(entry.link) + entry_form_layout.addRow("Link:", self.entry_link) + + # Submit and cancel buttons + buttons_h_box = QHBoxLayout() + buttons_h_box.addStretch() + close_button = QPushButton("Cancel") + close_button.clicked.connect(self.close) + buttons_h_box.addWidget(close_button) + submit_button = QPushButton("Submit") + submit_button.clicked.connect(self.handleSubmit) + buttons_h_box.addWidget(submit_button) + buttons_h_box.addStretch() + + entry_form_layout.addRow(buttons_h_box) + + self.setLayout(entry_form_layout) + + def handleSubmit(self): + desc_text = self.entry_desc.text() + due_text = self.entry_due.date() # due_text is a QDate + due_alt_text = self.entry_due_alt.text() + link_text = self.entry_link.text() + + if not desc_text: + QMessageBox.warning(self, "Error Message", + "Name cannot be blank", + QMessageBox.Close, + QMessageBox.Close) + return + + # Update DB + entry = list(filter(lambda e: e.id == self.id, Globals.entries))[0] + entry.desc = desc_text + entry.due = due_text + entry.due_alt = due_alt_text + entry.link = link_text + DB.updateEntry(entry) + + # Update global variables + Globals.entries = list(filter(lambda e: e.id != self.id, Globals.entries)) + Globals.entries.append(Entry(self.id, entry.parent_id, desc_text, due_text, due_alt_text, link_text, entry.done, entry.hidden)) + self.close() + +if __name__ == "__main__": + app = QApplication(sys.argv) + window = editEntryForm() + sys.exit(app.exec_()) + |