summaryrefslogtreecommitdiff
path: root/src/rules_dialog.py
blob: 0980fdd5a1a1e152f52378162065c711e7dc46b1 (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
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
import sys
from PyQt5.QtCore import QDate
from PyQt5.QtWidgets import QApplication, QDialog, QHBoxLayout, QPushButton, QScrollArea, QVBoxLayout
from src.config import Config
from src.rule import Rule
import src.db_sqlite as DB
import src.globals as Globals

class RulesDialog(QDialog):
    """
    Show the list of rules associated with an entry
    """
    def __init__(self, entry_id):
        super().__init__()
        
        self.entry_id = entry_id
        # class globals
        self.config = Config()
        self.relevant_rules = self.getRelevantRules()

        self.initializeUI()

    def initializeUI(self):
        self.resize(500, 320)
        self.setWindowTitle("Rules")
        self.displayWidgets()
        self.exec()

    def displayWidgets(self):
        main_layout = QVBoxLayout()
        main_layout_scroll_area = QScrollArea()
        main_layout_scroll_area.setWidgetResizable(True)
        main_layout_scroll_area.setLayout(main_layout)

        self.rules_layout = QVBoxLayout()
        self.drawRules()
        main_layout.addLayout(self.rules_layout)
        
        main_layout.addStretch()
        # Create Close and Save buttons
        buttons_hbox = QHBoxLayout()
        buttons_hbox.addStretch()

        close_button = QPushButton("Close", self)
        close_button.clicked.connect(self.close)
        buttons_hbox.addWidget(close_button)

        save_button = QPushButton("Save", self)
        save_button.clicked.connect(self.save)
        buttons_hbox.addWidget(save_button)

        main_layout.addLayout(buttons_hbox)
        self.setLayout(main_layout)

    def drawRules(self):
        # Remove all children from layout
        def recursiveClear(layout):
            while layout.count():
                child = layout.takeAt(0)
                if child.widget():
                    child.widget().deleteLater()
                elif child.layout():
                    recursiveClear(child)

        recursiveClear(self.rules_layout)

        # Draw each rule
        self.r_layouts_dict = {} # Use to help update things in the save() function
        for r in self.relevant_rules:
            r_layout = r.buildLayout()
            self.r_layouts_dict[r.id] = r_layout
            del_button = QPushButton("Delete", self)
            del_button.clicked.connect((lambda id: lambda: self.deleteRule(id))(r.id))
            r_layout.addWidget(del_button)
            self.rules_layout.addLayout(r_layout)

        # Draw a button to add rules
        rules_buttons_hbox = QHBoxLayout()
        add_rule_button = QPushButton("Add Rule", self)
        add_rule_button.clicked.connect(self.addRule)
        rules_buttons_hbox.addWidget(add_rule_button)
        rules_buttons_hbox.addStretch()
        self.rules_layout.addLayout(rules_buttons_hbox)

    def addRule(self):
        self.apply()

        new_rule = Rule(0, self.entry_id, "before", QDate.currentDate())
        new_rule_id = DB.insertRule(new_rule)
        new_rule.id = new_rule_id
        Globals.rules.append(new_rule)
        self.relevant_rules = self.getRelevantRules()
        self.drawRules()

    def deleteRule(self, rule_id):
        DB.removeRule(rule_id)
        Globals.rules = list(filter(lambda r: r.id != rule_id, Globals.rules))
        self.relevant_rules = self.getRelevantRules()
        self.drawRules()

    def getRelevantRules(self):
        return list(filter(lambda r: r.entry_id == self.entry_id, Globals.rules))

    def apply(self):
        for id, layout in self.r_layouts_dict.items():
            updated_rule = Rule(
                id,
                self.entry_id,
                layout.itemAt(0).widget().currentText(),
                layout.itemAt(1).widget().date(),
                layout.itemAt(3).widget().text(),
                layout.itemAt(4).widget().text())
            DB.updateRule(updated_rule)
            Globals.rules = list(filter(lambda r: r.id != id, Globals.rules))
            Globals.rules.append(updated_rule)

    def save(self):
        """
        Save any existing rules. Added rules are automatically saved, 
        but changing rules is not, hence the need for a manual save
        """
        self.apply()
        self.done(1)

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