-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathortho.py
118 lines (96 loc) · 3.6 KB
/
ortho.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
import sqlite3
import json
from PyQt6 import uic
import sys
import os
import py7zr
import typing as type
from PyQt6 import QtGui
from PyQt6.QtWidgets import (
QMainWindow,
QPushButton,
QLabel,
QApplication,
QTextEdit,
QScrollArea,
)
try:
from ctypes import windll
# for Windows taskbar logo
myappid = "Redwan_Hossain.Ortho_Dictionary.1.0.0"
windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
except ImportError:
pass
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
class Main_Ui(QMainWindow):
UI_FILE = os.path.join(BASE_DIR, "ortho.ui")
DB_PATH = os.path.join(BASE_DIR, "ortho.db")
BN_JSON_DB_PATH = os.path.join(BASE_DIR, "word_db_bangla.json")
if not os.path.exists(DB_PATH):
print("Extracting DataBase")
with py7zr.SevenZipFile("./ortho_db.7z", mode="r") as file:
file.extractall()
print("DataBase successfully extracted ")
DB_CONNECTION: type.Any = ""
JSON_BN_DB: dict = {}
def init_db_connection(self):
self.DB_CONNECTION = sqlite3.connect(self.DB_PATH)
def init_json_bn_db(self):
with open(f"{self.BN_JSON_DB_PATH}") as json_file:
self.JSON_BN_DB = json.load(json_file)
def __init__(self):
super(Main_Ui, self).__init__()
# load the Main_Ui
uic.loadUi(self.UI_FILE, self)
self.setFixedSize(self.size())
# load the db
self.init_db_connection()
# load the json map for db query
self.init_json_bn_db()
# defining widgets
self.word_search_bar = self.findChild(QTextEdit, "word_search_box")
self.word_search_btn = self.findChild(QPushButton, "word_search_button")
self.scrollable = self.findChild(QScrollArea, "scroll_able_area_bn_word")
self.picture_display_box = self.findChild(QLabel, "bn_word_meaning_box")
# button click handler
self.word_search_btn.clicked.connect(self.word_search_button_click_handler)
# Initially hide the result area
self.scrollable.hide()
self.picture_display_box.hide()
def query_json_bn_db(self, word_search) -> type.Any:
word_index = self.JSON_BN_DB.get(f"{word_search}")
return word_index
def query_db(self, key) -> None:
cursor: type.Any = self.DB_CONNECTION.cursor()
cursor.execute(
f"""SELECT bangla_definition FROM bangla_dictionary WHERE _id = {key};"""
)
result = cursor.fetchone()
image = result[0]
pixmap = QtGui.QPixmap()
pixmap.loadFromData(image, "gif")
pic_width = pixmap.width()
pic_height = pixmap.height()
self.picture_display_box.setMinimumSize(pic_width, pic_height)
self.picture_display_box.setPixmap(pixmap)
def word_search_button_click_handler(self) -> None:
word_from_user = str(self.word_search_bar.toPlainText())
if word_from_user != "":
word_key = self.query_json_bn_db(word_from_user)
if word_key:
self.scrollable.show()
self.picture_display_box.show()
self.statusBar().clearMessage()
self.picture_display_box.clear()
self.query_db(word_key)
else:
self.scrollable.hide()
self.picture_display_box.hide()
self.statusBar().showMessage("Word not found.")
self.statusBar().repaint()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon(os.path.join(BASE_DIR, "ortho.ico")))
window = Main_Ui()
window.show()
sys.exit(app.exec())