|
1 | 1 | import sys
|
2 |
| -from PyQt5.QtGui import QColor, QMouseEvent, QPalette |
3 |
| -from PyQt5.QtCore import QSize, Qt |
4 |
| -from PyQt5.QtWidgets import * |
5 |
| -import widgetObjects |
6 |
| -import dataObjects |
7 |
| - |
8 |
| -class Window(QMainWindow): |
9 |
| - """Main Window.""" |
10 |
| - def __init__(self, model): |
11 |
| - """Initializer.""" |
12 |
| - super().__init__() |
13 |
| - |
14 |
| - self.model = model |
15 |
| - |
16 |
| - self._init_ui() |
17 |
| - self.import_data() |
18 |
| - |
19 |
| - |
20 |
| - def _init_ui(self): |
21 |
| - self.setWindowTitle('TODOlist') |
22 |
| - self.setFixedSize(400, 700) |
23 |
| - |
24 |
| - self.generalLayout = QVBoxLayout() |
25 |
| - |
26 |
| - self._centralWidget = QWidget(self) |
27 |
| - self.setCentralWidget(self._centralWidget) |
28 |
| - self._centralWidget.setLayout(self.generalLayout) |
29 |
| - |
30 |
| - self._createMenu() |
31 |
| - self._createComboBox() |
32 |
| - self._createScrollAreaRow() |
33 |
| - self._createAddButtons() |
34 |
| - |
35 |
| - self._darkMode() |
36 |
| - |
37 |
| - def _darkMode(self): |
38 |
| - palette = QPalette() |
39 |
| - palette.setColor(QPalette.Window, QColor(53, 53, 53)) |
40 |
| - palette.setColor(QPalette.WindowText, Qt.white) |
41 |
| - palette.setColor(QPalette.Base, QColor(25, 25, 25)) |
42 |
| - palette.setColor(QPalette.AlternateBase, QColor(53, 53, 53)) |
43 |
| - palette.setColor(QPalette.ToolTipBase, Qt.white) |
44 |
| - palette.setColor(QPalette.ToolTipText, Qt.white) |
45 |
| - palette.setColor(QPalette.Text, Qt.white) |
46 |
| - palette.setColor(QPalette.Button, QColor(53, 53, 53)) |
47 |
| - palette.setColor(QPalette.ButtonText, Qt.white) |
48 |
| - palette.setColor(QPalette.BrightText, Qt.red) |
49 |
| - palette.setColor(QPalette.Link, QColor(42, 130, 218)) |
50 |
| - palette.setColor(QPalette.Highlight, QColor(42, 130, 218)) |
51 |
| - palette.setColor(QPalette.HighlightedText, Qt.black) |
52 |
| - self.setPalette(palette) |
53 |
| - |
54 |
| - def _createMenu(self): |
55 |
| - self.menu = self.menuBar().addMenu("&Menu") |
56 |
| - self.menu.addAction('&Exit', self.close) |
57 |
| - |
58 |
| - self.menu = self.menuBar().addMenu("&Add") |
59 |
| - self.menu.addAction('&Task', lambda: self.create_element('Task')) |
60 |
| - self.menu.addAction('&Section', lambda: self.create_element('Section')) |
61 |
| - self.menu.addAction('&List') |
62 |
| - |
63 |
| - def _createComboBox(self): |
64 |
| - self.combo = QComboBox() |
65 |
| - self.combo.activated.connect(lambda index: self.change_focus(index)) |
66 |
| - self.generalLayout.addWidget(self.combo) |
67 |
| - |
68 |
| - def _createScrollAreaRow(self): |
69 |
| - self.scrollAreaRow = QWidget() |
70 |
| - self.scrollAreaRowLayout = QHBoxLayout(self.scrollAreaRow) |
71 |
| - self.scrollAreaRowLayout.setContentsMargins(0,0,0,0) |
72 |
| - |
73 |
| - self.generalLayout.addWidget(self.scrollAreaRow) |
74 |
| - |
75 |
| - def _createAddButtons(self): |
76 |
| - self.addButtonsLayout = QHBoxLayout() |
77 |
| - |
78 |
| - self.addTaskButton = QPushButton('Add Task') |
79 |
| - self.addSectionButton = QPushButton('Add Section') |
80 |
| - |
81 |
| - self.addButtonsLayout.addWidget(self.addTaskButton) |
82 |
| - self.addButtonsLayout.addWidget(self.addSectionButton) |
83 |
| - |
84 |
| - self.addTaskButton.clicked.connect(lambda: self.create_element(type='Task')) |
85 |
| - self.addSectionButton.clicked.connect(lambda: self.create_element(type='Section')) |
86 |
| - |
87 |
| - self.generalLayout.addLayout(self.addButtonsLayout) |
88 |
| - |
89 |
| - def add_combo_items(self, items, focused): |
90 |
| - self.combo.addItems(items) |
91 |
| - self.combo.setCurrentText(focused) |
92 |
| - |
93 |
| - def add_lists(self, data, focused): |
94 |
| - for todolist in data: |
95 |
| - self.scrollAreaRowLayout.addWidget(widgetObjects.List(str(todolist), True if str(todolist) == focused else False)) |
96 |
| - |
97 |
| - def import_data(self): |
98 |
| - self.add_combo_items(self.model.get_list_names(), self.model.app_data['focused']) |
99 |
| - self.add_lists(self.model.data, self.model.app_data['focused']) |
100 |
| - self.set_focused_list(self.model.app_data['focused']) |
101 |
| - |
102 |
| - |
103 |
| - def set_focused_list(self, focused): |
104 |
| - lists = self.scrollAreaRow.children()[1:] |
105 |
| - for list in lists: |
106 |
| - if list.list_name == focused: |
107 |
| - self.focused_list = list |
108 |
| - return |
109 |
| - else: |
110 |
| - self.focused_list = None |
111 |
| - |
112 |
| - def change_focus(self, index): |
113 |
| - lists = self.scrollAreaRow.children()[1:] |
114 |
| - for list in lists: |
115 |
| - list.setVisible(lists.index(list) == index) |
116 |
| - if lists.index(list) == index: |
117 |
| - self.focused_list = list |
118 |
| - |
119 |
| - |
120 |
| - def create_element(self, **kwargs): |
121 |
| - type_of_element = kwargs['type'] if 'type' in kwargs else kwargs['action'].text() |
122 |
| - element_name, ok = QInputDialog.getText(self, f"create {type_of_element.lower()}", f"enter name of {type_of_element.lower()}") |
123 |
| - |
124 |
| - if not ok or element_name == '': |
125 |
| - return |
126 |
| - |
127 |
| - element = eval(f"widgetObjects.{type_of_element}(element_name)") |
128 |
| - if 'action' in kwargs: |
129 |
| - action = kwargs['action'] |
130 |
| - # index = self.scrollAreaLayout.indexOf(action.parentWidget().parentWidget().parentWidget()) |
131 |
| - index = self.focused_list.scrollAreaLayout.indexOf(eval(f"action{'.parentWidget()'*3}")) |
132 |
| - |
133 |
| - # print(action.parentWidget().parentWidget().add_menu.actions()[3].isChecked()) |
134 |
| - insert_position = 0 if action.parentWidget().parentWidget().insert_menu.actions()[3].isChecked() is True else 1 |
135 |
| - |
136 |
| - self.focused_list.scrollAreaLayout.insertWidget(index + insert_position, element) |
137 |
| - else: |
138 |
| - self.focused_list.scrollAreaLayout.addWidget(element) |
139 |
| - |
140 |
| - ## Listen for when an action in the element's menu is triggered |
141 |
| - eval(f"element.{type_of_element.lower()}RightClick.triggered.connect(self.right_click_menu_clicked)") |
142 |
| - |
143 |
| - def delete_element(self, action): |
144 |
| - parent_widget = action.parentWidget().parentWidget() |
145 |
| - print(parent_widget) |
146 |
| - self.scrollAreaLayout.removeWidget(parent_widget) |
147 |
| - parent_widget.deleteLater() |
148 |
| - |
149 |
| - def right_click_menu_clicked(self, action): |
150 |
| - switch_case_dict = { |
151 |
| - 'Delete': self.delete_element, |
152 |
| - 'Task': self.create_element, |
153 |
| - 'Section': self.create_element |
154 |
| - } |
155 |
| - |
156 |
| - if action is not None and action.text() in switch_case_dict and action.parentWidget().title() == 'Insert': |
157 |
| - print(action.text()) |
158 |
| - switch_case_dict[action.text()](action=action) |
159 |
| - |
160 |
| - |
161 |
| -class Model: |
162 |
| - def __init__(self) -> None: |
163 |
| - self.data = [dataObjects.List('Project 1'), dataObjects.List('Side Project')] |
164 |
| - self.app_data = { |
165 |
| - 'focused': 'Side Project' |
166 |
| - } |
167 |
| - |
168 |
| - def get_list_names(self): |
169 |
| - return [str(i) for i in self.data] |
170 |
| - |
171 |
| -class Controller: |
172 |
| - def __init__(self, model, view) -> None: |
173 |
| - self.model = model # type: Model |
174 |
| - self.view = view # type: Window |
175 |
| - |
176 |
| - |
177 |
| - |
178 |
| - self.view.add_combo_items(self.model.get_list_names(), self.model.app_data['focused']) |
179 |
| - self.view.import_data(self.model.data, self.model.app_data['focused']) |
180 |
| - self.view.set_focused_list(self.model.app_data['focused']) |
181 |
| - |
182 |
| - self.view.show() |
| 2 | +from PyQt5.QtWidgets import QApplication |
| 3 | +from model.model import Model |
| 4 | +from view.view import Window |
183 | 5 |
|
184 | 6 | class TODOlistApplication():
|
185 | 7 | def __init__(self) -> None:
|
|
0 commit comments