tahoma2d/toonz/sources/include/toonzqt/treemodel.h

194 lines
5.6 KiB
C
Raw Normal View History

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