eric7/WebBrowser/Download/DownloadItem.py

branch
eric7
changeset 8563
3c6547443fb2
parent 8556
766e1566cb74
child 8856
df77fbfc150f
equal deleted inserted replaced
8562:9250a6d5fde2 8563:3c6547443fb2
5 5
6 """ 6 """
7 Module implementing a widget controlling a download. 7 Module implementing a widget controlling a download.
8 """ 8 """
9 9
10 import enum
10 import os 11 import os
11 12
12 from PyQt6.QtCore import ( 13 from PyQt6.QtCore import (
13 pyqtSlot, pyqtSignal, Qt, QTime, QUrl, QStandardPaths, QFileInfo, QDateTime 14 pyqtSlot, pyqtSignal, Qt, QTime, QUrl, QStandardPaths, QFileInfo, QDateTime
14 ) 15 )
23 from .DownloadUtilities import timeString, dataString, speedString 24 from .DownloadUtilities import timeString, dataString, speedString
24 from WebBrowser.WebBrowserWindow import WebBrowserWindow 25 from WebBrowser.WebBrowserWindow import WebBrowserWindow
25 26
26 import UI.PixmapCache 27 import UI.PixmapCache
27 import Utilities.MimeTypes 28 import Utilities.MimeTypes
29
30
31 class DownloadState(enum.Enum):
32 """
33 Class implementing the various download states.
34 """
35 Downloading = 0
36 Successful = 1
37 Cancelled = 2
28 38
29 39
30 class DownloadItem(QWidget, Ui_DownloadItem): 40 class DownloadItem(QWidget, Ui_DownloadItem):
31 """ 41 """
32 Class implementing a widget controlling a download. 42 Class implementing a widget controlling a download.
37 """ 47 """
38 statusChanged = pyqtSignal() 48 statusChanged = pyqtSignal()
39 downloadFinished = pyqtSignal(bool) 49 downloadFinished = pyqtSignal(bool)
40 progress = pyqtSignal(int, int) 50 progress = pyqtSignal(int, int)
41 51
42 # TODO: convert this to an enum
43 Downloading = 0
44 DownloadSuccessful = 1
45 DownloadCancelled = 2
46
47 def __init__(self, downloadRequest=None, pageUrl=None, parent=None): 52 def __init__(self, downloadRequest=None, pageUrl=None, parent=None):
48 """ 53 """
49 Constructor 54 Constructor
50 55
51 @param downloadRequest reference to the download object containing the 56 @param downloadRequest reference to the download object containing the
69 self.stopButton.setIcon(UI.PixmapCache.getIcon("stopLoading")) 74 self.stopButton.setIcon(UI.PixmapCache.getIcon("stopLoading"))
70 self.openButton.setIcon(UI.PixmapCache.getIcon("open")) 75 self.openButton.setIcon(UI.PixmapCache.getIcon("open"))
71 self.openButton.setEnabled(False) 76 self.openButton.setEnabled(False)
72 self.openButton.setVisible(False) 77 self.openButton.setVisible(False)
73 78
74 self.__state = DownloadItem.Downloading 79 self.__state = DownloadState.Downloading
75 80
76 icon = self.style().standardIcon(QStyle.StandardPixmap.SP_FileIcon) 81 icon = self.style().standardIcon(QStyle.StandardPixmap.SP_FileIcon)
77 self.fileIcon.setPixmap(icon.pixmap(48, 48)) 82 self.fileIcon.setPixmap(icon.pixmap(48, 48))
78 83
79 self.__downloadRequest = downloadRequest 84 self.__downloadRequest = downloadRequest
108 # start timer for the download estimation 113 # start timer for the download estimation
109 self.__downloadTime = QTime.currentTime() 114 self.__downloadTime = QTime.currentTime()
110 115
111 # attach to the download item object 116 # attach to the download item object
112 self.__url = self.__downloadRequest.url() 117 self.__url = self.__downloadRequest.url()
113 self.__downloadRequest.receivedBytesChanged.connect(self.__downloadProgress) 118 self.__downloadRequest.receivedBytesChanged.connect(
119 self.__downloadProgress)
114 self.__downloadRequest.isFinishedChanged.connect(self.__finished) 120 self.__downloadRequest.isFinishedChanged.connect(self.__finished)
115 121
116 # reset info 122 # reset info
117 self.datetimeLabel.clear() 123 self.datetimeLabel.clear()
118 self.datetimeLabel.hide() 124 self.datetimeLabel.hide()
305 self.openButton.setEnabled(False) 311 self.openButton.setEnabled(False)
306 self.openButton.setVisible(False) 312 self.openButton.setVisible(False)
307 self.pauseButton.setEnabled(False) 313 self.pauseButton.setEnabled(False)
308 self.pauseButton.setVisible(False) 314 self.pauseButton.setVisible(False)
309 self.setUpdatesEnabled(True) 315 self.setUpdatesEnabled(True)
310 self.__state = DownloadItem.DownloadCancelled 316 self.__state = DownloadState.Cancelled
311 self.__downloadRequest.cancel() 317 self.__downloadRequest.cancel()
312 self.__setDateTime() 318 self.__setDateTime()
313 self.downloadFinished.emit(False) 319 self.downloadFinished.emit(False)
314 320
315 @pyqtSlot() 321 @pyqtSlot()
461 """ 467 """
462 Public method to determine, if a download is in progress. 468 Public method to determine, if a download is in progress.
463 469
464 @return flag indicating a download is in progress (boolean) 470 @return flag indicating a download is in progress (boolean)
465 """ 471 """
466 return self.__state == DownloadItem.Downloading 472 return self.__state == DownloadState.Downloading
467 473
468 def downloadedSuccessfully(self): 474 def downloadedSuccessfully(self):
469 """ 475 """
470 Public method to check for a successful download. 476 Public method to check for a successful download.
471 477
472 @return flag indicating a successful download (boolean) 478 @return flag indicating a successful download (boolean)
473 """ 479 """
474 return self.__state == DownloadItem.DownloadSuccessful 480 return self.__state == DownloadState.Successful
475 481
476 def downloadCanceled(self): 482 def downloadCanceled(self):
477 """ 483 """
478 Public method to check, if the download was cancelled. 484 Public method to check, if the download was cancelled.
479 485
480 @return flag indicating a canceled download (boolean) 486 @return flag indicating a canceled download (boolean)
481 """ 487 """
482 return self.__state == DownloadItem.DownloadCancelled 488 return self.__state == DownloadState.Cancelled
483 489
484 def __finished(self): 490 def __finished(self):
485 """ 491 """
486 Private slot to handle the download finished. 492 Private slot to handle the download finished.
487 """ 493 """
495 self.pauseButton.setVisible(False) 501 self.pauseButton.setVisible(False)
496 self.stopButton.setEnabled(False) 502 self.stopButton.setEnabled(False)
497 self.stopButton.setVisible(False) 503 self.stopButton.setVisible(False)
498 self.openButton.setEnabled(noError) 504 self.openButton.setEnabled(noError)
499 self.openButton.setVisible(noError) 505 self.openButton.setVisible(noError)
500 self.__state = DownloadItem.DownloadSuccessful 506 self.__state = DownloadState.Successful
501 self.__updateInfoLabel() 507 self.__updateInfoLabel()
502 self.__setDateTime() 508 self.__setDateTime()
503 509
504 self.__adjustSize() 510 self.__adjustSize()
505 511
586 self.stopButton.setEnabled(False) 592 self.stopButton.setEnabled(False)
587 self.stopButton.setVisible(False) 593 self.stopButton.setVisible(False)
588 self.openButton.setEnabled(data["Done"]) 594 self.openButton.setEnabled(data["Done"])
589 self.openButton.setVisible(data["Done"]) 595 self.openButton.setVisible(data["Done"])
590 if data["Done"]: 596 if data["Done"]:
591 self.__state = DownloadItem.DownloadSuccessful 597 self.__state = DownloadState.Successful
592 else: 598 else:
593 self.__state = DownloadItem.DownloadCancelled 599 self.__state = DownloadState.Cancelled
594 self.progressBar.setVisible(False) 600 self.progressBar.setVisible(False)
595 601
596 self.__adjustSize() 602 self.__adjustSize()
597 603
598 def getInfoData(self): 604 def getInfoData(self):

eric ide

mercurial