Shinya Kitaoka 810553
#pragma once
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
Shinya Kitaoka 120a6e
class DVAPI TreeModel : public QAbstractItemModel {
Shinya Kitaoka 120a6e
  Q_OBJECT
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  class DVAPI Item {
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    Item();
Shinya Kitaoka 120a6e
    virtual ~Item();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    Item *getParent() const { return m_parent; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    int getChildCount() const { return m_childItems.count(); }
Shinya Kitaoka 120a6e
    Item *getChild(int row) const { return m_childItems.value(row); }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    Item *appendChild(Item *child);  // gets ownership; returns child
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    //! see setChildren() below
Shinya Kitaoka 120a6e
    virtual void *getInternalPointer() const { return 0; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Replace old children with new ones.
Shinya Kitaoka 120a6e
    // If old and new child internalPointer are equal
Shinya Kitaoka 120a6e
    // and not zero then the old child is used and the new one
Shinya Kitaoka 120a6e
    // is discarded
Shinya Kitaoka 120a6e
    // This method is used to "refresh" the child list.
Shinya Kitaoka 120a6e
    // Take ownership of children
Shinya Kitaoka 120a6e
    // After the call 'children' contains the new children only
Shinya Kitaoka 120a6e
    // Unused old child are queued into m_itemsToDelete. They will eventually
Shinya Kitaoka 120a6e
    // be deleted by endRefresh()
Shinya Kitaoka 120a6e
    void setChildren(QList<item *=""> &children);</item>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // called by setChildren on "used" old child
Shinya Kitaoka 120a6e
    virtual void refresh() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    TreeModel *getModel() const { return m_model; }
Shinya Kitaoka 120a6e
    void setModel(TreeModel *model) { m_model = model; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    //! row is the Item index in the parent. (used by QModelIndex)
Shinya Kitaoka 120a6e
    int getRow() const { return m_row; }
Shinya Kitaoka 120a6e
    void setRow(int row) { m_row = row; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    //! root_depth == 0; son_depth == parent_depth + 1
Shinya Kitaoka 120a6e
    int getDepth() const { return m_depth; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    bool isOpen() const { return m_opened; }
Shinya Kitaoka 120a6e
    void setIsOpen(bool opened) { m_opened = opened; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    //! used by TreeModel::data(); default implementation provides
Shinya Kitaoka 120a6e
    //! open/close folder icons
Shinya Kitaoka 120a6e
    virtual QVariant data(int role) const;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    QModelIndex createIndex();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  private:
Shinya Kitaoka 120a6e
    // update children data (e.g.: parent, model, depth and row)
Shinya Kitaoka 120a6e
    void updateChild(Item *child, int row);
Shinya Kitaoka 120a6e
    void updateChildren();  // Note: is not ricursive, i.e. doesn't call itself
Shinya Kitaoka 120a6e
                            // on children
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    TreeModel *m_model;
Shinya Kitaoka 120a6e
    Item *m_parent;
Shinya Kitaoka 120a6e
    QList<item *=""> m_childItems;</item>
Shinya Kitaoka 120a6e
    int m_depth;
Shinya Kitaoka 120a6e
    int m_row;
Shinya Kitaoka 120a6e
    bool m_opened;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  public:
Shinya Kitaoka 120a6e
    // int index() const {return
Shinya Kitaoka 120a6e
    // (m_parent)?m_parent->m_childItems.indexOf(const_cast<item*>(this)):0;}</item*>
Shinya Kitaoka 120a6e
  };  // class Item
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TreeModel(TreeView *parent = 0);
Shinya Kitaoka 120a6e
  virtual ~TreeModel();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setExpandedItem(const QModelIndex &index, bool expanded);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TreeView *getView() { return m_view; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public slots:
Shinya Kitaoka 120a6e
  virtual void onExpanded(const QModelIndex &index);
Shinya Kitaoka 120a6e
  virtual void onCollapsed(const QModelIndex &index);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  //! to update the model:
Shinya Kitaoka 120a6e
  //!   beginRefresh(),
Shinya Kitaoka 120a6e
  //!   Item::setChildren() and/or Item::appendChild() and/or setRootItem(),
Shinya Kitaoka 120a6e
  //!   endRefresh().
Shinya Kitaoka 120a6e
  void beginRefresh();
Shinya Kitaoka 120a6e
  void endRefresh();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // const QList<item*> &getItemsToDelete() const {return m_itemsToDelete;}</item*>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  Qt::ItemFlags flags(const QModelIndex &index) const override;
Shinya Kitaoka 120a6e
  QModelIndex index(int row, int column,
Shinya Kitaoka 473e70
                    const QModelIndex &parent = QModelIndex()) const override;
Shinya Kitaoka 473e70
  QModelIndex parent(const QModelIndex &index) const override;
Shinya Kitaoka 473e70
  int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Shinya Kitaoka 473e70
  int columnCount(const QModelIndex &parent = QModelIndex()) const override;
Shinya Kitaoka 473e70
  QVariant data(const QModelIndex &index, int role) const override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setRowHidden(int row, const QModelIndex &parent, bool hide);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  void setRootItem(Item *rootItem);
Campbell Barton 6e785f
  void setRootItem_NoFree(Item *rootItem);
Shinya Kitaoka 120a6e
  Item *getRootItem() const { return m_rootItem; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  Item *m_rootItem;
Shinya Kitaoka 120a6e
  QList<item *=""> m_itemsToDelete;</item>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  TreeView *m_view;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DVAPI TreeView : public QTreeView {
Shinya Kitaoka 120a6e
  Q_OBJECT
Shinya Kitaoka 120a6e
  bool m_dragging;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TreeView(QWidget *parent);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka d1f6c4
class Delegate final : public QItemDelegate
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
public:
Shinya Kitaoka 120a6e
Delegate(TreeView *parent) : QItemDelegate(parent), m_treeView(parent) {}
Shinya Kitaoka 120a6e
bool editorEvent(QEvent *e, QAbstractItemModel *model, const
Shinya Kitaoka 120a6e
QStyleOptionViewItem &option, const QModelIndex &index);
Shinya Kitaoka 120a6e
private:
Shinya Kitaoka 120a6e
TreeView *m_treeView;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
};
Shinya Kitaoka 120a6e
friend Delegate;
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // virtual void onClick(TreeModel::Item *item, const QPoint &pos, const
Shinya Kitaoka 120a6e
  // QStyleOptionViewItem &option) {}
Shinya Kitaoka 473e70
  void mouseDoubleClickEvent(QMouseEvent *) override;
Shinya Kitaoka 473e70
  void mousePressEvent(QMouseEvent *) override;
Shinya Kitaoka 473e70
  void mouseReleaseEvent(QMouseEvent *) override;
Shinya Kitaoka 473e70
  void mouseMoveEvent(QMouseEvent *) override;
Shinya Kitaoka 473e70
  void resizeEvent(QResizeEvent *) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void setModel(TreeModel *model);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  virtual void onClick(TreeModel::Item *item, const QPoint &itemPos,
Shinya Kitaoka 120a6e
                       QMouseEvent *e) {}
Shinya Kitaoka 120a6e
  virtual void onDrag(TreeModel::Item *item, const QPoint &itemPos,
Shinya Kitaoka 120a6e
                      QMouseEvent *e) {}
Shinya Kitaoka 120a6e
  virtual void onRelease() {}
Shinya Kitaoka 120a6e
  virtual void onMidClick(TreeModel::Item *item, const QPoint &itemPos,
Shinya Kitaoka 120a6e
                          QMouseEvent *e) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  virtual void openContextMenu(TreeModel::Item *item, const QPoint &globalPos) {
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public slots:
Shinya Kitaoka 120a6e
  void resizeToConts(void);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif