2024-09-30 15:54:09 +05:30
|
|
|
import Tools from "../../../canvas/constants/tools"
|
2024-09-30 19:13:26 +05:30
|
|
|
import { convertObjectToKeyValueString, removeKeyFromObject } from "../../../utils/common"
|
2024-09-30 15:54:09 +05:30
|
|
|
import { getPythonAssetPath } from "../../utils/pythonFilePath"
|
2024-09-30 19:13:26 +05:30
|
|
|
import { CustomTkWidgetBase } from "./base"
|
2024-09-30 15:54:09 +05:30
|
|
|
|
|
|
|
|
|
2024-09-30 19:13:26 +05:30
|
|
|
class Label extends CustomTkWidgetBase{
|
2024-09-30 15:54:09 +05:30
|
|
|
|
|
|
|
|
static widgetType = "label"
|
2025-03-11 07:23:17 +05:30
|
|
|
static displayName = "Label"
|
2024-09-30 15:54:09 +05:30
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
|
2024-09-30 19:13:26 +05:30
|
|
|
// border color and width is not available for label in customtkinter
|
|
|
|
|
let newAttrs = removeKeyFromObject("styling.borderColor", this.state.attrs)
|
|
|
|
|
newAttrs = removeKeyFromObject("styling.borderWidth", newAttrs)
|
|
|
|
|
|
2024-09-30 15:54:09 +05:30
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
...this.state,
|
|
|
|
|
widgetName: "Label",
|
|
|
|
|
size: { width: 80, height: 40 },
|
|
|
|
|
attrs: {
|
2024-09-30 19:13:26 +05:30
|
|
|
...newAttrs,
|
2024-09-30 15:54:09 +05:30
|
|
|
labelWidget: {
|
|
|
|
|
label: "Text",
|
|
|
|
|
tool: Tools.INPUT,
|
|
|
|
|
toolProps: {placeholder: "text", maxLength: 100},
|
|
|
|
|
value: "Label",
|
|
|
|
|
onChange: (value) => this.setAttrValue("labelWidget", value)
|
|
|
|
|
},
|
|
|
|
|
imageUpload: {
|
|
|
|
|
label: "Image",
|
|
|
|
|
tool: Tools.UPLOADED_LIST,
|
|
|
|
|
toolProps: {filterOptions: ["image/jpg", "image/jpeg", "image/png"]},
|
|
|
|
|
value: "",
|
|
|
|
|
onChange: (value) => this.setAttrValue("imageUpload", value)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
|
|
|
super.componentDidMount()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setAttrValue("styling.backgroundColor", "#E4E2E2")
|
2024-09-30 19:13:26 +05:30
|
|
|
|
2024-09-30 15:54:09 +05:30
|
|
|
// this.setWidgetName("label") // Don't do this this causes issues while loading data
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getImports(){
|
|
|
|
|
const imports = super.getImports()
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("imageUpload"))
|
|
|
|
|
imports.push("import os", "from PIL import Image, ImageTk", )
|
|
|
|
|
|
|
|
|
|
return imports
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getRequirements(){
|
|
|
|
|
const requirements = super.getRequirements()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("imageUpload"))
|
|
|
|
|
requirements.push("pillow")
|
|
|
|
|
|
|
|
|
|
return requirements
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateCode(variableName, parent){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const labelText = this.getAttrValue("labelWidget")
|
2024-09-30 19:13:26 +05:30
|
|
|
// Border color and border width are not implemented for label
|
|
|
|
|
const {border_color, border_width, ...config} = this.getConfigCode()
|
2024-09-30 15:54:09 +05:30
|
|
|
const image = this.getAttrValue("imageUpload")
|
|
|
|
|
|
2024-09-30 22:18:08 +05:30
|
|
|
// console.log("Object: ", config)
|
2024-09-30 19:13:26 +05:30
|
|
|
|
|
|
|
|
let labelInitialization = `${variableName} = ctk.CTkLabel(master=${parent}, text="${labelText}")`
|
2024-09-30 15:54:09 +05:30
|
|
|
|
|
|
|
|
const code = []
|
|
|
|
|
|
|
|
|
|
if (image?.name){
|
|
|
|
|
code.push(`${variableName}_img = Image.open(${getPythonAssetPath(image.name, "image")})`)
|
|
|
|
|
code.push(`${variableName}_img = ImageTk.PhotoImage(${variableName}_img)`)
|
|
|
|
|
// code.push("\n")
|
2024-09-30 19:13:26 +05:30
|
|
|
labelInitialization = `${variableName} = ctk.CTkLabel(master=${parent}, image=${variableName}_img, text="${labelText}", compound=ctk.TOP)`
|
2024-09-30 15:54:09 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// code.push("\n")
|
|
|
|
|
code.push(labelInitialization)
|
|
|
|
|
return [
|
|
|
|
|
...code,
|
2024-09-30 19:13:26 +05:30
|
|
|
`${variableName}.configure(${convertObjectToKeyValueString(config)})`,
|
2024-09-30 15:54:09 +05:30
|
|
|
`${variableName}.${this.getLayoutCode()}`
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getToolbarAttrs(){
|
|
|
|
|
const toolBarAttrs = super.getToolbarAttrs()
|
|
|
|
|
|
|
|
|
|
return ({
|
|
|
|
|
id: this.__id,
|
|
|
|
|
widgetName: toolBarAttrs.widgetName,
|
|
|
|
|
labelWidget: this.state.attrs.labelWidget,
|
|
|
|
|
size: toolBarAttrs.size,
|
|
|
|
|
|
|
|
|
|
...this.state.attrs,
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-28 15:56:17 +05:30
|
|
|
getAnchorStyle = (anchor) => {
|
|
|
|
|
const anchorStyles = {
|
|
|
|
|
n: { justifyContent: 'center', alignItems: 'flex-start' },
|
|
|
|
|
s: { justifyContent: 'center', alignItems: 'flex-end' },
|
|
|
|
|
e: { justifyContent: 'flex-end', alignItems: 'center' },
|
|
|
|
|
w: { justifyContent: 'flex-start', alignItems: 'center' },
|
|
|
|
|
ne: { justifyContent: 'flex-end', alignItems: 'flex-start' },
|
|
|
|
|
se: { justifyContent: 'flex-end', alignItems: 'flex-end' },
|
|
|
|
|
nw: { justifyContent: 'flex-start', alignItems: 'flex-start' },
|
|
|
|
|
sw: { justifyContent: 'flex-start', alignItems: 'flex-end' },
|
|
|
|
|
center: { justifyContent: 'center', alignItems: 'center' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return anchorStyles[anchor] || anchorStyles["w"];
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 15:54:09 +05:30
|
|
|
renderContent(){
|
|
|
|
|
|
|
|
|
|
const image = this.getAttrValue("imageUpload")
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<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
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<div className="tw-p-2 tw-w-full tw-h-full tw-flex tw-place-content-center tw-place-items-center "
|
2025-03-22 11:11:19 +05:30
|
|
|
ref={this.styleAreaRef}
|
2024-09-30 15:54:09 +05:30
|
|
|
style={this.getInnerRenderStyling()}>
|
|
|
|
|
{/* {this.props.children} */}
|
|
|
|
|
{
|
|
|
|
|
image && (
|
|
|
|
|
<img src={image.previewUrl} className="tw-bg-contain tw-w-full tw-h-full" />
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-03-28 15:56:17 +05:30
|
|
|
<div className={`tw-flex ${!image ? "tw-w-full tw-h-full" : ""}`} style={{
|
|
|
|
|
color: this.getAttrValue("styling.foregroundColor"),
|
|
|
|
|
...this.getAnchorStyle(this.getAttrValue("styling.anchor"))
|
|
|
|
|
}}>
|
2024-09-30 15:54:09 +05:30
|
|
|
{this.getAttrValue("labelWidget")}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default Label
|