-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHierarchyModel.h
92 lines (78 loc) · 3.51 KB
/
HierarchyModel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once
#include <vector>
#include "qstring.h"
#include "qmimedata.h"
#include "qmimetype.h"
#include "nesteddefs.h"
#include "qabstractitemmodel.h"
#include "qitemselectionmodel.h"
/**
* @class HierarchyModel
* @brief 对场景树的ui显示进行统一的管理
* @note
* 继承了QAbstractItemModel,用于在treeview中显示场景树
*/
class HierarchyModel : public QAbstractItemModel
{
Q_OBJECT
private:
/** @brief 储存所有被创建的节点 */
std::vector<HierarchyObject*> objects;
public slots:
/** @brief 接收ui中选择变更事件 */
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
public:
/** @brief 树根节点 */
HierarchyObject* root;
/** @brief 对应的widget */
Widget* widget;
/** @brief 右键菜单触发时记录下右键点击的节点 */
QModelIndex lastRightClick;
/** @brief 记录用户选中的最后一个物体 */
HierarchyObject* lastSelected = NULL;
/** @brief 节点总数 */
int objectCount() { return objects.size(); }
/** @brief 按某种顺序访问节点 */
HierarchyObject* getObject(int i) { return objects[i]; }
HierarchyModel();
~HierarchyModel();
/** @brief 创建HierarchyObject并进行一些初始化操作 */
HierarchyObject* createObject(const QString& name);
/** @brief 移动树结构中的节点 */
void moveObject(HierarchyObject* obj, HierarchyObject* toParent, int toIndex);
/** @brief 移除一个节点 */
void removeObject(HierarchyObject* obj);
/** @brief 根据QModelIndex取出对应HierarchyObject */
HierarchyObject* index2obj(const QModelIndex &index) const;
/** @brief 根据obj创建index */
QModelIndex obj2index(HierarchyObject* obj) const;
/** @brief 取出mime里面的拖放来源 */
bool HierarchyModel::retrieveDroppedObjects(const QMimeData * data, std::vector<HierarchyObject*>& out_objects) const;
// -------------实现QAbstractItemModel的必选接口-----------------
// 获取给定元素的第row个子元素
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
// 获取给定元素的父元素
QModelIndex parent(const QModelIndex &index) const;
// 获取某元素的子节点数
int rowCount(const QModelIndex &parent = QModelIndex()) const;
// (永远return1)
int columnCount(const QModelIndex &parent = QModelIndex()) const;
// 取出数据(name)
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
// ----------------------重写可选函数----------------------------
// 接收编辑name事件
bool setData(const QModelIndex & index, const QVariant & value, int role);
// 返回指定节点的各种显示属性
Qt::ItemFlags flags(const QModelIndex &index) const;
// 是否允许拖放到指定节点
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const; //drop是否允许
// 拖放拖出时产生mimeData
QMimeData *mimeData(const QModelIndexList &indexes) const;
// 拖放事件接收mimeData
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); //drop事件
// 返回允许的mimeType
QStringList mimeTypes() const;
// 允许的action
Qt::DropActions supportedDropActions() const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
};