# MIT License # # Copyright (c) 2023 Eugenio Parodi # # 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. import os import TermTk as ttk from TermTk.TTkCore.canvas import TTkCanvas from .superobj.superwidgetmenubutton import SuperWidgetMenuButton class _MenuItem(ttk.TTkWidget): ''' Generic Control Button for the MenuEditor [Menu[+][x] ''' __slots__ = ('_text', '_lineEdit', '_autoResize', '_menuButton', '_superMenuButton', '_designer', # Signals 'closeClicked') def __init__(self, menuButton, designer, autoResize=True, *args, **kwargs): self._text = ttk.TTkString(menuButton.text()) self._menuButton = menuButton self._autoResize = autoResize self._designer = designer self._superMenuButton = SuperWidgetMenuButton.factoryGetSuperWidgetMenuButton(wid=menuButton, designer=self._designer) self.closeClicked = ttk.pyTTkSignal(_MenuItem) super().__init__(*args, **(kwargs|{'size':(self._text.termWidth()+7,1)})) self.processWidgetName(self._menuButton.name()) self._lineEdit = ttk.TTkLineEdit(parent=self, visible=False, text=self._text) self._lineEdit.returnPressed.connect(self._lineEdit.hide) self._lineEdit.textEdited.connect(self._textEdited) self._lineEdit.focusChanged.connect(self._lineEdit.setVisible) def processWidgetName(self, name): names = {w.name() for w in self._designer.getWidgets() if w is not self._menuButton} index = 1 className = name while className in names: className = f"{name}-{index}" index += 1 self._menuButton.setName(className) @ttk.pyTTkSlot(str) def _textEdited(self, text): self._text = text self._menuButton.setText(text) width = text.termWidth() if self._autoResize: self.resize(width+7,1) self._lineEdit.setGeometry(1,0,width,1) self.processWidgetName(f"menu_{text}") self._designer.weModified.emit() self.update() def mouseDoubleClickEvent(self, evt) -> bool: w,h = self.size() if evt.x <= w-7: self._lineEdit.setText(self._text) self._lineEdit.setGeometry(1,0,w-7,1) self._lineEdit.show() self._lineEdit.setFocus() return True def expandMenuItem(self): subMenuEditor = _SubMenuEditor(size=(20,10), menuButton=self._menuButton, designer=self._designer) subMenuEditor.itemsChanged.connect(self.update) wi = subMenuEditor.widgetItem() wi.setLayer(wi.LAYER1) w = self.width() ttk.TTkHelper.overlay(self, subMenuEditor, w-3, 0) def mouseReleaseEvent(self, evt) -> bool: w = self.width() if evt.x > w-4: self.closeClicked.emit(self) elif evt.x > w-7: self.expandMenuItem() else: self._designer.thingSelected.emit(self._menuButton,self._superMenuButton) return True def paintEvent(self, canvas: TTkCanvas): w = self.width() text = ( ttk.TTkString( "[", ttk.TTkColor.fg("#FFFF66"))+ ttk.TTkString( self._text, ttk.TTkColor.RST)) canvas.drawText(text=text) expandIcon = ">" if len(self._menuButton._submenu)> 0 else "+" text = ( ttk.TTkString( "[", ttk.TTkColor.fg("#AAAA44"))+ ttk.TTkString( expandIcon, ttk.TTkColor.fg("#00FF00"))+ ttk.TTkString( "][", ttk.TTkColor.fg("#AAAA44"))+ ttk.TTkString( "x", ttk.TTkColor.fg("#FF0000"))+ ttk.TTkString( "]", ttk.TTkColor.fg("#FFFF66"))) canvas.drawText(text=text, pos=(w-6,0)) class _SubMenuSpacer(ttk.TTkWidget): ''' Generic Control Splitter for the MenuEditor ---------[x] ''' __slots__ = ('closeClicked', '_menuButton') def __init__(self, menuSpacer, *args, **kwargs): self._menuButton = menuSpacer self.closeClicked = ttk.pyTTkSignal(_MenuItem) super().__init__(*args, **kwargs) def mouseReleaseEvent(self, evt) -> bool: w = self.width() if evt.x > w-4: self.closeClicked.emit(self) return True def paintEvent(self, canvas): w = self.width() canvas.drawText(pos=(0,0), text="-"*self.width()) text = ( ttk.TTkString( "[", ttk.TTkColor.fg("#AAAA44"))+ ttk.TTkString( "x", ttk.TTkColor.fg("#FF0000"))+ ttk.TTkString( "]", ttk.TTkColor.fg("#FFFF66"))) canvas.drawText(text=text, pos=(w-3,0)) class _SubMenuAreaWidget(ttk.TTkAbstractScrollView): ''' ┌────────┤Left├────────╥──────── │╿+╿[menu[>]┌──────────────────┐ │╽+╽