Coverage for pyPhotoAlbum/mixins/dialog_mixin.py: 100%

18 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-20 12:55 +0000

1""" 

2Dialog operations mixin for pyPhotoAlbum 

3 

4Provides common functionality for creating and managing dialogs. 

5""" 

6 

7from typing import Optional, Any, Callable 

8from PyQt6.QtWidgets import QDialog 

9 

10 

11class DialogMixin: 

12 """ 

13 Mixin providing dialog creation and management capabilities. 

14 

15 This mixin separates dialog UI concerns from business logic, 

16 making it easier to create, test, and maintain complex dialogs. 

17 """ 

18 

19 def create_dialog(self, dialog_class: type, title: Optional[str] = None, **kwargs) -> Optional[Any]: 

20 """ 

21 Create and show a dialog, handling the result. 

22 

23 Args: 

24 dialog_class: Dialog class to instantiate 

25 title: Optional title override 

26 **kwargs: Additional arguments passed to dialog constructor 

27 

28 Returns: 

29 Dialog result if accepted, None if rejected 

30 """ 

31 # Create dialog instance 

32 dialog = dialog_class(parent=self, **kwargs) 

33 

34 # Set title if provided 

35 if title: 

36 dialog.setWindowTitle(title) 

37 

38 # Show dialog and handle result 

39 if dialog.exec() == QDialog.DialogCode.Accepted: 

40 # Check if dialog has a get_values method 

41 if hasattr(dialog, "get_values"): 

42 return dialog.get_values() 

43 return True 

44 

45 return None 

46 

47 def show_dialog(self, dialog_class: type, on_accept: Optional[Callable] = None, **kwargs) -> bool: 

48 """ 

49 Show a dialog and execute callback on acceptance. 

50 

51 Args: 

52 dialog_class: Dialog class to instantiate 

53 on_accept: Callback to execute if dialog is accepted. 

54 Will receive dialog result as parameter. 

55 **kwargs: Additional arguments passed to dialog constructor 

56 

57 Returns: 

58 True if dialog was accepted, False otherwise 

59 """ 

60 result = self.create_dialog(dialog_class, **kwargs) 

61 

62 if result is not None and on_accept: 

63 on_accept(result) 

64 return True 

65 

66 return result is not None