File: PropertyUtility.cpp

package info (click to toggle)
mygui 3.4.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,792 kB
  • sloc: cpp: 133,849; ansic: 30,249; xml: 15,794; cs: 12,601; tcl: 776; python: 400; makefile: 35; sh: 4
file content (113 lines) | stat: -rw-r--r-- 2,811 bytes parent folder | download
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
/*!
	@file
	@author		Albert Semenov
	@date		07/2012
*/

#include "Precompiled.h"
#include "PropertyUtility.h"
#include "FactoryManager.h"
#include "ActionManager.h"
#include "ActionChangeDataProperty.h"
#include "DataManager.h"

namespace tools
{

	bool PropertyUtility::isUniqueName(DataPtr _data, std::string_view _propertyName)
	{
		DataPtr parent = _data->getParent();
		const std::string& name = _data->getPropertyValue(_propertyName);

		const Data::VectorData& childs = parent->getChilds();
		for (const auto& child : childs)
		{
			if (child->getPropertyValue(_propertyName) == name && child != _data)
				return false;
		}

		return true;
	}

	void PropertyUtility::executeAction(PropertyPtr _property, std::string_view _value, bool _merge)
	{
		if (_property->getValue() == _value)
			return;

		const std::string& actionName = _property->getType()->getAction();

		ActionChangeDataProperty* action =
			components::FactoryManager::GetInstance().CreateItem<ActionChangeDataProperty>(actionName);
		if (action != nullptr)
		{
			action->setProperty(_property);
			action->setValue(_value);
			action->setMerge(_merge);

			ActionManager::getInstance().doAction(action);
		}
	}

	void PropertyUtility::storeUniqueNameProperty(
		std::string_view _propertyName,
		std::string_view _propertyUnique,
		DataPtr _parent,
		VectorPairProperty& _store)
	{
		const Data::VectorData& childs = _parent->getChilds();
		for (const auto& child : childs)
		{
			bool unique = isUniqueName(child, _propertyName);
			PropertyPtr property = child->getProperty(_propertyUnique);

			if (property->getValue<bool>() != unique)
			{
				_store.emplace_back(property, property->getValue());
				property->setValue(unique);
			}
		}
	}

	void PropertyUtility::restoreUniqueNameProperty(VectorPairProperty& _store)
	{
		for (auto& value : _store)
			value.first->setValue(value.second);
		_store.clear();
	}

	PropertyPtr PropertyUtility::getPropertyByName(
		DataPtr _data,
		std::string_view _dataType,
		std::string_view _propertyName)
	{
		if (_data == nullptr)
			return nullptr;

		if (_data->getType()->getName() == _dataType)
			return _data->getProperty(_propertyName);

		return getPropertyByName(_data->getChildSelected(), _dataType, _propertyName);
	}

	PropertyPtr PropertyUtility::getPropertyByName(std::string_view _dataType, std::string_view _propertyName)
	{
		return getPropertyByName(DataManager::getInstance().getRoot(), _dataType, _propertyName);
	}

	bool PropertyUtility::isDataSelected(DataPtr _data)
	{
		return isDataSelected(DataManager::getInstance().getRoot(), _data);
	}

	bool PropertyUtility::isDataSelected(DataPtr _parent, DataPtr _data)
	{
		if (_parent == nullptr)
			return false;

		if (_parent == _data)
			return true;

		return isDataSelected(_parent->getChildSelected(), _data);
	}

}