Skip to content

Commit 17e2b79

Browse files
Created a Globals class to manage shared objects. Additionally, tray_icon and EffectManager were added to it to optimize memory usage.
1 parent a1c65f8 commit 17e2b79

File tree

7 files changed

+63
-35
lines changed

7 files changed

+63
-35
lines changed

core_functions/athkar/athkar_scheduler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import json
3-
import utils.const as const
43
from datetime import datetime, time
54
from pathlib import Path
65
from typing import Dict, Optional, Union
@@ -11,6 +10,7 @@
1110
from .athkar_db_manager import AthkarDBManager
1211
from .athkar_refresher import AthkarRefresher
1312
from utils.audio_player import AthkarPlayer
13+
from utils.const import Globals
1414

1515
class AthkarScheduler:
1616
def __init__(self, athkar_db_folder: Union[Path, str], default_category_path: Optional[Union[Path, str]] = None, text_athkar_path: Optional[Union[Path, str]] = None, default_category_settings: Optional[Dict[str, int]] = None) -> None:
@@ -55,7 +55,7 @@ def text_athkar_job(self, category_id: int) -> None:
5555
title = "البيان"
5656
description = text
5757

58-
const.tray_icon.showMessage(title, description, msecs=5000)
58+
Globals.TRAY_ICON.showMessage(title, description, msecs=5000)
5959

6060
@staticmethod
6161
def _parse_time(time_str: str) -> time:

ui/dialogs/info_dialog.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from PyQt6.QtGui import QKeySequence, QClipboard
55
from ui.widgets.qText_edit import ReadOnlyTextEdit
66
from utils.universal_speech import UniversalSpeech
7-
from utils.audio_player import SoundEffectPlayer
8-
7+
from utils.const import Globals
98

