
/*

  KLayout Layout Viewer
  Copyright (C) 2006-2016 Matthias Koefferlein

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/



#ifndef HDR_layMarker
#define HDR_layMarker

#include "config.h"

#include "layViewObject.h"
#include "layViewOp.h"
#include "dbLayout.h"
#include "dbShape.h"
#include "dbInstances.h"
#include "dbBox.h"
#include "dbPath.h"
#include "dbPolygon.h"
#include "dbText.h"
#include "dbEdge.h"
#include "dbEdgePair.h"
#include "dbArray.h"
#include "gsi.h"

#include <QtGui/QColor>

namespace lay
{

class LayoutView;

/**
 *  @brief The marker base class
 *
 *  This base class defines the properties common to all marker objects
 */

class KLAYOUT_DLL MarkerBase
  : public lay::ViewObject
{
public: 
  /** 
   *  @brief The constructor 
   */ 
  MarkerBase (lay::LayoutView *view);

  /**
   *  @brief Get the color by which the marker is drawn
   *
   *  If the color is invalid, the marker is drawn with the canvases foreground color.
   */
  QColor get_color () const
  {
    return m_color;
  }

  /**
   *  @brief Set the color by which the marker is drawn
   *
   *  If the color is invalid, the marker is drawn with the canvases foreground color.
   */
  void set_color (QColor color);

  /**
   *  @brief Get the color by which the marker's frame is drawn
   *
   *  If the color is invalid, the marker's frame is drawn with the fill color.
   */
  QColor get_frame_color () const
  {
    return m_frame_color;
  }

  /**
   *  @brief Set the frame color by which the marker is drawn
   *
   *  If the color is invalid, the marker's frame is drawn with the fill color.
   */
  void set_frame_color (QColor color);

  /**
   *  @brief Get the line width with which the marker is drawn
   */
  int get_line_width () const
  {
    return m_line_width;
  }

  /**
   *  @brief Set the line width with which the marker is drawn
   *
   *  The maximum line width is 15 and 13 if a halo is enabled.
   */
  void set_line_width (int lw);

  /**
   *  @brief Get the vertex size with which the marker is drawn
   */
  int get_vertex_size () const
  {
    return m_vertex_size;
  }

  /**
   *  @brief Set the vertex size with which the marker is drawn
   *
   *  The maximum vertex size is 15 and 13 if a halo is enabled.
   */
  void set_vertex_size (int vs);

  /**
   *  @brief Get the vertex shape with which the marker is drawn
   */
  lay::ViewOp::Shape get_vertex_shape () const
  {
    return m_vertex_shape;
  }

  /**
   *  @brief Set the vertex shape with which the marker is drawn
   */
  void set_vertex_shape (lay::ViewOp::Shape);

  /**
   *  @brief Get the frame pattern index for the marker
   */
  int get_frame_pattern () const
  {
    return m_frame_pattern;
  }

  /**
   *  @brief Set the framee pattern index for the marker
   *
   *  The default pattern is 0 (solid)
   */
  void set_frame_pattern (int index);

  /**
   *  @brief Get the stipple pattern index for the marker
   */
  int get_dither_pattern () const
  {
    return m_dither_pattern;
  }

  /**
   *  @brief Set the stipple pattern index for the marker
   *
   *  A pattern index of -1 indicates that no pattern is set.
   */
  void set_dither_pattern (int index);

  /**
   *  @brief Get the halo flag
   *
   *  See "set_halo" for a description of the halo flag.
   */
  int get_halo () const
  {
    return m_halo;
  }

  /**
   *  @brief Set the halo flag
   *
   *  If the halo is enabled (1), a 1 pixel halo is drawn around that marker this giving
   *  it a better visibility in front of complex drawing. If disabled (0), no halo is drawn.
   *  A negative value (-1) instructs the marker to use the global setting.
   */
  void set_halo (int halo);

  /**
   *  @brief Gets the bounding box
   */
  virtual db::DBox bbox () const = 0;

protected:
  void get_bitmaps (const Viewport &vp, ViewObjectCanvas &canvas, lay::CanvasPlane *&fill, lay::CanvasPlane *&frame, lay::CanvasPlane *&vertex, lay::CanvasPlane *&text);

  QColor m_color;
  QColor m_frame_color;
  char m_line_width, m_vertex_size, m_halo;
  lay::ViewOp::Shape m_vertex_shape;
  short m_dither_pattern, m_frame_pattern;
  lay::LayoutView *mp_view;
};

