-
Notifications
You must be signed in to change notification settings - Fork 0
/
droparea.py
286 lines (245 loc) · 11.2 KB
/
droparea.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
import threading
from PyQt5.QtCore import QObject, QSize, Qt, QThread, QUrl, pyqtSignal
from PyQt5.QtGui import QDesktopServices, QPalette, QPixmap
from PyQt5.QtWidgets import (QFrame, QGraphicsOpacityEffect, QLabel,
QPushButton, QSizePolicy, QWidget)
from addtabdialog import AddTabDialog
MIN_FOR_CLOSE = 100
class DropArea(QLabel):
# INITIALIZE SIGNALS
tabReordered = pyqtSignal(QWidget, int)
tabAdded = pyqtSignal(QWidget, int)
tabDeleted = pyqtSignal(QWidget)
imageLoaded = pyqtSignal()
def __init__(self, options, tabNumber, driver, minWidth, minHeight, aspectRatio, imageFolder, tabFileName, archiveTabFileName):
super().__init__()
# CONNECT EVENTS (MOUSE/SLOTS & SIGNALS)
self.mousePressEvent = self.areaClicked
self.mouseReleaseEvent = self.areaReleased
# INITIALIZE CLASS VARIABLES
self.taken = False
self.startClick = 0
self.endClick = 0
self.aspectRatio = aspectRatio
self.minWidth = minWidth
self.minHeight = minHeight
self.imageFolder = imageFolder
self.driver = driver
self.options = options
self.tabFileName = tabFileName
self.archiveTabFileName = archiveTabFileName
self.setMinimumSize(self.minWidth, self.minHeight)
self.setFrameStyle(QFrame.Plain)
self.setAlignment(Qt.AlignCenter)
self.setAcceptDrops(True)
self.setAutoFillBackground(True)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Maximum)
self.setCursor(Qt.PointingHandCursor)
self.setMouseTracking(True)
self.tabNumber = tabNumber
self.defaultTab('Drag URL Here')
# ON MOUSE ENTER
def enterEvent(self, event):
if self.taken:
self.setToolTip(self.url)
self.setToolTipDuration(1000)
self.button.setVisible(True)
else: self.setBackgroundRole(QPalette.Highlight)
# ON MOUSE LEAVE
def leaveEvent(self, event):
if not self.taken: self.setBackgroundRole(QPalette.Light)
else: self.button.setVisible(False)
def areaClicked(self, event):
self.startClick = event.pos()
if self.taken:
self.setFrameStyle(QFrame.Plain | QFrame.Box)
self.setLineWidth(5)
# IF DRAG UP REMOVE TAB, IF CLICK OPEN URL
def areaReleased(self, event):
self.endClick = event.pos()
xDiff = self.endClick.x() - self.startClick.x()
yDiff = abs(self.endClick.y() - self.startClick.y())
# IF X/Y DELTA GREATER THAN RESPECTIVE MINS REORDER/DELETE
if yDiff > MIN_FOR_CLOSE or abs(xDiff) > self.sizeHint().width():
# IF Y DELTA GREATER THAN MIN CLOSE/DELETE TAB
if yDiff > MIN_FOR_CLOSE:self.tabDeleted.emit(self)
# IF X DELTA GREATER THAN TAB WIDTH REORDER TAB
elif abs(xDiff) > self.sizeHint().width():
if self.taken:
index = (self.tabNumber-1) % 3
# GET TABS TO MOVE E.G. -1 (1 LEFT) OR 2 (2 RIGHT)
tabsToSwap = (int)(xDiff/self.sizeHint().width())
left = True if (xDiff < 0) else False
# IF NOT LEFT MOST TAB AND TRYING TO DRAG LEFT
if not (index == 0 and left):
#GET PARENT WIDGET AND MOVE TAB
parent = self.options.tabRows[int((self.tabNumber-1)/self.options.tabsPerRow)].layout()
parent.removeWidget(self)
parent.insertWidget(index + tabsToSwap, self)
# EMIT SIGNAL SO LIST CAN BE REORGANIZED AND CHAGNES SAVED TO FILE
self.tabReordered.emit(self, tabsToSwap)
# OPEN TAB IN BROWSER OR IF EMPTY ENTER URL MANUALLY
else:
if self.taken: QDesktopServices.openUrl(QUrl(self.url))
else: dialog = AddTabDialog(self)
# RESET FRAME WHEN NOT CLICKED
if self.taken:
self.setFrameStyle(QFrame.NoFrame)
self.setLineWidth(2)
# SET TEXT TO DROP AND CHANGE COLOR
def dragEnterEvent(self, event):
if not self.taken:
self.highlightTab()
event.acceptProposedAction()
def dragMoveEvent(self, event):
event.setDropAction(Qt.MoveAction)
def dragLeaveEvent(self, event):
self.defaultTab()
event.accept()
#IF NOT TAKEN GET IMAGE AND SAVE TAB
def dropEvent(self, event):
if not self.taken:
self.defaultTab('Loading...')
self.addCloseButton()
self.url = event.mimeData().text()
duplicateTab = self.checkDuplicateTab(self.url, self.tabFileName)
duplicateArchiveTab = self.checkDuplicateTab(self.url, self.archiveTabFileName)
if duplicateTab or duplicateArchiveTab:
if duplicateTab: self.setDuplicateTab(self.url)
elif duplicateArchiveTab: self.setDuplicateArchiveTab(self.url)
self.loadSave()
else:
t = threading.Thread(target=self.downloadImage, args=(self.url,True))
t.start()
self.taken = True
event.acceptProposedAction()
def loadSave(self):
self.setLocalPixmap()
self.saveTab()
self.tabAdded.emit(self, self.tabNumber)
# ON RESIZE UPDATE PIXMAP SIZE
def resizeEvent(self, e):
if self.pixmap() != None: self.setPixmap(self._pixmap.scaled(self.sizeHint(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
self.setFixedHeight(self.sizeHint().height())
# OVERLOAD SIZEHINT SO IT GETS SIZE OF PARENT LAYOUT CHILDREN / PARENT LAYOUT WIDTH
def sizeHint(self):
# WHEN CLEARING TABS SOMETIMES THE ROW IS NOT THERE WHEN RESIZE EVENT OCCURS
if self.parent() != None:
if self.parent().layout().itemAt(0).count() > 0:
hasAttr = hasattr(self.parent().layout().itemAt(0), "count")
if hasAttr:
count = self.parent().layout().itemAt(0).count()
width = self.parent().width() / count
if width >= self.minWidth:
return QSize(width, width*self.aspectRatio)
return QSize(self.minWidth, self.minHeight)
def addCloseButton(self):
self.button = QPushButton('x', self)
buttonOpacity = QGraphicsOpacityEffect()
buttonOpacity.setOpacity(0.45)
self.button.setGraphicsEffect(buttonOpacity)
self.button.move(0, 0)
self.button.show()
self.button.setVisible(False)
self.button.clicked.connect(lambda: self.tabDeleted.emit(self))
# LOAD TAB PIXMAP AND SET CLASS VARIABLES
def load(self, url, imagePath):
self.taken = True
self.url = url
setImagePath = False
if self.checkFileExists(self.archiveTabFileName):
with open(self.archiveTabFileName, 'r') as f:
for line in f:
currentUrl = line.split('\n')[0].split(' ')[0]
currentPath = line.split('\n')[0].split(' ')[1]
if currentUrl == url:
if not os.path.isfile(os.path.join(self.imageFolder, currentPath[1:])): os.rename(os.path.join(self.imageFolder, currentPath), os.path.join(self.imageFolder, currentPath[1:]))
self.imagePath = currentPath[1:]
setImagePath = True
if not setImagePath: self.imagePath = imagePath
self._pixmap = QPixmap(os.path.join(self.imageFolder, self.imagePath))
self.setPixmap(self._pixmap.scaled(self.sizeHint(), Qt.KeepAspectRatio))
self.tabAdded.emit(self, self.tabNumber)
self.addCloseButton()
# DOWNLOAD IMAGE FROM DRIVER
def downloadImage(self, url, callback=False):
self.imagePath = str(url).replace('/', '').replace(':', '') + '.png'
self.driver.get(url)
path = os.path.join(self.imageFolder, self.imagePath)
self.driver.save_screenshot(path)
self._pixmap = QPixmap(path, '1')
if callback:
self.loadSave()
# SET PIXMAP FROM _PIXMAP
def setLocalPixmap(self):
self.setPixmap(self._pixmap.scaled(self.sizeHint(), Qt.KeepAspectRatio))
self.options.loadedTabCount += 1
self.imageLoaded.emit()
# CHECK IF FILE EXISTS AND IF ITS NOT EMPTY
def checkFileExists(self, fileName):
exists = os.path.isfile(os.path.join(os.getcwd(), fileName))
notEmpty = False
if exists:
notEmpty = True if (os.path.getsize(os.path.join(os.getcwd(), fileName)) > 0) else False
return (exists and notEmpty)
# CHECK IF URL IS IN FILENAME
def checkDuplicateTab(self, url, fileName):
if self.checkFileExists(fileName):
with open(fileName, 'r') as f:
for line in f:
if url == line.split(' ')[0]: return True
return False
# CHECK THROUGH TABS FILE AND SET IMAGEPATH AND PIXMAP IF URL EXISTS
def setDuplicateTab(self, url):
with open(self.tabFileName, 'r') as f:
for line in f:
if url == line.split(' ')[0]:
imagePath = line.split(' ')[1]
self.imagePath = imagePath
self._pixmap = QPixmap(os.path.join(self.imageFolder, imagePath), '1')
# CHECK THROUGH ARCHIVED TABS FILE AND SET IMAGEPATH AND PIXMAP IF URL EXISTS
def setDuplicateArchiveTab(self, url):
with open(self.archiveTabFileName, 'r') as f:
for line in f:
if url == line.split(' ')[0]:
imagePath = line.split('\n')[0].split(' ')[1]
# RENAME IMAGE TO MAKE IT NOT HIDDEN
os.rename(os.path.join(self.imageFolder, imagePath), os.path.join(self.imageFolder, imagePath[1:]))
self.imagePath = imagePath[1:]
self._pixmap = QPixmap(os.path.join(self.imageFolder, self.imagePath), '1')
# SAVE TAB ENTRY TO FILE
def saveTab(self):
with open(self.tabFileName, 'a') as f:
info = self.url + ' ' + self.imagePath + ' ' + str(self.tabNumber) + '\n'
f.write(info)
def setTaken(self, url):
self.taken = True
self.url = url
def addedLoaded(self):
self.tabAdded.emit(self, self.tabNumber)
self.imageLoaded.emit()
### QUICK METHODS TO SET TAB STYLING
def defaultTab(self, text):
self.setText(text)
self.setBackgroundRole(QPalette.Light)
def highlightTab(self):
self.setBackgroundRole(QPalette.Mid)
self.setText('Drop Here')
class DownloadThread(QThread):
def __init__(self, tab, driver, callback, url, imageFolder):
QThread.__init__(self)
self.tab = tab
self.driver = driver
self.callback = callback
self.url = url
self.imageFolder = imageFolder
def __del__(self):
pass
def run(self):
self.tab.imagePath = str(self.url).replace('/', '') + '.png'
self.driver.get(self.url)
path = os.path.join(self.imageFolder, self.tab.imagePath)
self.driver.save_screenshot(path)
self.tab._pixmap = QPixmap(path, '1')
self.callback()