WSF
AbstractModel.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11
12#ifndef ABSTRACTMODEL_HPP
13#define ABSTRACTMODEL_HPP
14
15#include <vector>
16
17#include <QAbstractItemModel>
18
19#include "ViExport.hpp"
20
21namespace wizard
22{
24
26class VI_EXPORT AbstractModel : public QAbstractItemModel
27{
28public:
29 friend class AbstractModelItem;
30
32 : mRootPtr()
33 , mChangeMode(0)
34 {
35 }
36 void SetRoot(AbstractModelItem* aItemPtr);
37
38 AbstractModelItem* FindItem(const QModelIndex& idx) const;
39
40 int rowCount(const QModelIndex& parent) const override;
41
42 int columnCount(const QModelIndex& parent) const override;
43 QModelIndex index(int row, int column, const QModelIndex& parent) const override;
44
45 QModelIndex parent(const QModelIndex& child) const override;
46
47 QModelIndex GetRowIndex(AbstractModelItem& aRow) const;
48
49 void FakeInsert(AbstractModelItem& aParent);
50
51protected:
52 void BeginInsert(AbstractModelItem& aParent, int aIndex);
53 void EndInsert();
54 void BeginRemove(AbstractModelItem& aParent, int aIndex, int aCount = 1);
55 void EndRemove();
56 void BeginMove(AbstractModelItem& aSource, int aSourceIndex, int aCount, AbstractModelItem& aDestination, int aDestinationIndex);
57 void EndMove();
58
61};
62
64class VI_EXPORT AbstractModelItem : public QObject
65{
66public:
67 friend class AbstractModel;
68
70 ~AbstractModelItem() override;
71
72 AbstractModelItem* GetAbstractItemParent() const { return mItemParentPtr; }
73
74 int GetItemIndex() { return mItemIndex; }
75 // Returns the model that contains this item. May be null
76 AbstractModel* GetModel() const { return mModelPtr; }
77 AbstractModelItem* GetAbstractItem(int aIdx) const { return mItemChildren[aIdx]; }
78 int GetAbstractItemCount() const { return (int)mItemChildren.size(); }
79
80 // Warning: These mutators should be used carefully. Normally these are called by the
81
82 // derived class.
83 void AddAbstractItem(AbstractModelItem* aItemPtr, int aIndex = -1);
84 void RemoveAbstractItem(AbstractModelItem* aItemPtr);
85 AbstractModelItem* RemoveAbstractItem(int aIndex);
86 void RemoveAllAbstractItems(bool aDelete = false);
87
88 void MoveAbstractItem(AbstractModelItem* aItemPtr, int aIndex = -1);
89
90 void AbstractItemChanged();
91
92 void RemoveItemFromParent();
93
94protected:
95 // By default, when an abstract item is deleted, it deletes all children.
96 // Override this method to change auto-delete behavior
97 virtual void RequestItemDelete();
98 virtual void PreRemoveAbstractItem(int aIndex) {}
99 virtual void AbstractItemParentChanged() {}
100 virtual void AddedToModel() {}
102 {
103 if (mItemParentPtr != aParent)
104 {
105 mItemParentPtr = aParent;
107 }
108 }
109
110 bool BeginSetModel(AbstractModel* aModelPtr);
111
112 void FinalizeSetModel();
113 bool IsInModel() const { return mModelPtr != nullptr; }
114
115private:
116 void ReIndexChildrenP(int aFirstIndex);
117
118 AbstractModelItem* mItemParentPtr;
119 AbstractModel* mModelPtr;
120 int mItemIndex;
121 std::vector<AbstractModelItem*> mItemChildren;
122};
123} // namespace wizard
124#endif
#define VI_EXPORT
Definition ViExport.hpp:38
Objects inheriting this class may be added to a AbstractModel.
Definition AbstractModel.hpp:65
AbstractModelItem()
Definition AbstractModel.cpp:158
virtual void AbstractItemParentChanged()
Definition AbstractModel.hpp:99
virtual void PreRemoveAbstractItem(int aIndex)
Definition AbstractModel.hpp:98
virtual void AddedToModel()
Definition AbstractModel.hpp:100
AbstractModel * GetModel() const
Definition AbstractModel.hpp:76
bool IsInModel() const
Definition AbstractModel.hpp:113
AbstractModelItem * GetAbstractItem(int aIdx) const
Definition AbstractModel.hpp:77
AbstractModelItem * GetAbstractItemParent() const
Definition AbstractModel.hpp:72
int GetItemIndex()
Definition AbstractModel.hpp:74
void SetAbstractItemParent(AbstractModelItem *aParent)
Definition AbstractModel.hpp:101
int GetAbstractItemCount() const
Definition AbstractModel.hpp:78
friend class AbstractModel
Definition AbstractModel.hpp:67
Implements a model of a tree where each item in the tree is derived from AbstractModelItem.
Definition AbstractModel.hpp:27
AbstractModelItem * mRootPtr
Definition AbstractModel.hpp:59
int mChangeMode
Definition AbstractModel.hpp:60
friend class AbstractModelItem
Definition AbstractModel.hpp:29
AbstractModel()
Definition AbstractModel.hpp:31
Definition Runner.hpp:22
Copyrights Multiple, All Rights Reserved