-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
490 lines (366 loc) · 18.6 KB
/
app.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import webbrowser
from PyQt5 import QtWidgets, uic, QtCore, QtGui
import sys
import pandas as pd
import sqlite3
from docx import Document
from docx.shared import Inches
from docx.enum.style import WD_STYLE_TYPE
from docx.enum.text import WD_ALIGN_PARAGRAPH
import urllib.request
from io import BytesIO
import numpy as np
waiting_app = QtWidgets.QApplication([])
splash_screen = QtWidgets.QSplashScreen()
pixmap_ = QtGui.QPixmap('intro.png')
splash_screen.setPixmap(pixmap_)
splash_screen.show()
splash_screen.showMessage('.'*3 + "جاري تفكيك البيانات",alignment=QtCore.Qt.AlignRight, color=QtGui.QColor(255,255,255))
def data_version():
f_version = open('data/version.sai')
data_v = float(f_version.read())
f_version.close()
return data_v
def update_log(update):
with open('data/update.log', 'a') as update_log_f:
update_log_f.write(update)
try:
page = urllib.request.urlopen('https://raw.githubusercontent.com/Al-Sharabi/SAI-project/main/version.txt')
details = page.read().decode().split()
latest_version = float(details[0])
if data_version() != latest_version:
print(details[1])
splash_screen.showMessage("...تحميل قاعدة بيانات جديدة",alignment=QtCore.Qt.AlignRight, color=QtGui.QColor(255,255,255))
urllib.request.urlretrieve(details[1], 'data/inscr_data.db')
update_log(f"Data updated from version {data_version()} to version {latest_version} \n")
with open('data/version.sai', 'r+') as f_v:
f_v.seek(0)
f_v.truncate(0)
f_v.write(str(latest_version))
except Exception as e:
if str(e) != "<urlopen error [Errno 11001] getaddrinfo failed>":
update_log(str(e) + " \n")
def purifying(x):
a = x.replace('³', '')
a = a.replace(']', '')
a = a.replace('[', '')
a = a.replace('>', '')
a = a.replace('<', '')
a = a.replace('(', '')
a = a.replace(')', '')
a = a.replace('.', '')
a = a.replace(';', '')
a = a.replace('"', '')
a = a.replace('\'', '')
a = a.replace('↯', '')
a = a.replace('—', '')
a = a.replace('-', '')
a = a.replace('\n', '')
a = a.replace('\t', '')
return a
#getting inscriptions
conn = sqlite3.connect('data\inscr_data.db')
inscriptions_df = pd.read_sql('SELECT * FROM inscriptions', conn)
titles = inscriptions_df['TITLES']
epigraphs = inscriptions_df['EPIGRAPH']
inscriptions_df['AR_TRANSLITERATION_pure'] = inscriptions_df['AR_TRANSLITERATION'].apply(lambda x: purifying(x))
splash_screen.close()
class imgWidget(QtWidgets.QWidget):
def __init__(self):
super(imgWidget, self).__init__()
uic.loadUi('img_widget.ui', self)
self.setWindowTitle("صورة النقش")
self.setWindowIcon(QtGui.QIcon('sai_icon.ico'))
class searchByContentWidget(QtWidgets.QWidget):
def __init__(self):
super(searchByContentWidget, self).__init__()
uic.loadUi('search_by_content.ui', self)
self.setWindowTitle("البحث في النص")
self.setWindowIcon(QtGui.QIcon('sai_icon.ico'))
class mainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(mainWindow, self).__init__()
uic.loadUi('saipc.ui', self)
self.showMaximized()
self.setWindowIcon(QtGui.QIcon('sai_icon.ico'))
self.title_inscr_label.setAlignment(QtCore.Qt.AlignCenter)
self.language_label.setAlignment(QtCore.Qt.AlignCenter)
self.source_code_action.triggered.connect(lambda: webbrowser.open('https://github.com/Al-Sharabi/SAI-project'))
self.dataversion.setText(f"v {data_version()}")
self.title_inscr_label.setText(inscriptions_df.iloc[0]['TITLES'])
self.ar_transliteration_text_browser.setText(inscriptions_df.iloc[0]['AR_TRANSLITERATION'])
self.en_transliteration_text_browser.setText(inscriptions_df.iloc[0]['TRANSLITERATION'])
self.language_label.setText(inscriptions_df.iloc[0]['LANGUAGE'])
self.show_image_btn.setDisabled(True)
self.show_img_action.setDisabled(True)
ar_translation = inscriptions_df.iloc[0]['ar_translation']
en_translation = inscriptions_df.iloc[0]['TRANSLATION']
try:
devnote = urllib.request.urlopen('https://raw.githubusercontent.com/Al-Sharabi/SAI-project/main/devnote.html')
self.notefromdev.setText(devnote.read().decode())
except:
pass
site = f"""
Object name:
{inscriptions_df.iloc[0]['TITLES']}
Modern site:
{inscriptions_df.iloc[0]['Modern site']}
Ancient site:
{inscriptions_df.iloc[0]['Ancient site']}
Geographical area:
{inscriptions_df.iloc[0]['Geographical area']}
Country:
{inscriptions_df.iloc[0]['Country']}
"""
self.sitelabel.setText(site)
if ar_translation:
self.ar_translation_text_browser.setText(ar_translation)
self.ar_translation_text_browser.selectAll().setAlignment(QtCore.Qt.AlignCenter)
self.ar_translation_text_browser.setAlignment(QtCore.Qt.AlignCenter)
else:
self.ar_translation_text_browser.setVisible(False)
if en_translation:
self.en_translation_text_browser.setText(en_translation)
else:
self.en_translation_text_browser.setVisible(False)
self.inscr_comboBox.addItems(epigraphs)
search_le_completer = QtWidgets.QCompleter(epigraphs)
search_le_completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
search_le_completer.setFilterMode(QtCore.Qt.MatchContains)
self.search_le.setCompleter(search_le_completer)
self.img_widget = imgWidget()
self.search_by_content_widget = searchByContentWidget()
##btns and slots connections
##btns
self.search_btn.clicked.connect(lambda : te_search_func(self))
self.inscr_comboBox.currentTextChanged.connect(lambda : combobox_changed_func(self))
self.search_for_text_action.triggered.connect(lambda: search_by_content(self))
self.searchbytextbtn.clicked.connect(lambda: search_by_content(self))
self.show_image_btn.clicked.connect(lambda: show_img_btn_func(self))
#menu
self.save_file_action.triggered.connect(lambda : save_file_dialog(self))
self.show_img_action.triggered.connect(lambda: show_img_btn_func(self))
self.contact_us_action.triggered.connect(lambda: webbrowser.open('mailto:sai.app1290@gmail.com'))
##functions
def te_search_func(self):
searched_epigraph = self.search_le.text()
lower_epigraph_list = [i.lower() for i in epigraphs]
if searched_epigraph.lower() in lower_epigraph_list:
self.inscr_comboBox.setCurrentIndex(lower_epigraph_list.index(searched_epigraph.lower()))
result = inscriptions_df[inscriptions_df['EPIGRAPH'].str.lower() == searched_epigraph.lower()].iloc[0]
result_title = result['TITLES']
result_en_transliteration = result['TRANSLITERATION']
result_ar_transliteration = result['AR_TRANSLITERATION']
result_language = result['LANGUAGE']
result_ar_translation = result['ar_translation']
result_en_translation = result['TRANSLATION']
site = f"""Object name:
{result['TITLES']}
Modern site:
{result['Modern site']}
Ancient site:
{result['Ancient site']}
Geographical area:
{result['Geographical area']}
Country:
{result['Country']}
"""
if result['IMAGES']:
self.show_img_action.setDisabled(False)
self.show_image_btn.setDisabled(False)
else:
self.show_img_action.setDisabled(True)
self.show_image_btn.setDisabled(True)
self.sitelabel.setText(site)
self.title_inscr_label.setText(result_title)
self.ar_transliteration_text_browser.setText(result_ar_transliteration)
self.en_transliteration_text_browser.setText(result_en_transliteration)
self.language_label.setText(result_language)
if result_ar_translation:
self.ar_translation_text_browser.setVisible(True)
self.ar_translation_text_browser.setText(result_ar_translation)
else:
self.ar_translation_text_browser.setVisible(False)
if result_en_translation:
self.en_translation_text_browser.setVisible(True)
self.en_translation_text_browser.setText(result_en_translation)
else:
self.en_translation_text_browser.setVisible(False)
def combobox_changed_func(self):
searched_epigraph = self.inscr_comboBox.currentText()
if searched_epigraph in list(epigraphs):
result = inscriptions_df[inscriptions_df['EPIGRAPH'] == searched_epigraph].iloc[0]
result_title = result['TITLES']
result_en_transliteration = result['TRANSLITERATION']
result_ar_transliteration = result['AR_TRANSLITERATION']
result_language = result['LANGUAGE']
result_ar_translation = result['ar_translation']
result_en_translation = result['TRANSLATION']
site = f"""
Object name:
{result['TITLES']}
Modern site:
{result['Modern site']}
Ancient site:
{result['Ancient site']}
Geographical area:
{result['Geographical area']}
Country:
{result['Country']}
"""
if result['IMAGES']:
self.show_img_action.setDisabled(False)
self.show_image_btn.setDisabled(False)
else:
self.show_img_action.setDisabled(True)
self.show_image_btn.setDisabled(True)
self.sitelabel.setText(site)
self.title_inscr_label.setText(result_title)
self.ar_transliteration_text_browser.setText(result_ar_transliteration)
self.en_transliteration_text_browser.setText(result_en_transliteration)
self.language_label.setText(result_language)
if result_ar_translation:
self.ar_translation_text_browser.setVisible(True)
self.ar_translation_text_browser.setText(result_ar_translation)
else:
self.ar_translation_text_browser.setVisible(False)
if result_en_translation:
self.en_translation_text_browser.setVisible(True)
self.en_translation_text_browser.setText(result_en_translation)
else:
self.en_translation_text_browser.setVisible(False)
def save_file_dialog(self):
saved_inscription_title = self.title_inscr_label.text()
from pathlib import Path
downloads_path = str(Path.home() / "Desktop")
file , check = QtWidgets.QFileDialog.getSaveFileName(None, "save file", f"{downloads_path}/" + saved_inscription_title, "Word document (*.docx)")
if check:
saved_inscription = inscriptions_df[inscriptions_df['TITLES'] == saved_inscription_title].iloc[0]
doc = Document()
#header 1 title
doc.add_heading(saved_inscription_title, 0)
##img in docment
if saved_inscription['IMAGES']:
try:
data = urllib.request.urlopen(saved_inscription['IMAGES']).read()
inscr_img = doc.add_paragraph()
inscr_img.alignment = WD_ALIGN_PARAGRAPH.CENTER
r_inscr_img = inscr_img.add_run()
r_inscr_img.add_picture(BytesIO(data), width=Inches(4.5))
except:
pass
h1 = doc.add_heading("الترجمات العربية", 1)
h1.alignment = WD_ALIGN_PARAGRAPH.RIGHT
h2 = doc.add_heading("الترجمة الحرفية", 2)
h2.alignment = WD_ALIGN_PARAGRAPH.RIGHT
mystyle = doc.styles.add_style('mystyle', WD_STYLE_TYPE.CHARACTER)
ar_translit_paragarph = doc.add_paragraph()
ar_translit_paragarph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
r= ar_translit_paragarph.add_run(saved_inscription['AR_TRANSLITERATION'])
r.style = mystyle
font = r.font
font.rtl = True
if saved_inscription['ar_translation']:
h2_ar_translation = doc.add_heading("معنى النقش", 2)
h2_ar_translation.alignment = WD_ALIGN_PARAGRAPH.RIGHT
ar_translation_paragarph = doc.add_paragraph()
ar_translation_paragarph.alignment = WD_ALIGN_PARAGRAPH.RIGHT
r = ar_translation_paragarph.add_run(saved_inscription['ar_translation'])
r.style = mystyle
font = r.font
font.rtl = True
h1_en = doc.add_heading("الترجمات الاجنبية", 1)
h1_en.alignment = WD_ALIGN_PARAGRAPH.RIGHT
h2_en = doc.add_heading("الترجمة الحرفية", 2)
h2_en.alignment = WD_ALIGN_PARAGRAPH.RIGHT
doc.add_paragraph(saved_inscription["TRANSLITERATION"])
h2_en_translation = doc.add_heading("معنى النقش", 2)
h2_en_translation.alignment = WD_ALIGN_PARAGRAPH.RIGHT
if saved_inscription["TRANSLATION"]:
doc.add_paragraph(saved_inscription["TRANSLATION"])
doc.save(file)
def show_img_btn_func(self):
try: self.img_widget.save_img_btn.clicked.disconnect()
except: pass
try:
saved_inscription_title = self.title_inscr_label.text()
saved_inscription_img = inscriptions_df[inscriptions_df['TITLES'] == saved_inscription_title].iloc[0]['IMAGES']
data = urllib.request.urlopen(saved_inscription_img).read()
pixmap = QtGui.QPixmap()
pixmap.loadFromData(data)
self.img_widget.img_label.setBackgroundRole(QtGui.QPalette.Base)
self.img_widget.img_label.setPixmap(pixmap)
self.img_widget.save_img_btn.clicked.connect(lambda : save_img_func(self))
def save_img_func(self):
from pathlib import Path
downloads_path = str(Path.home() / "Downloads")
file , check = QtWidgets.QFileDialog.getSaveFileName(None, "save file", f"{downloads_path}/" + saved_inscription_title, "Image (*.jpg)")
if check:
urllib.request.urlretrieve(saved_inscription_img, file)
except:
self.img_widget.img_label.setText("لا يوجد اتصال بالشبكة")
self.img_widget.show()
def search_by_content(self):
self.search_by_content_widget.search_by_content_te.textChanged.connect(lambda: search_content_func(self))
self.search_by_content_widget.result_list.itemDoubleClicked.connect(lambda: view_result_from_list(self))
def view_result_from_list(self):
pass
def search_content_func(self):
def finditems(x, z):
if z in x and z.strip() != '':
return x
else:
return np.nan
searched_content = self.search_by_content_widget.search_by_content_te.text()
result_df = inscriptions_df.iloc[inscriptions_df['AR_TRANSLITERATION_pure'].apply(lambda x: finditems(x, searched_content)).dropna().index]
self.search_by_content_widget.result_list.clear()
self.search_by_content_widget.result_list.addItems(result_df['EPIGRAPH'].drop_duplicates())
def view_result_from_list(self):
selected_item = self.search_by_content_widget.result_list.currentItem().text()
self.inscr_comboBox.setCurrentIndex(list(epigraphs).index(selected_item))
result = inscriptions_df[inscriptions_df['EPIGRAPH'] == selected_item].iloc[0]
result_title = result['TITLES']
result_en_transliteration = result['TRANSLITERATION']
result_ar_transliteration = result['AR_TRANSLITERATION']
result_language = result['LANGUAGE']
result_ar_translation = result['ar_translation']
result_en_translation = result['TRANSLATION']
site = f"""
Object name:
{result['TITLES']}
Modern site:
{result['Modern site']}
Ancient site:
{result['Ancient site']}
Geographical area:
{result['Geographical area']}
Country:
{result['Country']}
"""
if result['IMAGES']:
self.show_img_action.setDisabled(False)
self.show_image_btn.setDisabled(False)
else:
self.show_img_action.setDisabled(True)
self.show_image_btn.setDisabled(True)
self.sitelabel.setText(site)
self.title_inscr_label.setText(result_title)
self.ar_transliteration_text_browser.setText(result_ar_transliteration)
self.en_transliteration_text_browser.setText(result_en_transliteration)
self.language_label.setText(result_language)
if result_ar_translation:
self.ar_translation_text_browser.setVisible(True)
self.ar_translation_text_browser.setText(result_ar_translation)
else:
self.ar_translation_text_browser.setVisible(False)
if result_en_translation:
self.en_translation_text_browser.setVisible(True)
self.en_translation_text_browser.setText(result_en_translation)
else:
self.en_translation_text_browser.setVisible(False)
self.search_by_content_widget.show()
self.show()
app = QtWidgets.QApplication(sys.argv)
window = mainWindow()
waiting_app.quit()
app.exec_()