/***************************************************************************
 *   Copyright (C) 2004 by Hans Oischinger                                 *
 *   hans.oischinger@kdemail.net                                           *
 *                                                                         *
 *   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 Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef VDRAWELEMENTS_H
#define VDRAWELEMENTS_H

#include "visitor.h"
#include <kcanvas/KCanvasPath.h>

class KCanvas;
class KCanvasItem;
class KAbstractRenderingStyle;
class KRenderingDevice;
class Matrix;
class UseElement;
class KRenderingPaintServer;
class Gradient;
class RadialGradient;
class LinearGradient;
class Transform;
class Stylable;
/**
Use this to draw elements on the canvas
 
@author Hans Oischinger
*/
class VDrawElements : public Visitor
{
public:
  /**
   * Constructor
   * @param canvas a valid KCanvas
   * @param device a valid KRenderingDevice
   * @param removeItemsOnly tells the visitor only to remove KCanvasItems and return (off by default)
   * @param setZindex tells the visitor to set an items ZIndex according to it's position in the tree
   * when visiting. Be aware that this only works when starting from the Document root (off by default)
   * @param globalTrans Post-multiplies an additional transformation matrix to all visited elements
   */
  VDrawElements( KCanvas *canvas, KRenderingDevice *device, bool removeItemsOnly = false, bool setZindex = false, Transform* globalTrans = 0 );
  virtual ~VDrawElements();

  /**
   * Allows you to set the ZIndex counter that is incremented with every visible element visited
   */
  void setZIndex( unsigned int zindex ) { m_zindex = zindex; }

  virtual bool visit( DocumentElement& element );
  virtual bool visit( VectorElement& element );
  virtual bool visit( GroupElement& element );
  virtual bool visit( UseElement& element );
  virtual bool visit( ElementSelection& element );
  virtual bool visit( LinearGradient& element );
  virtual bool visit( RadialGradient& element );

  /**
   * Set to true if you want all gradients to show up w/o transformations and with default
   * start and stop points
   */
  void setNormalizePaintServers(bool theValue)
  {
    m_normalizePaintServers = theValue;
  }

  /**
   * @see setNormalizePaintServers(bool theValue)
   */
  bool getNormalizePaintServers() const
  {
    return m_normalizePaintServers;
  }


protected:
  /**
   * Creates a KCanvas RadialGradient PaintServer
   * @param gradient from
   */
  void updatePaintServer( RadialGradient& gradient );
  /**
   * Creates a KCanvas LinearGradient PaintServer
   * @param gradient from
   */
  void updatePaintServer( LinearGradient& gradient );
  /**
   * Sets all attributes of the abstract class Gradient on a concrete gradient object
   * Usually called from createPaintServer() to finalize a KRenderingPaintServer
   * Be aware thet the gradient element must have a valid PaintServer accessible through context()
   * @param gradient Source Gradient element
   */
  void finalizePaintServer( Gradient& gradient );

  /**
   * Create KCPathDataList object from VectorElement depending on it's VectorPath's
   * ShapeMetaType
   * @param element element to process
   * @return filled KCPathDataList
   */
  KCPathDataList createKCPathDataList( VectorElement& element );

  /**
   * Sets all attributes of the Stylable element on the renderStyle
   * @param renderStyle the renderStyle object to work on
   * @param element Stylable that will be used as source
   */
  void updateRenderStyle( KAbstractRenderingStyle *renderStyle, Stylable& element );

  /**
   * Creates the ElementSelection handle on the given point of the bbox
   * @param node the node that should be created
   * @param element the selection element
   */
  void createSelectionHandles( unsigned int node, ElementSelection& element );
  void createPathNodeHandles( ElementSelection& element );

  /**
   * Handles hiding and showing (usually one of the first things called in visit()
   * according to it's getState()
   * @param element the element to check
   * @param canvasItem the element's canvasItem (will be hidden or shown) or 0 if it has none.
   * @return true if hidden, false if visible
   */
  bool checkHideShow( ElementNode& element, KCanvasItem* canvasItem = 0 );
  /**
   * Same as above but for ElementSelection rather than Element
   */
  bool checkHideShow( ElementSelection& element );

private:
  KCanvas* m_canvas;
  KRenderingDevice *m_device;
  bool m_removeItemsOnly;
  bool m_setZindex;
  Transform* m_globalTransform;
  unsigned int m_zindex; // incremented at on every item, sets the Z-Index
  bool m_normalizePaintServers;

  UseElement* m_referencedContext;
};

#endif
