69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
# Standard library imports
|
|
import datetime as dt
|
|
from typing import Dict, Optional
|
|
|
|
# PyQt imports
|
|
from PyQt6.QtCore import QUrl
|
|
from PyQt6.QtWebEngineWidgets import QWebEngineView
|
|
from PyQt6.QtWidgets import QTabWidget, QWidget
|
|
|
|
# Third party imports
|
|
|
|
# App imports
|
|
from config import Config
|
|
|
|
|
|
class InfoTabs(QTabWidget):
|
|
"""
|
|
Class to manage info tabs
|
|
"""
|
|
|
|
def __init__(self, parent: Optional[QWidget] = None) -> None:
|
|
super().__init__(parent)
|
|
|
|
if Config.USE_INTERNAL_BROWSER:
|
|
# re-use the oldest one later)
|
|
self.last_update: Dict[QWebEngineView, dt.datetime] = {}
|
|
self.tabtitles: Dict[int, str] = {}
|
|
|
|
# Create one tab which (for some reason) creates flickering if
|
|
# done later
|
|
widget = QWebEngineView()
|
|
widget.setZoomFactor(Config.WEB_ZOOM_FACTOR)
|
|
self.last_update[widget] = dt.datetime.now()
|
|
_ = self.addTab(widget, "")
|
|
|
|
def open_tab(self, url: str, title: str) -> None:
|
|
"""
|
|
Open passed URL. If URL currently displayed, switch to that tab.
|
|
Create new tab if we're below the maximum
|
|
number otherwise reuse oldest content tab.
|
|
"""
|
|
|
|
if url in self.tabtitles.values():
|
|
self.setCurrentIndex(
|
|
list(self.tabtitles.keys())[list(self.tabtitles.values()).index(url)]
|
|
)
|
|
return
|
|
|
|
short_title = title[: Config.INFO_TAB_TITLE_LENGTH]
|
|
|
|
if self.count() < Config.MAX_INFO_TABS:
|
|
# Create a new tab
|
|
widget = QWebEngineView()
|
|
widget.setZoomFactor(Config.WEB_ZOOM_FACTOR)
|
|
tab_index = self.addTab(widget, short_title)
|
|
|
|
else:
|
|
# Reuse oldest widget
|
|
widget = min(self.last_update, key=self.last_update.get) # type: ignore
|
|
tab_index = self.indexOf(widget)
|
|
self.setTabText(tab_index, short_title)
|
|
|
|
widget.setUrl(QUrl(url))
|
|
self.last_update[widget] = dt.datetime.now()
|
|
self.tabtitles[tab_index] = url
|
|
|
|
# Show newly updated tab
|
|
self.setCurrentIndex(tab_index)
|