Introduced a navigation bar button to open the downloads manager window.

Sun, 11 Feb 2018 16:04:47 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 11 Feb 2018 16:04:47 +0100
changeset 6134
cb0985e8da91
parent 6133
eba07240fac1
child 6135
12e48d27f1d7

Introduced a navigation bar button to open the downloads manager window.

E5Gui/E5ToolButton.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadItem.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadManager.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadManagerButton.py file | annotate | diff | comparison | revisions
WebBrowser/Download/DownloadModel.py file | annotate | diff | comparison | revisions
WebBrowser/Navigation/NavigationBar.py file | annotate | diff | comparison | revisions
WebBrowser/Network/EricSchemeHandler.py file | annotate | diff | comparison | revisions
icons/default/downloads.png file | annotate | diff | comparison | revisions
--- a/E5Gui/E5ToolButton.py	Sun Feb 11 16:04:16 2018 +0100
+++ b/E5Gui/E5ToolButton.py	Sun Feb 11 16:04:47 2018 +0100
@@ -11,7 +11,7 @@
 
 from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QTimer, QSize
 from PyQt5.QtWidgets import QToolButton, QStyle, QStyleOptionToolButton, \
-    QStyleOption, QApplication
+    QStyleOption, QApplication, QLabel
 
 
 class E5ToolButton(QToolButton):
@@ -50,6 +50,12 @@
         self.__menu = None
         self.__options = E5ToolButton.NoOptions
         
+        self.__badgeLabel = QLabel(self)
+        font = self.__badgeLabel.font()
+        font.setPixelSize(self.__badgeLabel.height() / 2.5)
+        self.__badgeLabel.setFont(font)
+        self.__badgeLabel.hide()
+        
         opt = QStyleOptionToolButton()
         self.initStyleOption(opt)
         
@@ -251,3 +257,32 @@
             return
         
         super(E5ToolButton, self).contextMenuEvent(evt)
+    
+    ##################################################################
+    ## Methods to handle the tool button badge
+    ##################################################################
+    
+    def setBadgeText(self, text):
+        """
+        Public method to set the badge text.
+        
+        @param text badge text to be set
+        @type str
+        """
+        if text:
+            self.__badgeLabel.setText(text)
+            self.__badgeLabel.resize(self.__badgeLabel.sizeHint())
+            self.__badgeLabel.move(self.width() - self.__badgeLabel.width(), 0)
+            self.__badgeLabel.show()
+        else:
+            self.__badgeLabel.clear()
+            self.__badgeLabel.hide()
+    
+    def badgeText(self):
+        """
+        Public method to get the badge text.
+        
+        @return badge text
+        @rtype str
+        """
+        return self.__badgeLabel.text()
--- a/WebBrowser/Download/DownloadItem.py	Sun Feb 11 16:04:16 2018 +0100
+++ b/WebBrowser/Download/DownloadItem.py	Sun Feb 11 16:04:47 2018 +0100
@@ -46,14 +46,17 @@
     DownloadSuccessful = 1
     DownloadCancelled = 2
     
-    def __init__(self, downloadItem=None, parent=None):
+    def __init__(self, downloadItem=None, pageUrl="", parent=None):
         """
         Constructor
         
         @param downloadItem reference to the download object containing the
         download data.
-        @keyparam parent reference to the parent widget (QWidget)
         @type QWebEngineDownloadItem
+        @param pageUrl URL of the calling page
+        @type QUrl
+        @param parent reference to the parent widget
+        @type QWidget
         """
         super(DownloadItem, self).__init__(parent)
         self.setupUi(self)
@@ -80,8 +83,7 @@
         self.fileIcon.setPixmap(icon.pixmap(48, 48))
         
         self.__downloadItem = downloadItem
-        self.__pageUrl = \
-            WebBrowserWindow.mainWindow().getWindow().currentBrowser().url()
+        self.__pageUrl = pageUrl
         self.__bytesReceived = 0
         self.__bytesTotal = -1
         self.__downloadTime = QTime()
--- a/WebBrowser/Download/DownloadManager.py	Sun Feb 11 16:04:16 2018 +0100
+++ b/WebBrowser/Download/DownloadManager.py	Sun Feb 11 16:04:47 2018 +0100
@@ -9,7 +9,7 @@
 
 from __future__ import unicode_literals
 
-from PyQt5.QtCore import pyqtSlot, Qt, QModelIndex, QFileInfo, QUrl
+from PyQt5.QtCore import pyqtSlot, pyqtSignal, Qt, QModelIndex, QFileInfo, QUrl
 from PyQt5.QtGui import QCursor, QKeySequence
 from PyQt5.QtWidgets import QDialog, QStyle, QFileIconProvider, QMenu, \
     QApplication, QShortcut
