summaryrefslogtreecommitdiff
path: root/src/edit_entry_form.py
blob: c1d3664ab75b32a128f70337f7a3ede701fc599d (plain)
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
import os
import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox
from PyQt5.QtCore import QDate

import src.globals as Globals
from src.entry import Entry
import src.db_sqlite as DB

# Reuses the add_entry_form UI file
class editEntryForm(QDialog):
    """
    Form to edit/update an entry
    """
    def __init__(self, id):
        self.id = id
        super().__init__()
        uic.loadUi(os.path.join("src", "add_entry_form.ui"), self)
        self.initializeUI()

    def initializeUI(self):
        self.setWindowTitle("Edit Entry")
        self.displayWidgets()
        self.exec()

    def displayWidgets(self):
        entry = list(filter(lambda e: e.id == self.id, Globals.entries))[0]

        self.title.setText("Edit Entry")
        self.new_entry_desc.setText(entry.desc)
        self.new_entry_due.setDate(QDate.currentDate())
        if entry.due:
            self.new_entry_due.setDate(entry.due)
            self.new_entry_due_checkbox.setChecked(True)
        else:
            self.new_entry_due_checkbox.setChecked(False)
        self.new_entry_due_alt.setText(entry.due_alt)
        self.new_entry_link.setText(entry.link)
        self.new_entry_color.setText(entry.color)
        self.new_entry_highlight.setText(entry.highlight)
        self.buttonBox.rejected.connect(self.close)
        self.buttonBox.accepted.connect(self.handleSubmit)

    def handleSubmit(self):
        desc_text = self.new_entry_desc.text()
        if self.new_entry_due_checkbox.isChecked():
            due_text = self.new_entry_due.date() # due_text is a QDate
        else:
            due_text = "" # due is unchecked
        due_alt_text = self.new_entry_due_alt.text()
        link_text = self.new_entry_link.text()
        color_text = self.new_entry_color.text()
        highlight_text = self.new_entry_highlight.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
        entry.color = color_text
        entry.highlight = highlight_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, color_text, highlight_text, entry.done, entry.hidden))
        self.close()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = editEntryForm()
    sys.exit(app.exec_())