From bd61abd5f463664273e2aae373fe75f57e9e4048 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Tue, 1 Sep 2020 01:07:22 +0200 Subject: [PATCH] Add optional tooltip for toolbar button Signed-off-by: Juraj Oravec --- tabcounter/__init__.py | 6 ++-- tabcounter/config.py | 20 +++++++++++- tabcounter/label.py | 60 ++++++++++++++++++++++++++++++------ tabcounter/settings.ui | 12 +++++++- tabcounter/settingsDialog.py | 3 ++ 5 files changed, 86 insertions(+), 15 deletions(-) diff --git a/tabcounter/__init__.py b/tabcounter/__init__.py index 9ed1ceb..2dbbe37 100644 --- a/tabcounter/__init__.py +++ b/tabcounter/__init__.py @@ -34,7 +34,8 @@ class tabCounter(Falkon.PluginInterface, QtCore.QObject): def init(self, state, settingsPath): self.config = Config( settingsFile=os.path.join(settingsPath, "tabcounter", "settings.ini"), - displayType=DISPLAY_TYPE.LOADED_AND_ALL + displayType=DISPLAY_TYPE.LOADED_AND_ALL, + enableTooltip=False ) self.config.load() @@ -61,8 +62,7 @@ class tabCounter(Falkon.PluginInterface, QtCore.QObject): def onMainWindowCreated(self, window): - labelWidget = Label(window, self.config.displayType) - self.config.displayTypeChanged.connect(labelWidget.displayTypeChanged) + labelWidget = Label(window, self.config) window.navigationBar().addWidget(labelWidget, labelWidget.id(), labelWidget.name()) self.panelWidgets[window] = labelWidget diff --git a/tabcounter/config.py b/tabcounter/config.py index e1c2052..75befae 100644 --- a/tabcounter/config.py +++ b/tabcounter/config.py @@ -23,16 +23,18 @@ from PySide2.QtCore import QObject, Signal, QSettings class Config(QObject): displayTypeChanged = Signal(int) + enableTooltipChanged = Signal(bool) settingsFileChanged = Signal(str) configLoaded = Signal() configSaved = Signal() - def __init__(self, displayType=None, settingsFile=None): + def __init__(self, displayType=None, settingsFile=None, enableTooltip=None): super().__init__() self._displayType = displayType self._settingsFile = settingsFile + self._enableTooltip = enableTooltip def load(self): if not self.settingsFile: @@ -44,6 +46,7 @@ class Config(QObject): settings.beginGroup("Config") self.displayType = int(settings.value("displayType", self.displayType)) + self.enableTooltip = settings.value("enableTooltip", self.enableTooltip) == "true" settings.endGroup() self.configLoaded.emit() @@ -58,6 +61,7 @@ class Config(QObject): settings.beginGroup("Config") settings.setValue("displayType", self.displayType) + settings.setValue("enableTooltip", self.enableTooltip) settings.endGroup() settings.sync() @@ -78,6 +82,20 @@ class Config(QObject): self._displayType = displayType self.displayTypeChanged.emit(displayType) + @property + def enableTooltip(self): + return self._enableTooltip + + @enableTooltip.setter + def enableTooltip(self, enableTooltip): + if not isinstance(enableTooltip, bool): + return + if self._enableTooltip == enableTooltip: + return + + self._enableTooltip = enableTooltip + self.enableTooltipChanged.emit(enableTooltip) + @property def settingsFile(self): return self._settingsFile diff --git a/tabcounter/label.py b/tabcounter/label.py index 7a09206..d436425 100644 --- a/tabcounter/label.py +++ b/tabcounter/label.py @@ -16,24 +16,26 @@ # along with this program. If not, see . # ============================================================ +import Falkon from PySide2 import QtWidgets from tabcounter.constants import DISPLAY_TYPE +from tabcounter.i18n import i18n class Label(QtWidgets.QLabel): window = None - displayType = None + config = None allTabs = 0 loadedTabs = 0 - def __init__(self, window, displayType): + def __init__(self, window, config): super().__init__() self.window = window - self.displayType = displayType + self.config = config self.setFrameStyle(QtWidgets.QFrame.StyledPanel) @@ -41,6 +43,7 @@ class Label(QtWidgets.QLabel): self.window.tabWidget().changed.connect(self.updateText) self.window.tabWidget().tabRemoved.connect(self.updateText) + self.config.displayTypeChanged.connect(lambda x: self.updateText()) def id(self): return "tab-counter-button" @@ -51,13 +54,13 @@ class Label(QtWidgets.QLabel): def updateText(self): self.ubdateCounts() - if self.displayType == DISPLAY_TYPE.ALL: + if self.config.displayType == DISPLAY_TYPE.ALL: self.setText("%d" % self.allTabs) - elif self.displayType == DISPLAY_TYPE.LOADED: + elif self.config.displayType == DISPLAY_TYPE.LOADED: self.setText("%d" % self.loadedTabs) - elif self.displayType == DISPLAY_TYPE.UNLOADED: + elif self.config.displayType == DISPLAY_TYPE.UNLOADED: self.setText("%d" % self.unloadedTabs()) - elif self.displayType == DISPLAY_TYPE.LOADED_AND_ALL: + elif self.config.displayType == DISPLAY_TYPE.LOADED_AND_ALL: self.setText("%d/%d" % (self.loadedTabs, self.allTabs)) else: self.setText("%d/%d" % (self.loadedTabs, self.allTabs)) @@ -75,7 +78,44 @@ class Label(QtWidgets.QLabel): def unloadedTabs(self): return self.allTabs - self.loadedTabs - def displayTypeChanged(self, displayType): - self.displayType = displayType + def updateToolTip(self): + windows_all = 0 + tabs_all = 0 + tabs_active = 0 - self.updateText() + windows = Falkon.MainApplication.instance().windows() + windows_all = len(windows) + + for window in windows: + tabs = self.window.tabWidget().allTabs() + tabs_all += len(tabs) + + for tab in tabs: + if tab.isRestored(): + tabs_active += 1 + + tooltip = """ + + + + +
{tr_windows}:{windows}
{tr_active}:{tabs_active}
{tr_inactive}:{tabs_inactive}
{tr_all}:{tabs_all}
""" + + self.setToolTip(tooltip.format( + windows=windows_all, + tabs_active=tabs_active, + tabs_inactive=(tabs_all - tabs_active), + tabs_all=tabs_all, + tr_windows=i18n("Windows"), + tr_active=i18n("Active"), + tr_inactive=i18n("Inactive"), + tr_all=i18n("All") + )) + + def enterEvent(self, event): + self.setToolTip(""); + + if self.config.enableTooltip: + self.updateToolTip() + + super().enterEvent(event) diff --git a/tabcounter/settings.ui b/tabcounter/settings.ui index 4e1e29d..e4d7a51 100644 --- a/tabcounter/settings.ui +++ b/tabcounter/settings.ui @@ -6,7 +6,7 @@ 0 0 - 298 + 299 96 @@ -49,6 +49,16 @@ + + + + Enable tooltip + + + + + + diff --git a/tabcounter/settingsDialog.py b/tabcounter/settingsDialog.py index 30acddc..a1d4ecc 100644 --- a/tabcounter/settingsDialog.py +++ b/tabcounter/settingsDialog.py @@ -43,6 +43,7 @@ class SettingsDialog(QtWidgets.QDialog): self.translations() self.ui.displayTypeComboBox.setCurrentIndex(self.config.displayType - 1) + self.ui.enableTooltipCheckBox.setChecked(self.config.enableTooltip) self.ui.buttonBoxConfirm.accepted.connect(self.accept) self.ui.buttonBoxConfirm.rejected.connect(self.reject) @@ -50,9 +51,11 @@ class SettingsDialog(QtWidgets.QDialog): def translations(self): self.setWindowTitle(i18n("Tab Counter Settings")) self.ui.displayTypeLabel.setText(i18n("Show count of tabs:")) + self.ui.enableTooltipLabel.setText(i18n("Enable tooltip")) def updateData(self): self.config.displayType = self.ui.displayTypeComboBox.currentIndex() + 1 + self.config.enableTooltip = self.ui.enableTooltipCheckBox.isChecked() def accept(self): self.updateData()