From 3e95e04610ae945b3178ed49443a21626f7d2159 Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Sat, 29 Feb 2020 21:18:13 +0100 Subject: [PATCH] Add Config class, ability to display different data Signed-off-by: Juraj Oravec --- tabcounter/__init__.py | 17 +++++- tabcounter/config.py | 93 ++++++++++++++++++++++++++++++ tabcounter/constants.py | 27 +++++++++ tabcounter/{button.py => label.py} | 29 +++++++++- 4 files changed, 161 insertions(+), 5 deletions(-) create mode 100644 tabcounter/config.py create mode 100644 tabcounter/constants.py rename tabcounter/{button.py => label.py} (63%) diff --git a/tabcounter/__init__.py b/tabcounter/__init__.py index e26f6d9..07474e6 100644 --- a/tabcounter/__init__.py +++ b/tabcounter/__init__.py @@ -16,16 +16,27 @@ # along with this program. If not, see . # ============================================================ + +import os import Falkon from PySide2 import QtCore -from tabcounter.button import Button +from tabcounter.label import Label +from tabcounter.config import Config +from tabcounter.constants import DISPLAY_TYPE class tabCounter(Falkon.PluginInterface, QtCore.QObject): panelWidgets = {} + config = None def init(self, state, settingsPath): + self.config = Config( + settingsFile=os.path.join(settingsPath, "tabcounter", "settings.ini"), + displayType=DISPLAY_TYPE.LOADED_AND_ALL + ) + self.config.load() + plugins = Falkon.MainApplication.instance().plugins() plugins.mainWindowCreated.connect(self.onMainWindowCreated) @@ -43,7 +54,9 @@ class tabCounter(Falkon.PluginInterface, QtCore.QObject): return True def onMainWindowCreated(self, window): - labelWidget = Button(window) + labelWidget = Label(window, self.config.displayType) + self.config.displayTypeChanged.connect(labelWidget.displayTypeChanged) + window.navigationBar().addWidget(labelWidget, labelWidget.id(), labelWidget.name()) self.panelWidgets[window] = labelWidget diff --git a/tabcounter/config.py b/tabcounter/config.py new file mode 100644 index 0000000..e1c2052 --- /dev/null +++ b/tabcounter/config.py @@ -0,0 +1,93 @@ +# ============================================================ +# Tab Counter - plugin for Falkon +# Copyright (C) 2020 Juraj Oravec +# +# 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 . +# ============================================================ + + +import os +from PySide2.QtCore import QObject, Signal, QSettings + + +class Config(QObject): + displayTypeChanged = Signal(int) + settingsFileChanged = Signal(str) + + configLoaded = Signal() + configSaved = Signal() + + def __init__(self, displayType=None, settingsFile=None): + super().__init__() + + self._displayType = displayType + self._settingsFile = settingsFile + + def load(self): + if not self.settingsFile: + return + if os.path.exists(self.settingsFile): + pass + + settings = QSettings(self.settingsFile, QSettings.IniFormat) + + settings.beginGroup("Config") + self.displayType = int(settings.value("displayType", self.displayType)) + settings.endGroup() + + self.configLoaded.emit() + + def save(self): + if not self.settingsFile: + return + if os.path.exists(self.settingsFile): + pass + + settings = QSettings(self.settingsFile, QSettings.IniFormat) + + settings.beginGroup("Config") + settings.setValue("displayType", self.displayType) + settings.endGroup() + + settings.sync() + + self.configSaved.emit() + + @property + def displayType(self): + return self._displayType + + @displayType.setter + def displayType(self, displayType): + if not displayType: + return + if self._displayType == displayType: + return + + self._displayType = displayType + self.displayTypeChanged.emit(displayType) + + @property + def settingsFile(self): + return self._settingsFile + + @settingsFile.setter + def settingsFile(self, settingsFile): + if not settingsFile: + return + if self._settingsFile == settingsFile: + return + + self._settingsFile = settingsFile + self.settingsFileChanged.emit(settingsFile) diff --git a/tabcounter/constants.py b/tabcounter/constants.py new file mode 100644 index 0000000..7b81b7e --- /dev/null +++ b/tabcounter/constants.py @@ -0,0 +1,27 @@ +# ============================================================ +# Tab Counter - plugin for Falkon +# Copyright (C) 2020 Juraj Oravec +# +# 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 . +# ============================================================ + + +from enum import IntEnum + + +class DISPLAY_TYPE(IntEnum): + ALL = 1 + LOADED = 2 + UNLOADED = 3 + LOADED_AND_ALL = 4 diff --git a/tabcounter/button.py b/tabcounter/label.py similarity index 63% rename from tabcounter/button.py rename to tabcounter/label.py index 58ae3b3..6ae90ae 100644 --- a/tabcounter/button.py +++ b/tabcounter/label.py @@ -16,18 +16,24 @@ # along with this program. If not, see . # ============================================================ + from PySide2 import QtWidgets +from tabcounter.constants import DISPLAY_TYPE -class Button(QtWidgets.QLabel): + +class Label(QtWidgets.QLabel): window = None + displayType = None + allTabs = 0 loadedTabs = 0 - def __init__(self, window): + def __init__(self, window, displayType): super().__init__() self.window = window + self.displayType = displayType self.setFrameStyle(QtWidgets.QFrame.StyledPanel) @@ -44,7 +50,16 @@ class Button(QtWidgets.QLabel): def updateText(self): self.ubdateCounts() - self.setText("%d/%d" % (self.loadedTabs, self.allTabs)) + if self.displayType == DISPLAY_TYPE.ALL: + self.setText("%d" % self.allTabs) + elif self.displayType == DISPLAY_TYPE.LOADED: + self.setText("%d" % self.loadedTabs) + elif self.displayType == DISPLAY_TYPE.UNLOADED: + self.setText("%d" % self.unloadedTabs()) + elif self.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() @@ -55,3 +70,11 @@ class Button(QtWidgets.QLabel): for tab in tabs: if tab.isRestored(): self.loadedTabs += 1 + + def unloadedTabs(self): + return self.allTabs - self.loadedTabs + + def displayTypeChanged(self, displayType): + self.displayType = displayType + + self.updateText()