/**
 *  @brief The generic marker object
 *
 *  This marker object is the base for shape and instance markers. 
 *  This object is the base for specialized markers, i.e. instance and shape markers.
 *  The basic functionality is to keep a set of transformations.
 *  certain instance object in the layout. It is given an instance reference
 *  thus pointing to a instance rather than keeping a copy of it.
 */

class KLAYOUT_DLL GenericMarkerBase
  : public MarkerBase
{
public: 
  /** 
   *  @brief The constructor 
   */ 
  GenericMarkerBase (lay::LayoutView *view, unsigned int cv_index);

  /**
   *  @brief The destructor
   */
  ~GenericMarkerBase ();

  /**
   *  @brief Set the transformation
   */
  void set (const db::CplxTrans &trans);

  /**
   *  @brief Set a transformation and a vector of relative transformations
   */
  void set (const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /** 
   *  @brief Obtain the global transformation
   */
  const db::CplxTrans &trans () const
  {
    return m_trans;
  }

  /** 
   *  @brief Obtain the transformation vector (0 if none is set)
   */
  const std::vector<db::CplxTrans> *trans_vector () const
  {
    return mp_trans_vector;
  }

  /**
   *  @brief Get the cellview index that this marker refers to
   */
  unsigned int cv_index () const
  {
    return m_cv_index;
  }

  /**
   *  @brief Gets the bounding box
   */
  db::DBox bbox () const;

protected:
  db::CplxTrans m_trans;
  std::vector<db::CplxTrans> *mp_trans_vector;
  lay::LayoutView *mp_view;
  unsigned int m_cv_index;

  /**
   *  @brief Gets the item's box
   */
  virtual db::DBox item_bbox () const = 0;
};

/**
 *  @brief The shape marker object
 *
 *  The marker is a visual object that "marks" (highlights) a 
 *  certain shape object in the layout.
 */

class KLAYOUT_DLL ShapeMarker
  : public GenericMarkerBase
{
public: 
  /** 
   *  @brief The constructor 
   */ 
  ShapeMarker (lay::LayoutView *view, unsigned int cv_index);

  /**
   *  @brief The destructor
   */
  ~ShapeMarker ();

  /**
   *  @brief Set the shape the marker is to display
   *
   *  The marker just stores the shape which is a proxy. The actual object
   *  must be stored somewhere else.
   */
  void set (const db::Shape &shape, const db::CplxTrans &trans);

  /**
   *  @brief Set the shape the marker is to display
   *
   *  The marker just stores the shape which is a proxy. The actual object
   *  must be stored somewhere else.
   */
  void set (const db::Shape &shape, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

private:
  virtual void render (const Viewport &vp, ViewObjectCanvas &canvas);

  virtual db::DBox item_bbox () const;

  db::Shape m_shape; 
};

/**
 *  @brief The instance marker object
 *
 *  The marker is a visual object that "marks" (highlights) a 
 *  certain instance object in the layout. It is given an instance reference
 *  thus pointing to a instance rather than keeping a copy of it.
 */

class KLAYOUT_DLL InstanceMarker
  : public GenericMarkerBase
{
public: 
  /** 
   *  @brief The constructor 
   *
   *  @param view The view the marker is intended for
   *  @param cv_index The cell view index of the layout that this marker is intended for
   *  @param draw_outline True to have instances drawing their outline
   *  @param max_shapes The maximum number of shapes to draw for instances (just a box is drawn if more shapes are present)
   */ 
  InstanceMarker (lay::LayoutView *view, unsigned int cv_index, bool draw_outline = true, size_t max_shapes = 0);

  /**
   *  @brief The destructor
   */
  ~InstanceMarker ();

  /**
   *  @brief Set the instance the marker is to display
   *
   *  The marker just stores the instance which is a proxy. The actual object
   *  must be stored somewhere else.
   */
  void set (const db::Instance &inst, const db::CplxTrans &trans);

  /**
   *  @brief Set the instance the marker is to display
   *
   *  The marker just stores the instance which is a proxy. The actual object
   *  must be stored somewhere else.
   */
  void set (const db::Instance &inst, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the outline drawing flag (for instances)
   */
  void set_draw_outline (bool d);

  /**
   *  @brief Get The outline drawing flag
   */
  bool draw_outline () const
  {
    return m_draw_outline;
  }

  /**
   *  @brief Set the maximum number of shapes to draw to instances
   */
  void set_max_shapes (size_t s);

  /**
   *  @brief Get the maximum number of shapes to draw to instances
   */
  size_t max_shapes () const
  {
    return m_max_shapes;
  }

private:
  virtual void render (const Viewport &vp, ViewObjectCanvas &canvas);

  virtual db::DBox item_bbox () const;

  bool m_draw_outline;
  size_t m_max_shapes;
  db::Instance m_inst; 
};

/**
 *  @brief The marker object
 *
 *  The marker is a visual object that "marks" (highlights) a 
 *  certain area of the layout.
 */

class KLAYOUT_DLL Marker
  : public GenericMarkerBase
{
public: 
  /** 
   *  The constructor
   *
   *  @param view The view the marker is intended for
   *  @param cv_index The cell view index of the layout that this marker is intended for
   *  @param draw_outline True to have instances drawing their outline
   *  @param max_shapes The maximum number of shapes to draw for instances (just a box is drawn if more shapes are present)
   */ 
  Marker (lay::LayoutView *view, unsigned int cv_index, bool draw_outline = true, size_t max_shapes = 0);

  /**
   *  @brief The destructor
   */
  ~Marker ();

  /**
   *  @brief Set the marker to display nothing.
   */
  void set ();

  /**
   *  @brief Set the box the marker is to display
   *
   *  If the box is empty, no marker is drawn.
   */
  void set (const db::Box &box, const db::CplxTrans &trans);

  /**
   *  @brief Set the box the marker is to display
   *
   *  If the box is empty, no marker is drawn.
   *  This variant draws the box multiple times at the given transformations.
   */
  void set (const db::Box &box, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the floating-point box the marker is to display
   *
   *  If the box is empty, no marker is drawn.
   */
  void set (const db::DBox &box, const db::CplxTrans &trans);

  /**
   *  @brief Set the floating-point box the marker is to display
   *
   *  If the box is empty, no marker is drawn.
   *  This variant draws the box multiple times at the given transformations.
   */
  void set (const db::DBox &box, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the polygon the marker is to display
   */
  void set (const db::Polygon &poly, const db::CplxTrans &trans);

  /**
   *  @brief Set the polygon the marker is to display
   *  
   *  This variant draws the polygon multiple times at the given transformations.
   */
  void set (const db::Polygon &poly, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the floating-point polygon the marker is to display
   */
  void set (const db::DPolygon &poly, const db::CplxTrans &trans);

  /**
   *  @brief Set the floating-point polygon the marker is to display
   *  
   *  This variant draws the polygon multiple times at the given transformations.
   */
  void set (const db::DPolygon &poly, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the edge pair the marker is to display
   */
  void set (const db::EdgePair &edge_pair, const db::CplxTrans &trans);

  /**
   *  @brief Set the edge pair the marker is to display
   *  
   *  This variant draws the edge pair multiple times at the given transformations.
   */
  void set (const db::EdgePair &edge_pair, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the floating-point edge pair the marker is to display
   */
  void set (const db::DEdgePair &edge_pair, const db::CplxTrans &trans);

  /**
   *  @brief Set the floating-point edge pair the marker is to display
   *  
   *  This variant draws the edge pair multiple times at the given transformations.
   */
  void set (const db::DEdgePair &edge_pair, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the edge the marker is to display
   */
  void set (const db::Edge &edge, const db::CplxTrans &trans);

  /**
   *  @brief Set the edge the marker is to display
   *  
   *  This variant draws the edge multiple times at the given transformations.
   */
  void set (const db::Edge &edge, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the floating-point edge the marker is to display
   */
  void set (const db::DEdge &edge, const db::CplxTrans &trans);

  /**
   *  @brief Set the floating-point edge the marker is to display
   *  
   *  This variant draws the edge multiple times at the given transformations.
   */
  void set (const db::DEdge &edge, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the path the marker is to display
   */
  void set (const db::Path &path, const db::CplxTrans &trans);

  /**
   *  @brief Set the path the marker is to display
   *  
   *  This variant draws the path multiple times at the given transformations.
   */
  void set (const db::Path &path, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the floating-point path the marker is to display
   */
  void set (const db::DPath &path, const db::CplxTrans &trans);

  /**
   *  @brief Set the floating-point path the marker is to display
   *  
   *  This variant draws the path multiple times at the given transformations.
   */
  void set (const db::DPath &path, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the text the marker is to display
   */
  void set (const db::Text &text, const db::CplxTrans &trans);

  /**
   *  @brief Set the text the marker is to display
   *  
   *  This variant draws the text multiple times at the given transformations.
   */
  void set (const db::Text &text, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the floating-point text the marker is to display
   */
  void set (const db::DText &text, const db::CplxTrans &trans);

  /**
   *  @brief Set the floating-point text the marker is to display
   *  
   *  This variant draws the text multiple times at the given transformations.
   */
  void set (const db::DText &text, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the instance the marker is to display
   */
  void set (const db::CellInstArray &instance, const db::CplxTrans &trans);

  /**
   *  @brief Set the instance the marker is to display
   *  
   *  This variant draws the instance multiple times at the given transformations.
   */
  void set (const db::CellInstArray &instance, const db::CplxTrans &t1, const std::vector<db::CplxTrans> &trans);

  /**
   *  @brief Set the outline drawing flag (for instances)
   */
  void set_draw_outline (bool d);

  /**
   *  @brief Get The outline drawing flag
   */
  bool draw_outline () const
  {
    return m_draw_outline;
  }

  /**
   *  @brief Set the maximum number of shapes to draw to instances
   */
  void set_max_shapes (size_t s);

  /**
   *  @brief Get the maximum number of shapes to draw to instances
   */
  size_t max_shapes () const
  {
    return m_max_shapes;
  }

protected:
  virtual db::DBox item_bbox () const;
  
private:
  virtual void render (const Viewport &vp, ViewObjectCanvas &canvas);

  void draw (lay::Renderer &r, const db::CplxTrans &t, lay::CanvasPlane *fill, lay::CanvasPlane *contour, lay::CanvasPlane *vertex, lay::CanvasPlane *text);
  void remove_object ();

  bool m_draw_outline;
  size_t m_max_shapes;

  enum { 
    None, Box, DBox, Polygon, DPolygon, EdgePair, DEdgePair, Edge, DEdge, Path, DPath, Text, DText, Instance
  } m_type;

  union {
    db::Box *box;
    db::DBox *dbox;
    db::Polygon *polygon;
    db::DPolygon *dpolygon;
    db::EdgePair *edge_pair;
    db::DEdgePair *dedge_pair;
    db::Edge *edge;
    db::DEdge *dedge;
    db::Path *path;
    db::DPath *dpath;
    db::Text *text;
    db::DText *dtext;
    db::CellInstArray *inst;
    void *any;
  } m_object;
};

/**
 *  @brief The floating-point coordinate marker object
 *
 *  The marker is a visual object that "marks" (highlights) a 
 *  certain area of the layout. In contrast to the "Marker" object, this
 *  object accepts objects with coordinates in floating-point coordinates in micron values.
 *  It does not need a DBU value hence no cellview to display itself.
 */

class KLAYOUT_DLL DMarker
  : public MarkerBase
{
public: 
  /** 
   *  @brief The constructor 
   */ 
  DMarker (lay::LayoutView *view);

  /**
   *  @brief The destructor
   */
  ~DMarker ();

  /**
   *  @brief Set the box the marker is to display
   *
   *  If the box is empty, no marker is drawn.
   */
  void set (const db::DBox &box);

  /**
   *  @brief Set the polygon the marker is to display
   */
  void set (const db::DPolygon &poly);

  /**
   *  @brief Set the edge pair the marker is to display
   */
  void set (const db::DEdgePair &edge_pair);

  /**
   *  @brief Set the edge the marker is to display
   */
  void set (const db::DEdge &edge);

  /**
   *  @brief Set the path the marker is to display
   */
  void set (const db::DPath &path);

  /**
   *  @brief Set the text the marker is to display
   */
  void set (const db::DText &text);

  /**
   *  @brief Gets the bounding box
   */
  virtual db::DBox bbox () const;
  
private:
  virtual void render (const Viewport &vp, ViewObjectCanvas &canvas);

  void remove_object ();

  enum { 
    None, Box, Polygon, EdgePair, Edge, Path, Text
  } m_type;

  union {
    db::DBox *box;
    db::DPolygon *polygon;
    db::DEdgePair *edge_pair;
    db::DEdge *edge;
    db::DPath *path;
    db::DText *text;
    void *any;
  } m_object;

  lay::LayoutView *mp_view;
};

}

namespace tl {
  template <> struct type_traits<lay::Marker> : public type_traits<void> {
    typedef tl::false_tag has_copy_constructor;
    typedef tl::false_tag has_default_constructor;
  };
  template <> struct type_traits<lay::DMarker> : public type_traits<void> {
    typedef tl::false_tag has_copy_constructor;
    typedef tl::false_tag has_default_constructor;
  };
}

#endif


