-
Notifications
You must be signed in to change notification settings - Fork 0
/
soti.py
209 lines (181 loc) · 6.37 KB
/
soti.py
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
A dialog for entering extra dwon payments.
"""
__author__ = "Sofie & Bernd Krietenstein"
__copyright__ = "Copyright (C) 2018 Sofie & Bernd Krietenstein"
__license__ = "see LICENSE file"
import sys
from PyQt5.QtWidgets import (
QDialog,
QApplication,
QWidget,
QPushButton,
QLineEdit,
QDateEdit,
QDateTimeEdit,
QMessageBox,
QAction,
QTableWidget,
QTableWidgetItem,
QHeaderView,
QVBoxLayout,
QHBoxLayout,
QSizePolicy
)
from PyQt5.QtCore import (
Qt,
QDate
)
from PyQt5.QtGui import (
QPalette,
QIcon
)
import conf
import log
class SotiDialog(QDialog):
def __init__(self, parent=None, sotis=None):
super(SotiDialog, self).__init__(parent)
self.title = 'Enter Unscheduled Redemptions'
self.width = 600
self.height = 600
if parent:
pg = parent.frameGeometry()
self.left = pg.left() + pg.width() / 2 - self.width / 2
self.top = pg.top() + pg.height() / 2 - self.height / 2
else:
self.left = 200
self.top = 200
self._table_widget = None
self.initUI()
if sotis:
for payment in sotis:
self._add_payment(payment)
self._table_widget.clearFocus()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self._table_widget = QTableWidget()
# self._table_widget.setRowCount(1)
self._table_widget.setColumnCount(2)
self._table_widget.setHorizontalHeaderItem(0, QTableWidgetItem("Date"))
self._table_widget.setHorizontalHeaderItem(1, QTableWidgetItem("Payment"))
add_button = QPushButton('+')
add_button.clicked.connect(self._add_payment)
remove_button = QPushButton('-')
remove_button.clicked.connect(self._remove_payment)
sort_button = QPushButton("Sort")
sort_button.clicked.connect(self._sort_table)
close_button = QPushButton('Close')
close_button.clicked.connect(self._close)
# Vertical layout for +- and sort buttons
table_buttons_layout = QVBoxLayout()
table_buttons_layout.addWidget(add_button)
table_buttons_layout.addWidget(remove_button)
table_buttons_layout.addWidget(sort_button)
# Horizontal layout for table and buttons
table_layout = QHBoxLayout()
table_layout.addWidget(self._table_widget)
table_layout.addLayout(table_buttons_layout)
# Vertical layout for table/buttons and close button
layout = QVBoxLayout()
layout.addLayout(table_layout)
layout.addWidget(close_button)
self.setLayout(layout)
# Set size policy
self._table_widget.horizontalHeader().setSectionResizeMode(
QHeaderView.Stretch)
# Cosmetics
self._table_widget.horizontalHeader().setStyleSheet(
"::section { background-color:lightGray }")
def _add_payment(self, payment=None):
"""
Add a line with a new extra payment.
:param payment (tuple(str, float)): Extra payment for a month.
"""
self._table_widget.insertRow(self._table_widget.rowCount())
current_row = self._table_widget.rowCount() - 1
if payment:
month = QDate.fromString(payment[0], conf.DATE_FORMAT)
amount = payment[1]
else:
month = QDate.currentDate()
amount = 0.0
month_edit = QDateEdit(month)
month_edit.setDisplayFormat(conf.DATE_FORMAT)
month_edit.currentSection = QDateTimeEdit.MonthSection
self._table_widget.setCellWidget(current_row, 0, month_edit)
self._table_widget.setItem(current_row, 1, QTableWidgetItem(str(amount)))
def _remove_payment(self):
"""
Remove the current row.
"""
self._table_widget.removeRow(self._table_widget.currentRow())
def _sort_table(self):
"""
Sort the table date-wise
"""
# later...
pass
def _close(self):
"""
Close dialog after successful validation.
"""
# Implement validation here
validation_error = self._validate()
if not validation_error:
self.accept()
else:
QMessageBox.critical(
self,
"Validation error",
validation_error)
def _validate(self):
"""
Validate table. Payments must be convertable into float. Only one
payment per month is allowed.
"""
# Make sure, payments are convertable into float.
current_row = 0
try:
for row_no in range(self._table_widget.rowCount()):
current_row = row_no
float(self._table_widget.item(row_no, 1).text())
except ValueError:
return "Invalid values in table in row {}".format(current_row + 1)
# Make sure, only on payment per month is made
row_count = self._table_widget.rowCount()
if row_count > 1:
for row_no in range(row_count):
for row_no2 in range(row_no + 1, row_count):
date1 = self._table_widget.cellWidget(row_no, 0).date()
date2 = self._table_widget.cellWidget(row_no2, 0).date()
if date1 == date2:
return (
"Ambiguous payments in rows {} and {}\n"
"Only one payment per month is allowed.".format(
row_no + 1,
row_no2 + 1))
return None
@property
def payments(self):
"""
:returns: List of extra payments.
:rtype: list(QDate, float)
"""
payment_list = []
try:
for row_no in range(self._table_widget.rowCount()):
date_edit = self._table_widget.cellWidget(row_no, 0)
date = date_edit.date().toString(conf.DATE_FORMAT)
amount = float(self._table_widget.item(row_no, 1).text())
payment_list.append((date, amount))
except Exception as ex:
log.LOGGER.error(ex)
return payment_list
if __name__ == '__main__':
app = QApplication(sys.argv)
main = SotiDialog()
main.show()
sys.exit(app.exec_())