File: param.cpp

package info (click to toggle)
olive-editor 20200620-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 40,228 kB
  • sloc: cpp: 51,932; sh: 56; makefile: 7; xml: 7
file content (291 lines) | stat: -rw-r--r-- 6,405 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/***

  Olive - Non-Linear Video Editor
  Copyright (C) 2019 Olive Team

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

***/

#include "param.h"

#include <QDebug>
#include <QMatrix4x4>
#include <QVector2D>
#include <QVector3D>
#include <QVector4D>

#include "node/node.h"
#include "node/input.h"
#include "node/output.h"
#include "render/color.h"

OLIVE_NAMESPACE_ENTER

NodeParam::NodeParam(const QString &id) :
  id_(id),
  connectable_(true)
{
  Q_ASSERT(!id_.isEmpty());
}

NodeParam::~NodeParam()
{
  // Clear all connected edges
  while (!edges_.isEmpty()) {
    DisconnectEdge(edges_.last());
  }
}

const QString NodeParam::id() const
{
  return id_;
}

QString NodeParam::name()
{
  if (name_.isEmpty()) {
    return tr("Value");
  }

  return name_;
}

void NodeParam::set_name(const QString &name)
{
  name_ = name;
}

Node *NodeParam::parentNode() const
{
  QObject* p = parent();

  while (p) {
    Node* cast_test = dynamic_cast<Node*>(p);
    if (cast_test) {
      return cast_test;
    } else {
      p = p->parent();
    }
  }

  return nullptr;
}

int NodeParam::index()
{
  return parentNode()->IndexOfParameter(this);
}

bool NodeParam::is_connected() const
{
  return !edges_.isEmpty();
}

bool NodeParam::is_connectable() const
{
  return connectable_;
}

void NodeParam::set_connectable(bool connectable)
{
  connectable_ = connectable;
}

const QVector<NodeEdgePtr> &NodeParam::edges()
{
  return edges_;
}

void NodeParam::DisconnectAll()
{
  while (!edges_.isEmpty()) {
    DisconnectEdge(edges_.first());
  }
}

NodeEdgePtr NodeParam::ConnectEdge(NodeOutput *output, NodeInput *input)
{
  if (!input->is_connectable()) {
    return nullptr;
  }

  // If the input can only accept one input (the default) and has one already, disconnect it
  DisconnectForNewOutput(input);

  // Make sure it's not a duplicate of an edge that already exists
  foreach (NodeEdgePtr existing, input->edges()) {
    if (existing->output() == output) {
      return nullptr;
    }
  }

  // Ensure both nodes are in the same graph
  Q_ASSERT(output->parentNode()->parent() == input->parentNode()->parent());

  NodeEdgePtr edge = std::make_shared<NodeEdge>(output, input);

  // The nodes should never be the same, and since we lock both nodes here, this can lead to a entire program freeze
  // that's difficult to diagnose. This makes that issue very clear.
  Q_ASSERT(output->parentNode() != input->parentNode());

  output->edges_.append(edge);
  input->edges_.append(edge);

  // Emit a signal than an edge was added (only one signal needs emitting)
  emit input->EdgeAdded(edge);

  return edge;
}

void NodeParam::DisconnectEdge(NodeEdgePtr edge)
{
  NodeOutput* output = edge->output();
  NodeInput* input = edge->input();

  output->edges_.removeOne(edge);
  input->edges_.removeOne(edge);

  emit input->EdgeRemoved(edge);
}

void NodeParam::DisconnectEdge(NodeOutput *output, NodeInput *input)
{
  for (int i=0;i<output->edges_.size();i++) {
    NodeEdgePtr edge = output->edges_.at(i);
    if (edge->input() == input) {
      DisconnectEdge(edge);
      break;
    }
  }
}

NodeEdgePtr NodeParam::DisconnectForNewOutput(NodeInput *input)
{
  // If the input can only accept one input (the default) and has one already, disconnect it
  if (!input->edges_.isEmpty()) {
    NodeEdgePtr edge = input->edges_.first();

    DisconnectEdge(edge);

    return edge;
  }

  return nullptr;
}

QString NodeParam::GetPrettyDataTypeName(const NodeParam::DataType &type)
{
  switch (type) {
  case kNone:
    return tr("None");
  case kInt:
  case kCombo:
    return tr("Integer");
  case kFloat:
    return tr("Float");
  case kRational:
    return tr("Rational");
  case kBoolean:
    return tr("Boolean");
  case kColor:
    return tr("Color");
  case kMatrix:
    return tr("Matrix");
  case kText:
    return tr("Text");
  case kFont:
    return tr("Font");
  case kFile:
    return tr("File");
  case kTexture:
    return tr("Texture");
  case kSamples:
    return tr("Samples");
  case kFootage:
    return tr("Footage");
  case kVec2:
    return tr("Vector 2D");
  case kVec3:
    return tr("Vector 3D");
  case kVec4:
    return tr("Vector 4D");

  case kDecimal:
  case kNumber:
  case kString:
  case kBuffer:
  case kVector:
  case kShaderJob:
  case kSampleJob:
  case kGenerateJob:
  case kAny:
    break;
  }

  return tr("Unknown");
}

QByteArray NodeParam::ValueToBytes(const NodeParam::DataType &type, const QVariant &value)
{
  switch (type) {
  case kInt: return ValueToBytesInternal<int>(value);
  case kFloat: return ValueToBytesInternal<float>(value);
  case kColor: return ValueToBytesInternal<Color>(value);
  case kText: return value.toString().toUtf8();
  case kBoolean: return ValueToBytesInternal<bool>(value);
  case kFont: return value.toString().toUtf8();
  case kFile: return value.toString().toUtf8();
  case kMatrix: return ValueToBytesInternal<QMatrix4x4>(value);
  case kRational: return ValueToBytesInternal<rational>(value);
  case kVec2: return ValueToBytesInternal<QVector2D>(value);
  case kVec3: return ValueToBytesInternal<QVector3D>(value);
  case kVec4: return ValueToBytesInternal<QVector4D>(value);
  case kCombo: return ValueToBytesInternal<int>(value);

  // These types have no persistent input
  case kNone:
  case kFootage:
  case kTexture:
  case kSamples:
  case kDecimal:
  case kNumber:
  case kString:
  case kBuffer:
  case kVector:
  case kShaderJob:
  case kSampleJob:
  case kGenerateJob:
  case kAny:
    break;
  }

  return QByteArray();
}

template<typename T>
QByteArray NodeParam::ValueToBytesInternal(const QVariant &v)
{
  QByteArray bytes;

  int size_of_type = sizeof(T);

  bytes.resize(size_of_type);
  T raw_val = v.value<T>();
  memcpy(bytes.data(), &raw_val, static_cast<size_t>(size_of_type));

  return bytes;
}

OLIVE_NAMESPACE_EXIT