# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2021.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

from qiskit_metal import draw, Dict
from qiskit_metal.toolbox_metal import math_and_overrides
from qiskit_metal.qlibrary.core import QComponent

dir(QComponent)

class MyQComponent(QComponent):
    """
    Use this class as a template for your components - have fun
    
    Description:
    
    Options:
    """

    # Edit these to define your own tempate options for creation
    # Default drawing options
    default_options = Dict(width='500um',
                           height='300um',
                           pos_x='0um',
                           pos_y='0um',
                           orientation='0',
                           layer='1')
    """Default drawing options"""

    # Name prefix of component, if user doesn't provide name
    component_metadata = Dict(short_name='component',
                             _qgeometry_table_poly='True')
    """Component metadata"""
    
    def make(self):
        """Convert self.options into QGeometry."""

        p = self.parse_options()  # Parse the string options into numbers

        # EDIT HERE - Replace the following with your code
        # Create some raw geometry
        # Use autocompletion for the `draw.` module (use tab key)
        rect = draw.rectangle(p.width, p.height, p.pos_x, p.pos_y)
        
        rect = draw.rotate(rect, p.orientation)
        rect = draw.translate(rect,p.pos_x,p.pos_y)
        
        geom = {'my_polygon': rect}
        self.add_qgeometry('poly', geom, layer=p.layer, subtract=False)

draw.rectangle(1,2,0,0)

draw.rotate(draw.rectangle(1,2,0,0), 45)

face = draw.shapely.geometry.Point(0, 0).buffer(1)
eye = draw.shapely.geometry.Point(0, 0).buffer(0.2)
eye_l = draw.translate(eye, -0.4, 0.4)
eye_r = draw.translate(eye, 0.4, 0.4)

smile = draw.shapely.geometry.Point(0, 0).buffer(0.8)
cut_sq = draw.shapely.geometry.box(-1, -0.3, 1, 1)
smile = draw.subtract(smile, cut_sq)
face = draw.subtract(face, smile)
face = draw.subtract(face, eye_r)
face = draw.subtract(face, eye_l)
face

import qiskit_metal as metal
?metal.qlibrary.core.QComponent.add_qgeometry

?metal.qlibrary.core.QComponent.add_pin

from qiskit_metal import draw, Dict
from qiskit_metal.toolbox_metal import math_and_overrides
from qiskit_metal.qlibrary.core import QComponent

class MySimpleGapCapacitor(QComponent):
    """
    Inherits 'QComponent' class.
    
    Description:
        A simple CPW style gap capacitor, with endcap islands each coupled to their own
        cpw transmission line that ends in a pin.
    
    Options:
        * cpw_width: width of the cpw trace of the transmission line
        * cpw_gap: dielectric gap of the cpw transmission line
        * cap_width: width of the gap capacitor (size of the charge islands)
        * cap_gap: dielectric space between the two islands
        * pos_x/_y: position of the capacitor on chip
        * orientation: 0-> is parallel to x-axis, with orientation (in degrees) counterclockwise.
        * layer: the layer number for the layout 
        
    
    """

    # Edit these to define your own tempate options for creation
    # Default drawing options
    default_options = Dict(cpw_width='15um',
                           cpw_gap='9um',
                           cap_width='35um',
                           cap_gap='3um',
                           pos_x='0um',
                           pos_y='0um',
                           orientation='0',
                           layer='1')
    """Default drawing options"""

    # Name prefix of component, if user doesn't provide name
    component_metadata = Dict(short_name='component', 
                             _qgeometry_table_poly='True',
                             _qgeometry_table_path='True')
    """Component metadata"""
    
    def make(self):
        """Convert self.options into QGeometry."""

        p = self.parse_options()  # Parse the string options into numbers

        pad = draw.rectangle(p.cpw_width, p.cap_width, 0, 0)
        pad_left = draw.translate(pad,-(p.cpw_width+p.cap_gap)/2,0)
        pad_right = draw.translate(pad,(p.cpw_width+p.cap_gap)/2,0)
        pad_etch = draw.rectangle(2*p.cpw_gap+2*p.cpw_width+p.cap_gap,2*p.cpw_gap+p.cap_width)
        cpw_left = draw.shapely.geometry.LineString([[-(p.cpw_width+p.cap_gap/2),0],[-(p.cpw_width*3 +p.cap_gap/2),0]])
        cpw_right = draw.shapely.geometry.LineString([[(p.cpw_width+p.cap_gap/2),0],[(p.cpw_width*3 +p.cap_gap/2),0]])
        
        geom_list = [pad_left,pad_right,cpw_left,cpw_right,pad_etch]
        geom_list = draw.rotate(geom_list,p.orientation)
        geom_list = draw.translate(geom_list,p.pos_x,p.pos_y)
        [pad_left,pad_right,cpw_left,cpw_right,pad_etch] = geom_list

        self.add_qgeometry('path', {'cpw_left':cpw_left, 'cpw_right':cpw_right}, layer=p.layer, width = p.cpw_width)
        self.add_qgeometry('path', {'cpw_left_etch':cpw_left, 'cpw_right_etch':cpw_right}, layer=p.layer, width = p.cpw_width+2*p.cpw_gap, subtract=True)
        self.add_qgeometry('poly', {'pad_left':pad_left, 'pad_right':pad_right}, layer=p.layer)
        self.add_qgeometry('poly', {'pad_etch':pad_etch}, layer=p.layer, subtract=True)
        
        self.add_pin('cap_left', cpw_left.coords, width = p.cpw_width, gap = p.cpw_gap, input_as_norm=True)
        self.add_pin('cap_right', cpw_right.coords, width = p.cpw_width, gap = p.cpw_gap, input_as_norm=True)

design = metal.designs.DesignPlanar()

gui = metal.MetalGUI(design)

my_cap = MySimpleGapCapacitor(design,'my_cap')
gui.rebuild()

design.overwrite_enabled = True

gui.main_window.close()