working on code generation

This commit is contained in:
paul
2024-09-26 11:59:24 +05:30
parent 37e7bea0fa
commit e34751c20c
13 changed files with 252 additions and 108 deletions

View File

@@ -0,0 +1,33 @@
import MainWindow from "../widgets/mainWindow"
import { message } from "antd"
async function generateTkinterCode(projectName, widgetList=[], widgetRefs=[]){
console.log("widgetList and refs", projectName, widgetList, widgetRefs)
let mainWindowCount = 0
for (let widget of widgetList){
if (widget.widgetType === MainWindow){
mainWindowCount += 1
}
if (mainWindowCount > 1){
message.error("Multiple instances of Main window found, delete one and try again.")
return
}
}
if (mainWindowCount === 0){
message.error("Aborting. No instances of Main window found. Add one and try again")
}
}
export default generateTkinterCode

View File

@@ -1,16 +1,46 @@
import { Layouts, PosType } from "../../../canvas/constants/layouts"
import Tools from "../../../canvas/constants/tools";
import Widget from "../../../canvas/widgets/base";
import Tools from "../../../canvas/constants/tools"
import Widget from "../../../canvas/widgets/base"
class TkinterBase extends Widget {
setParentLayout(layout){
static requiredImports = ['import tkinter as tk']
constructor(props) {
super(props)
this.getLayoutCode = this.getLayoutCode.bind(this)
}
getLayoutCode(){
const {layout: parentLayout, direction, gap} = this.getParentLayout()
let layoutManager = `pack()`
if (parentLayout === Layouts.FLEX){
layoutManager = `pack(${direction === "horizontal"? "tk.LEFT" : "tk.TOP"})`
}else if (parentLayout === Layouts.GRID){
const row = this.getAttrValue("gridManager.row")
const col = this.getAttrValue("gridManager.col")
layoutManager = `grid(row=${row}, col=${col})`
}else{
// FIXME: position may not be correct
layoutManager = `place(x=${this.state.pos.x}, y=${this.state.pos.y})`
}
return layoutManager
}
setParentLayout(parentLayout){
console.log("parent layout: ", parentLayout)
const {layout, direction, gap} = parentLayout
// show attributes related to the layout manager
let updates = {
parentLayout: layout,
parentLayout: parentLayout,
}
this.removeAttr("gridManager")
@@ -21,7 +51,7 @@ class TkinterBase extends Widget {
positionType: PosType.NONE
}
if (layout === Layouts.GRID) {
if (parentLayout === Layouts.GRID) {
// Set attributes related to grid layout manager
updates = {
...updates,
@@ -145,14 +175,14 @@ class TkinterBase extends Widget {
parentLayout: parentLayout
}
if (parentLayout === Layouts.FLEX || parentLayout === Layouts.GRID){
if (parentLayout.layout === Layouts.FLEX || parentLayout.layout === Layouts.GRID){
layoutUpdates = {
...layoutUpdates,
positionType: PosType.NONE
}
}else if (parentLayout === Layouts.PLACE){
}else if (parentLayout.layout === Layouts.PLACE){
layoutUpdates = {
...layoutUpdates,
positionType: PosType.ABSOLUTE

View File

@@ -10,7 +10,7 @@ class Frame extends TkinterBase{
super(props)
this.droppableTags = {
exclude: ["image", "video", "media", "toplevel"]
exclude: ["image", "video", "media", "toplevel", "main_window"]
}
this.state = {

View File

@@ -2,6 +2,7 @@ import Widget from "../../../canvas/widgets/base"
import Tools from "../../../canvas/constants/tools"
import { removeKeyFromObject } from "../../../utils/common"
import TkinterBase from "./base"
import { Layouts } from "../../../canvas/constants/layouts"
class Label extends TkinterBase{
@@ -45,6 +46,16 @@ class Label extends TkinterBase{
}
}
generateCode(parent){
const label = this.getAttrValue("labelWidget")
return (`
${this.getWidgetName()} = tk.Label(master=${parent}, text="${label}")
${this.getWidgetName()}.${this.getLayoutCode()}
`)
}
componentDidMount(){
super.componentDidMount()
this.setAttrValue("styling.backgroundColor", "#fff0")

View File

@@ -36,6 +36,13 @@ class MainWindow extends Widget{
this.setWidgetName("main")
}
generateCode(parent){
return (`
${this.getWidgetName()} = tk.Tk()
`)
}
getToolbarAttrs(){
const toolBarAttrs = super.getToolbarAttrs()