-
Notifications
You must be signed in to change notification settings - Fork 0
/
edtmisc.py
238 lines (182 loc) · 8.27 KB
/
edtmisc.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
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
#! /usr/local/bin/python
# -*- coding: utf-8- -*-
from PyQt4 import QtGui,QtCore,Qt
import month
import os,sys
"""
Edit additional incomes for a month. This creates a new Window with a table
where several Incomes can be stored.
"""
class MiscTable(QtGui.QTableWidget):
def __init__(self):
super(MiscTable,self).__init__()
self.setSortingEnabled(True)
self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
self.setSelectionBehavior(QtGui.QTableView.SelectRows)
self.setDropIndicatorShown(True)
self.setAcceptDrops(True)
self.setDragEnabled(True)
def cellToFloat(self,col,row):
try:
value = str(self.item(col,row).text())
value = value.replace(",",".")
return float(value)
except:
return 0.0
def event(self, event):
if (event.type()== QtCore.QEvent.KeyPress) and (event.key()==QtCore.Qt.Key_Tab):
self.emit(QtCore.SIGNAL("tabPressed"))
return True
if (event.type()== QtCore.QEvent.KeyPress) and ( (event.key()==QtCore.Qt.Key_Return) or (event.key()==QtCore.Qt.Key_Enter) ) :
self.emit(QtCore.SIGNAL("returnPressed"))
return True
return QtGui.QTableWidget.event(self, event)
class miscWindow(QtGui.QMainWindow):
def __init__(self,monat=None):
super(miscWindow,self).__init__()
self.miscwidget = miscWidget(monat)
self.setCentralWidget(self.miscwidget)
menubar = self.menuBar()
filemenu = menubar.addMenu("&Datei")
editmenu = menubar.addMenu("Bearbeiten")
exitAction = QtGui.QAction(u"Schließen", self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip(u"Fenster Schließen")
exitAction.triggered.connect(self.close)
filemenu.addAction(exitAction)
addRowAction =QtGui.QAction(u"Eintrag hinzufügen", self)
addRowAction.setShortcut('Ctrl++')
addRowAction.setStatusTip(u"Füge neue Zeile hinzu")
addRowAction.triggered.connect(self.miscwidget.addNewEntry)
delRowAction =QtGui.QAction(u"Eintrag löschen", self)
delRowAction.setShortcut('Ctrl+-')
delRowAction.setStatusTip(u"Lösche ausgewählte Zeile")
delRowAction.triggered.connect(self.miscwidget.delEntry)
editmenu.addAction(addRowAction)
editmenu.addAction(delRowAction)
self.miscwidget.setStatusBar(self.statusBar())
self.show()
def closeEvent(self, event):
self.miscwidget.saveData()
class TableItem(QtGui.QTableWidgetItem):
""" The main working area: Windows with table of current month """
def __init__(self,text):
super(TableItem,self).__init__(text)
#def __init__(self):
# super(TableItem,self).__init__()
def event(self, event):
if (event.type()== QtCore.QEvent.KeyPress) and (event.key()==QtCore.Qt.Key_Tab):
self.emit(QtCore.SIGNAL("tabPressed"))
return True
if (event.type()== QtCore.QEvent.KeyPress) and ( (event.key()==QtCore.Qt.Key_Return) or (event.key()==QtCore.Qt.Key_Enter) ) :
self.emit(QtCore.SIGNAL("returnPressed"))
return True
return QtGui.QTableWidgetItem.event(self, event)
class miscWidget(QtGui.QWidget):
""" Edit/Add aditional incomes """
def __init__(self,monat=None):
""" Create new Misc-Window
Parameters:
----------
month: current month object
"""
super(miscWidget, self).__init__()
self.monat = monat
self.lblSum = QtGui.QLabel("Summe: ")
self.initUI()
self.loadData()
self.show()
def initUI(self):
""" Create all new items for misc window """
self.setWindowTitle(u"Sonstige Einnahmen")
maingrid = QtGui.QVBoxLayout()
buttongrid = QtGui.QHBoxLayout()
btnAddLine = QtGui.QPushButton(u"Hinzufügen")
btnAddLine.clicked.connect(self.addNewEntry)
btnDelLine = QtGui.QPushButton(u"Löschen")
btnDelLine.clicked.connect(self.delEntry)
self.miscTable = MiscTable() # Create new Table
self.miscTable.setColumnCount(2)
self.miscTable.setHorizontalHeaderLabels([u"Beschreibung",u"Betrag in €"])
self.miscTable.itemChanged.connect(self.valueFormat)
buttongrid.addWidget(btnAddLine)
buttongrid.addWidget(btnDelLine)
maingrid.addWidget(self.miscTable)
maingrid.addItem(buttongrid)
self.setLayout(maingrid) # Grid for Layout
self.show()
def addNewEntry(self):
self.miscTable.insertRow(self.miscTable.rowCount())
def delEntry(self):
self.miscTable.removeRow(self.miscTable.currentRow())
def setStatusBar(self,bar):
self.statusbar = bar
self.statusbar.addWidget(self.lblSum)
def valueFormat(self,editItem):
""" Format number Cells """
red = QtGui.QColor()
red.setRgb(200,0,0)
black = QtGui.QColor()
black.setRgb(0,0,0)
# editItem.setTextColor(black) # on default all Columns are black
if editItem.column() in [1]: # Format only Currency-Related cols
origText = editItem.text().replace(",",".") # First replace all ,s as they're entered in Germany with .s
try:
newText = u"%0.2f" %(float(origText))
editItem.setText(newText.replace(".",",")) # now convert .s back to ,s
editItem.setTextColor(black)
except: # in case the number could not be formatted - print the cell in red
editItem.setTextColor(red)
self.updateSum()
elif editItem.column() in [0]: # Convert first Letter of Names to Capital letter
itemtext = str(editItem.text()).title()
editItem.setText(itemtext)
def updateSum(self):
""" Update Sum in Statusbar """
sumOfMisc = 0.0
for row in range(self.miscTable.rowCount()):
if self.miscTable.item(row,1) is None:
value = "0.00"
else:
value = self.miscTable.item(row,1).text()
value = value.replace(",",".") # First replace all ,s as they're entered in Germany with .s
try:
sumOfMisc += float(value)
except:
pass
self.lblSum.setText(u"Summe: %0.2f €" %sumOfMisc)
def loadData(self):
""" Load Data from month-object """
readerrors = 0
if ("misc" in self.monat.data) :
for entry in self.monat.data["misc"]: # Iterate through all Data-Lines
self.miscTable.insertRow(self.miscTable.rowCount()) # insert new Row at end of table
try:
self.miscTable.setItem(self.miscTable.rowCount()-1,0,TableItem((entry["text"])))
self.miscTable.setItem(self.miscTable.rowCount()-1,1,TableItem((str(entry["value"]))))
except (KeyError) as name:
readerrors+=1 # only Count Errors
else: # Empty table
self.addNewEntry()
if readerrors:
QtGui.QMessageBox.critical(self,"Fehler",str(readerrors)+u" Datensätze konnten nicht gelesen werden oder waren unvollständig.\n \n Bitte überprüfen Sie die Daten")
def saveData(self):
""" Save Data back to month Object """
data = []
print(self.miscTable.rowCount())
for row in range(self.miscTable.rowCount()):
if (self.miscTable.item(row,0) is None):
text = str("")
else:
text = str(self.miscTable.item(row,0).text())
value = self.miscTable.cellToFloat(row,1)
print(value)
print("Saving misc entry: %s %f" %(text,value))
data.append({'text':text,'value':value})
self.monat.data["misc"] = data
def main():
app = QtGui.QApplication(sys.argv)
window = miscWidget("Monat")
sys.exit(app.exec_())
if __name__ == '__main__' :
main()