summaryrefslogtreecommitdiff
path: root/src/main/python/utils/tabs.py
blob: 3dfb641c31c54c8add5ae7681e7d253e5ea80de2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from PyQt5.QtWidgets import QTabBar, QPushButton, QTabWidget
from PyQt5.QtCore import pyqtSignal, QSize

class tabBarPlus(QTabBar):
    layoutChanged = pyqtSignal()
    def resizeEvent(self, event):
        super().resizeEvent(event)
        self.layoutChanged.emit()

    def tabLayoutChange(self):
        super().tabLayoutChange()
        self.layoutChanged.emit()


class customTabWidget(QTabWidget):
    plusClicked = pyqtSignal()
    def __init__(self, parent=None):
        super(customTabWidget, self).__init__(parent)

        self.tab = tabBarPlus()
        self.setTabBar(self.tab)
        
        self.plusButton = QPushButton('+', self)
        self.plusButton.setFixedSize(35, 25)
        self.plusButton.clicked.connect(self.plusClicked.emit)
        self.setMovable(True)
        self.setTabsClosable(True)

        self.tab.layoutChanged.connect(self.movePlusButton)
    
    def movePlusButton(self):
        size = sum([self.tab.tabRect(i).width() for i in range(self.tab.count())])
        h = max(self.tab.geometry().bottom() - 25, 0)
        w = self.tab.width()
        if size > w:
            self.plusButton.move(w-self.plusButton.width(), h)
        else:
            self.plusButton.move(size-3, h)