from typing import Optional from PySide6.QtWidgets import QWidget, QTabWidget, QVBoxLayout from raven.pluginregistry import PluginRegistry from raven.plugins.ravenlog.Tab import Tab from settings import Settings class Tabs(QWidget): def __init__(self, settings: Settings): super(Tabs, self).__init__() self.settings = settings self.tabs = QTabWidget() self.tabs.setTabsClosable(True) self.tabs.setMovable(True) self.tabs.tabCloseRequested.connect(self._close_tab) self.tabs.currentChanged.connect(self._current_tab_changed) self.layout = QVBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.addWidget(self.tabs) def add_tab(self, tab: Tab): # if tab already exists then open it for tab_index in range(0, self.tabs.count()): widget: Tab = self.tabs.widget(tab_index) if widget.unique_id == tab.unique_id: self.tabs.setCurrentIndex(tab_index) return tab_index = self.tabs.addTab(tab, tab.title) self.tabs.setCurrentIndex(tab_index) def _current_tab_changed(self, tab_index: int): tab: Tab = self.tabs.widget(tab_index) if tab: PluginRegistry.execute("update_window_title", tab.title) PluginRegistry.execute("update_status_bar", tab.get_status_text()) else: PluginRegistry.execute("update_window_title", "") PluginRegistry.execute("update_status_bar", "") def _close_tab(self, tab_index: int): full_tab: Tab = self.tabs.widget(tab_index) full_tab.destruct() self.tabs.removeTab(tab_index) def destruct(self): while self.tabs.count() > 0: self._close_tab(0) def _current_tab(self) -> int: return self.tabs.currentIndex() def current_file(self) -> Optional[str]: if self.tabs.currentIndex() < 0: return None tab: Tab = self.tabs.widget(self.tabs.currentIndex()) return tab.get_file()