This repository was archived by the owner on May 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl_MainWindow.py
More file actions
244 lines (204 loc) · 9.55 KB
/
Control_MainWindow.py
File metadata and controls
244 lines (204 loc) · 9.55 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import locale, copy, functools
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
class Control_MainWindow:
# Getting UI and ImportBase
def accountChanged(self, index):
data = self.accountsLists[index]
self.accountChangedToId = data['acId']
print("Account changed:", index, data)
#if (data != None):
# self.populateTable(data.get("imId"))
def importChanged(self, index):
ui = self.ui
data = self.importsList[index]
print("New import:", index, data)
if (data != None):
self.currentImportIndex = index
self.currentImportId = data.get("imId")
self.populateTable(data.get("imId"))
ui.txt_Comment.setText(data.get("imComment"))
self.importComment = data.get("imComment")
# Populate accounts dropdown
try:
ui.sel_Accounts.currentIndexChanged.disconnect()
except Exception as e:
pass
ui.sel_Accounts.clear()
index = 0
for entry in self.accountsLists:
#print(entry)
value = data.get("imAccount")
account = entry.get("acId")
ui.sel_Accounts.addItem("")
ui.sel_Accounts.setItemText(index, entry.get("acName"))
if (account == value):
ui.sel_Accounts.setCurrentIndex(index)
index += 1
ui.sel_Accounts.currentIndexChanged.connect(self.accountChanged)
def tableValueChanged(self, row, cell, newValue = None, obj = None):
entryType = self.tableData[row][cell]['type']
if (entryType == "trIgnore"):
if newValue == 2:
newValue = "Y"
else:
newValue = "N"
elif (entryType == "trDate"):
newValue = newValue.toString('yyyy-MM-dd')
elif (entryType == "trCategory"):
obj = newValue
i = obj.currentIndex()
if (i < len(self.categoryList)):
newValue = self.categoryList[i].get("caId")
else:
newValue = None
print("Row:", row, "Cell", cell, "Value:", newValue, "Obj:", obj, "Index:", i)
#pass
elif (entryType == "trAmount"):
item = self.ui.tableWidget.item(row, cell)
newValue = item.text()
elif (entryType == "trDescription"):
item = self.ui.tableWidget.item(row, cell)
newValue = item.text()
recordId = self.tableData[row]['trId']
oldValue = self.tableData[row][cell]['originalValue']
#print("Old Value:", self.tableData[row][cell]['originalValue'])
#print("New Value:", newValue)
#print("entryType:", entryType)
#print("recordId:", recordId)
#uniq = str(recordId) + "_" + entryType
updatedRecord = {'id':recordId, 'type':entryType, 'value':newValue, 'old':oldValue}
self.actionHistory.append(updatedRecord)
def populateTable(self, importId):
ib = self.ib
ui = self.ui
try:
ui.tableWidget.cellChanged.disconnect()
except Exception as e:
pass
transactions = ib.getTransactions(importId)
ui.tableWidget.clearContents()
ui.tableWidget.setRowCount(len(transactions))
ui.tableWidget.setSortingEnabled(False)
self.tableData = {}
rowNum = 0
for row in transactions:
cellNum = 0
rowId = row.get('trId')
self.tableData[rowNum] = {'trId':rowId}
tr = QtWidgets.QTableWidgetItem(str(rowNum +1))
tr.setTextAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter)
ui.tableWidget.setVerticalHeaderItem(rowNum, tr)
fieldName = "trDate"
td = self.createDateBox(row.get(fieldName))
self.tableData[rowNum][cellNum] = {'type':fieldName, 'originalValue': row.get(fieldName)}
ui.tableWidget.setCellWidget(rowNum, cellNum, td)
td.dateChanged.connect(functools.partial(self.tableValueChanged, rowNum, cellNum))
cellNum += 1
fieldName = "trCategory"
td = self.createCategoryDropdown(row.get('caName'))
self.tableData[rowNum][cellNum] = {'type':fieldName, 'originalValue': row.get(fieldName)}
ui.tableWidget.setCellWidget(rowNum, cellNum, td)
td.currentIndexChanged.connect(functools.partial(self.tableValueChanged, rowNum, cellNum, td))
cellNum += 1
fieldName = "trAmount"
td = QtWidgets.QTableWidgetItem()
td.setTextAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignVCenter)
td.setText(locale.currency(row.get(fieldName), 0, 1))
self.tableData[rowNum][cellNum] = {'type':fieldName, 'originalValue': row.get(fieldName)}
ui.tableWidget.setItem(rowNum, cellNum, td)
cellNum += 1
fieldName = "trIgnore"
td = QtWidgets.QCheckBox()
if (row.get(fieldName) == "Y"):
td.setChecked(True)
self.tableData[rowNum][cellNum] = {'type':fieldName, 'originalValue': row.get(fieldName)}
ui.tableWidget.setCellWidget(rowNum, cellNum, td)
td.stateChanged.connect(functools.partial(self.tableValueChanged, rowNum, cellNum))
cellNum += 1
fieldName = "trDescription"
td = QtWidgets.QTableWidgetItem()
td.setText(str(row.get(fieldName)))
self.tableData[rowNum][cellNum] = {'type':fieldName, 'originalValue': row.get(fieldName)}
ui.tableWidget.setItem(rowNum, cellNum, td)
cellNum += 1
rowNum += 1
#print("Transactions:", transactions)
#print("TableData:", self.tableData)
ui.tableWidget.setSortingEnabled(True)
ui.tableWidget.cellChanged.connect(self.tableValueChanged)
def createDateBox(self, dateValue):
dw = QtWidgets.QDateEdit(self.ui.centralWidget)
dw.setDate(QtCore.QDate.fromString(dateValue, 'yyyy-MM-dd'))
#dw.setDateTime(QtCore.QDateTime(QtCore.QDate(2012, 12, 1), QtCore.QTime(0, 0, 0)))
dw.setCalendarPopup(True)
#self.horizontalLayout.addWidget(dw)
return dw
def createCategoryDropdown(self, categoryValue):
sel = QtWidgets.QComboBox(self.ui.centralWidget)
index = 0
for entry in self.categoryList:
sel.addItem("")
value = entry.get("caName")
sel.setItemText(index, value)
if (categoryValue == value):
sel.setCurrentIndex(index)
index += 1
if (categoryValue == None):
sel.addItem("Undefined")
sel.setCurrentIndex(index)
return sel
def saveTable(self):
for data in self.actionHistory:
transactionId = data['id']
fieldName = data['type']
newValue = data['value']
self.ib.setTransactionValue(transactionId, fieldName, newValue)
print(data)
if (self.accountChangedToId and self.currentImportId):
self.ib.setAccount(self.currentImportId, self.accountChangedToId)
self.accountChangedToId = None
print("Saved account.")
if (self.ui.txt_Comment.text() != self.importComment):
self.importComment = self.ui.txt_Comment.text()
self.ib.setImportComment(self.currentImportId, self.importComment)
print("Saved comment.")
self.ib.commit()
print("Saved data!")
print(self.actionHistory)
self.actionHistory = []
def setCategories(self):
title = "Change categories?"
msgtext = "This will attempt to change category of all entries in this list that are currently undefined, and reload the current set. This will undo any unsaved changes!\n\nWould you like to do this?"
buttons = QMessageBox.Yes | QMessageBox.No
defaultButton = QMessageBox.Yes
reply = QMessageBox.question(None, title, msgtext, buttons, defaultButton)
print("Reply:", reply)
if (reply == QMessageBox.Yes):
self.ib.updateBadDescriptions(self.currentImportId)
self.ib.updateMissingCategories(self.currentImportId)
self.importChanged(self.currentImportIndex)
#QtGui.QMessageBox.information(self, 'Message Title', 'The Bosy Text', QtGui.MessageBox.No | QtGui.MessageBox.Yes | QtGui.MessageBox.Cancel)
def __init__(self, ui, ib):
#locale.setlocale(locale.LC_ALL, 'sv_SE')
#_translate = QtCore.QCoreApplication.translate
self.ui = ui
self.ib = ib
self.categoryList = self.ib.getCategories()
self.accountsLists = self.ib.getAccounts()
self.actionHistory = []
self.accountChangedToId = None
self.currentImportId = None
ui.btn_Save.clicked.connect(self.saveTable)
ui.btn_AutoInfo.clicked.connect(self.setCategories)
# Populate imports dropdown and flll the table with the last import
ui.sel_Import.clear()
index = 0
self.importsList = ib.getImports()
for entry in self.importsList:
ui.sel_Import.addItem("")
ui.sel_Import.setItemText(index, entry.get("imComment"))
index += 1
ui.sel_Import.currentIndexChanged.connect(self.importChanged)
ui.sel_Import.setCurrentIndex(index -1)
# This will execute self.importChanged(index -1)