tabCounter/tabcounter/label.py
Juraj Oravec bd61abd5f4
Add optional tooltip for toolbar button
Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
2020-09-01 01:07:22 +02:00

122 lines
3.8 KiB
Python

# ============================================================
# Tab Counter - plugin for Falkon
# Copyright (C) 2020 Juraj Oravec <sgd.orava@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# ============================================================
import Falkon
from PySide2 import QtWidgets
from tabcounter.constants import DISPLAY_TYPE
from tabcounter.i18n import i18n
class Label(QtWidgets.QLabel):
window = None
config = None
allTabs = 0
loadedTabs = 0
def __init__(self, window, config):
super().__init__()
self.window = window
self.config = config
self.setFrameStyle(QtWidgets.QFrame.StyledPanel)
self.updateText()
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"
def name(self):
return "Tab Counter"
def updateText(self):
self.ubdateCounts()
if self.config.displayType == DISPLAY_TYPE.ALL:
self.setText("%d" % self.allTabs)
elif self.config.displayType == DISPLAY_TYPE.LOADED:
self.setText("%d" % self.loadedTabs)
elif self.config.displayType == DISPLAY_TYPE.UNLOADED:
self.setText("%d" % self.unloadedTabs())
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))
def ubdateCounts(self):
tabs = self.window.tabWidget().allTabs()
self.allTabs = len(tabs)
self.loadedTabs = 0
for tab in tabs:
if tab.isRestored():
self.loadedTabs += 1
def unloadedTabs(self):
return self.allTabs - self.loadedTabs
def updateToolTip(self):
windows_all = 0
tabs_all = 0
tabs_active = 0
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 = """<table>
<tr><td>{tr_windows}:</td><td style='text-align:right;'>{windows}</td></tr>
<tr><td>{tr_active}:</td><td style='text-align:right;'>{tabs_active}</td></tr>
<tr><td>{tr_inactive}:</td><td style='text-align:right;'>{tabs_inactive}</td></tr>
<tr><td>{tr_all}:</td><td style='text-align:right;'>{tabs_all}</td></tr>
</table>"""
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)