kiconnect/freecad/kiconnect/init_gui.py
2025-06-10 18:01:25 -07:00

80 lines
2.7 KiB
Python

import os
import sys
# temp hack to run kipy from source until 0.1.0 is looking ~
sys.path.insert(1, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', '.venv', 'lib', 'python3.13', 'site-packages'))
import FreeCADGui as Gui
import FreeCAD as App
from .commands import cmd_edit_prefs, cmd_new_pcb, cmd_reload, cmd_sync_from, cmd_sync_to
from . import settings
translate=App.Qt.translate
QT_TRANSLATE_NOOP=App.Qt.QT_TRANSLATE_NOOP
TRANSLATIONSPATH = os.path.join(os.path.dirname(__file__), "resources", "translations")
# Add translations path
Gui.addLanguagePath(TRANSLATIONSPATH)
Gui.updateLocale()
default_preferences = [
('Bool', 'debug_reload', True),
('Bool', 'prefs_toolbar', True),
('Float', 'default_thickness', 1.6),
]
class KiConnect(Gui.Workbench):
MenuText = translate("Workbench", "KiConnect")
ToolTip = translate("Workbench", "KiConnect PCB Workbench")
Icon = os.path.join(settings.ICONPATH, "kiconnect.svg")
toolbox = [ 'kiconn_new', 'kiconn_sync_to', 'kiconn_sync_from' ]
def GetClassName(self):
return "Gui::PythonWorkbench"
def Initialize(self):
"""
This function is called at the first activation of the workbench.
here is the place to import all the commands
"""
# setup default preferences on first load
if settings.preferences.IsEmpty():
for pref in default_preferences:
print('setting pref: ', f'Set{pref[0]}', pref[1], pref[2])
getattr(settings.preferences, f'Set{pref[0]}')(pref[1], pref[2])
# add debug reload button if enabled
if settings.preferences.GetBool('debug_reload'):
self.toolbox.append('kiconn_reload')
if settings.preferences.GetBool('prefs_toolbar'):
self.toolbox.append('cmd_edit_prefs')
print('setting up toolbar')
# NOTE: Context for this commands must be "Workbench"
self.appendToolbar(QT_TRANSLATE_NOOP("Workbench", "KiConnect"), self.toolbox)
self.appendMenu(QT_TRANSLATE_NOOP("Workbench", "KiConnect"), self.toolbox)
Gui.addPreferencePage(os.path.join(settings.UIPATH, 'preferences.ui'), 'KiConnect')
def Activated(self):
App.Console.PrintMessage(translate("Log", "Workbench KiConnect activated.") + "\n")
def Deactivated(self):
App.Console.PrintMessage(translate("Log", "Workbench KiConnect de-activated.") + "\n")
def ContextMenu(self, recipient):
boards = [sel for sel in Gui.Selection.getSelection() if sel.Type == 'KiConnect::Board']
if boards:
self.appendContextMenu("", "Separator")
self.appendContextMenu("", "kiconn_sync_from")
self.appendContextMenu("", "Separator")
Gui.addWorkbench(KiConnect())