Skip to content

Commit

Permalink
Remove duplication in the method of displaying a random message.
Browse files Browse the repository at this point in the history
  • Loading branch information
MahmoudAtef999 committed Dec 10, 2024
1 parent 8eecc92 commit e6e7180
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
33 changes: 22 additions & 11 deletions ui/dialogs/info_dialog.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import sys
import json
import random
from PyQt6.QtWidgets import QDialog, QVBoxLayout, QLabel, QTextEdit, QPushButton, QApplication
from PyQt6.QtWidgets import QDialog, QVBoxLayout, QLabel, QTextEdit, QPushButton, QApplication, QMessageBox
from PyQt6.QtCore import QTimer
from PyQt6.QtGui import QKeySequence, QClipboard
from ui.widgets.qText_edit import ReadOnlyTextEdit
from utils.universal_speech import UniversalSpeech
from utils.const import Globals, data_folder
from exceptions.json import JSONFileNotFoundError, InvalidJSONFormatError
from exceptions.error_decorators import exception_handler

class InfoDialog(QDialog):
def __init__(self, parent, title: str, label: str, text: str, is_html_content: bool = False, show_message_button: bool = False):
Expand Down Expand Up @@ -44,7 +46,7 @@ def init_ui(self):

# Message to you button (conditionally added)
message_to_you_button = QPushButton('رسالة لك', self)
message_to_you_button.clicked.connect(self.message_to_you)
message_to_you_button.clicked.connect(self.OnNewMessage)
message_to_you_button.setShortcut(QKeySequence("Ctrl+M"))
message_to_you_button.setStyleSheet('background-color: red; color: white;')
message_to_you_button.setVisible(self.show_message_button)
Expand Down Expand Up @@ -82,14 +84,23 @@ def copy_text(self):
def reject(self):
Globals.effects_manager.play("clos")
self.deleteLater()

def choose_QuotesMessage(self):
file_path = data_folder/"quotes/QuotesMessages.json"
if not file_path.exists():
raise JSONFileNotFoundError(file_path)

try:
with open(file_path, "r", encoding="utf-8") as file:
quotes_list = json.load(file)
message = random.choice(quotes_list)
except json.JSONDecodeError as e:
raise InvalidJSONFormatError(file_path, e)

self.text_edit.setText(message)
UniversalSpeech.say(message)

def message_to_you(self):
# Load the quotes and choose a random message
with open(data_folder/"quotes/QuotesMessages.json", "r", encoding="utf-8") as file:
quotes_list = json.load(file)
new_message = random.choice(quotes_list)

# Update the text without opening a new dialog
self.text_edit.setText(new_message)
UniversalSpeech.say(new_message)
def OnNewMessage(self):
self.choose_QuotesMessage()
Globals.effects_manager.play("message")

18 changes: 4 additions & 14 deletions ui/quran_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from utils.const import program_name, program_icon, user_db_path, data_folder, Globals
from utils.audio_player import bass, SoundEffectPlayer
from exceptions.error_decorators import exception_handler
from exceptions.json import JSONFileNotFoundError, InvalidJSONFormatError


class QuranInterface(QMainWindow):
Expand Down Expand Up @@ -364,17 +363,8 @@ def closeEvent(self, event):
self.tray_manager.hide_icon()
bass.BASS_Free()

@exception_handler(ui_element=QMessageBox )
@exception_handler(ui_element=QMessageBox)
def OnRandomMessages(self, event):
file_path = data_folder/"quotes/QuotesMessages.json"
if not file_path.exists():
raise JSONFileNotFoundError(file_path)

try:
with open(file_path, "r", encoding="utf-8") as file:
quotes_list = json.load(file)
message = random.choice(quotes_list)
except json.JSONDecodeError as e:
raise InvalidJSONFormatError(file_path, e)

InfoDialog(self, 'رسالة لك', '', message, is_html_content=False, show_message_button=True).exec()
info_dialog = InfoDialog(self, 'رسالة لك', '', "", is_html_content=False, show_message_button=True)
info_dialog.choose_QuotesMessage()
info_dialog.exec()

0 comments on commit e6e7180

Please sign in to comment.