Skip to content
Snippets Groups Projects
Commit 63d2556d authored by morkolai's avatar morkolai
Browse files

#52 Fikset QTimer og fikset add_code_to med mye dokumentasjon

parent 8139cd30
No related branches found
No related tags found
1 merge request!27#52 Automatisk oppdatering av kodebok basert på tid
......@@ -33,6 +33,7 @@ class Database:
If db is created, tables are created and filled.
Runs a thread that upadtes Code in CodeBook every 24h since last update.
"""
def __init__(self):
......@@ -51,10 +52,11 @@ class Database:
self.fill_tables()
print("Tables filled with data.")
# Initiates timer that triggers CodeBook update
# Initiate timer that triggers CodeBook update
self.timer = QTimer()
self.timer.setInterval(self.seconds_to_next_update())
self.timer.timeout.connect(self.update_codebook_auto)
self.timer.timeout.connect(lambda: self.update_codebook_auto())
self.timer.start()
self.conn.row_factory = sqlite3.Row # Enables row['columnName']
......@@ -247,6 +249,14 @@ class Database:
"""
Generate and insert a code for the new word in DB-table CodeBook.
This function is espescially designed for when a single word is added
to the CodeBook table. A unique code is generate and inserted in
Code which for the parameter word from before were NULL. Dependent on
the number of entries in the table various actions are performed. If
the new word makes the number of entries pass 26^x and the length of
the codes does not have he capacity, all the codes are updatet to an
appropriate length.
Parameters
----------
word : string
......@@ -260,10 +270,15 @@ class Database:
stmt = "SELECT COUNT(*) FROM CodeBook"
number_of_entries = self.conn.execute(stmt).fetchall()[0][0]
stmt = "SELECT Code FROM CodeBook"
# Incase db is approximate empty, min code lenght is 2
# In special case where table is empty or has a single word, the
# minimal length of codes will be applied
if number_of_entries < 2:
actual_code_len = soitool.coder.get_code_length_needed(0)
actual_code_len = soitool.coder.get_code_length_needed(
number_of_entries
)
else:
# Since the newly added word's code is NULL and at [0][0],
# get [1][0] to get code length from an actual code
actual_code_len = len(self.conn.execute(stmt).fetchall()[1][0])
needed_code_len = soitool.coder.get_code_length_needed(
......@@ -283,6 +298,3 @@ class Database:
# Insert code to the param word in db
stmt = "UPDATE CodeBook SET Code = ? WHERE Word = ?"
self.conn.execute(stmt, (code, word))
DB = Database()
......@@ -9,7 +9,7 @@ from enum import Enum
from PySide2.QtWidgets import QMainWindow, QApplication, QTabWidget, QAction
from PySide2.QtGui import QIcon
from soitool.soi_workspace_widget import SOIWorkspaceWidget
from soitool.database import DB
from soitool.database import Database
class ModuleType(Enum):
......@@ -28,8 +28,8 @@ class MainWindow(QMainWindow):
self.setWindowTitle("SOI-tool")
self.statusBar()
# Start Qtimer that triggers db update
DB.timer.start()
# Database instance
Database()
# flytt ut til egen funksjon, for setup av menubar
menu = self.menuBar()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment