/* This file is part of the KDE project
   Copyright (C) 2004 Cedric Pasteur <cedric.pasteur@free.fr>

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

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#ifndef OBJECTTREEVIEW_H
#define OBJECTTREEVIEW_H

#include <klistview.h>

namespace KFormDesigner {

class ObjectTreeItem;
class Form;

//! @short An item in ObjectTreeView associated with an ObjectTreeItem.
class KFORMEDITOR_EXPORT ObjectTreeViewItem : public KListViewItem
{
	public:
		ObjectTreeViewItem(ObjectTreeViewItem *parent, ObjectTreeItem *item);
		ObjectTreeViewItem(KListView *list, ObjectTreeItem *item=0);
		virtual ~ObjectTreeViewItem();

		//! \return the item name, ie the ObjectTreeItem name
		TQString name() const;

		//! \return the ObjectTreeItem associated to this item.
		ObjectTreeItem* objectTree() const { return m_item; }

		virtual void setOpen( bool o );

	protected:
		//! Reimplemented to draw custom contents (copied from Property Editor)
		virtual void paintCell(TQPainter *p, const TQColorGroup & cg, int column, int width, int align);

		//! Reimplemented to draw custom contents (copied from Property Editor)
		virtual void paintBranches(TQPainter *p, const TQColorGroup &cg, int w, int y, int h);

		//! Reimplemented to draw custom contents (copied from Property Editor)
		virtual void setup();

	private:
		ObjectTreeItem *m_item;

	friend class ObjectTreeView;
};

/*! @short A graphical view of Form's ObjectTree.
 This is a KListView which represents an item for each widget in the form. 
 The actually selected widget is written bold
 and selected. Clicking on a list item selects the corresponding widget in the Form.
 */
class KFORMEDITOR_EXPORT ObjectTreeView : public KListView
{
	Q_OBJECT
  

	public:
		ObjectTreeView(TQWidget *parent=0, const char *name=0, bool tabStop = false);
		virtual ~ObjectTreeView();

		virtual TQSize sizeHint() const;

		/*! Sets \a form as the current Form in the list. The list will automatically 
		 be filled with an item for each widget in the Form, and selection will be synced. 
		 Nothing happens if \a form is already the current Form.
		 */
		void setForm(Form *form);

		//! \return the pixmap name for a given class, to be shown next to the widget name.
		TQString iconNameForClass(const TQCString &classname);

	public slots:
		/*! Sets the widget \a w as selected item, so it will be written bold. 
		 It is added to current selection if \a add is true. */
		void setSelectedWidget(TQWidget *w, bool add=false);

		/*! Adds the ObjectTreeItem \a item in the list, with the appropriate parent. */
		void addItem(ObjectTreeItem *item);

		/*! Removess the ObjectTreeItem \a item from the list. */
		void removeItem(ObjectTreeItem *item);

		/*! Just renames the list item from \a oldname to \a newname. */
		void renameItem(const TQCString &oldname, const TQCString &newname);

	protected slots:
		/*! This slot is called when the user right-click a list item. 
		 The widget context menu is shown, as inisde the Form. */
		void displayContextMenu(KListView *list, TQListViewItem *item, const TQPoint &p);

		void slotColumnSizeChanged(int);

		/*! The selected list item has changed, so we emit a signal to update the Form. */
		void slotSelectionChanged();

		/*! Called before Form object is destroyed. */
		void slotBeforeFormDestroyed();

	protected:
		//! Internal function to fill the list.
		ObjectTreeViewItem* loadTree(ObjectTreeItem *item, ObjectTreeViewItem *parent);

		//! \return The item whose name is \a name.
		ObjectTreeViewItem* findItem(const TQString &name);

	private:
		Form *m_form;
		ObjectTreeViewItem *m_topItem;

	friend class TabStopDialog;
};

}

#endif