From 07669043eb955d4056853f30716009dd5ec79fa6 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 20 Oct 2023 20:49:52 +0100 Subject: [PATCH] WIP V3 --- archive/todo/.DS_Store | Bin 0 -> 6148 bytes archive/todo/data.db | 1 + archive/todo/mainwindow.ui | 71 +++++++++++++++++++++++++ archive/todo/tick.png | Bin 0 -> 634 bytes archive/todo/todo.py | 106 +++++++++++++++++++++++++++++++++++++ 5 files changed, 178 insertions(+) create mode 100644 archive/todo/.DS_Store create mode 100644 archive/todo/data.db create mode 100644 archive/todo/mainwindow.ui create mode 100755 archive/todo/tick.png create mode 100644 archive/todo/todo.py diff --git a/archive/todo/.DS_Store b/archive/todo/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 + + MainWindow + + + + 0 + 0 + 275 + 314 + + + + Todo + + + + + + + QAbstractItemView::SingleSelection + + + + + + + + + + Delete + + + + + + + Complete + + + + + + + + + + + + + Add Todo + + + + + + + + + 0 + 0 + 275 + 22 + + + + + + + + diff --git a/archive/todo/tick.png b/archive/todo/tick.png new file mode 100755 index 0000000000000000000000000000000000000000..2414885b8576481fb6350ed9ce01ab033169fd75 GIT binary patch literal 634 zcmV-=0)_pFP)tYd4K$mX5uyr2F@fdffp{&DSHtl4|Bn9=ukpCx`#%PTUr-D(@b7;C zhJXJjrnw{;1KBM=6&|E`fsNrGL!Y6%zUh}QUl`(@V)PmQFtotEKmafTHP0uP}7&%m4pcKQ#X)BiAJ2y+PrtBI>9eEIt2-_c7)?*Lsf z5vX5*Af@m-wBJRV%#Fiz=BdPr0!2^a2d-yv7gSU);Z6{`~Oy?E5qSnHUln4*Y%!){F!Y1~5WXoC44g zb7l_)X~q^V6MmWR7e3wj|L|Wb!|^}Y86N$^h+kv_Kw-fP!~#If$6(B4$)LlK#&G6; zC&ShMSAb$5tA9Xg5dOsg@-z^@3;;QS9f&!gG&3|;{Da~@Q2ZB({s%XJ5&#fj0J|In U+D>(nEdT%j07*qoM6N<$g0@&8X#fBK literal 0 HcmV?d00001 diff --git a/archive/todo/todo.py b/archive/todo/todo.py new file mode 100644 index 0000000..eb1aa3b --- /dev/null +++ b/archive/todo/todo.py @@ -0,0 +1,106 @@ +import sys +import datetime +import json +from PyQt5 import QtCore, QtGui, QtWidgets, uic +from PyQt5.QtCore import Qt + + +qt_creator_file = "mainwindow.ui" +Ui_MainWindow, QtBaseClass = uic.loadUiType(qt_creator_file) +tick = QtGui.QImage('tick.png') + + +class TodoModel(QtCore.QAbstractListModel): + def __init__(self, *args, todos=None, **kwargs): + super(TodoModel, self).__init__(*args, **kwargs) + self.todos = todos or [] + + def data(self, index, role): + if role == Qt.DisplayRole: + _, text = self.todos[index.row()] + return text + + if role == Qt.DecorationRole: + status, _ = self.todos[index.row()] + if status: + return tick + + def rowCount(self, index): + return len(self.todos) + + def flags(self, index): + print(datetime.datetime.now().time().strftime("%H:%M:%S")) + return super().flags(index) + + +class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow): + def __init__(self): + QtWidgets.QMainWindow.__init__(self) + Ui_MainWindow.__init__(self) + self.setupUi(self) + self.model = TodoModel() + self.load() + self.todoView.setModel(self.model) + self.addButton.pressed.connect(self.add) + self.deleteButton.pressed.connect(self.delete) + self.completeButton.pressed.connect(self.complete) + + def add(self): + """ + Add an item to our todo list, getting the text from the QLineEdit .todoEdit + and then clearing it. + """ + text = self.todoEdit.text() + if text: # Don't add empty strings. + # Access the list via the model. + self.model.todos.append((False, text)) + # Trigger refresh. + self.model.layoutChanged.emit() + # Empty the input + self.todoEdit.setText("") + self.save() + + def delete(self): + indexes = self.todoView.selectedIndexes() + if indexes: + # Indexes is a list of a single item in single-select mode. + index = indexes[0] + # Remove the item and refresh. + del self.model.todos[index.row()] + self.model.layoutChanged.emit() + # Clear the selection (as it is no longer valid). + self.todoView.clearSelection() + self.save() + + def complete(self): + indexes = self.todoView.selectedIndexes() + if indexes: + index = indexes[0] + row = index.row() + status, text = self.model.todos[row] + self.model.todos[row] = (True, text) + # .dataChanged takes top-left and bottom right, which are equal + # for a single selection. + self.model.dataChanged.emit(index, index) + # Clear the selection (as it is no longer valid). + self.todoView.clearSelection() + self.save() + + def load(self): + try: + with open('data.db', 'r') as f: + self.model.todos = json.load(f) + except Exception: + pass + + def save(self): + with open('data.db', 'w') as f: + data = json.dump(self.model.todos, f) + + +app = QtWidgets.QApplication(sys.argv) +window = MainWindow() +window.show() +app.exec_() + +