1 /*! 2 @file 3 @author Albert Semenov 4 @date 07/2012 5 */ 6 7 #include "Precompiled.h" 8 #include "Data.h" 9 #include "MyGUI.h" 10 11 namespace tools 12 { 13 Data()14 Data::Data() : 15 mType(nullptr), 16 mParent(nullptr), 17 mIndexSelected(MyGUI::ITEM_NONE) 18 { 19 } 20 ~Data()21 Data::~Data() 22 { 23 clear(); 24 } 25 setType(DataTypePtr _value)26 void Data::setType(DataTypePtr _value) 27 { 28 mType = _value; 29 30 mProperties.clear(); 31 32 if (mType != nullptr) 33 { 34 const DataType::VectorProperty& properties = mType->getProperties(); 35 for (DataType::VectorProperty::const_iterator property = properties.begin(); property != properties.end(); property++) 36 { 37 PropertyPtr data = Property::CreateInstance(*property, mWeakThis.lock()); 38 data->initialise(); 39 40 mProperties[(*property)->getName()] = data; 41 } 42 } 43 } 44 getType() const45 DataTypePtr Data::getType() const 46 { 47 return mType; 48 } 49 getParent()50 DataPtr Data::getParent() 51 { 52 return mParent; 53 } 54 getChilds() const55 const Data::VectorData& Data::getChilds() const 56 { 57 return mChilds; 58 } 59 clear()60 void Data::clear() 61 { 62 while (!mChilds.empty()) 63 { 64 DataPtr child = mChilds.back(); 65 removeChild(child); 66 } 67 } 68 getProperties() const69 const Data::MapProperty& Data::getProperties() const 70 { 71 return mProperties; 72 } 73 insertChild(size_t _index,DataPtr _child)74 void Data::insertChild(size_t _index, DataPtr _child) 75 { 76 MYGUI_ASSERT(_child != nullptr, "Child is nullptr"); 77 MYGUI_ASSERT(_child->getParent() == nullptr, "Child already attached"); 78 MYGUI_ASSERT(_child->getType() != nullptr, "Type not found"); 79 MYGUI_ASSERT(getType() != nullptr, "Type not found"); 80 MYGUI_ASSERT(getType()->isChild(_child->getType()->getName()), "Type is not child"); 81 82 MYGUI_ASSERT_RANGE_INSERT(_index, mChilds.size(), "Data::insertChild"); 83 84 if (_index == MyGUI::ITEM_NONE) 85 _index = mChilds.size(); 86 87 mChilds.insert(mChilds.begin() + _index, _child); 88 _child->mParent = mWeakThis.lock(); 89 } 90 addChild(DataPtr _child)91 void Data::addChild(DataPtr _child) 92 { 93 insertChild(MyGUI::ITEM_NONE, _child); 94 } 95 removeChild(DataPtr _child)96 void Data::removeChild(DataPtr _child) 97 { 98 MYGUI_ASSERT(_child->getParent() == mWeakThis.lock(), "Child not found"); 99 100 if (_child == getChildSelected()) 101 mIndexSelected = MyGUI::ITEM_NONE; 102 103 mChilds.erase(std::remove(mChilds.begin(), mChilds.end(), _child), mChilds.end()); 104 _child->mParent = nullptr; 105 } 106 getPropertyValue(const std::string & _name) const107 const std::string& Data::getPropertyValue(const std::string& _name) const 108 { 109 return getProperty(_name)->getValue(); 110 } 111 setPropertyValue(const std::string & _name,const std::string & _value) const112 void Data::setPropertyValue(const std::string& _name, const std::string& _value) const 113 { 114 getProperty(_name)->setValue(_value); 115 } 116 getChildIndex(DataPtr _child)117 size_t Data::getChildIndex(DataPtr _child) 118 { 119 if (_child == nullptr) 120 return MyGUI::ITEM_NONE; 121 122 for (size_t index = 0; index < mChilds.size(); index ++) 123 { 124 if (_child == mChilds[index]) 125 return index; 126 } 127 128 MYGUI_ASSERT(false, "Child data not found"); 129 } 130 getChildByIndex(size_t _index)131 DataPtr Data::getChildByIndex(size_t _index) 132 { 133 MYGUI_ASSERT_RANGE_AND_NONE(_index, mChilds.size(), "Data::getChildSelected"); 134 135 if (_index == MyGUI::ITEM_NONE) 136 return nullptr; 137 return mChilds[_index]; 138 } 139 getChildSelected()140 DataPtr Data::getChildSelected() 141 { 142 return getChildByIndex(mIndexSelected); 143 } 144 setChildSelected(DataPtr _child)145 void Data::setChildSelected(DataPtr _child) 146 { 147 mIndexSelected = getChildIndex(_child); 148 } 149 getProperty(const std::string & _name) const150 PropertyPtr Data::getProperty(const std::string& _name) const 151 { 152 MapProperty::const_iterator property = mProperties.find(_name); 153 MYGUI_ASSERT(property != mProperties.end(), "Property " << _name << " not found"); 154 155 return (*property).second; 156 } 157 CreateInstance()158 DataPtr Data::CreateInstance() 159 { 160 DataPtr result = DataPtr(new Data()); 161 result->mWeakThis = DataWeak(result); 162 return result; 163 } 164 165 } 166