File: inputarray.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 (215 lines) | stat: -rw-r--r-- 5,314 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
/***

  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 "inputarray.h"

#include <QApplication>

#include "common/xmlutils.h"
#include "node.h"

OLIVE_NAMESPACE_ENTER

NodeInputArray::NodeInputArray(const QString &id, const DataType &type, const QVariant &default_value) :
  NodeInput(id, type, default_value),
  default_value_(default_value)
{
}

bool NodeInputArray::IsArray() const
{
  return true;
}

int NodeInputArray::GetSize() const
{
  return sub_params_.size();
}

void NodeInputArray::Prepend()
{
  InsertAt(0);
}

void NodeInputArray::SetSize(int size)
{
  int old_size = GetSize();

  if (size == old_size) {
    return;
  }

  if (size < old_size) {
    // If the new size is less, delete all extraneous parameters
    for (int i=size;i<old_size;i++) {
      delete sub_params_.at(i);
    }
  }

  sub_params_.resize(size);

  if (size > old_size) {
    // If the new size is greater, create valid parameters for each slot
    for (int i=old_size;i<size;i++) {
      QString sub_id = id();
      sub_id.append(QString::number(i));

      Q_ASSERT(!parentNode()->HasParamWithID(sub_id));

      NodeInput* new_param = new NodeInput(sub_id, data_type(), default_value_);
      new_param->setParent(this);
      sub_params_.replace(i, new_param);

      connect(new_param, &NodeInput::ValueChanged, this, &NodeInput::ValueChanged);
      connect(new_param, &NodeInput::EdgeAdded, this, &NodeInputArray::SubParamEdgeAdded);
      connect(new_param, &NodeInput::EdgeRemoved, this, &NodeInputArray::SubParamEdgeRemoved);
    }
  }

  emit SizeChanged(size);
}

bool NodeInputArray::ContainsSubParameter(NodeInput *input) const
{
  return sub_params_.contains(input);
}

int NodeInputArray::IndexOfSubParameter(NodeInput *input) const
{
  return sub_params_.indexOf(input);
}

NodeInput *NodeInputArray::At(int index) const
{
  return sub_params_.at(index);
}

NodeInput *NodeInputArray::First() const
{
  return sub_params_.first();
}

NodeInput *NodeInputArray::Last() const
{
  return sub_params_.last();
}

const QVector<NodeInput *> &NodeInputArray::sub_params()
{
  return sub_params_;
}

void NodeInputArray::InsertAt(int index)
{
  // Add another input at the end
  Append();

  // Shift all connections from index down
  for (int i=sub_params_.size()-1;i>index;i--) {
    NodeInput* this_param = sub_params_.at(i);
    NodeInput* prev_param = sub_params_.at(i-1);

    if (this_param->is_connected()) {
      // Disconnect whatever is at this parameter (presumably its connection has already been copied so we can just remove it)
      NodeParam::DisconnectEdge(this_param->edges().first());
    }

    if (prev_param->is_connected()) {
      // Get edge here (only one since it's an input)
      NodeEdgePtr edge = prev_param->edges().first();

      // Disconnect it
      NodeParam::DisconnectEdge(edge);

      // Create a new edge between it and the next one down
      NodeParam::ConnectEdge(edge->output(),
                             this_param);
    }
  }
}

void NodeInputArray::Append()
{
  SetSize(GetSize() + 1);
}

void NodeInputArray::RemoveLast()
{
  SetSize(GetSize() - 1);
}

void NodeInputArray::RemoveAt(int index)
{
  // Shift all connections from index down
  for (int i=index;i<sub_params_.size();i++) {
    NodeInput* this_param = sub_params_.at(i);

    if (this_param->is_connected()) {
      // Disconnect current edge
      NodeParam::DisconnectEdge(this_param->edges().first());
    }

    if (i < sub_params_.size() - 1) {
      NodeInput* next_param = sub_params_.at(i + 1);
      if (next_param->is_connected()) {
        // Get edge from next param
        NodeEdgePtr edge = next_param->edges().first();

        // Disconnect it
        NodeParam::DisconnectEdge(edge);

        // Reconnect it to this param
        NodeParam::ConnectEdge(edge->output(),
                               this_param);
      }
    }
  }

  RemoveLast();
}

void NodeInputArray::LoadInternal(QXmlStreamReader *reader, XMLNodeData &xml_node_data, const QAtomicInt* cancelled)
{
  if (reader->name() == QStringLiteral("subparameters")) {
    while (XMLReadNextStartElement(reader)) {
      if (reader->name() == QStringLiteral("input")) {
        Append();
        At(GetSize() - 1)->Load(reader, xml_node_data, cancelled);
      } else {
        reader->skipCurrentElement();
      }
    }
  } else {
    NodeInput::Load(reader, xml_node_data, cancelled);
  }
}

void NodeInputArray::SaveInternal(QXmlStreamWriter *writer) const
{
  writer->writeStartElement("subparameters");

  foreach (NodeInput* sub, sub_params_) {
    sub->Save(writer);
  }

  writer->writeEndElement(); // subparameters
}

OLIVE_NAMESPACE_EXIT