-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragAndDropWidgets.py
233 lines (195 loc) · 9.27 KB
/
DragAndDropWidgets.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
import PyQt5.QtWidgets
import mimetypes
import os
import time
from ML.MLManagers import MLManager_MonoValued, MLManager_MultiValued, CFG
from Animations import QRoundProgressWidget
from history_messages import HistoryMessageLeftLabel
from VideoPlayer import VideoWidget
import sys
if sys.platform == 'win32':
current_directory = ''.join([x + '/' for x in os.path.realpath(__file__).split('\\')[:-1]])[:-1]
else:
current_directory = ''.join([x + '/' for x in os.path.realpath(__file__).split('/')[:-1]])[:-1]
class DragAndDropWidget_single(PyQt5.QtWidgets.QLabel):
def __init__(self, parent, shadow = False):
super().__init__()
self.setAcceptDrops(True)
self.parent = parent
self.MLManager = MLManager_MonoValued(parent = self)
self.MLManager.parent = self
self.MLManager.finished.connect(self.send_history_message)
self.prediction = str()
if shadow:
# set shadow to the label
shadow = PyQt5.QtWidgets.QGraphicsDropShadowEffect()
shadow.setBlurRadius(5)
self.setGraphicsEffect(shadow)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def mousePressEvent(self, event):
path = self.parent.fileManager.open_file()[0]
if path and path is not None:
self.startAnimation()
self.display_prediction(path)
def is_video(self, path):
try:
if mimetypes.guess_type(path)[0].startswith('video'):
return path
else:
PyQt5.QtWidgets.QMessageBox.warning(None,
"path does not consist the video",
f"{os.path.split(path)[1]} is not a video")
except:
PyQt5.QtWidgets.QMessageBox.warning(None,
"path does not consist the video",
f"{os.path.split(path)[1]} is not a video")
def dropEvent(self, event):
files = [u.toLocalFile() for u in event.mimeData().urls()]
for file in files:
path = self.is_video(file)
if path and path is not None:
self.startAnimation()
self.display_prediction(path)
@PyQt5.QtCore.pyqtSlot(str)
def MonoValuePrediction(self, prediction):
if not PyQt5.sip.isdeleted(self.parent.right_label):
self.startAnimation()
self.progress_bar.close()
self.parent.right_label.setText(prediction)
self.parent.right_layout.replaceWidget(self.progress_bar, self.parent.right_label)
self.prediction = prediction
def display_prediction(self, path):
self.create_pbar()
self.MLManager.PATH = path
self.MLManager.parent = self
self.MLManager.switch_to_MonoValue()
# Start Animation
def startAnimation(self):
self.parent.right_layout.setCurrentIndex(0)
if self.parent.right_layout.count() == 2:
self.parent.right_layout.itemAt(1).widget().deleteLater()
self.setPixmap(PyQt5.QtGui.QPixmap(f"{current_directory}/icons/checkmark.png"))
self.parent.DropLabel_single.setText('Ждите результата...')
# Stop Animation(According to need)
def stopAnimation(self):
text = 'перетащите файл \n или выберите для перевода'
self.setPixmap(PyQt5.QtGui.QPixmap(f"{current_directory}/icons/drop_file.ico"))
self.parent.DropLabel_single.setText(text)
def create_pbar(self):
self.progress_bar = QRoundProgressWidget()
self.progress_bar.setObjectName('RightLabel')
self.progress_bar.bar.setRange(0, CFG.NUM_FRAMES)
self.progress_bar.bar.setValue(0)
self.parent.right_label.setText('')
self.parent.right_layout.replaceWidget(self.parent.right_label, self.progress_bar)
def send_history_message(self):
message = self.prediction
filename = self.MLManager.PATH
left_label = HistoryMessageLeftLabel(parent = self, filename = filename)
left_label.setText(os.path.split(filename)[1])
left_label.clicked.connect(left_label.openPlayer)
left_label.setObjectName('LeftHistoryMessage')
left_label.setAlignment(PyQt5.QtCore.Qt.AlignTop | PyQt5.QtCore.Qt.AlignHCenter)
self.parent.left_history_layout.addWidget(left_label)
right_label = PyQt5.QtWidgets.QLabel(message, objectName = 'RightHistoryMessage')
right_label.setAlignment(PyQt5.QtCore.Qt.AlignTop | PyQt5.QtCore.Qt.AlignLeft)
self.parent.right_history_layout.addWidget(right_label)
self.stopAnimation()
class DragAndDropWidget_Multiple(PyQt5.QtWidgets.QLabel):
def __init__(self, parent, shadow = False):
super().__init__()
self.setAcceptDrops(True)
self.parent = parent
self.MLManager = MLManager_MultiValued(parent = self)
self.MLManager.parent = self
self.MLManager.finished.connect(self.send_history_message)
self.prediction = str()
if shadow:
# set shadow to the label
shadow = PyQt5.QtWidgets.QGraphicsDropShadowEffect()
shadow.setBlurRadius(5)
self.setGraphicsEffect(shadow)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def mousePressEvent(self, event):
path = self.parent.fileManager.open_file()[0]
if path and path is not None:
self.startAnimation()
self.display_prediction(path)
# self.send_history_message(filename=self.MLManager.PATH, message=self.prediction)
def is_video(self, path):
try:
if mimetypes.guess_type(path)[0].startswith('video'):
return path
else:
PyQt5.QtWidgets.QMessageBox.warning(None,
"в данном пути не содержится видео",
f"{os.path.split(path)[1]} is not a video")
except:
PyQt5.QtWidgets.QMessageBox.warning(None,
"в данном пути не содержится видео",
f"{os.path.split(path)[1]} is not a video")
def dropEvent(self, event):
files = [u.toLocalFile() for u in event.mimeData().urls()]
for file in files:
path = self.is_video(file)
if path and path is not None:
self.startAnimation()
self.display_prediction(path)
@PyQt5.QtCore.pyqtSlot(list)
def MonoValuePrediction(self, prediction):
if not PyQt5.sip.isdeleted(self.parent.right_label):
self.prediction, self.time_series = prediction
self.progress_bar.close()
self.parent.right_label.setText(self.prediction)
self.parent.right_layout.replaceWidget(self.progress_bar, self.parent.right_label)
def display_prediction(self, path):
self.create_pbar()
self.MLManager.PATH = path
self.MLManager.parent = self
self.MLManager.switch_to_MonoValue()
# Start Animation
def startAnimation(self):
self.parent.right_layout.setCurrentIndex(0)
self.parent.DropLabel.setText('Ждите результата...')
self.parent.DropIcon.setPixmap(PyQt5.QtGui.QPixmap(f"{current_directory}/icons/checkmark.png"))
try:
self.player.deleteLater()
except AttributeError:
pass
# Stop Animation(According to need)
def stopAnimation(self):
text = 'перетащите файл \n или выберите для перевода'
self.parent.DropIcon.setPixmap(PyQt5.QtGui.QPixmap(f"{current_directory}/icons/drop_file.ico"))
self.parent.DropLabel.setText(text)
def create_pbar(self):
self.progress_bar = QRoundProgressWidget()
self.progress_bar.setObjectName('RightLabel')
self.progress_bar.bar.setValue(0)
self.parent.right_label.setText('')
self.parent.right_layout.replaceWidget(self.parent.right_label, self.progress_bar)
def add_video(self):
self.player = VideoWidget(filename = self.MLManager.PATH, time_series=self.time_series)
self.parent.right_layout.addWidget(self.player)
self.parent.right_layout.setCurrentIndex(1)
def send_history_message(self):
message = self.prediction
filename = self.MLManager.PATH
self.stopAnimation()
self.add_video()
left_label = HistoryMessageLeftLabel(parent = self, filename = filename)
left_label.setText(os.path.split(filename)[1])
left_label.clicked.connect(left_label.openPlayer)
left_label.setObjectName('LeftHistoryMessage')
left_label.setAlignment(PyQt5.QtCore.Qt.AlignTop | PyQt5.QtCore.Qt.AlignHCenter)
self.parent.left_history_layout.addWidget(left_label)
right_label = PyQt5.QtWidgets.QLabel(message, objectName = 'RightHistoryMessage')
right_label.setAlignment(PyQt5.QtCore.Qt.AlignTop | PyQt5.QtCore.Qt.AlignLeft)
self.parent.right_history_layout.addWidget(right_label)