-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettingsWindow.py
More file actions
124 lines (102 loc) · 4.79 KB
/
settingsWindow.py
File metadata and controls
124 lines (102 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import json
import sqlite3
from PyQt5 import QtGui
from PyQt5.QtCore import QRect
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import QFileDialog, QMainWindow, QMessageBox
from const.CONSTANTS import ENCODING, settingsWindow_styles, dark_settingsWindow_styles, writingWindow_styles
from const.settingsWindow_UI import SettingsWindow
class SettingsPage(QMainWindow, SettingsWindow):
def __init__(self):
super(SettingsPage, self).__init__()
self.previousWindow = None
self.pix = QPixmap('icons/arrow.png')
self.dpix = QPixmap('icons/dark-arrow.png')
self.is_dark_theme = None
self.is_letter_ignore = None
self.settings = {}
self.average_time = 0
self.setupUi(self)
self.load_settings()
self.quit.setPixmap(self.pix)
self.darkTheme.clicked.connect(self.set_dark_theme)
self.letterIgnore.clicked.connect(self.set_letter_ignore)
self.addFiles.clicked.connect(self.add_new_file)
def setPreviousWindow(self, window):
self.previousWindow = window
def set_dark_theme(self):
if self.darkTheme.isChecked():
self.darkTheme.setIcon(QIcon('icons/toggle_on.png'))
self.setStyleSheet(dark_settingsWindow_styles)
self.quit.setPixmap(self.dpix)
self.is_dark_theme = True
else:
self.setStyleSheet(settingsWindow_styles)
self.darkTheme.setIcon(QtGui.QIcon('icons/toggle_off.png'))
self.quit.setPixmap(self.pix)
self.is_dark_theme = False
self.settings = {'darkTheme': self.is_dark_theme, 'letterIgnore': self.is_letter_ignore}
self.write_settings(self.settings)
def set_letter_ignore(self):
if self.letterIgnore.isChecked():
self.letterIgnore.setIcon(QIcon('icons/toggle_on.png'))
self.is_letter_ignore = True
else:
self.letterIgnore.setIcon(QtGui.QIcon('icons/toggle_off.png'))
self.is_letter_ignore = False
self.settings = {'darkTheme': self.is_dark_theme, 'letterIgnore': self.is_letter_ignore}
self.write_settings(self.settings)
def load_settings(self):
with open('data/settings.json', 'r') as fileobject:
settings = json.load(fileobject)
self.is_dark_theme = settings['darkTheme']
self.is_letter_ignore = settings['letterIgnore']
self.average_time = self.load_time()
self.averageTimeView.setText('Среднее время выполнения: ' + self.average_time)
self.settings = settings
if self.is_dark_theme:
self.darkTheme.setChecked(True)
if self.is_letter_ignore:
self.letterIgnore.setChecked(True)
self.set_dark_theme()
self.set_letter_ignore()
def write_settings(self, settings):
with open('data/settings.json', 'w') as fileobject:
json.dump(settings, fileobject)
def load_time(self):
with open('data/times.txt', 'r') as f:
times = f.readlines()
times = list(map(int, list(map(str.rstrip, times))))
return self.format_time(sum(times) // len(times) if len(times) > 0 else 0)
def format_time(self, time):
time = time // 10
minutes = time // 6000
seconds = (time - minutes * 6000) // 100
mseconds = time - seconds * 100 - minutes * 6000
minutes, seconds, mseconds = str(minutes), str(seconds), str(mseconds)
return f'{minutes}:{seconds if len(seconds) > 1 else "0" + seconds}:{mseconds if len(mseconds) > 1 else "0" + mseconds}'
def add_new_file(self):
filename = QFileDialog.getOpenFileName(self, 'Choose file', '', 'Text files(*.txt)')[0]
if filename != '':
with open(filename, 'r', encoding=ENCODING) as f:
text = f.read()
try:
if text != '' and len(text.split()) > 2:
self.load_file_to_db(text)
except Exception as e:
msgbox = QMessageBox(self)
msgbox.setText('Файл не соответсвует требованиям или возникла другая ошибка.\n'
'Файл не должен быть пустым.\n'
"Файл должен иметь более 2 слов")
msgbox.setStyleSheet(writingWindow_styles)
msgbox.setGeometry(QRect(810, 490, 300, 100))
def load_file_to_db(self, text):
con = sqlite3.connect("data.db")
cur = con.cursor()
num = cur.execute(f"""SELECT id FROM texts """).fetchall()
num = num[-1][0] + 1
res = (num, text)
cur.execute(
f"""INSERT INTO texts ( id, text ) VALUES {res};""")
cur.execute("""COMMIT TRANSACTION;""")
con.close()