Files
krowlog/src/ui/tabs.py

74 lines
2.3 KiB
Python

from typing import Optional
from PySide6.QtWidgets import QWidget, QTabWidget, QVBoxLayout
from src.pluginregistry import PluginRegistry
from src.plugins.krowlog.Tab import Tab
from src.settings.settings import Settings
from src.ui.CustomTabBar import CustomTabBar
class Tabs(QWidget):
def __init__(self, settings: Settings):
super(Tabs, self).__init__()
self.settings = settings
self.tabs = QTabWidget()
self.tabs.setTabBar(CustomTabBar())
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())
tab.on_reveal()
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()
def open_files(self) -> [str]:
result = []
for i in range(self.tabs.count()):
tab: Tab = self.tabs.widget(i)
result.append(tab.get_file())
return result