DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
DAFigureTreeModel.h
1#ifndef DAFIGURETREEMODEL_H
2#define DAFIGURETREEMODEL_H
3#include "DAFigureAPI.h"
4#include <QAbstractItemModel>
5#include <QList>
6#include <qwt_axis_id.h>
7#include <QStandardItemModel>
8
9class QwtPlot;
10class QwtFigure;
11class QwtScaleWidget;
12class QwtPlotItem;
13namespace DA
14{
15class DAFigureWidget;
16
17class DAFIGURE_API DAFigureTreeModel : public QStandardItemModel
18{
19 Q_OBJECT
20
21public:
23 {
24 NodeTypeUnknow = 1001,
25 NodeTypeFigure,
31 NodeTypePlotItem
32 };
33
34 enum CustomRoles
35 {
36 RolePlot = Qt::UserRole + 1, // 存储QwtPlot指针
37 RoleScale = Qt::UserRole + 2, // 存储QwtScaleWidget指针
38 RolePlotItem = Qt::UserRole + 3, // 存储QwtPlotItem指针
39 RoleAxisId = Qt::UserRole + 4, // 存储QwtAxisId
40 RoleNodeType = Qt::UserRole + 5 // 存储节点类型
41 };
42
43 explicit DAFigureTreeModel(QObject* parent = nullptr);
44 ~DAFigureTreeModel();
45
46 void setFigure(QwtFigure* figure);
47 QwtFigure* figure() const
48 {
49 return m_figure;
50 }
51
52 void refresh();
53
54 // item的类型
55 NodeType itemType(QStandardItem* item) const;
56
57 template< typename T >
58 T* pointerFromItem(const QStandardItem* item, CustomRoles role) const
59 {
60 if (!item) {
61 return nullptr;
62 }
63 QVariant v = item->data(role);
64 return v.isValid() ? reinterpret_cast< T* >(v.value< quintptr >()) : nullptr;
65 }
66
67 template< typename T >
68 T* pointerFromIndex(const QModelIndex& index, CustomRoles role) const
69 {
70 if (!index.isValid())
71 return nullptr;
72 QStandardItem* item = itemFromIndex(index);
73 return pointerFromItem< T >(item, role);
74 }
75 QwtPlot* plotFromItem(const QStandardItem* item) const;
76 QwtPlot* plotFromIndex(const QModelIndex& index) const;
77 QwtScaleWidget* scaleFromItem(const QStandardItem* item) const;
78 QwtScaleWidget* scaleFromIndex(const QModelIndex& index) const;
79 QwtPlotItem* plotItemFromItem(const QStandardItem* item) const;
80 QwtPlotItem* plotItemFromIndex(const QModelIndex& index) const;
81 QwtAxisId axisIdFromItem(const QStandardItem* item) const;
82 QwtAxisId axisIdFromItem(const QModelIndex& index) const;
83
84 // 返回绘图的名字
85 virtual QString generatePlotTitleText(QwtPlot* plot) const;
86 // 返回QwtPlotItem的名字
87 virtual QString generatePlotItemName(QwtPlotItem* item) const;
88 // 返回QwtPlotItem对应的图标
89 virtual QIcon generatePlotItemIcon(QwtPlotItem* item) const;
90 // 创建一个纯颜色图标
91 virtual QIcon generateBrushIcon(const QBrush& b) const;
92Q_SIGNALS:
93 void chartItemAttached(QwtPlotItem* item, bool on);
94private slots:
95 void onAxesAdded(QwtPlot* plot);
96 void onAxesRemoved(QwtPlot* plot);
97 void onFigureCleared();
98 void onCurrentAxesChanged(QwtPlot* plot);
99 void onItemAttached(QwtPlotItem* item, bool on);
100
101private:
102 void setupModel();
103 void clearAllConnections();
104 void addPlotToModel(QwtPlot* plot, QStandardItem* parentItem);
105 void addLayerToModel(QwtPlot* plot, QStandardItem* parentItem);
106 void addAxesToLayer(QwtPlot* plot, QStandardItem* layerItem);
107 void addPlotItemsToLayer(QwtPlot* plot, QStandardItem* layerItem);
108 void addPlotItem(QwtPlotItem* item, QStandardItem* parentItem);
109 void removePlotItem(QwtPlotItem* item, QStandardItem* parentItem);
110 void removePlotFromModel(QwtPlot* plot);
111 // 创建一个空item,用于树形节点没有对应的2,3列的情况
112 QStandardItem* createEmptyItem() const;
113 // 创建绘图属性item
114 QStandardItem* createAxesPropertyItem(QwtPlot* plot) const;
115 // 更新绘图属性,把当前选中的绘图更新掉
116 void updateAxesPropertyItem();
117
118 QStandardItem* findPlotItem(QwtPlot* plot) const;
119 QStandardItem* findItemsFolderForPlot(QStandardItem* plotItem, QwtPlot* plot) const;
120
121private:
122 QwtFigure* m_figure;
123 QHash< QwtPlot*, QStandardItem* > m_plotItems;
124 QHash< QwtPlotItem*, QStandardItem* > m_plotItemItems;
125
126 // 连接管理
127 QList< QMetaObject::Connection > m_figureConnections;
128 QHash< QwtPlot*, QList< QMetaObject::Connection > > m_plotConnections;
129};
130
131} // End Of Namespace DA
132#endif // DAFIGURETREEMODEL_H
Definition DAFigureTreeModel.h:18
NodeType
Definition DAFigureTreeModel.h:23
@ NodeTypeItemsFolder
RolePlot有效,可提取QwtPlot指针
Definition DAFigureTreeModel.h:30
@ NodeTypePlot
RolePlot有效,可提取QwtPlot指针(含host和parasite)
Definition DAFigureTreeModel.h:27
@ NodeTypeAxesFolder
RolePlot有效,可提取host QwtPlot指针
Definition DAFigureTreeModel.h:28
@ NodeTypeAxis
RoleScale有效,可提取QwtScaleWidget指针;RoleAxisId有效,可提取QwtAxisId;RolePlot有效,可提取对应QwtPlot指针
Definition DAFigureTreeModel.h:29
@ NodeTypePlotFolder
RolePlot有效,可提取host QwtPlot指针
Definition DAFigureTreeModel.h:26
序列化类都是带异常的,使用中需要处理异常
Definition AppMainWindow.cpp:44