-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_editor_module.py
More file actions
140 lines (112 loc) · 4.16 KB
/
document_editor_module.py
File metadata and controls
140 lines (112 loc) · 4.16 KB
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
from PyQt5.QtCore import Qt, QTimer, pyqtSignal, QPoint
from PyQt5.QtGui import QColor, QDragEnterEvent, QDropEvent
from .mvc import (
DocumentEditorModel, DocumentEditorView, DocumentEditorController
)
from PyQt5.QtWidgets import (
QDialog, QVBoxLayout, QGraphicsDropShadowEffect, QApplication
)
from ui.custom_widgets.modal_window import ShadowContainer, ModalOverlay
from utils import NotificationService
class EditorWindow(QDialog):
document_saved = pyqtSignal()
document_deleted = pyqtSignal()
files_dropped = pyqtSignal(list)
drag_entered = pyqtSignal()
drag_left = pyqtSignal()
def __init__(
self,
parent=None,
mode: str = None,
category_id: int = None,
document_data: dict = None,
pages: list[dict] = None
) -> None:
super().__init__(parent)
self.overlay = None
# === Window flags ===
self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog)
self.setWindowModality(Qt.ApplicationModal)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAcceptDrops(True)
# === Container & Layout ===
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(20, 20, 20, 20)
main_layout.setAlignment(Qt.AlignCenter)
self.container = ShadowContainer(self)
self.container.setObjectName("editorContainer")
main_layout.addWidget(self.container)
# === Shadow ===
shadow = QGraphicsDropShadowEffect()
shadow.setBlurRadius(20)
shadow.setColor(QColor(0, 0, 0, int(255 * 0.10)))
shadow.setOffset(0, 5)
self.container.setGraphicsEffect(shadow)
# MVC Initialization
self.model = DocumentEditorModel(
category_id=category_id,
document_data=document_data,
pages=pages
)
self.view = DocumentEditorView(container=self.container)
self.controller = DocumentEditorController(
mode=mode,
model=self.model,
view=self.view,
window=self,
)
# Set initial geometry to prevent flickering
self.center_on_screen()
def keyPressEvent(self, event):
"""Prevents the window from closing when the Enter key is pressed."""
if event.key() in (Qt.Key_Return, Qt.Key_Enter):
return
super().keyPressEvent(event)
# Center modal on screen
def showEvent(self, event):
super().showEvent(event)
self.create_overlay()
self.center_on_screen()
def closeEvent(self, event):
if self.overlay:
self.overlay.close()
if self.parent():
NotificationService().set_main_window(self.parent())
super().closeEvent(event)
def create_overlay(self):
if self.parent():
# Try to find the top level window to cover
parent_window = self.parent().window()
self.overlay = ModalOverlay(parent_window)
self.overlay.resize(parent_window.size())
self.overlay.show()
self.overlay.raise_()
def center_on_screen(self):
if self.parent():
parent = self.parent().window()
self.resize(parent.size())
self.move(parent.mapToGlobal(QPoint(0, 0)))
else:
self.adjustSize()
screen = QApplication.primaryScreen().availableGeometry()
x = screen.center().x() - self.width() // 2
y = screen.center().y() - self.height() // 2
self.move(x, y)
def dragEnterEvent(self, event: QDragEnterEvent):
if event.mimeData().hasUrls():
event.accept()
self.drag_entered.emit()
else:
event.ignore()
def dragLeaveEvent(self, event):
self.drag_left.emit()
super().dragLeaveEvent(event)
def dropEvent(self, event: QDropEvent):
self.drag_left.emit()
files = []
for url in event.mimeData().urls():
file_path = url.toLocalFile()
if file_path:
files.append(file_path)
if files:
self.files_dropped.emit(files)