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

#include <klistview.h>
#include "core/coreinterfaces.h"

#include <qptrdict.h>
#include <qptrlist.h>

class ElementNode;
class ElementTreeViewItem;
class QListViewItem;
/**
Specialised KListView to display ElementNodes
 
@author Hans Oischinger
*/
class ElementTreeView : public KListView,
      public ElementListenerInterface,
      public ElementSelectionListenerInterface
{
  Q_OBJECT
  friend class TimelineDocker;  // accesses elemtentsToItems()
public:
  ElementTreeView(QWidget *parent = 0, const char *name = 0);

  ~ElementTreeView();

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

  /**
   * Overloaded setSelected to keep track of selected items in m_selectedItems
   */
  void setSelected( QListViewItem * item, bool selected );

signals:
  /**
   * Emitted whenever a change to the ElementItems occurs
   */
  void contentsChanged();

protected slots:
  void selectionChangedInside();
  void selectionChangedOutside();

protected:
  void appendTreeNode( ElementNode* e, ElementTreeViewItem* parent, ElementTreeViewItem* after = 0,  bool addNeighbours=false );

  /**
   * Returns the internal elements - items map
   */
  const QPtrDict<ElementTreeViewItem>& elemtentsToItems() const { return m_elementsToItems; }

private:
  QPtrDict<ElementTreeViewItem> m_elementsToItems;
  QPtrList<QListViewItem> m_selectedItems;
};


/**
ListViewItems for the ElementTreeDocker. They hold a reference on the
element they represent.
 
@author Hans Oischinger
 */
class ElementTreeViewItem : public KListViewItem
{
public:
  ElementTreeViewItem(ElementNode* element, QListView *parent);
  ElementTreeViewItem(ElementNode* element, QListViewItem *parent);
  ElementTreeViewItem(ElementNode* element, QListView *parent, QListViewItem *after);
  ElementTreeViewItem(ElementNode* element, QListViewItem *parent, QListViewItem *after);

  ElementTreeViewItem(ElementNode* element, QListView *parent,
                      QString, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null);

  ElementTreeViewItem(ElementNode* element, QListViewItem *parent,
                      QString, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null);

  ElementTreeViewItem(ElementNode* element, QListView *parent, QListViewItem *after,
                      QString, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null);

  ElementTreeViewItem(ElementNode* element, QListViewItem *parent, QListViewItem *after,
                      QString, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null,
                      QString = QString::null, QString = QString::null);

  ~ElementTreeViewItem();

  ElementNode* element() const { return m_element; }

protected:
  void init( ElementNode* element );

  ElementNode* m_element;
};

#endif
