Added ability to edit report further.

This commit is contained in:
Faerbit 2020-02-24 18:18:35 +01:00
parent 1d3f6384e2
commit 2d3c06be67
3 changed files with 50 additions and 9 deletions

View File

@ -216,3 +216,6 @@ class Report:
def next(self):
self._date = self._date + timedelta(days=1)
def date(self):
return self._date.strftime("%Y-%m-%d")

View File

@ -9,6 +9,8 @@ class Report(QtWidgets.QDialog):
self._report = None
self._report_data = None
self._changing_items = False
self._new_log_task = ""
self._new_log_pos = -1
self.setWindowTitle("Report")
@ -30,6 +32,11 @@ class Report(QtWidgets.QDialog):
self.next_button.setIcon(QtGui.QIcon.fromTheme("arrow-right"))
self.next_button.pressed.connect(self.next)
new_button = QtWidgets.QPushButton()
new_button.setText("New item")
new_button.setIcon(QtGui.QIcon.fromTheme("list-add"))
new_button.pressed.connect(self.new_log)
del_button = QtWidgets.QPushButton()
del_button.setText("Delete item")
del_button.setIcon(QtGui.QIcon.fromTheme("list-remove"))
@ -43,6 +50,7 @@ class Report(QtWidgets.QDialog):
blayout = QtWidgets.QHBoxLayout()
blayout.addWidget(self.previous_button)
blayout.addWidget(self.next_button)
blayout.addWidget(new_button)
blayout.addWidget(del_button)
blayout.addWidget(ok_button)
@ -53,12 +61,16 @@ class Report(QtWidgets.QDialog):
def set_data(self, data):
self._report = data
self.update_title()
self.refresh_table()
self.update_prev_next()
def save(self):
self._report.save(self._report_data)
def update_title(self):
self.setWindowTitle(f"Report {self._report.date()}")
def refresh_table(self):
self._report_data = self._report.report()
self.tableWidget.setRowCount(len(self._report_data))
@ -71,10 +83,11 @@ class Report(QtWidgets.QDialog):
self.tableWidget.setItem(row, 1, item1)
item2 = QtWidgets.QTableWidgetItem(self._report_data[row][2])
self.tableWidget.setItem(row, 2, item2)
item0.setFlags(item0.flags() & QtCore.Qt.ItemIsEnabled)
item2.setFlags(item2.flags() & QtCore.Qt.ItemIsEnabled)
if row > len(self._report_data) - 4:
if row > len(self._report_data) - 3:
item1.setFlags(item1.flags() & QtCore.Qt.ItemIsEnabled)
if row > len(self._report_data) - 4:
item0.setFlags(item0.flags() & QtCore.Qt.ItemIsEnabled)
self._changing_items = False
self.tableWidget.resizeColumnsToContents()
@ -102,6 +115,16 @@ class Report(QtWidgets.QDialog):
self.save()
self.refresh_table()
@QtCore.Slot()
def new_log(self):
after = min(self.tableWidget.currentItem().row(), len(self._report_data) - 4) + 1
self._new_log_pos = after
self.tableWidget.insertRow(after)
self.tableWidget.setCurrentCell(after, 0)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setItem(after, 0, item)
self.tableWidget.editItem(item)
@QtCore.Slot()
def _accept(self):
self.save()
@ -111,6 +134,7 @@ class Report(QtWidgets.QDialog):
def previous(self):
self.save()
self._report.previous()
self.update_title()
self.refresh_table()
self.update_prev_next()
@ -118,6 +142,7 @@ class Report(QtWidgets.QDialog):
def next(self):
self.save()
self._report.next()
self.update_title()
self.refresh_table()
self.update_prev_next()
@ -125,12 +150,26 @@ class Report(QtWidgets.QDialog):
def cell_changed(self, row, column):
if self._changing_items:
return
if column == 2:
self.tableWidget.item(row, column).setText(self._report_data[row][column])
return
item = self.tableWidget.item(row, column)
if column == 0:
if self._new_log_pos == row:
self._new_log_task = self.tableWidget.item(row, column).text() or "Unnamed"
else:
self._report_data[row][column] = item.text() or "Unnamed"
if column == 1:
try:
new_time = datetime.strptime(item.text(), "%H:%M").time()
except ValueError:
item.setText(self._report_data[row][column])
return
if self._new_log_pos == row:
self._report_data.insert(row, [self._new_log_task, new_time.strftime("%H:%M"), ""])
else:
self._report_data[row][column] = new_time.strftime("%H:%M")
self._new_log_pos = -1
self._new_log_task = ""
self.save()
self.refresh_table()

View File

@ -60,4 +60,3 @@ class TaskEdit(QtWidgets.QDialog):
@tasks.setter
def tasks(self, tasks):
self.list.setStringList(tasks)