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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*!
@file
@author Albert Semenov
@date 07/2012
*/
#include "Precompiled.h"
#include "Data.h"
#include "MyGUI.h"
namespace tools
{
Data::Data() :
mType(nullptr),
mParent(nullptr),
mIndexSelected(MyGUI::ITEM_NONE)
{
}
Data::~Data()
{
clear();
}
void Data::setType(DataTypePtr _value)
{
mType = _value;
mProperties.clear();
if (mType != nullptr)
{
const DataType::VectorProperty& properties = mType->getProperties();
for (DataType::VectorProperty::const_iterator property = properties.begin(); property != properties.end(); property++)
{
PropertyPtr data = Property::CreateInstance(*property, mWeakThis.lock());
data->initialise();
mProperties[(*property)->getName()] = data;
}
}
}
DataTypePtr Data::getType() const
{
return mType;
}
DataPtr Data::getParent()
{
return mParent;
}
const Data::VectorData& Data::getChilds() const
{
return mChilds;
}
void Data::clear()
{
while (!mChilds.empty())
{
DataPtr child = mChilds.back();
removeChild(child);
}
}
const Data::MapProperty& Data::getProperties() const
{
return mProperties;
}
void Data::insertChild(size_t _index, DataPtr _child)
{
MYGUI_ASSERT(_child != nullptr, "Child is nullptr");
MYGUI_ASSERT(_child->getParent() == nullptr, "Child already attached");
MYGUI_ASSERT(_child->getType() != nullptr, "Type not found");
MYGUI_ASSERT(getType() != nullptr, "Type not found");
MYGUI_ASSERT(getType()->isChild(_child->getType()->getName()), "Type is not child");
MYGUI_ASSERT_RANGE_INSERT(_index, mChilds.size(), "Data::insertChild");
if (_index == MyGUI::ITEM_NONE)
_index = mChilds.size();
mChilds.insert(mChilds.begin() + _index, _child);
_child->mParent = mWeakThis.lock();
}
void Data::addChild(DataPtr _child)
{
insertChild(MyGUI::ITEM_NONE, _child);
}
void Data::removeChild(DataPtr _child)
{
MYGUI_ASSERT(_child->getParent() == mWeakThis.lock(), "Child not found");
if (_child == getChildSelected())
mIndexSelected = MyGUI::ITEM_NONE;
mChilds.erase(std::remove(mChilds.begin(), mChilds.end(), _child), mChilds.end());
_child->mParent = nullptr;
}
const std::string& Data::getPropertyValue(const std::string& _name) const
{
return getProperty(_name)->getValue();
}
void Data::setPropertyValue(const std::string& _name, const std::string& _value) const
{
getProperty(_name)->setValue(_value);
}
size_t Data::getChildIndex(DataPtr _child)
{
if (_child == nullptr)
return MyGUI::ITEM_NONE;
for (size_t index = 0; index < mChilds.size(); index ++)
{
if (_child == mChilds[index])
return index;
}
MYGUI_ASSERT(false, "Child data not found");
}
DataPtr Data::getChildByIndex(size_t _index)
{
MYGUI_ASSERT_RANGE_AND_NONE(_index, mChilds.size(), "Data::getChildSelected");
if (_index == MyGUI::ITEM_NONE)
return nullptr;
return mChilds[_index];
}
DataPtr Data::getChildSelected()
{
return getChildByIndex(mIndexSelected);
}
void Data::setChildSelected(DataPtr _child)
{
mIndexSelected = getChildIndex(_child);
}
PropertyPtr Data::getProperty(const std::string& _name) const
{
MapProperty::const_iterator property = mProperties.find(_name);
MYGUI_ASSERT(property != mProperties.end(), "Property " << _name << " not found");
return (*property).second;
}
DataPtr Data::CreateInstance()
{
DataPtr result = DataPtr(new Data());
result->mWeakThis = DataWeak(result);
return result;
}
}
|