@@ -35,6 +35,8 @@
     RemoveExit = 1
     RemoveSuccessFullDownload = 2
     
+    downloadsCountChanged = pyqtSignal()
+    
     def __init__(self, parent=None):
         """
         Constructor
@@ -128,7 +130,7 @@
         self.save()
         self.close()
     
-    def activeDownloads(self):
+    def activeDownloadsCount(self):
         """
         Public method to get the number of active downloads.
         
@@ -147,13 +149,13 @@
         
         @return flag indicating allowance to quit (boolean)
         """
-        if self.activeDownloads() > 0:
+        if self.activeDownloadsCount() > 0:
             res = E5MessageBox.yesNo(
                 self,
                 self.tr(""),
                 self.tr("""There are %n downloads in progress.\n"""
                         """Do you want to quit anyway?""", "",
-                        self.activeDownloads()),
+                        self.activeDownloadsCount()),
                 icon=E5MessageBox.Warning)
             if not res:
                 self.show()
@@ -194,8 +196,11 @@
                     downloadItem.cancel()
                     return
         
+        pageUrl = \
+            WebBrowserWindow.mainWindow().getWindow().currentBrowser().url()
         from .DownloadItem import DownloadItem
-        itm = DownloadItem(downloadItem, parent=self)
+        itm = DownloadItem(downloadItem=downloadItem, pageUrl=pageUrl,
+                           parent=self)
         self.__addItem(itm)
         
         if itm.canceledFileSelect():
@@ -221,7 +226,7 @@
         
         # insert at top of window
         if append:
-            row = len(self.__downloads)
+            row = self.downloadsCount()
         else:
             row = 0
         self.__model.beginInsertRows(QModelIndex(), row, row)
@@ -240,6 +245,8 @@
         self.__updateRow(itm)
         self.changeOccurred()
         self.__updateActiveItemCount()
+        
+        self.downloadsCountChanged.emit()
     
     def __updateRow(self, itm):
         """
@@ -275,7 +282,7 @@
             self.__model.removeRow(row)
         
         self.cleanupButton.setEnabled(
-            (len(self.__downloads) - self.activeDownloads()) > 0)
+            (self.downloadsCount() - self.activeDownloadsCount()) > 0)
         
         # record the change
         self.changeOccurred()
@@ -350,10 +357,12 @@
                     itm.setData(download)
                     self.__addItem(itm, append=True)
             self.cleanupButton.setEnabled(
-                (len(self.__downloads) - self.activeDownloads()) > 0)
+                (self.downloadsCount() - self.activeDownloadsCount()) > 0)
         
         self.__loaded = True
         self.__updateActiveItemCount()
+        
+        self.downloadsCountChanged.emit()
     
     def closeEvent(self, evt):
         """
@@ -375,29 +384,31 @@
         """
         Private slot cleanup the downloads.
         """
-        if len(self.__downloads) == 0:
+        if self.downloadsCount() == 0:
             return
         
-        self.__model.removeRows(0, len(self.__downloads))
-        if len(self.__downloads) == 0 and \
+        self.__model.removeRows(0, self.downloadsCount())
+        if self.downloadsCount() == 0 and \
            self.__iconProvider is not None:
             self.__iconProvider = None
         
         self.changeOccurred()
         self.__updateActiveItemCount()
+        
+        self.downloadsCountChanged.emit()
     
     def __updateItemCount(self):
         """
         Private method to update the count label.
         """
-        count = len(self.__downloads)
+        count = self.downloadsCount()
         self.countLabel.setText(self.tr("%n Download(s)", "", count))
     
     def __updateActiveItemCount(self):
         """
         Private method to update the window title.
         """
-        count = self.activeDownloads()
+        count = self.activeDownloadsCount()
         if count > 0:
             self.setWindowTitle(
                 self.tr("Downloading %n file(s)", "", count))
@@ -411,6 +422,8 @@
         self.__updateActiveItemCount()
         if self.isVisible():
             QApplication.alert(self)
+        
+        self.downloadsCountChanged.emit()
     
     def setDownloadDirectory(self, directory):
         """
@@ -430,11 +443,12 @@
         """
         return self.__downloadDirectory
     
-    def count(self):
+    def downloadsCount(self):
         """
         Public method to get the number of downloads.
         
