aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Workflow/Nodes/TextNodes.cpp
blob: 4628dd3ba7ded7cd089293995a498fde71f1eed0 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "TextNodes.hpp"

#include "Model/Workflow/Evaluation.hpp"
#include "Model/Workflow/Values/Basic.hpp"
#include "Utils/Macros.hpp"
#include "Utils/RTTI.hpp"
#include "Utils/Variant.hpp"

#include <cassert>
#include <utility>
#include <variant>
#include <vector>

class TextFormatterNode::Impl
{
public:
	template <class TFunction>
	static void ForArguments(std::vector<Element>::iterator begin, std::vector<Element>::iterator end, const TFunction& func)
	{
		for (auto it = begin; it != end; ++it) {
			auto& elm = *it;
			if (auto arg = std::get_if<Argument>(&elm)) {
				func(*arg);
			}
		}
	}

	/// Find the pin index that the \c elmIdx -th element should have, based on the elements coming before it.
	static int FindPinForElement(const std::vector<Element>& vec, int elmIdx)
	{
		for (int i = elmIdx; i >= 0; --i) {
			auto& elm = vec[i];
			if (auto arg = std::get_if<Argument>(&elm)) {
				return arg->PinIdx + 1;
			}
		}
		return 0;
	}
};

BaseValue::Kind TextFormatterNode::ArgumentTypeToValueKind(TextFormatterNode::ArgumentType arg)
{
	switch (arg) {
		case NumericArgument: return BaseValue::KD_Numeric;
		case TextArgument: return BaseValue::KD_Text;
		case DateTimeArgument: return BaseValue::KD_DateTime;
	}
}

bool TextFormatterNode::IsInstance(const WorkflowNode* node)
{
	return node->GetKind() == KD_TextFormatting;
}

TextFormatterNode::TextFormatterNode()
	: WorkflowNode(KD_TextFormatting, false)
{
}

int TextFormatterNode::GetElementCount() const
{
	return mElements.size();
}

const TextFormatterNode::Element& TextFormatterNode::GetElement(int idx) const
{
	return mElements[idx];
}

void TextFormatterNode::SetElement(int idx, std::string text)
{
	assert(idx >= 0 && idx < mElements.size());

	std::visit(
		Overloaded{
			[&](const std::string& original) { mMinOutputChars -= original.size(); },
			[&](const Argument& original) { PreRemoveElement(idx); },
		},
		mElements[idx]);

	mMinOutputChars += text.size();
	mElements[idx] = std::move(text);
}

void TextFormatterNode::SetElement(int idx, ArgumentType argument)
{
	assert(idx >= 0 && idx < mElements.size());

	std::visit(
		Overloaded{
			[&](const std::string& original) {
				mMinOutputChars -= original.size();

				mElements[idx] = Argument{
					.Type = argument,
					.PinIdx = Impl::FindPinForElement(mElements, idx),
				};
				/* `original` is invalid from this point */
			},
			[&](const Argument& original) {
				int pinIdx = original.PinIdx;

				// Create pin
				auto& pin = mInputs[pinIdx];
				pin.MatchingType = ArgumentTypeToValueKind(argument);

				// Create element
				mElements[idx] = Argument{
					.Type = argument,
					.PinIdx = pinIdx,
				};
				/* `original` is invalid from this point */
			},
		},
		mElements[idx]);
}

void TextFormatterNode::InsertElement(int idx, std::string text)
{
	assert(idx >= 0);
	if (idx >= mElements.size()) AppendElement(std::move(text));

	mMinOutputChars += text.size();
	mElements.insert(mElements.begin() + idx, std::move(text));
}

void TextFormatterNode::InsertElement(int idx, ArgumentType argument)
{
	assert(idx >= 0);
	if (idx >= mElements.size()) AppendElement(argument);

	int pinIdx = Impl::FindPinForElement(mElements, idx);

	// Create pin
	auto& pin = InsertInputPin(pinIdx);
	pin.MatchingType = ArgumentTypeToValueKind(argument);

	// Create element
	mElements.insert(
		mElements.begin() + idx,
		Argument{
			.Type = argument,
			.PinIdx = pinIdx,
		});
}

void TextFormatterNode::AppendElement(std::string text)
{
	mMinOutputChars += text.size();
	mElements.push_back(std::move(text));
}

void TextFormatterNode::AppendElement(ArgumentType argument)
{
	int pinIdx = mInputs.size();
	// Create pin
	mInputs.push_back(InputPin{});
	mInputs.back().MatchingType = ArgumentTypeToValueKind(argument);
	// Creat eelement
	mElements.push_back(Argument{
		.Type = argument,
		.PinIdx = pinIdx,
	});
}

void TextFormatterNode::RemoveElement(int idx)
{
	assert(idx >= 0 && idx < mElements.size());

	PreRemoveElement(idx);
	if (auto arg = std::get_if<Argument>(&mElements[idx])) {
		RemoveInputPin(arg->PinIdx);
	}
	mElements.erase(mElements.begin() + idx);
}

void TextFormatterNode::Evaluate(WorkflowEvaluationContext& ctx)
{
	std::string result;
	result.reserve((size_t)(mMinOutputChars * 1.5f));

	auto HandleText = [&](const std::string& str) {
		result += str;
	};
	auto HandleArgument = [&](const Argument& arg) {
		switch (arg.Type) {
			case NumericArgument: {
				if (auto val = dyn_cast<NumericValue>(ctx.GetConnectionValue(mInputs[arg.PinIdx]))) {
					result += val->GetString();
				} else {
					// TODO localize
					ctx.ReportError("Non-numeric value connected to a numeric text format parameter.", *this);
				}
			} break;
			case TextArgument: {
				if (auto val = dyn_cast<TextValue>(ctx.GetConnectionValue(mInputs[arg.PinIdx]))) {
					result += val->GetValue();
				} else {
					// TODO localize
					ctx.ReportError("Non-text value connected to a textual text format parameter.", *this);
				}
			} break;
			case DateTimeArgument: {
				if (auto val = dyn_cast<DateTimeValue>(ctx.GetConnectionValue(mInputs[arg.PinIdx]))) {
					result += val->GetString();
				} else {
					// TODO localize
					ctx.ReportError("Non-date/time value connected to a date/time text format parameter.", *this);
				}
			} break;
		}
	};

	for (auto& elm : mElements) {
		std::visit(Overloaded{ HandleText, HandleArgument }, elm);
	}
}

void TextFormatterNode::PreRemoveElement(int idx)
{
	auto& elm = mElements[idx];
	if (auto arg = std::get_if<Argument>(&elm)) {
		RemoveInputPin(arg->PinIdx);
		Impl::ForArguments(
			mElements.begin() + idx + 1,
			mElements.end(),
			[&](Argument& arg) {
				arg.PinIdx--;
			});
	}
}