This repository has been archived on 2025-03-13. You can view files and clone it, but cannot push or open issues or pull requests.
Ion-Todo-Planner/main.py
2024-03-29 13:36:13 +01:00

161 lines
5.4 KiB
Python
Executable file

#!/bin/python3
# Ion-Todo-Planner
# Copyright (C) 2024 Ione 15
#
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
# Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License along with this program. If not,
# see <http://www.gnu.org/licenses/>.
import sqlite3
from datetime import datetime
import time
import tkinter as tk
from collections import OrderedDict
con = sqlite3.connect("todo.db")
cur = con.cursor()
tasklist = OrderedDict()
def get_time():
return (datetime.now() - datetime(1970, 1, 1)).total_seconds() + time.timezone # timezone so we get utc time!
def add_item_dialog():
input_field = tk.Tk()
input_field.title("Add Item")
input_window_width = 640
input_window_height = 320
# find the center point
input_center_x = int(screen_width / 2 - input_window_width / 2)
input_center_y = int(screen_height / 2 - input_window_height / 2)
input_field.geometry(f'{input_window_width}x{input_window_height}+{input_center_x}+{input_center_y}')
# Create an entry for adding items
entry = tk.Entry(input_field, width=50)
entry.pack(padx=10, pady=5)
entry.focus()
priority_selection_frame = tk.Frame(input_field) # container for prio and scale
priority_selection_frame.pack()
priority_label = tk.Label(priority_selection_frame, text="Priority:")
priority_label.pack(side=tk.LEFT, padx=15, pady=15)
priority_selection = tk.Scale(priority_selection_frame, from_=1, to=5, orient=tk.HORIZONTAL, width=20)
priority_selection.pack()
input_confirm = tk.Button(input_field, text="Confirm",
command=lambda: add_item(entry, priority_selection, input_field))
input_confirm.pack(side=tk.BOTTOM, padx=5, pady=5)
input_field.mainloop()
def add_item(entry, priority_selection, input_field):
item = entry.get()
priority = priority_selection.get()
if item:
listbox.insert(tk.END, item)
cur.execute("INSERT INTO todo(\"ID\",\"TODO\",\"CREATION_DATE\",\"DUE_DATE\",\"PRIO\",\"IMP\") VALUES (NULL, "
"'%s', '%s','%s', '%s', '%s' )" % (item, get_time(), get_time() + 60, priority, 0))
con.commit()
update_list()
input_field.destroy()
def remove_item():
if listbox.curselection():
selected_index = listbox.curselection()[0] # selection returns tupel
selected_task_id = list(tasklist.keys())[selected_index]
listbox.delete(selected_index)
cur.execute("DELETE FROM todo WHERE ID = %s" % selected_task_id)
con.commit()
update_list()
def update_list():
selected_task = listbox.curselection() # remember selection before rebuilding
listbox.delete(0, listbox.size()) # clear listbox
cur_time = get_time()
total_time = [] # cleanup and init
remaining_time = []
importance = []
tasklist.clear()
res = cur.execute("SELECT * FROM todo").fetchall()
for i, _tmp1 in enumerate(res):
total_time.append(res[i][3] - res[i][2])
remaining_time.append(res[i][3] - cur_time)
importance.append((total_time[i] - remaining_time[i]) / total_time[i] * res[i][4] * 100)
cur.execute("UPDATE todo SET IMP = %s WHERE ID = %s" % (int(importance[i]), res[i][0]))
con.commit()
res = cur.execute("SELECT * FROM todo ORDER BY IMP DESC").fetchall()
for i, _tmp2 in enumerate(res):
tasklist[res[i][0]] = res[i][1] # create dictionary with id and the task
redraw_list(selected_task)
def redraw_list(selected_task):
listbox.delete(0, listbox.size()) # clear listbox
for task in tasklist.items():
listbox.insert(tk.END, task[1]) # [0] = id
if selected_task:
listbox.selection_set(selected_task) # restore selection before refresh, if existing
# Create the main window
root = tk.Tk()
root.title("Ion-Todo-Planner")
# get the screen dimension
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = 1280
window_height = 720
# find the center point
center_x = int(screen_width / 2 - window_width / 2)
center_y = int(screen_height / 2 - window_height / 2)
# set the position of the window to the center of the screen
root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
root.minsize(window_width, window_height) # prevent making it smaller than min size
# Create a listbox to display the items
listbox = tk.Listbox(root, width=100)
listbox.pack(padx=5, pady=5, expand=True, fill=tk.BOTH)
# Create buttons to add and remove items
add_button = tk.Button(root, text="Add", command=add_item_dialog)
add_button.pack(side=tk.LEFT, padx=5, pady=5)
update_button = tk.Button(root, text="Reload", command=update_list)
update_button.pack(side=tk.LEFT, padx=5, pady=5)
remove_button = tk.Button(root, text="Remove", command=remove_item)
remove_button.pack(side=tk.LEFT, padx=5, pady=5)
update_list()
# Run the Tkinter event loop
refresh_time = 15
def refresh_ever_x_seconds():
update_list()
current_time = root.after(1000 * refresh_time, refresh_ever_x_seconds)
refresh_ever_x_seconds()
root.mainloop()