|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +__all__ = ['TTkAbstractScrollArea'] |
| 24 | + |
| 25 | +from dataclasses import dataclass |
| 26 | +from typing import List,Any,Type |
| 27 | + |
| 28 | +from TermTk.TTkCore.constant import TTkK |
| 29 | +# from TermTk.TTkCore.log import TTkLog |
| 30 | +from TermTk.TTkCore.signal import pyTTkSlot |
| 31 | +from TermTk.TTkWidgets.widget import TTkWidget |
| 32 | +from TermTk.TTkWidgets.container import TTkContainer |
| 33 | +from TermTk.TTkWidgets.scrollbar import TTkScrollBar |
| 34 | +from TermTk.TTkLayouts.gridlayout import TTkGridLayout |
| 35 | +from TermTk.TTkAbstract.abstractscrollview import TTkAbstractScrollViewInterface |
| 36 | + |
| 37 | +@dataclass |
| 38 | +class _ForwardData(): |
| 39 | + forwardClass: Type |
| 40 | + instance: str |
| 41 | + signals: List[str] |
| 42 | + methods: List[str] |
| 43 | + |
| 44 | +class TTkAbstractScrollArea(TTkContainer): |
| 45 | + __slots__ = ( |
| 46 | + '_processing', # this flag is required to avoid unnecessary loop on edge cases |
| 47 | + '_viewport', |
| 48 | + '_verticalScrollBar', '_verticalScrollBarPolicy', |
| 49 | + '_horizontalScrollBar', '_horizontalScrollBarPolicy',) |
| 50 | + |
| 51 | + def __init__(self, *, |
| 52 | + verticalScrollBarPolicy:TTkK.ScrollBarPolicy=TTkK.ScrollBarPolicy.ScrollBarAsNeeded, |
| 53 | + horizontalScrollBarPolicy:TTkK.ScrollBarPolicy=TTkK.ScrollBarPolicy.ScrollBarAsNeeded, |
| 54 | + **kwargs) -> None: |
| 55 | + self._processing = False |
| 56 | + self._viewport = None |
| 57 | + # self.setLayout(TTkGridLayout()) |
| 58 | + self._verticalScrollBar = TTkScrollBar(orientation=TTkK.VERTICAL, visible=False) |
| 59 | + self._horizontalScrollBar = TTkScrollBar(orientation=TTkK.HORIZONTAL, visible=False) |
| 60 | + self._verticalScrollBarPolicy = verticalScrollBarPolicy |
| 61 | + self._horizontalScrollBarPolicy = horizontalScrollBarPolicy |
| 62 | + super().__init__(**kwargs) |
| 63 | + self.layout().addWidget(self._verticalScrollBar) |
| 64 | + self.layout().addWidget(self._horizontalScrollBar) |
| 65 | + |
| 66 | + def _resizeEvent(self): |
| 67 | + if self._processing: return |
| 68 | + self._processing = True |
| 69 | + w,h = self.size() |
| 70 | + vert = 1 if self._verticalScrollBar.isVisible() else 0 |
| 71 | + hori = 1 if self._horizontalScrollBar.isVisible() else 0 |
| 72 | + if vert: |
| 73 | + self._verticalScrollBar.setGeometry(w-1,0,1,h-hori) |
| 74 | + if hori: |
| 75 | + self._horizontalScrollBar.setGeometry(0,h-1,w-vert,1) |
| 76 | + if self._viewport: |
| 77 | + self._viewport.setGeometry(0,0,w-vert,h-hori) |
| 78 | + self._processing = False |
| 79 | + |
| 80 | + def resizeEvent(self, w: int, h: int): |
| 81 | + self._resizeEvent() |
| 82 | + |
| 83 | + @pyTTkSlot() |
| 84 | + def _viewportChanged(self): |
| 85 | + if not self.isVisible(): return |
| 86 | + w,h = self.size() |
| 87 | + fw, fh = self._viewport.viewFullAreaSize() |
| 88 | + dw, dh = self._viewport.viewDisplayedSize() |
| 89 | + ox, oy = self._viewport.getViewOffsets() |
| 90 | + if 0 in [fw,fh,dw,dh]: |
| 91 | + return |
| 92 | + hpage = dw |
| 93 | + vpage = dh |
| 94 | + hrange = fw - dw |
| 95 | + vrange = fh - dh |
| 96 | + # TTkLog.debug(f"f:{fw,fh=}, d:{dw,dh=}, o:{ox,oy=}") |
| 97 | + self._verticalScrollBar.setPageStep(vpage) |
| 98 | + self._verticalScrollBar.setRange(0, vrange) |
| 99 | + self._verticalScrollBar.setValue(oy) |
| 100 | + self._horizontalScrollBar.setPageStep(hpage) |
| 101 | + self._horizontalScrollBar.setRange(0, hrange) |
| 102 | + self._horizontalScrollBar.setValue(ox) |
| 103 | + |
| 104 | + if self._verticalScrollBarPolicy == TTkK.ScrollBarAsNeeded: |
| 105 | + if h<=4 or w<=1 or vrange<=0: |
| 106 | + self._verticalScrollBar.hide() |
| 107 | + elif dh>self._verticalScrollBar.minimumHeight()+1: |
| 108 | + # we need enough space to display the bar to avoid endless loop |
| 109 | + self._verticalScrollBar.show() |
| 110 | + elif self._verticalScrollBarPolicy == TTkK.ScrollBarAlwaysOn: |
| 111 | + self._verticalScrollBar.show() |
| 112 | + else: |
| 113 | + self._verticalScrollBar.hide() |
| 114 | + |
| 115 | + if self._horizontalScrollBarPolicy == TTkK.ScrollBarAsNeeded: |
| 116 | + if w<=4 or h<=1 or hrange<=0: |
| 117 | + self._horizontalScrollBar.hide() |
| 118 | + elif dw>self._horizontalScrollBar.minimumWidth()+1: |
| 119 | + # we need enough space to display the bar to avoid endless loop |
| 120 | + self._horizontalScrollBar.show() |
| 121 | + elif self._horizontalScrollBarPolicy == TTkK.ScrollBarAlwaysOn: |
| 122 | + self._horizontalScrollBar.show() |
| 123 | + else: |
| 124 | + self._horizontalScrollBar.hide() |
| 125 | + self._resizeEvent() |
| 126 | + |
| 127 | + @pyTTkSlot(int) |
| 128 | + def _vscrollMoved(self, val): |
| 129 | + ox, _ = self._viewport.getViewOffsets() |
| 130 | + self._viewport.viewMoveTo(ox, val) |
| 131 | + |
| 132 | + @pyTTkSlot(int) |
| 133 | + def _hscrollMoved(self, val): |
| 134 | + _, oy = self._viewport.getViewOffsets() |
| 135 | + self._viewport.viewMoveTo(val, oy) |
| 136 | + |
| 137 | + def setViewport(self, viewport): |
| 138 | + if not isinstance(viewport, TTkAbstractScrollViewInterface): |
| 139 | + raise TypeError("TTkAbstractScrollViewInterface is required in TTkAbstractScrollArea.setVewport(viewport)") |
| 140 | + if self._viewport: |
| 141 | + self._viewport.viewChanged.disconnect(self._viewportChanged) |
| 142 | + # TODO: Remove this check once |
| 143 | + # unified "addWidget" and "addItem" in the TTKGridLayout |
| 144 | + if isinstance(viewport, TTkWidget): |
| 145 | + self.layout().removeWidget(self._viewport) |
| 146 | + else: |
| 147 | + self.layout().removeItem(self._viewport) |
| 148 | + self._viewport = viewport |
| 149 | + self._viewport.viewChanged.connect(self._viewportChanged) |
| 150 | + self._verticalScrollBar.sliderMoved.connect(self._vscrollMoved) |
| 151 | + self._horizontalScrollBar.sliderMoved.connect(self._hscrollMoved) |
| 152 | + # TODO: Remove this check once |
| 153 | + # unified "addWidget" and "addItem" in the TTKGridLayout |
| 154 | + if isinstance(viewport, TTkWidget): |
| 155 | + self.layout().addWidget(viewport) |
| 156 | + else: |
| 157 | + self.layout().addItem(viewport) |
| 158 | + self._resizeEvent() |
| 159 | + |
| 160 | + def setVerticalScrollBarPolicy(self, policy): |
| 161 | + if policy != self._verticalScrollBarPolicy: |
| 162 | + self._verticalScrollBarPolicy = policy |
| 163 | + self._viewportChanged() |
| 164 | + |
| 165 | + def setHorizontalScrollBarPolicy(self, policy): |
| 166 | + if policy != self._horizontalScrollBarPolicy: |
| 167 | + self._horizontalScrollBarPolicy = policy |
| 168 | + self._viewportChanged() |
| 169 | + |
| 170 | + def viewport(self): |
| 171 | + return self._viewport |
| 172 | + |
| 173 | + def update(self, repaint=True, updateLayout=False, updateParent=False): |
| 174 | + if self._viewport: |
| 175 | + self._viewport.update(repaint, updateLayout, updateParent=False) |
| 176 | + return super().update(repaint, updateLayout, updateParent) |
0 commit comments