summaryrefslogtreecommitdiff
path: root/src/main/python/utils/toolbar.py
blob: b870259262d0f80253a10942b159e0928713dcc0 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtCore import QSize, Qt, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import (QBoxLayout, QDockWidget, QGridLayout, QLineEdit,
                             QScrollArea, QToolButton, QWidget)

from .data import defaultToolbarItems, toolbarItems
from .funcs import grouper
from .layout import flowLayout

resourceManager = ApplicationContext()

class toolbar(QDockWidget):
    toolbuttonClicked = pyqtSignal(dict)
    
    def __init__(self, parent = None):
        super(toolbar, self).__init__(parent)
        self.toolbarButtonDict = dict()
        self.toolbarItems(defaultToolbarItems)
        self.diagAreaLayout = None
        
        self.setFeatures(QDockWidget.DockWidgetFloatable | QDockWidget.DockWidgetMovable)
        
        self.widget = QWidget(self)
        self.layout = QBoxLayout(QBoxLayout.TopToBottom, self.widget)
        self.setAllowedAreas(Qt.AllDockWidgetAreas)
        
        self.searchBox = QLineEdit(self.widget)
        
        self.searchBox.textChanged.connect(self.searchQuery)
        self.layout.addWidget(self.searchBox, alignment=Qt.AlignHCenter)
        
        self.diagArea = QScrollArea(self)
        self.layout.addWidget(self.diagArea)
        self.diagAreaWidget = QWidget()

        self.setWidget(self.widget)
              
    def populateToolbar(self, list):
        if self.diagAreaLayout:
            self.diagAreaLayout.deleteLater()
        self.diagAreaLayout = flowLayout(self.diagAreaWidget)
        for item in list:
            self.diagAreaLayout.addWidget(self.toolbarButtonDict[item])
        self.diagArea.setWidget(self.diagAreaWidget)
            
    def searchQuery(self):
        # shorten toolbaritems list with search items
        # self.populateToolbar() # populate with toolbar items
        text = self.searchBox.text()
        if text == '':
            self.populateToolbar(self.toolbarItemList)
        else:
            pass
            #implement shortlisting

    def resize(self):
        parent = self.parentWidget()
        # print(self.orientation())
        if parent.dockWidgetArea in [Qt.TopDockWidgetArea, Qt.BottomDockWidgetArea] and not self.isFloating():
            self.layout.setDirection(QBoxLayout.LeftToRight)
            self.setFixedHeight(.12*parent.height())
            self.setFixedWidth(parent.width())
            self.diagAreaWidget.setFixedHeight(.12*parent.height() - 45)
        else:
            self.layout.setDirection(QBoxLayout.TopToBottom)
            self.setFixedWidth(.12*parent.width())
            self.setFixedHeight(self.height())
            self.diagAreaWidget.setFixedWidth(.12*parent.width() - 45)
    
    def resizeEvent(self, event):
        parent = self.parentWidget()
        self.layout.setDirection(QBoxLayout.TopToBottom)
        self.setFixedWidth(.12*parent.width())
        self.setFixedHeight(self.height())
        width = .12*parent.width() - 45
        self.diagAreaWidget.setFixedWidth(width)
        self.diagAreaWidget.setFixedHeight(self.diagAreaLayout.heightForWidth(width))
        return super(toolbar, self).resizeEvent(event)

    # @property
    # def toolbarItems(self):
    #     return self._toolbarItems
    
    # @toolbarItems.setter
    def toolbarItems(self, items):
        for item in items:
            obj = toolbarItems[item]
            button = toolbarButton(self, obj)
            button.clicked.connect(lambda : self.toolbuttonClicked.emit(obj))
            self.toolbarButtonDict[item] = button
            
    @property
    def toolbarItemList(self):
        return self.toolbarButtonDict.keys()
            
class toolbarButton(QToolButton):
    
    def __init__(self, parent = None, item = None):
        super(toolbarButton, self).__init__(parent)
        self.setIcon(QIcon(resourceManager.get_resource(f'toolbar/{item["icon"]}')))
        self.setIconSize(QSize(40, 40))
        self.setText(f'item["name"]')
        # self.setFixedSize(30, 30)

    def sizeHint(self):
        return self.minimumSizeHint()
    
    def minimumSizeHint(self):
        return QSize(30, 30)