You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
268 lines
10 KiB
268 lines
10 KiB
|
3 years ago
|
# MIT License
|
||
|
|
#
|
||
|
|
# Copyright (c) 2023 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
|
||
|
|
#
|
||
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
|
# of this software and associated documentation files (the "Software"), to deal
|
||
|
|
# in the Software without restriction, including without limitation the rights
|
||
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
|
# copies of the Software, and to permit persons to whom the Software is
|
||
|
|
# furnished to do so, subject to the following conditions:
|
||
|
|
#
|
||
|
|
# The above copyright notice and this permission notice shall be included in all
|
||
|
|
# copies or substantial portions of the Software.
|
||
|
|
#
|
||
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
|
# SOFTWARE.
|
||
|
|
|
||
|
|
from random import randint
|
||
|
|
|
||
|
|
import TermTk as ttk
|
||
|
|
|
||
|
|
import ttkDesigner.app.superobj as so
|
||
|
3 years ago
|
from .superobj import SuperObject
|
||
|
3 years ago
|
|
||
|
3 years ago
|
class SuperWidget(ttk.TTkContainer):
|
||
|
|
__slots__ = ('_wid', '_superRootWidget', '_designer',
|
||
|
3 years ago
|
'superMoved', 'superResized')
|
||
|
3 years ago
|
def __init__(self, designer, wid, *args, **kwargs):
|
||
|
3 years ago
|
self.superMoved = ttk.pyTTkSignal(int,int)
|
||
|
|
self.superResized = ttk.pyTTkSignal(int,int)
|
||
|
3 years ago
|
self._designer = designer
|
||
|
3 years ago
|
self._wid = wid
|
||
|
|
self._wid.move(*kwargs['pos'])
|
||
|
|
self._wid._canvas.show()
|
||
|
|
self._superRootWidget = kwargs.get('superRootWidget',False)
|
||
|
|
kwargs['maxSize'] = wid.maximumSize()
|
||
|
|
kwargs['minSize'] = wid.minimumSize()
|
||
|
|
kwargs['size'] = wid.size()
|
||
|
|
super().__init__(*args, **kwargs)
|
||
|
|
#self.resize(*self._wid.size())
|
||
|
3 years ago
|
h,s,l = randint(0,359),100,randint(60,80)
|
||
|
3 years ago
|
r,g,b = ttk.TTkColor.hsl2rgb(((h+5)%360,s,l))
|
||
|
|
self._layoutColor = ttk.TTkColor.bg(f"#{r:02X}{g:02X}{b:02X}", modifier=ttk.TTkColorGradient(increment=+2))
|
||
|
|
r,g,b = ttk.TTkColor.hsl2rgb(((h+5)%360,s,l))
|
||
|
|
self._layoutPadColor = ttk.TTkColor.bg(f"#{r:02X}{g:02X}{b:02X}", modifier=ttk.TTkColorGradient(increment=-2))
|
||
|
|
self.setFocusPolicy(ttk.TTkK.ClickFocus)
|
||
|
|
SuperWidget.toggleHighlightLayout.connect(self._toggleHighlightLayout)
|
||
|
|
|
||
|
|
_showLayout = False
|
||
|
|
toggleHighlightLayout = ttk.pyTTkSignal(bool)
|
||
|
|
|
||
|
3 years ago
|
# TODO: Find a better way to handle this exception
|
||
|
|
# It may require some major rewrite
|
||
|
|
def hasControlWidget(self):
|
||
|
|
return True
|
||
|
|
|
||
|
3 years ago
|
@ttk.pyTTkSlot(str)
|
||
|
|
def setSuperName(self, name):
|
||
|
3 years ago
|
if name and name not in [w.name() for w in self._designer.getWidgets()]:
|
||
|
3 years ago
|
oldName = self._wid._name
|
||
|
3 years ago
|
self._wid.setName(name)
|
||
|
3 years ago
|
self._designer.widgetNameChanged.emit(oldName, name)
|
||
|
3 years ago
|
self._designer.weModified.emit()
|
||
|
3 years ago
|
|
||
|
3 years ago
|
def getSuperProperties(self):
|
||
|
3 years ago
|
additions = {}
|
||
|
3 years ago
|
exceptions = {
|
||
|
3 years ago
|
'Name': {
|
||
|
|
'get': { 'cb':ttk.TTkWidget.name, 'type':str} ,
|
||
|
|
'set': { 'cb':lambda _,n: self.setSuperName(n), 'type':str} },
|
||
|
3 years ago
|
}
|
||
|
|
exclude = []
|
||
|
3 years ago
|
return additions, exceptions, exclude
|
||
|
3 years ago
|
|
||
|
3 years ago
|
@ttk.pyTTkSlot(bool)
|
||
|
|
def _toggleHighlightLayout(self, state):
|
||
|
|
SuperWidget._showLayout = state
|
||
|
|
self.update()
|
||
|
|
|
||
|
|
def dumpDict(self):
|
||
|
|
wid = self._wid
|
||
|
|
ret = {
|
||
|
|
'class' : wid.__class__.__name__,
|
||
|
3 years ago
|
'params' : SuperObject.dumpParams(wid),
|
||
|
3 years ago
|
}
|
||
|
|
return ret
|
||
|
|
|
||
|
3 years ago
|
@staticmethod
|
||
|
3 years ago
|
def _swFromWidget(wid, swClass, *args, **kwargs):
|
||
|
|
sw = swClass(wid=wid, *args, **kwargs)
|
||
|
3 years ago
|
return sw
|
||
|
|
|
||
|
3 years ago
|
@staticmethod
|
||
|
3 years ago
|
def swFromWidget(wid:object, *args, **kwargs):
|
||
|
|
swClass = so.SuperWidget
|
||
|
|
for c, sc in {
|
||
|
2 years ago
|
# ttk.TTkTextEdit: so.SuperWidgetTextEdit,
|
||
|
3 years ago
|
ttk.TTkRadioButton: so.SuperWidgetRadioButton,
|
||
|
3 years ago
|
# ttk.TTkResizableFrame: so.SuperWidgetFrame,
|
||
|
|
# ttk.TTkWindow: so.SuperWidgetFrame,
|
||
|
|
ttk.TTkSplitter: so.SuperWidgetSplitter,
|
||
|
2 years ago
|
# ttk.TTkList: so.SuperWidgetList,
|
||
|
3 years ago
|
ttk.TTkMenuButton: so.SuperWidgetMenuButton,
|
||
|
3 years ago
|
ttk.TTkFrame: so.SuperWidgetFrame,
|
||
|
2 years ago
|
ttk.TTkAbstractScrollArea: so.SuperWidgetAbstractScrollArea,
|
||
|
3 years ago
|
ttk.TTkContainer: so.SuperWidgetContainer,
|
||
|
2 years ago
|
ttk.TTkWidget: so.SuperWidget,
|
||
|
3 years ago
|
}.items():
|
||
|
|
if c in type(wid).mro():
|
||
|
|
swClass = sc
|
||
|
|
break
|
||
|
|
return swClass._swFromWidget(wid=wid, swClass=swClass, *args, **kwargs)
|
||
|
3 years ago
|
|
||
|
3 years ago
|
@staticmethod
|
||
|
3 years ago
|
def loadDict(designer, parent, widProp):
|
||
|
3 years ago
|
ttkClass = getattr(ttk,widProp['class'])
|
||
|
|
if issubclass(ttkClass,ttk.TTkLayout):
|
||
|
|
demiProp = {
|
||
|
1 year ago
|
'type': ttk.TTkUiSignature,
|
||
|
|
'version':'2.1.0',
|
||
|
3 years ago
|
'tui':{
|
||
|
|
'class' : widProp['class'],
|
||
|
|
'params' : widProp['params'],
|
||
|
|
'children' : []
|
||
|
|
},
|
||
|
|
'connections':[]
|
||
|
|
}
|
||
|
|
layout = ttk.TTkUiLoader.loadDict(demiProp)
|
||
|
|
x,y,w,h = layout.geometry()
|
||
|
3 years ago
|
sl = sup = so.SuperLayout.slFromLayout(designer=designer, layout=layout, lay=ttk.TTkLayout(), pos=(x,y), size=(w,h), selectable=True)
|
||
|
3 years ago
|
children = widProp['children']
|
||
|
3 years ago
|
elif issubclass(ttkClass,ttk.TTkContainer):
|
||
|
3 years ago
|
setLayout = 'layout' in widProp
|
||
|
|
setMenuBar = 'menuBar' in widProp
|
||
|
3 years ago
|
tui = {
|
||
|
|
'class' : widProp['class'],
|
||
|
|
'params' : widProp['params'] }
|
||
|
|
tui |= {
|
||
|
|
'layout': {
|
||
|
|
'class' : widProp['layout']['class'],
|
||
|
|
'params' : widProp['layout']['params'],
|
||
|
|
'children' : []
|
||
|
|
} } if setLayout else {}
|
||
|
3 years ago
|
tui |= {
|
||
|
|
'menuBar': widProp['menuBar']
|
||
|
|
} if setMenuBar else {}
|
||
|
3 years ago
|
demiProp = {
|
||
|
1 year ago
|
'type': ttk.TTkUiSignature,
|
||
|
|
'version':'2.1.0',
|
||
|
3 years ago
|
'tui': tui,
|
||
|
3 years ago
|
'connections':[]
|
||
|
|
}
|
||
|
|
wid = ttk.TTkUiLoader.loadDict(demiProp)
|
||
|
3 years ago
|
sup = so.SuperWidgetContainer.swFromWidget(designer=designer, wid=wid, pos=wid.pos())
|
||
|
3 years ago
|
sl = sup._superLayout
|
||
|
3 years ago
|
children = widProp['layout']['children'] if setLayout else []
|
||
|
3 years ago
|
else:
|
||
|
|
setMenuBar = 'menuBar' in widProp
|
||
|
|
tui = {
|
||
|
|
'class' : widProp['class'],
|
||
|
|
'params' : widProp['params'] }
|
||
|
|
demiProp = {
|
||
|
1 year ago
|
'type': ttk.TTkUiSignature,
|
||
|
|
'version':'2.1.0',
|
||
|
3 years ago
|
'tui': tui,
|
||
|
|
'connections':[]
|
||
|
|
}
|
||
|
|
wid = ttk.TTkUiLoader.loadDict(demiProp)
|
||
|
|
sup = so.SuperWidget.swFromWidget(designer=designer, wid=wid, pos=wid.pos())
|
||
|
|
children = []
|
||
|
3 years ago
|
|
||
|
|
for ch in children:
|
||
|
3 years ago
|
sch = SuperWidget.loadDict(designer, parent, ch)
|
||
|
3 years ago
|
if issubclass(type(sch),so.SuperLayout):
|
||
|
|
schl = sch
|
||
|
3 years ago
|
elif issubclass(type(sch),so.SuperWidgetContainer):
|
||
|
3 years ago
|
schl = sch._superLayout
|
||
|
3 years ago
|
else:
|
||
|
|
schl = None
|
||
|
3 years ago
|
|
||
|
3 years ago
|
if issubclass(type(sl),so.SuperLayoutGrid):
|
||
|
|
sl.layout().addWidget(sch,ch['row'],ch['col'],ch['rowspan'],ch['colspan'])
|
||
|
3 years ago
|
if schl:
|
||
|
|
schl.setDropBorder(1)
|
||
|
3 years ago
|
else:
|
||
|
|
sl.layout().addWidget(sch)
|
||
|
3 years ago
|
if schl:
|
||
|
|
schl.setDropBorder(0)
|
||
|
3 years ago
|
return sup
|
||
|
|
|
||
|
3 years ago
|
def updateAll(self):
|
||
|
|
self.resize(*(self._wid.size()))
|
||
|
|
self.move(*(self._wid.pos()))
|
||
|
|
self.setMaximumSize(*(self._wid.maximumSize()))
|
||
|
|
self.setMinimumSize(*(self._wid.minimumSize()))
|
||
|
|
self.update()
|
||
|
|
|
||
|
|
def mousePressEvent(self, evt) -> bool:
|
||
|
|
return True
|
||
|
|
|
||
|
3 years ago
|
def isRootWidget(self):
|
||
|
|
return self._superRootWidget
|
||
|
|
|
||
|
3 years ago
|
def makeRootWidget(self):
|
||
|
|
self._superRootWidget = True
|
||
|
|
|
||
|
3 years ago
|
def pushSuperControlWidget(self):
|
||
|
3 years ago
|
# if self._superRootWidget: return False
|
||
|
3 years ago
|
scw = so.SuperControlWidget(self)
|
||
|
|
ttk.TTkHelper.removeOverlay()
|
||
|
|
ttk.TTkHelper.overlay(self, scw, -1,-1, forceBoundaries=False)
|
||
|
|
|
||
|
|
def mouseReleaseEvent(self, evt) -> bool:
|
||
|
2 years ago
|
w,h = self.size()
|
||
|
|
if 0<=evt.x<w and 0<=evt.y<h:
|
||
|
|
self.pushSuperControlWidget()
|
||
|
|
self._designer.thingSelected.emit(self._wid,self)
|
||
|
3 years ago
|
return True
|
||
|
|
|
||
|
|
def mouseDragEvent(self, evt) -> bool:
|
||
|
|
if self._superRootWidget: return False
|
||
|
|
drag = ttk.TTkDrag()
|
||
|
|
data = self
|
||
|
|
data.paintChildCanvas()
|
||
|
1 year ago
|
drag.setHotSpot(pos=(evt.x, evt.y))
|
||
|
3 years ago
|
drag.setPixmap(data.getCanvas())
|
||
|
|
drag.setData(data)
|
||
|
|
drag.exec()
|
||
|
3 years ago
|
self.parentWidget().removeSuperWidget(self)
|
||
|
3 years ago
|
return True
|
||
|
|
|
||
|
3 years ago
|
def superChild(self):
|
||
|
|
return self._wid
|
||
|
|
|
||
|
3 years ago
|
def move(self, x: int, y: int):
|
||
|
|
self._wid.move(x,y)
|
||
|
|
self.update()
|
||
|
3 years ago
|
self.superMoved.emit(x,y)
|
||
|
3 years ago
|
return super().move(x, y)
|
||
|
|
|
||
|
|
def resizeEvent(self, w, h):
|
||
|
|
self._wid.resize(w,h)
|
||
|
|
self._wid._canvas.updateSize()
|
||
|
3 years ago
|
self.superResized.emit(w,h)
|
||
|
3 years ago
|
return super().resizeEvent(w, h)
|
||
|
|
|
||
|
3 years ago
|
def paintEvent(self, canvas):
|
||
|
3 years ago
|
w,h = self.size()
|
||
|
|
if SuperWidget._showLayout:
|
||
|
|
for y in range(h):
|
||
|
3 years ago
|
canvas.drawText(pos=(0,y),text='',width=w,color=self._layoutColor)
|
||
|
3 years ago
|
for y in range(0,h):
|
||
|
|
canvas.drawText(pos=(0,y),text='',width=w,color=self._layoutPadColor)
|
||
|
3 years ago
|
# canvas.fill(color=self._layoutColor)
|
||
|
|
# canvas.fill(pos=(l,t), size=(w-r-l,h-b-t), color=self._layoutPadColor)
|
||
|
3 years ago
|
else:
|
||
|
3 years ago
|
self._wid.getCanvas().updateSize()
|
||
|
3 years ago
|
self._wid.paintEvent(self._wid.getCanvas())
|
||
|
3 years ago
|
canvas.paintCanvas(
|
||
|
3 years ago
|
self._wid.getCanvas(),
|
||
|
|
( 0, 0, w, h), # geometry
|
||
|
|
( 0, 0, w, h), # slice
|
||
|
|
( 0, 0, w, h)) # bound
|