Add optional tooltip for toolbar button
Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
parent
c4f27426f8
commit
bd61abd5f4
@ -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
|
||||
|
@ -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
|
||||
|
@ -16,24 +16,26 @@
|
||||
# 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
|
||||
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 = """<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)
|
||||
|
@ -6,7 +6,7 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>298</width>
|
||||
<width>299</width>
|
||||
<height>96</height>
|
||||
</rect>
|
||||
</property>
|
||||
@ -49,6 +49,16 @@
|
||||
</item>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLabel" name="enableTooltipLabel">
|
||||
<property name="text">
|
||||
<string>Enable tooltip</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QCheckBox" name="enableTooltipCheckBox"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user