/***************************************************************************
 *   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 ELEMENTSELECTION_H
#define ELEMENTSELECTION_H

#include "elements/element.h"
#include "core/coreinterfaces.h"

#include <qptrlist.h>

class QRect;
class ElementNode;
class VectorElement;
class KCanvasItem;
class Visitor;
/**
VSelection manages a set of VectorElements
This class inherits Element because it is a visible element on the document
although it's not in the Document Tree
 
@author Hans Oischinger
*/
class ElementSelection :
      public Element,
      public ElementListenerInterface,
      public ElementSelectionInterface
{
  Q_OBJECT
public:
  /// Ids of manipulation nodes.
  enum HandleNode
  {
    node_none = 0,  // No/Invalid node
    node_body = 1,  // The selection rectangle
    node_lt = 2,
    node_mt = 3,
    node_rt = 4,
    node_lm = 5,
    node_mm = 6,
    node_rm = 7,
    node_lb = 8,
    node_mb = 9,
    node_rb = 10,
    node_path = 11  // A path node
  };

  ElementSelection();
  ~ElementSelection();
  ElementSelection( const ElementSelection & );

  ElementSelection &operator=(const ElementSelection &);
  virtual ElementSelection *clone();

  void onElementInserted( ElementNode* );
  void onElementRemoved( ElementNode* );
  void onElementModified( ElementNode* );

  virtual Element::Hit hits( const QPoint & );
  virtual QRect bbox( KCBBoxFlags flags = BBOX_DEFAULT ) const;
  virtual void accept( Visitor& visitor );

  virtual Element::State getState();

  virtual Attribute* getAttrByName( const QString& name ) { return 0; }

  KCanvasItem* context( unsigned int node ) const;
  void setContext( HandleNode node, KCanvasItem* context );

  QPtrList<KCanvasItem>& contextPathNodes() const { return *m_pathNodes; }

  /**
   * Adds an object to the selection.
   */
  void append( ElementNode& element, bool emitChanged = true );

  /**
   * Adds a list of objects to the selection.
   */
  void append( QPtrList<ElementNode>& elements, bool emitChanged = true );

  /**
   * Removes the reference to the object, not the object itself.
   */
  void take( ElementNode& element, bool emitChanged = true );

  /**
   * Removes the references to all objects, not the objects themselves.
   */
  void clear( bool emitChanged = true );

  /**
   * Read only access to the selected objects.
   */
  QPtrList<ElementNode>& elements() const { return *m_list; }
  /**
   * Read only access to the selected objects (only visual elements).
   */
  QPtrList<VectorElement>& visualElements() const { return *m_visualList; }

  // ElementSelectionInterface Interface
  void addElementSelectionListener( ElementSelectionListenerInterface* );
  void removeElementSelectionListener( ElementSelectionListenerInterface* );

  /**
   * Notify listeners that the selection has changed
   * Can also be called from outside if needed (emitChanged=false)
   */
  void notifyChange();

  /**
   * Returns the enum value for the HandleNode that hits the given point
   */
  unsigned int handleNode( const QPoint & );

protected:
  void init();

signals:
  void contentsChanged();

protected:
  QPtrList<ElementSelectionListenerInterface> m_listeners;
  QPtrList<ElementNode> *m_list;
  QPtrList<VectorElement> *m_visualList;
  KCanvasItem *m_context[11]; // 1 Rect and 9 Nodes
  QPtrList<KCanvasItem> *m_pathNodes;  // holds all KCanvasItems for the path segment's nodes
};

#endif
