-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewTaskTab.py
More file actions
164 lines (122 loc) · 5.36 KB
/
NewTaskTab.py
File metadata and controls
164 lines (122 loc) · 5.36 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from CustomControl import (
DateTimeEdit, DoubleSpinBox, ImportantPushButton, Label, LineEdit,
NormalPushButton, SpinBox, TabWidget, WarningBox)
from datetime import timedelta
from GlobalData import *
from PyQt5.QtCore import QDateTime
from PyQt5.QtWidgets import QApplication, QGridLayout, QWidget
class NewTaskTab(TabWidget):
'''"New Task" tab of the program main window.'''
TASK_DURATION_MAX = 10000
'''Maximum duration of a task in minutes.'''
def __init__(self, parent=None):
super(NewTaskTab, self).__init__(parent)
layout = QGridLayout()
layout.addWidget(Label('Task name:'), 0, 0)
layout.addWidget(Label('Duration:'), 1, 0)
layout.addWidget(Label('min'), 1, 3)
self.nameInput = LineEdit()
layout.addWidget(self.nameInput, 0, 1, 1, 3)
self.durationInput = SpinBox()
self.durationInput.setRange(1, NewTaskTab.TASK_DURATION_MAX)
layout.addWidget(self.durationInput, 1, 1, 1, 2)
self.editorTabs = self.createEditorTabs()
layout.addWidget(self.editorTabs, 2, 0, 1, 4)
self.addButton = ImportantPushButton('Add')
self.addButton.clicked.connect(lambda: self.onAddClicked())
layout.addWidget(self.addButton, 3, 2)
self.cancelButton = NormalPushButton('Cancel')
self.cancelButton.clicked.connect(lambda: self.onCancelClicked())
layout.addWidget(self.cancelButton, 3, 3)
self.setLayout(layout)
self.resetInputs()
def resetInputs(self):
'''Reset the values of inputs and restore the tab status.'''
self.editorTabs.setCurrentIndex(0)
self.nameInput.setText('')
self.durationInput.setValue(1)
self.weightInput.setValue(0.5)
currentDateTime = QDateTime.currentDateTime()
self.deadlineInput.setDateTime(currentDateTime)
self.deadlineInput.setMinimumDateTime(currentDateTime)
self.startFromInput.setDateTime(currentDateTime)
self.startFromInput.setMinimumDateTime(currentDateTime)
def onAddClicked(self):
'''Action when the "Add" button is clicked.'''
taskName = self.nameInput.text().strip()
if len(taskName) == 0:
WarningBox('No Task Name', 'Task name cannot be empty!').exec()
return
duration = self.durationInput.value()
currentIndex = self.editorTabs.currentIndex()
if currentIndex == 0:
self.onAddClickedScheduleForMe(taskName, duration)
elif currentIndex == 1:
self.onAddClickedLetMeDecide(taskName, duration)
else:
WarningBox('Unknown error',
'Cannot find the corresponding tab!').exec()
return
self.resetInputs()
def onAddClickedScheduleForMe(self, taskName, duration):
'''Action when the "Add" button is clicked and the "Schedule for Me" tab
is selected.'''
weight = self.weightInput.value()
assert weight >= 0.0 and weight <= 1.0
deadline = self.deadlineInput.dateTime().toPyDateTime()
task = Task(taskName, deadline, weight, duration)
try:
addFloatingTimeTask(task)
except FloatingTimeOverlapException:
WarningBox('Failed to Add Task',
'Failed to schedule for this task! Do nothing.').exec()
def onAddClickedLetMeDecide(self, taskName, duration):
'''Action when the "Add" button is clicked and the "Schedule for Me" tab
is selected.'''
startTime = self.startFromInput.dateTime().toPyDateTime()
delta = timedelta(seconds=duration * 60)
endTime = startTime + delta
try:
addFixedTimeTask(taskName, startTime, endTime)
except FixedTimeOverlapException as e:
WarningBox('Failed to Add Task',
'Failed to add task because it conflicts with the ' +
'following task:\n"{}"'.format(str(e))).exec()
def onCancelClicked(self):
self.resetInputs()
def createEditorTabs(self):
'''Create and return a tab widget containing two types of task
editors.'''
widget = TabWidget()
widget.addTab(self.createScheduleForMe(), 'Schedule for Me')
widget.addTab(self.createLetMeDecide(), 'Let Me Decide')
return widget
def createScheduleForMe(self):
'''Create and return the "Schedule for me" tab.'''
layout = QGridLayout()
layout.addWidget(Label('Weight:'), 0, 0)
layout.addWidget(Label('Deadline:'), 1, 0)
self.weightInput = DoubleSpinBox()
self.weightInput.setRange(0.0, 1.0)
layout.addWidget(self.weightInput, 0, 1)
self.deadlineInput = DateTimeEdit()
layout.addWidget(self.deadlineInput, 1, 1)
widget = QWidget()
widget.setLayout(layout)
return widget
def createLetMeDecide(self):
'''Create and return the "Let me decide" tab.'''
layout = QGridLayout()
layout.addWidget(Label('Start from:'), 0, 0)
self.startFromInput = DateTimeEdit()
self.startFromInput.setMinimumDateTime(QDateTime.currentDateTime())
layout.addWidget(self.startFromInput, 0, 1)
widget = QWidget()
widget.setLayout(layout)
return widget
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
adder = NewTaskTab()
adder.show()
app.exec_()