-        @return number of downloads (integer)
+        @return number of downloads
+        @type int
         """
         return len(self.__downloads)
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebBrowser/Download/DownloadManagerButton.py	Sun Feb 11 16:04:47 2018 +0100
@@ -0,0 +1,65 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a tool button for the download manager.
+"""
+
+from __future__ import unicode_literals
+
+from PyQt5.QtCore import pyqtSlot, Qt
+
+from E5Gui.E5ToolButton import E5ToolButton
+
+import UI.PixmapCache
+
+from WebBrowser.WebBrowserWindow import WebBrowserWindow
+
+
+class DownloadManagerButton(E5ToolButton):
+    """
+    Class implementing a tool button for the download manager.
+    """
+    def __init__(self, parent=None):
+        """
+        Constructor
+        
+        @param parent reference to the parent widget
+        @type QWidget
+        """
+        super(DownloadManagerButton, self).__init__(parent)
+        
+        self.__manager = WebBrowserWindow.downloadManager()
+        
+        self.setObjectName("navigation_download_manager_button")
+        self.setIcon(UI.PixmapCache.getIcon("downloads.png"))
+        self.setToolTip(self.tr("Open Download Manager"))
+        self.setToolButtonStyle(Qt.ToolButtonIconOnly)
+        self.setFocusPolicy(Qt.NoFocus)
+        self.setAutoRaise(True)
+        
+        self.clicked.connect(self.__buttonClicked)
+        self.__manager.downloadsCountChanged.connect(self.__updateState)
+        
+        self.__updateState()
+    
+    @pyqtSlot()
+    def __buttonClicked(self):
+        """
+        Private slot handling a user clicking the button.
+        """
+        self.__manager.show()
+    
+    @pyqtSlot()
+    def __updateState(self):
+        """
+        Private slot to update the button state.
+        """
+        self.setVisible(bool(self.__manager.downloadsCount()))
+        count = self.__manager.activeDownloadsCount()
+        if bool(count):
+            self.setBadgeText(str(count))
+        else:
+            self.setBadgeText("")
--- a/WebBrowser/Download/DownloadModel.py	Sun Feb 11 16:04:16 2018 +0100
+++ b/WebBrowser/Download/DownloadModel.py	Sun Feb 11 16:04:47 2018 +0100
@@ -58,7 +58,7 @@
         if parent.isValid():
             return 0
         else:
-            return self.__manager.count()
+            return self.__manager.downloadsCount()
     
     def removeRows(self, row, count, parent=None):
         """
--- a/WebBrowser/Navigation/NavigationBar.py	Sun Feb 11 16:04:16 2018 +0100
+++ b/WebBrowser/Navigation/NavigationBar.py	Sun Feb 11 16:04:47 2018 +0100
@@ -16,6 +16,7 @@
 from E5Gui.E5ToolButton import E5ToolButton
 
 from WebBrowser.WebBrowserWindow import WebBrowserWindow
+from WebBrowser.Download.DownloadManagerButton import DownloadManagerButton
 
 import UI.PixmapCache
 import Preferences
@@ -98,6 +99,8 @@
         self.__exitFullScreenButton.clicked.connect(self.__mw.toggleFullScreen)
         self.__exitFullScreenButton.setVisible(False)
         
+        self.__downloadManagerButton = DownloadManagerButton(self)
+        
         self.__superMenuButton = E5ToolButton(self)
         self.__superMenuButton.setObjectName(
             "navigation_supermenu_button")
@@ -131,6 +134,7 @@
         self.__layout.addWidget(self.__reloadStopButton)
         self.__layout.addWidget(self.__homeButton)
         self.__layout.addWidget(self.__navigationSplitter)
+        self.__layout.addWidget(self.__downloadManagerButton)
         self.__layout.addWidget(self.__exitFullScreenButton)
         self.__layout.addWidget(self.__superMenuButton)
         
--- a/WebBrowser/Network/EricSchemeHandler.py	Sun Feb 11 16:04:16 2018 +0100
+++ b/WebBrowser/Network/EricSchemeHandler.py	Sun Feb 11 16:04:47 2018 +0100
@@ -236,8 +236,8 @@
             page = page.replace("@TXT_SDSIZE@",
                                 self.tr("Change size of pages:"))
             page = page.replace("@JAVASCRIPT_DISABLED@",
-                                self.tr("SpeedDial requires JavaScript"
-                                        " enabled."))
+                                self.tr("SpeedDial requires enabled"
+                                        " JavaScript."))
             
             self._speedDialPage = page
         
Binary file icons/default/downloads.png has changed

eric ide

mercurial