All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.vis.svg.SvgSvgElement.kt Maven / Gradle / Ivy

/*
 * Copyright (c) 2019. JetBrains s.r.o.
 * Use of this source code is governed by the MIT license that can be found in the LICENSE file.
 */

package jetbrains.datalore.vis.svg

import jetbrains.datalore.base.geometry.DoubleRectangle
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.base.observable.property.Property
import jetbrains.datalore.base.observable.property.WritableProperty
import jetbrains.datalore.vis.svg.SvgContainer.Companion.CLIP_PATH
import jetbrains.datalore.vis.svg.SvgContainer.Companion.OPACITY

class SvgSvgElement() : SvgStylableElement(), SvgContainer,
    SvgLocatable {

    companion object {
        val X: SvgAttributeSpec =
            SvgAttributeSpec.createSpec("x")
        val Y: SvgAttributeSpec =
            SvgAttributeSpec.createSpec("y")
        val WIDTH: SvgAttributeSpec =
            SvgAttributeSpec.createSpec(SvgConstants.WIDTH)
        val HEIGHT: SvgAttributeSpec =
            SvgAttributeSpec.createSpec(SvgConstants.HEIGHT)
        val VIEW_BOX: SvgAttributeSpec =
            SvgAttributeSpec.createSpec("viewBox")
    }

    override val elementName = "svg"

    override val bBox: DoubleRectangle
        get() = container().getPeer()!!.getBBox(this)

    constructor(width: Double, height: Double) : this() {

        setAttribute(WIDTH, width)
        setAttribute(HEIGHT, height)
    }

    fun setStyle(css: SvgCssResource) {
        children().add(SvgStyleElement(css))
    }

    fun x(): Property {
        return getAttribute(X)
    }

    fun y(): Property {
        return getAttribute(Y)
    }

    fun width(): Property {
        return getAttribute(WIDTH)
    }

    fun height(): Property {
        return getAttribute(HEIGHT)
    }

    fun viewBox(): Property {
        return getAttribute(VIEW_BOX)
    }

    fun viewBoxRect(): WritableProperty {
        return object : WritableProperty {
            override fun set(value: DoubleRectangle) {
                viewBox().set(ViewBoxRectangle(value))
            }

        }
    }

    override fun opacity(): Property {
        return getAttribute(OPACITY)
    }

    override fun clipPath(): Property {
        return getAttribute(CLIP_PATH)
    }

    override fun pointToTransformedCoordinates(point: DoubleVector): DoubleVector {
        return container().getPeer()!!.invertTransform(this, point)
    }

    override fun pointToAbsoluteCoordinates(point: DoubleVector): DoubleVector {
        return container().getPeer()!!.applyTransform(this, point)
    }


    class ViewBoxRectangle {
        private var myX: Double = 0.toDouble()
        private var myY: Double = 0.toDouble()
        private var myWidth: Double = 0.toDouble()
        private var myHeight: Double = 0.toDouble()

        constructor(x: Double, y: Double, width: Double, height: Double) {
            myX = x
            myY = y
            myWidth = width
            myHeight = height
        }

        constructor(rect: DoubleRectangle) {
            myX = rect.origin.x
            myY = rect.origin.y
            myWidth = rect.dimension.x
            myHeight = rect.dimension.y
        }

//        constructor(rect: Rectangle) {
//            myX = rect.origin.x
//            myY = rect.origin.y
//            myWidth = rect.dimension.x
//            myHeight = rect.dimension.y
//        }

        override fun toString(): String {
            return "$myX $myY $myWidth $myHeight"
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy