Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef _FUNCTIONTREEMODEL_
Toshihiro Shimizu 890ddd
#define _FUNCTIONTREEMODEL_
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tcommon.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TOONZQT_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qlist></qlist>
Toshihiro Shimizu 890ddd
#include <qvariant></qvariant>
Toshihiro Shimizu 890ddd
#include <qabstractitemmodel></qabstractitemmodel>
Toshihiro Shimizu 890ddd
#include <qmodelindex></qmodelindex>
Toshihiro Shimizu 890ddd
#include <qtreeview></qtreeview>
Toshihiro Shimizu 890ddd
#include <qitemdelegate></qitemdelegate>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
class TreeView;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//! generic tree model. It maintains internally a tree structure
Toshihiro Shimizu 890ddd
//! of TreeModel::Item's. Each QModelIndex created by the TreeModel
Toshihiro Shimizu 890ddd
//! points internally to the corrispondent TreeModel::Item
Toshihiro Shimizu 890ddd
class DVAPI TreeModel : public QAbstractItemModel
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Q_OBJECT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	class DVAPI Item
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		Item();
Toshihiro Shimizu 890ddd
		virtual ~Item();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Item *getParent() const { return m_parent; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int getChildCount() const { return m_childItems.count(); }
Toshihiro Shimizu 890ddd
		Item *getChild(int row) const { return m_childItems.value(row); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		Item *appendChild(Item *child); // gets ownership; returns child
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//! see setChildren() below
Toshihiro Shimizu 890ddd
		virtual void *getInternalPointer() const { return 0; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Replace old children with new ones.
Toshihiro Shimizu 890ddd
		// If old and new child internalPointer are equal
Toshihiro Shimizu 890ddd
		// and not zero then the old child is used and the new one
Toshihiro Shimizu 890ddd
		// is discarded
Toshihiro Shimizu 890ddd
		// This method is used to "refresh" the child list.
Toshihiro Shimizu 890ddd
		// Take ownership of children
Toshihiro Shimizu 890ddd
		// After the call 'children' contains the new children only
Toshihiro Shimizu 890ddd
		// Unused old child are queued into m_itemsToDelete. They will eventually
Toshihiro Shimizu 890ddd
		// be deleted by endRefresh()
Toshihiro Shimizu 890ddd
		void setChildren(QList<item *=""> &children);</item>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// called by setChildren on "used" old child
Toshihiro Shimizu 890ddd
		virtual void refresh() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TreeModel *getModel() const { return m_model; }
Toshihiro Shimizu 890ddd
		void setModel(TreeModel *model) { m_model = model; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//! row is the Item index in the parent. (used by QModelIndex)
Toshihiro Shimizu 890ddd
		int getRow() const { return m_row; }
Toshihiro Shimizu 890ddd
		void setRow(int row) { m_row = row; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//! root_depth == 0; son_depth == parent_depth + 1
Toshihiro Shimizu 890ddd
		int getDepth() const { return m_depth; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		bool isOpen() const { return m_opened; }
Toshihiro Shimizu 890ddd
		void setIsOpen(bool opened) { m_opened = opened; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//! used by TreeModel::data(); default implementation provides
Toshihiro Shimizu 890ddd
		//! open/close folder icons
Toshihiro Shimizu 890ddd
		virtual QVariant data(int role) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		QModelIndex createIndex();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	private:
Toshihiro Shimizu 890ddd
		// update children data (e.g.: parent, model, depth and row)
Toshihiro Shimizu 890ddd
		void updateChild(Item *child, int row);
Toshihiro Shimizu 890ddd
		void updateChildren(); // Note: is not ricursive, i.e. doesn't call itself on children
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TreeModel *m_model;
Toshihiro Shimizu 890ddd
		Item *m_parent;
Toshihiro Shimizu 890ddd
		QList<item *=""> m_childItems;</item>
Toshihiro Shimizu 890ddd
		int m_depth;
Toshihiro Shimizu 890ddd
		int m_row;
Toshihiro Shimizu 890ddd
		bool m_opened;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		// int index() const {return (m_parent)?m_parent->m_childItems.indexOf(const_cast<item*>(this)):0;}</item*>
Toshihiro Shimizu 890ddd
	}; //class Item
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TreeModel(TreeView *parent = 0);
Toshihiro Shimizu 890ddd
	virtual ~TreeModel();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setExpandedItem(const QModelIndex &index, bool expanded);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TreeView *getView() { return m_view; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public slots:
Toshihiro Shimizu 890ddd
	virtual void onExpanded(const QModelIndex &index);
Toshihiro Shimizu 890ddd
	virtual void onCollapsed(const QModelIndex &index);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//!to update the model:
Toshihiro Shimizu 890ddd
	//!   beginRefresh(),
Toshihiro Shimizu 890ddd
	//!   Item::setChildren() and/or Item::appendChild() and/or setRootItem(),
Toshihiro Shimizu 890ddd
	//!   endRefresh().
Toshihiro Shimizu 890ddd
	void beginRefresh();
Toshihiro Shimizu 890ddd
	void endRefresh();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// const QList<item*> &getItemsToDelete() const {return m_itemsToDelete;}</item*>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Qt::ItemFlags flags(const QModelIndex &index) const;
Toshihiro Shimizu 890ddd
	QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
Toshihiro Shimizu 890ddd
	QModelIndex parent(const QModelIndex &index) const;
Toshihiro Shimizu 890ddd
	int rowCount(const QModelIndex &parent = QModelIndex()) const;
Toshihiro Shimizu 890ddd
	int columnCount(const QModelIndex &parent = QModelIndex()) const;
Toshihiro Shimizu 890ddd
	QVariant data(const QModelIndex &index, int role) const;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setRowHidden(int row, const QModelIndex &parent, bool hide);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	void setRootItem(Item *rootItem);
Toshihiro Shimizu 890ddd
	Item *getRootItem() const { return m_rootItem; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	Item *m_rootItem;
Toshihiro Shimizu 890ddd
	QList<item *=""> m_itemsToDelete;</item>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	TreeView *m_view;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TreeView : public QTreeView
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Q_OBJECT
Toshihiro Shimizu 890ddd
	bool m_dragging;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TreeView(QWidget *parent);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  class Delegate : public QItemDelegate
Toshihiro Shimizu 890ddd
  { 
Toshihiro Shimizu 890ddd
  public: 
Toshihiro Shimizu 890ddd
    Delegate(TreeView *parent) : QItemDelegate(parent), m_treeView(parent) {}
Toshihiro Shimizu 890ddd
    bool editorEvent(QEvent *e, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
Toshihiro Shimizu 890ddd
  private:
Toshihiro Shimizu 890ddd
    TreeView *m_treeView;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  };
Toshihiro Shimizu 890ddd
  friend Delegate;
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// virtual void onClick(TreeModel::Item *item, const QPoint &pos, const QStyleOptionViewItem &option) {}
Toshihiro Shimizu 890ddd
	void mouseDoubleClickEvent(QMouseEvent *);
Toshihiro Shimizu 890ddd
	void mousePressEvent(QMouseEvent *);
Toshihiro Shimizu 890ddd
	void mouseReleaseEvent(QMouseEvent *);
Toshihiro Shimizu 890ddd
	void mouseMoveEvent(QMouseEvent *);
Toshihiro Shimizu 890ddd
	void resizeEvent(QResizeEvent *);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void setModel(TreeModel *model);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void onClick(TreeModel::Item *item, const QPoint &itemPos, QMouseEvent *e) {}
Toshihiro Shimizu 890ddd
	virtual void onDrag(TreeModel::Item *item, const QPoint &itemPos, QMouseEvent *e) {}
Toshihiro Shimizu 890ddd
	virtual void onRelease() {}
Toshihiro Shimizu 890ddd
	virtual void onMidClick(TreeModel::Item *item, const QPoint &itemPos, QMouseEvent *e) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual void openContextMenu(TreeModel::Item *item, const QPoint &globalPos) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public slots:
Toshihiro Shimizu 890ddd
	void resizeToConts(void);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif