2024-09-22 22:24:35 +05:30
|
|
|
import Tools from "../../../canvas/constants/tools"
|
2024-09-27 19:22:33 +05:30
|
|
|
import { convertObjectToKeyValueString } from "../../../utils/common"
|
2024-09-28 21:50:30 +05:30
|
|
|
import { getPythonAssetPath } from "../../utils/pythonFilePath"
|
2024-09-27 19:22:33 +05:30
|
|
|
import { TkinterWidgetBase } from "./base"
|
2024-09-22 22:24:35 +05:30
|
|
|
|
|
|
|
|
|
2024-09-27 16:04:03 +05:30
|
|
|
class Label extends TkinterWidgetBase{
|
2024-09-22 22:24:35 +05:30
|
|
|
|
|
|
|
|
static widgetType = "label"
|
|
|
|
|
|
2024-09-28 21:50:30 +05:30
|
|
|
|
2024-09-22 22:24:35 +05:30
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
|
2024-09-23 12:31:01 +05:30
|
|
|
|
2024-09-22 22:24:35 +05:30
|
|
|
this.state = {
|
|
|
|
|
...this.state,
|
2024-09-27 16:04:03 +05:30
|
|
|
widgetName: "Label",
|
2024-09-22 22:24:35 +05:30
|
|
|
size: { width: 80, height: 40 },
|
|
|
|
|
attrs: {
|
2024-09-27 19:22:33 +05:30
|
|
|
...this.state.attrs,
|
2024-09-22 22:24:35 +05:30
|
|
|
labelWidget: {
|
|
|
|
|
label: "Text",
|
2024-09-27 11:42:34 +05:30
|
|
|
tool: Tools.INPUT,
|
2024-09-22 22:24:35 +05:30
|
|
|
toolProps: {placeholder: "text", maxLength: 100},
|
|
|
|
|
value: "Label",
|
|
|
|
|
onChange: (value) => this.setAttrValue("labelWidget", value)
|
2024-09-27 11:42:34 +05:30
|
|
|
},
|
|
|
|
|
imageUpload: {
|
|
|
|
|
label: "Image",
|
|
|
|
|
tool: Tools.UPLOADED_LIST,
|
|
|
|
|
toolProps: {filterOptions: ["image/jpg", "image/jpeg", "image/png"]},
|
|
|
|
|
value: "",
|
|
|
|
|
onChange: (value) => this.setAttrValue("imageUpload", value)
|
|
|
|
|
},
|
2024-09-22 22:24:35 +05:30
|
|
|
}
|
|
|
|
|
}
|
2024-09-27 16:04:03 +05:30
|
|
|
|
2024-09-22 22:24:35 +05:30
|
|
|
}
|
|
|
|
|
|
2024-09-26 23:16:43 +05:30
|
|
|
componentDidMount(){
|
|
|
|
|
super.componentDidMount()
|
2024-09-27 16:04:03 +05:30
|
|
|
|
|
|
|
|
|
2024-09-26 23:16:43 +05:30
|
|
|
this.setAttrValue("styling.backgroundColor", "#E4E2E2")
|
2024-09-27 16:04:03 +05:30
|
|
|
// this.setWidgetName("label") // Don't do this this causes issues while loading data
|
|
|
|
|
|
2024-09-26 23:16:43 +05:30
|
|
|
}
|
2024-09-26 11:59:24 +05:30
|
|
|
|
2024-09-28 21:50:30 +05:30
|
|
|
getImports(){
|
|
|
|
|
const imports = super.getImports()
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("imageUpload"))
|
|
|
|
|
imports.push("import os", "from PIL import Image, ImageTk", )
|
|
|
|
|
|
|
|
|
|
return imports
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRequirements(){
|
2024-09-29 19:18:55 +05:30
|
|
|
const requirements = super.getRequirements()
|
2024-09-28 21:50:30 +05:30
|
|
|
|
|
|
|
|
if (this.getAttrValue("imageUpload"))
|
|
|
|
|
requirements.push(`pillow`)
|
|
|
|
|
|
|
|
|
|
return requirements
|
|
|
|
|
}
|
2024-09-26 11:59:24 +05:30
|
|
|
|
2024-09-26 23:16:43 +05:30
|
|
|
generateCode(variableName, parent){
|
2024-09-26 11:59:24 +05:30
|
|
|
|
2024-09-28 21:50:30 +05:30
|
|
|
|
2024-09-26 23:16:43 +05:30
|
|
|
const labelText = this.getAttrValue("labelWidget")
|
2024-09-27 19:22:33 +05:30
|
|
|
const config = convertObjectToKeyValueString(this.getConfigCode())
|
2024-09-28 21:50:30 +05:30
|
|
|
const image = this.getAttrValue("imageUpload")
|
|
|
|
|
|
|
|
|
|
let labelInitialization = `tk.Label(master=${parent}, text="${labelText}")`
|
2024-09-27 19:22:33 +05:30
|
|
|
|
2024-09-28 21:50:30 +05:30
|
|
|
const code = []
|
|
|
|
|
|
|
|
|
|
if (image.name){
|
2024-09-29 19:17:11 +05:30
|
|
|
code.push(`${variableName}_img = Image.open(${getPythonAssetPath(image.name, "image")})`)
|
2024-09-28 21:50:30 +05:30
|
|
|
code.push(`${variableName}_img = ImageTk.PhotoImage(${variableName}_img)`)
|
|
|
|
|
labelInitialization = `tk.Label(master=${parent}, image="${variableName}_img", text="${labelText}")`
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 19:17:11 +05:30
|
|
|
// code.push("\n")
|
2024-09-28 21:50:30 +05:30
|
|
|
code.push(labelInitialization)
|
2024-09-26 23:16:43 +05:30
|
|
|
return [
|
2024-09-28 21:50:30 +05:30
|
|
|
...code,
|
2024-09-27 19:22:33 +05:30
|
|
|
`${variableName}.config(${config})`,
|
2024-09-26 23:16:43 +05:30
|
|
|
`${variableName}.${this.getLayoutCode()}`
|
|
|
|
|
]
|
2024-09-22 22:24:35 +05:30
|
|
|
}
|
|
|
|
|
|
2024-09-26 23:16:43 +05:30
|
|
|
|
2024-09-22 22:24:35 +05:30
|
|
|
getToolbarAttrs(){
|
2024-09-23 12:31:01 +05:30
|
|
|
const toolBarAttrs = super.getToolbarAttrs()
|
|
|
|
|
|
2024-09-22 22:24:35 +05:30
|
|
|
return ({
|
|
|
|
|
id: this.__id,
|
2024-09-23 12:31:01 +05:30
|
|
|
widgetName: toolBarAttrs.widgetName,
|
2024-09-22 22:24:35 +05:30
|
|
|
labelWidget: this.state.attrs.labelWidget,
|
2024-09-23 12:31:01 +05:30
|
|
|
size: toolBarAttrs.size,
|
2024-09-22 22:24:35 +05:30
|
|
|
|
|
|
|
|
...this.state.attrs,
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderContent(){
|
2024-09-28 21:50:30 +05:30
|
|
|
|
|
|
|
|
const image = this.getAttrValue("imageUpload")
|
2024-09-29 17:51:42 +05:30
|
|
|
|
2024-09-22 22:24:35 +05:30
|
|
|
return (
|
2024-09-29 17:51:42 +05:30
|
|
|
<div className="tw-w-flex tw-flex-col tw-w-full tw-content-start tw-h-full tw-rounded-md tw-overflow-hidden"
|
|
|
|
|
style={{
|
|
|
|
|
flexGrow: 1, // Ensure the content grows to fill the parent
|
|
|
|
|
minWidth: '100%', // Force the width to 100% of the parent
|
|
|
|
|
minHeight: '100%', // Force the height to 100% of the parent
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-09-27 16:04:03 +05:30
|
|
|
<div className="tw-p-2 tw-w-full tw-h-full tw-flex tw-place-content-center tw-place-items-center "
|
2024-09-29 17:51:42 +05:30
|
|
|
style={this.getInnerRenderStyling()}>
|
2024-09-22 22:24:35 +05:30
|
|
|
{/* {this.props.children} */}
|
2024-09-28 21:50:30 +05:30
|
|
|
{
|
|
|
|
|
image && (
|
|
|
|
|
<img src={image.previewUrl} className="tw-bg-contain tw-w-full tw-h-full" />
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-09-27 16:04:03 +05:30
|
|
|
<div className="" style={{color: this.getAttrValue("styling.foregroundColor")}}>
|
2024-09-22 22:24:35 +05:30
|
|
|
{this.getAttrValue("labelWidget")}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default Label
|