Quintiq file version 2.0
#parent: CustomDrawComponentSupplyChainVisualization/CustomDrawDataLayerSCVEdge
Response OnDrawObject (
  shadow[SCVEdge] object
) id:Response_CustomDrawComponentSupplyChainVisualization_CustomDrawDataLayerSCVEdge_OnDrawObject
{
  #keys: '[132894.0.877949609]'
  Body:
  [*
    // Draw PISPIPSCVEdge
    scvConfig := DataHolderSCVConfiguration.Data();
    
    node1 := select( scvConfig, SCVNode, n,
                     object.DestinationKey() = n.Key().AsQUILL() );
    
    node2 := select( scvConfig, SCVNode, n,
                     object.OriginKey() = n.Key().AsQUILL() );  
    
    sourceNode := ifexpr( node1.IsUpstream(), node1, node2 );
    targetNode := ifexpr( node1.IsUpstream(), node2, node1 );
    
    adjustmentX := scvConfig.NodeSize();
    adjustmentY := floor( scvConfig.NodeSize() / 2 );
    
    // Adjust line based on its orientations
    topdown := sourceNode.Y() + adjustmentY
               < targetNode.Y() + adjustmentY;
    leftright := sourceNode.X() + adjustmentX
                 < targetNode.X();
    padding := 6;
    
    fromy := region.Top() + padding;
    toy := region.Height() - padding;
    
    if( topdown )
    {
      if( not leftright )
      {
        fromy := region.Top() + padding;
        toy := region.Height() - padding;
      }
    }
    else
    {
      if( leftright )
      {
        fromy := region.Height() - padding;
        toy := region.Top() + padding;
      }
    }
    
    // Get line quantity and label
    quantity := 0.0;
    lineLabel := FormSupplyChainVisualization.ReturnEdgeLabel( sourceNode, targetNode, quantity );
    
    // Color the line gray if quantity is 0 or smaller
    lineColor := ColorScheme.SCVEdge();
    if( quantity <= 0.0 )
    {
      lineColor := ColorScheme.SCVNode();
      // Color the text gray too
      surface.TextColor( lineColor );
    }
    
    //Initialize drawing parameters
    surface.Pen().Color( lineColor )
                 .Width( 1 );
    surface.Brush().Color( lineColor );
    
    // Draw the directional line
    line := GraphicalLine::LineTo( region.Left(), fromy, region.Width(), toy );
    line.ArrowToEnd( true ).ArrowPos( GraphicalLine::ArrowPosMiddle() );
    surface.Draw( line );
    
    // Draw quantity label
    // Get rotation angle to rotate the text to the same angle as the line
    gradient := ( toy - fromy ) / ( region.Width() - region.Left() );
    degree := atan( -gradient ) * 180 / pi;
    // Get the mid length of the line, it is where the text should start, the reason being
    // rotation rotate with top left of the draw region as the corner. 
    radius := sqrt( sqr( [Real]region.Right() - region.Left() ) + sqr( [Real]region.Bottom() - region.Top() ) );
    
    surface.Font().Name( scvConfig.FontName() )
                  .Size( scvConfig.FontSize() )
                  .Angle( [Number]degree );
    
    textformat := TextFormat::Construct( TextFormat::AlignCenter() );
                            
    // Rotate the text to 
    if( sourceNode.Y() = targetNode.Y() )
    {
      textformat := TextFormat::Construct( TextFormat::AlignCentered() );
      surface.TextZoom( region.Left(), region.Top() - scvConfig.FontSize() * 2,
                        region.Width(), scvConfig.FontSize() * 2,
                        lineLabel, textformat );
    }
    else
    {
      // If targetnode and sourcenode have different Y positions,
      // it means there is a gradient and we need to rotate the 
      // text. The hardcoded offsets below are the sweet numbers
      // that we found that allows the text to have even distance to line
      // for any angle of rotation
      surface.TextZoom( region.Left(), ifexpr( gradient > 0, region.Top() - 10, region.Bottom() - 22 ),
                        [Number]radius, region.Height(),
                        lineLabel, textformat );    
    }
  *]
  CanBindMultiple: false
  DefinitionID: 'Responsedef_CustomDrawDataLayer_OnDrawObject'
}