109
class InfoDialog(QDialog):
1110
def __init__(self, title: str, label: str, text: str, is_html_content: bool = False):
@@ -15,8 +14,7 @@ def __init__(self, title: str, label: str, text: str, is_html_content: bool = Fa
1514
self.text = text
1615
self.is_html_content = is_html_content
1716
self.init_ui()
18-
self.effects_manager = SoundEffectPlayer("Audio/sounds")
19-
self.effects_manager.play("open")
17+
Globals.effects_manager.play("open")
2018

2119

2220
def init_ui(self):
@@ -61,9 +59,9 @@ def copy_text(self):
6159
clipboard = QApplication.clipboard()
6260
clipboard.setText(self.text_edit.toPlainText())
6361
UniversalSpeech.say("تم نسخ النص إلى الحافظة")
64-
self.effects_manager.play("copy")
62+
Globals.effects_manager.play("copy")
6563

6664
def reject(self):
67-
self.effects_manager.play("clos")
65+
Globals.effects_manager.play("clos")
6866
super(). reject()
6967

ui/dialogs/tafaseer_Dialog.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
from ui.widgets.qText_edit import ReadOnlyTextEdit
1616
from core_functions.tafaseer import TafaseerManager, Category
1717
from utils.universal_speech import UniversalSpeech
18-
from utils.const import albayan_documents_dir
19-
from utils.audio_player import SoundEffectPlayer
18+
from utils.const import albayan_documents_dir, Globals
2019

2120

2221
class TafaseerDialog(QDialog):
@@ -29,9 +28,7 @@ def __init__(self, parent, title, ayah_info, default_category):
2928
self.resize(500, 400)
3029
self.tafaseer_manager = TafaseerManager()
3130
self.tafaseer_manager.set(Category.get_category_by_arabic_name(self.default_category))
32-
self.effects_manager = SoundEffectPlayer("Audio/sounds")
33-
self.effects_manager.play("open")
34-
31+
Globals.effects_manager.play("open")
3532

3633
self.layout = QVBoxLayout(self)
3734

@@ -93,14 +90,14 @@ def handle_category_selection(self):
9390
self.tafaseer_manager.set(Category.get_category_by_arabic_name(selected_category))
9491
self.text_edit.setText(self.tafaseer_manager.get_tafaseer(self.ayah_info[0], self.ayah_info[1]))
9592
self.text_edit.setFocus()
96-
self.effects_manager.play("change")
93+
Globals.effects_manager.play("change")
9794

9895
def copy_content(self):
9996
copied_content = self.text_edit.toPlainText()
10097
clipboard = QApplication.clipboard()
10198
clipboard.setText(copied_content)
10299
UniversalSpeech.say("تم نسخ التفسير.")
103-
self.effects_manager.play("copy")
100+
Globals.effects_manager.play("copy")
104101
def save_content(self):
105102

106103
file_name = os.path.join(albayan_documents_dir, self.windowTitle())
@@ -114,5 +111,5 @@ def save_content(self):
114111
file.write(self.text_edit.toPlainText())
115112

116113
def reject(self):
117-
self.effects_manager.play("clos")
114+
Globals.effects_manager.play("clos")
118115
super(). reject()

ui/quran_interface.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@
3333
from ui.widgets.system_tray import SystemTrayManager
3434
from ui.widgets.toolbar import AudioToolBar
3535
from utils.settings import SettingsManager
36-
from utils.audio_player import SoundEffectPlayer
3736
from utils.universal_speech import UniversalSpeech
3837
from utils.user_data import UserDataManager
39-
from utils.const import program_name, program_icon, user_db_path, data_folder
40-
import utils.const as const
41-
from utils.audio_player import bass
38+
from utils.const import program_name, program_icon, user_db_path, data_folder, Globals
39+
from utils.audio_player import bass, SoundEffectPlayer
4240

4341

4442
class QuranInterface(QMainWindow):
@@ -49,8 +47,8 @@ def __init__(self, title):
4947
self.quran = quran_mgr()
5048
self.quran.load_quran(os.path.join("database", "quran", "quran.DB"))
5149
self.quran.aya_to_line = True
52-
self.effects_manager = SoundEffectPlayer("Audio/sounds")
5350
self.user_data_manager = UserDataManager(user_db_path)
51+
Globals.effects_manager = SoundEffectPlayer("Audio/sounds")
5452

5553
self.toolbar = AudioToolBar(self)
5654
self.menu_bar = MenuBar(self)
@@ -147,14 +145,14 @@ def set_focus_to_ayah(self, ayah_number: int):
147145
def OnNext(self):
148146
self.quran_view.setText(self.quran.next())
149147
self.set_text_ctrl_label()
150-
self.effects_manager.play("next")
148+
Globals.effects_manager.play("next")
151149
if self.quran.current_pos == self.quran.max_pos:
152150
self.quran_view.setFocus()
153151

154152
def OnBack(self):
155153
self.quran_view.setText(self.quran.back())
156154
self.set_text_ctrl_label()
157-
self.effects_manager.play("previous")
155+
Globals.effects_manager.play("previous")
158156
if self.quran.current_pos == 1:
159157
self.quran_view.setFocus()
160158

@@ -286,7 +284,7 @@ def on_copy_verse(self):
286284
clipboard = QApplication.clipboard()
287285
clipboard.setText(current_line)
288286
UniversalSpeech.say("تم نسخ الآية.")
289-
self.effects_manager.play("copy")
287+
Globals.effects_manager.play("copy")
290288

291289

292290
def OnSyntax(self):
@@ -345,22 +343,19 @@ def OnChangeNavigationMode(self, mode):
345343
self.quran_view.setText(self.quran.get_by_ayah_number(ayah_number)["full_text"])
346344
self.set_focus_to_ayah(ayah_number)
347345
self.set_text_ctrl_label()
348-
self.effects_manager.play("change")
346+
Globals.effects_manager.play("change")
349347

350348
def closeEvent(self, event):
351349
if SettingsManager.current_settings["general"]["run_in_background_enabled"]:
352350
event.ignore()
353351
self.hide()
354-
const.tray_icon.showMessage("البيان", "تم تصغير نافذة البيان على صينية النظام, البرنامج يعمل في الخلفية.", msecs=5000)
352+
self.tray_manager.tray_icon.showMessage("البيان", "تم تصغير نافذة البيان على صينية النظام, البرنامج يعمل في الخلفية.", msecs=5000)
355353
else:
356354
self.tray_manager.hide_icon()
357355
bass.BASS_Free()
358356

359357
def OnRandomMessages(self):
360-
# قراءة البيانات من ملف QuotesMessages.json
361358
with open(data_folder/"quotes/QuotesMessages.json", "r", encoding="utf-8") as file:
362-
quotes_list = json.load(file) # قراءة العناصر من الملف
363-
# اختيار عنصر عشوائي
359+
quotes_list = json.load(file)
364360
message = random.choice(quotes_list)
365361
InfoDialog('اقتباس عشوائي', 'رسالة لك', message, is_html_content=False).exec()
366-

ui/widgets/system_tray.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import utils.const as const
21
from PyQt6.QtWidgets import QSystemTrayIcon, QMenu
32
from PyQt6.QtGui import QIcon, QAction
4-
3+
from utils.const import Globals
54

65
class SystemTrayManager:
76
def __init__(self, main_window, program_name: str, program_icon: str):
87
self.tray_icon = QSystemTrayIcon(QIcon(program_icon), main_window)
98
self.tray_icon.setToolTip(program_name)
109
self.tray_icon.activated.connect(self.on_tray_icon_click)
1110
self.main_window = main_window
12-
const.tray_icon = self.tray_icon
11+
Globals.TRAY_ICON = self.tray_icon
1312

1413
tray_menu = QMenu()
1514
show_action = QAction("إظهار النافذة الرئيسية", main_window)

utils/const.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
from pathlib import Path
3-
from typing import Optional
43
from PyQt6.QtWidgets import QSystemTrayIcon
54

65
tecwindow_folder = os.path.join(os.getenv("AppData"), "tecwindow")
@@ -27,5 +26,10 @@
2726
program_english_name = "Albayan"
2827
program_version = "1.3.0"
2928
program_icon = "icon.webp"
30-
tray_icon: Optional[QSystemTrayIcon] = None
3129
website = "https://tecwindow.net/"
30+
31+
32+
class Globals:
33+
"""Class for managing global shared objects."""
34+
TRAY_ICON = None
35+
effects_manager = None
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pathlib import Path
2+
import os
3+
4+
class Paths:
5+
"""Class for managing application paths."""
6+
TECWINDOW_FOLDER = Path(os.getenv("AppData")) / "tecwindow"
7+
ALBAYAN_FOLDER = TECWINDOW_FOLDER / "albayan"
8+
USER_DB_PATH = ALBAYAN_FOLDER / "user_data.db"
9+
ATHKAR_DB_PATH = ALBAYAN_FOLDER / "athkar.db"
10+
DEFAULT_ATHKAR_PATH = ALBAYAN_FOLDER / "audio" / "athkar"
11+
TEST_ATHKAR_PATH = Path("audio/athkar")
12+
DATA_FOLDER = Path("database")
13+
14+
TEMP_FOLDER = Path(os.getenv("TEMP")) / "albayan"
15+
DOCUMENTS_DIR = Path(os.path.expanduser("~")) / "Documents" / "Albayan"
16+
17+
@staticmethod
18+
def initialize_directories():
19+
"""Ensure necessary directories exist."""
20+
Paths.ALBAYAN_FOLDER.mkdir(parents=True, exist_ok=True)
21+
Paths.TEMP_FOLDER.mkdir(parents=True, exist_ok=True)
22+
Paths.DOCUMENTS_DIR.mkdir(parents=True, exist_ok=True)
23+
24+
class ProgramInfo:
25+
"""Class for managing program metadata."""
26+
NAME = "البيان"
27+
ENGLISH_NAME = "Albayan"
28+
VERSION = "1.3.0"
29+
ICON = "icon.webp"
30+
WEBSITE = "https://tecwindow.net/"
31+
32+
class Globals:
33+
"""Class for managing global shared objects."""
34+
TRAY_ICON = None
35+
effects_manager = None

0 commit comments

Comments
 (0)