-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdialogs.py
262 lines (219 loc) · 10.2 KB
/
dialogs.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# CODE FOR DICTIONARY MAINTENANCE APPLICATION'S DIALOGS
# =====================================================
# LIBRARIES AND MODULES
import os
import json
import dictionaryMaintain
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from qt_material import apply_stylesheet # For theme adjustments
# CLASS DEFINITIONS
# Class for handling the settings file
class SettingsHandler(QDialog):
"""Reads and writes application settings file."""
def __init__(self):
super().__init__()
loadUi('settingsDialog.ui', self)
self.setWindowTitle('Sanakirja-asetukset')
# Read the settings file
self.settings = settingsFromJsonFile('settings.json')
scale = self.settings['scale']
self.extra = {'density_scale': f'{scale}'}
apply_stylesheet(self, theme=self.settings['theme'], extra=self.extra)
self.currentDictionary = self.settings['dictionary']
self.characterEncoding = self.settings['encoding']
self.densityScale = self.settings['scale']
# UI elements
self.browsePB = self.browsePushButton
self.browsePB.clicked.connect(self.fileDialog)
self.savePB = self.savePushButton
self.savePB.clicked.connect(self.saveSettings)
self.fileLE = self.fileLineEdit
self.fileLE.setText(os.path.normpath(self.currentDictionary))
self.encodingCB = self.characterEncodingComboBox
self.encodingCB.setCurrentText(self.characterEncoding)
# A Method for changing the spelling dictionary
def fileDialog(self):
self.fileLE.setText(self.currentDictionary)
fileName, check = QFileDialog.getOpenFileName(
None, 'Valitse sanakirja', self.currentDictionary, 'Sanakirjat (*.dic *.aff)')
if fileName:
self.fileLE.setText(os.path.normpath(fileName))
# Method for saving dictionary settings
def saveSettings(self):
self.settings['dictionary'] = self.fileLE.text()
self.settings['encoding'] = self.encodingCB.currentText()
saveSettingsToJsonFile('settings.json', self.settings)
self.close()
# A class for setting element sizes in all UIs
class SetSize(QDialog):
"""Increases or decreases the size of UI elements."""
def __init__(self):
super().__init__()
loadUi('sizeDialog.ui', self)
self.setWindowTitle('Muuta elementtien kokoa')
# Load settings from json file
self.settings = settingsFromJsonFile('settings.json')
scale = self.settings['scale']
self.extra = {'density_scale': f'{scale}'}
apply_stylesheet(self, theme=self.settings['theme'], extra=self.extra)
self.newSize = 0
# UI elements
self.sizeSB = self.sizeSpinBox
self.sizeSB.setRange(-5, 9)
self.sizeSB.setValue(int(scale))
self.sizeSB.valueChanged.connect(self.changeSize)
self.savePB = self.saveSizePushButton
self.savePB.clicked.connect(self.saveSizeSetting)
def changeSize(self):
try:
self.newSize = str(self.sizeSB.value())
self.newExtra = {'density_scale': f'{self.newSize}'}
apply_stylesheet(self, theme=self.settings['theme'], extra=self.newExtra)
except (Exception) as error:
pass
def saveSizeSetting(self):
self.settings['scale'] = self.newSize
saveSettingsToJsonFile('settings.json', self.settings)
self.close()
# A class for separate cleaning dialog for the dictionary
class SanitizeDictionary(QDialog):
"""Sorts dictionary, removes duplicates and updates the word counter."""
def __init__(self):
super().__init__()
loadUi('sanitizeDictionary.ui', self)
self.setWindowTitle('Sanakirjan tarkistus ja korjaus')
self.settings = settingsFromJsonFile('settings.json')
scale = self.settings['scale']
self.extra = {'density_scale': f'{scale}'}
apply_stylesheet(self, theme=self.settings['theme'], extra=self.extra)
self.currentDictionary = self.settings['dictionary']
self.characterEncoding = self.settings['encoding']
self.sanitizedWordList = []
self.sanitizedWordCount = 0
self.inFileLcd = self.inFileLcdNumber
self.actualLcd = self.actualLcdNumber
self.finalLcd = self.finalLcdNumber
self.okPB = self.okPushButton
self.okPB.clicked.connect(self.saveSanitized)
self.anlyzeData = self.sanitize(
self.currentDictionary, self.characterEncoding)
self.inFileLcd.display(int(self.anlyzeData[0]))
self.actualLcd.display(int(self.anlyzeData[1]))
self.finalLcd.display(int(self.anlyzeData[2]))
def sanitize(self, dictionary, encoding):
"""Reads the spelling dictionary, remove duplicates, sort and recount words
Args:
dictionary (str): file name of the dictionary
encoding (str): character encoding of the dictonary
Returns:
tuple: original, actual and final row counts
"""
with open(dictionary, 'r', encoding=encoding) as file:
originalWordCount = file.readline()
unsortedDictionary = file.readlines()
sortedDictionary = sorted(unsortedDictionary)
realWordCount = str(len(sortedDictionary)) + '\n'
# Change to a Python dictionary which does not allow duplicate keys
dictionaryFromList = dict.fromkeys(sortedDictionary)
# Make it an ordinary list again
distinctList = list(dictionaryFromList)
finalRowCount = len(distinctList)
result = (originalWordCount, realWordCount, finalRowCount)
self.sanitizedWordCount = finalRowCount
self.sanitizedWordList = distinctList
return result
def saveSanitized(self):
with open(self.currentDictionary, 'w', encoding=self.characterEncoding) as file:
file.write(str(self.sanitizedWordCount) + '\n')
file.writelines(self.sanitizedWordList)
self.close()
# A class for adding words from Joukahainen to the dictionary
class JoukahainenDialog(QDialog):
"""Dialog for getting words from Joukahainen dictionary."""
def __init__(self):
super().__init__()
loadUi('joukahainenDialog.ui', self)
self.setWindowTitle('Sanojen nouto Joukahaisesta')
self.settings = settingsFromJsonFile('settings.json')
scale = self.settings['scale']
self.extra = {'density_scale': f'{scale}'}
apply_stylesheet(self, theme=self.settings['theme'], extra=self.extra)
self.currentDictionary = self.settings['dictionary']
self.characterEncoding = self.settings['encoding']
self.xmlLE = self.xmlFileLineEdit
self.browsePB = self.browsePushButton
self.browsePB.clicked.connect(self.fileDialog)
self.savePB = self.savePushButton
self.savePB.clicked.connect(self.joukahainenToDictionary)
def fileDialog(self):
"""A methtod to create Dialog window for choosing Joukahainen xml file
"""
defaultDirectory = os.path.expanduser('~') + '\\Downloads'
self.xmlFileName, check = QFileDialog.getOpenFileName(
None, 'Valitse Joukahaisen sanasto', defaultDirectory, 'xml-tiestostot (*.xml)')
if self.xmlFileName:
self.xmlLE.setText(os.path.normpath(self.xmlFileName))
def joukahainenToDictionary(self):
"""A Method to convert and save words of Joukahainen to the spelling dictionary
"""
maintenanceOperation = dictionaryMaintain.MaintenanceOperation(
self.currentDictionary, self.characterEncoding)
self.joukahainenWords = maintenanceOperation.readFromJoukahainen(
self.xmlFileName)
result = maintenanceOperation.addSeveralWordsToDictionaryFile(
self.joukahainenWords)
# COMMON FUNCTIONS FOR FILE OPERATIONS
# A function for informing about errors eq. insufficient rights to use settings file
# in certain environments where administrative rights are needed to change files in program folders
def alert(windowTitle, alertMsg, additionalMsg, details):
"""Creates a message box for critical errors
Args:
windowTitle (str): Title of the message box
alertMsg (str): Short description of the error in Finnish
additionalMsg (str): Additional information in Finnish
details (str): Details about the error in English
"""
alertDialog = QMessageBox() # Create a message box object
# Add appropriate title to the message box
alertDialog.setWindowTitle(windowTitle)
alertDialog.setIcon(QMessageBox.Critical) # Set icon to critical
# Basic information about the error in Finnish
alertDialog.setText(alertMsg)
# Additional information about the error in Finnish
alertDialog.setInformativeText(additionalMsg)
# Technical details in English (from psycopg2)
alertDialog.setDetailedText(details)
# Only OK is needed to close the dialog
alertDialog.setStandardButtons(QMessageBox.Ok)
alertDialog.exec_() # Open the message box
# A function for reading settings from a JSON file
def settingsFromJsonFile(file):
"""Reads settings from json file and converts
json to pyhthon dictionary format
Args:
file (str): Name of JSON file containing setting parameters
Returns:
dict: Setting parameters
"""
try:
settingsFile = open(file, 'r')
settingsData = json.load(settingsFile)
settingsFile.close()
return settingsData
except Exception as e:
alert('Asetusten lukeminen ei onnistunut', 'Virhe avattaessa asetuksia', 'Lisätietoja Details-painikkeella', str(e))
# A Function to save connection settings to a JSON file
def saveSettingsToJsonFile(file, settingData):
"""Writes settings to json file.
Args:
file (str): Name of the file to write
settingData (dict): Dictionary of settings
"""
try:
settingsFile = open(file, 'w') # Opens settings file for writing
# Write dictionary in JSON format to file
json.dump(settingData, settingsFile)
settingsFile.close() # Close the file after
except Exception as e:
alert('Asetusten tallennus ei onnistunut', 'Virhe tallennettaessa asetuksia', 'Lisätietoja Details-painikkeella', str(e))