summaryrefslogtreecommitdiff
path: root/src/preferences_dialog.py
blob: 7f39fb70ee7604ad448e53b90f87b0d7062780f3 (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
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QFileDialog, QFormLayout, QHBoxLayout, QLineEdit, QPushButton, QTabWidget, QVBoxLayout, QWidget
from src.config import Config

class PreferencesDialog(QDialog):
    """
    Implemented to set configuration options in the program
    """
    def __init__(self):
        super().__init__()
        
        # class globals
        self.config = Config()

        self.initializeUI()

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

    def displayWidgets(self):
        # TODO make this a scrollable window
        # FIXME could use some work on sizing
        main_layout = QVBoxLayout()
        tab_bar = QTabWidget(self)
        paths_tab = self.pathsTabLayout()

        tab_bar.addTab(paths_tab, "Paths")
        main_layout.addWidget(tab_bar)
        main_layout.addStretch()

        buttons_hbox = QHBoxLayout()
        buttons_hbox.addStretch()

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

        apply_button = QPushButton("Apply", self)
        apply_button.clicked.connect(self.apply)
        buttons_hbox.addWidget(apply_button)

        reload_button = QPushButton("Reload", self)
        reload_button.clicked.connect(self.reload)
        buttons_hbox.addWidget(reload_button)
        
        main_layout.addLayout(buttons_hbox)
        self.setLayout(main_layout)

    def pathsTabLayout(self):
        output = QWidget()
        output_layout = QFormLayout()

        # Dialog for setting the database file path
        db_path_hbox = QHBoxLayout()
        self.db_path_edit = QLineEdit()
        if "paths" in self.config.config:
            self.db_path_edit.setText(self.config.config["paths"]["db_path"])
        db_path_hbox.addWidget(self.db_path_edit)
        db_path_button = QPushButton("...")
        db_path_button.setMaximumWidth(25)
        db_path_button.clicked.connect(self.dbPathDialog)
        db_path_hbox.addWidget(db_path_button)
        output_layout.addRow("Database File:", db_path_hbox)

        output.setLayout(output_layout)
        return output

    def dbPathDialog(self):
        file_dialog = QFileDialog(self)
        # TODO create filter to only allow selecting .db files
        new_path = file_dialog.getOpenFileName(self, "Open File")

        if new_path[0]:
            self.db_path_edit.setText(new_path[0])

    def apply(self):
        """
        Update the configuration in the config file
        """
        # Save paths
        if "paths" in self.config.config:
            try:
                with open(self.db_path_edit.text(), 'a'):
                    self.config.config["paths"]["db_path"] = self.db_path_edit.text()
            except:
                print("Warning: db_path '{}' does not exist; skipping".format(self.db_path_edit.text()))

        self.config.updateConfig()

    def reload(self):
        """
        Update, reload, and close the window
        """
        self.apply()
        self.done(1)

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