Compare commits

..

No commits in common. "2d3c06be6728b0bab5066b28ab3ba1027fd3ea29" and "233ca3fcee5ef4fba609a3c03ff16b54ef40ab85" have entirely different histories.

3 changed files with 13 additions and 76 deletions

11
data.py
View File

@ -179,14 +179,14 @@ class Report:
dsum += duration dsum += duration
dhours, rem = divmod(duration.seconds, 3600) dhours, rem = divmod(duration.seconds, 3600)
dmins, _ = divmod(rem, 60) dmins, _ = divmod(rem, 60)
ret.append([task, start_time.strftime("%H:%M"), f"{dhours:02d}:{dmins:02d}"]) ret.append((task, start_time.strftime("%H:%M"), f"{dhours:02d}:{dmins:02d}"))
else: else:
ret.append([task, start_time.strftime("%H:%M"), ""]) ret.append((task, start_time.strftime("%H:%M"), ""))
ret.append(["", "", ""]) ret.append(("", "", ""))
dhours, rem = divmod(dsum.seconds, 3600) dhours, rem = divmod(dsum.seconds, 3600)
dmins, _ = divmod(rem, 60) dmins, _ = divmod(rem, 60)
ret.append(["Sum", "", f"{dhours:02d}:{dmins:02d}"]) ret.append(("Sum", "", f"{dhours:02d}:{dmins:02d}"))
return ret return ret
def save(self, report): def save(self, report):
@ -216,6 +216,3 @@ class Report:
def next(self): def next(self):
self._date = self._date + timedelta(days=1) self._date = self._date + timedelta(days=1)
def date(self):
return self._date.strftime("%Y-%m-%d")

View File

@ -1,16 +1,11 @@
from PySide2 import QtCore, QtGui, QtWidgets from PySide2 import QtCore, QtGui, QtWidgets
from datetime import datetime
class Report(QtWidgets.QDialog): class Report(QtWidgets.QDialog):
def __init__(self, parent, *args, **kwargs): def __init__(self, parent, *args, **kwargs):
super().__init__(parent, *args, **kwargs) super().__init__(parent, *args, **kwargs)
self._report = None self._report = None
self._report_data = None self._report_data = None
self._changing_items = False
self._new_log_task = ""
self._new_log_pos = -1
self.setWindowTitle("Report") self.setWindowTitle("Report")
@ -18,7 +13,6 @@ class Report(QtWidgets.QDialog):
self.tableWidget.verticalHeader().hide() self.tableWidget.verticalHeader().hide()
self.tableWidget.setColumnCount(3) self.tableWidget.setColumnCount(3)
self.tableWidget.setHorizontalHeaderLabels(["Task", "Start time", "Duration"]) self.tableWidget.setHorizontalHeaderLabels(["Task", "Start time", "Duration"])
self.tableWidget.cellChanged.connect(self.cell_changed)
self.header = QtWidgets.QHeaderView(QtCore.Qt.Orientation.Horizontal) self.header = QtWidgets.QHeaderView(QtCore.Qt.Orientation.Horizontal)
self.tableWidget.setHorizontalHeader(self.header) self.tableWidget.setHorizontalHeader(self.header)
@ -32,11 +26,6 @@ class Report(QtWidgets.QDialog):
self.next_button.setIcon(QtGui.QIcon.fromTheme("arrow-right")) self.next_button.setIcon(QtGui.QIcon.fromTheme("arrow-right"))
self.next_button.pressed.connect(self.next) 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 = QtWidgets.QPushButton()
del_button.setText("Delete item") del_button.setText("Delete item")
del_button.setIcon(QtGui.QIcon.fromTheme("list-remove")) del_button.setIcon(QtGui.QIcon.fromTheme("list-remove"))
@ -50,7 +39,6 @@ class Report(QtWidgets.QDialog):
blayout = QtWidgets.QHBoxLayout() blayout = QtWidgets.QHBoxLayout()
blayout.addWidget(self.previous_button) blayout.addWidget(self.previous_button)
blayout.addWidget(self.next_button) blayout.addWidget(self.next_button)
blayout.addWidget(new_button)
blayout.addWidget(del_button) blayout.addWidget(del_button)
blayout.addWidget(ok_button) blayout.addWidget(ok_button)
@ -61,21 +49,13 @@ class Report(QtWidgets.QDialog):
def set_data(self, data): def set_data(self, data):
self._report = data self._report = data
self.update_title() self._report_data = self._report.report()
self.refresh_table() self.refresh_table()
self.update_prev_next() 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): def refresh_table(self):
self._report_data = self._report.report()
self.tableWidget.setRowCount(len(self._report_data)) self.tableWidget.setRowCount(len(self._report_data))
self._changing_items = True
for row, _ in enumerate(self._report_data): for row, _ in enumerate(self._report_data):
item0 = QtWidgets.QTableWidgetItem(self._report_data[row][0]) item0 = QtWidgets.QTableWidgetItem(self._report_data[row][0])
self.tableWidget.setItem(row, 0, item0) self.tableWidget.setItem(row, 0, item0)
@ -83,12 +63,10 @@ class Report(QtWidgets.QDialog):
self.tableWidget.setItem(row, 1, item1) self.tableWidget.setItem(row, 1, item1)
item2 = QtWidgets.QTableWidgetItem(self._report_data[row][2]) item2 = QtWidgets.QTableWidgetItem(self._report_data[row][2])
self.tableWidget.setItem(row, 2, item2) self.tableWidget.setItem(row, 2, item2)
item2.setFlags(item2.flags() & QtCore.Qt.ItemIsEnabled)
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) item0.setFlags(item0.flags() & QtCore.Qt.ItemIsEnabled)
self._changing_items = False item2.setFlags(item2.flags() & QtCore.Qt.ItemIsEnabled)
if row > len(self._report_data) - 4:
item1.setFlags(item1.flags() & QtCore.Qt.ItemIsEnabled)
self.tableWidget.resizeColumnsToContents() self.tableWidget.resizeColumnsToContents()
@ -112,64 +90,25 @@ class Report(QtWidgets.QDialog):
if row > len(self._report_data) - 4: if row > len(self._report_data) - 4:
return return
del self._report_data[row] del self._report_data[row]
self.save()
self.refresh_table() 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() @QtCore.Slot()
def _accept(self): def _accept(self):
self.save() self._report.save(self._report_data)
self.accept() self.accept()
@QtCore.Slot() @QtCore.Slot()
def previous(self): def previous(self):
self.save() self._report.save(self._report_data)
self._report.previous() self._report.previous()
self.update_title() self._report_data = self._report.report()
self.refresh_table() self.refresh_table()
self.update_prev_next() self.update_prev_next()
@QtCore.Slot() @QtCore.Slot()
def next(self): def next(self):
self.save() self._report.save(self._report_data)
self._report.next() self._report.next()
self.update_title() self._report_data = self._report.report()
self.refresh_table() self.refresh_table()
self.update_prev_next() self.update_prev_next()
@QtCore.Slot()
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,3 +60,4 @@ class TaskEdit(QtWidgets.QDialog):
@tasks.setter @tasks.setter
def tasks(self, tasks): def tasks(self, tasks):
self.list.setStringList(tasks) self.list.setStringList(tasks)