ADTF  3.18.2
EditorPluginBase.qml
Go to the documentation of this file.
1 
9 import QtQuick 2.15
10 import QtQuick.Controls 2.15
11 import QtQuick.Layouts 1.4
12 
13 import Helpers 1.0
14 import Utils 1.0
15 
22 Item {
23 
26  property var filenameHelper: FilenameHelper
27 
29  property var targetModel
31  property var commandCallback
33  property var editorDescription
35  property var dialogManager
36 
37 
39  signal execute()
40 
41 
42  property var getPropertyDescriptions: function(component) {
43 
44  if(!component) {
45  console.log("Component is null");
46  console.trace()
47  }
48  else if (!component.base_properties) {
49  console.log("Component dosn't contains a property set");
50  console.trace()
51  }
52  else if (!component.base_properties.property_set_description) {
53  console.log("Component dosn't contains a property set description");
54  console.trace()
55  }
56  else {
57  return component.base_properties.property_set_description.property_descriptions;
58  }
59  }
60 
61  property var getProperties: function(component) {
62 
63  if(!component) {
64  console.log("Component is null");
65  console.trace()
66  }
67  else if (!component.base_properties) {
68  console.log("Component dosn't contains a property set");
69  console.trace()
70  }
71  else {
72  return component.base_properties.properties;
73  }
74  }
76 
82  function findService(name) {
83  var result = null
84  Utils.each(modelmanager.system.services, function(service) {
85  if(service.name === name) {
86  result = service
87  }
88  })
89  return result;
90  }
91 
99  function findProperty(component, name) {
100  var properties = getProperties(component)
101  var result = null
102 
103  Utils.each(properties, function(prop) {
104  if (prop.name === name) {
105  result = prop
106  }
107  })
108  return result
109  }
110 
118  function findPropertyDescription(component, name) {
119  var propertyDescs = getPropertyDescriptions(component)
120  var result = null
121 
122  Utils.each(propertyDescs, function(prop) {
123  if (prop.name === name) {
124  result = prop
125  }
126  })
127  return result
128  }
129 
139  function createProperty(component, name, value, type, link) {
140  var description = findPropertyDescription(component, name)
141 
142  if (link === undefined) {
143  link = ""
144  }
145 
146  if(!description) {
147  if (type !== undefined) {
148  commandCallback(function(options) {
149  return component.base_properties.createDynamicProperty(name, value, type, link, options)
150  })
151  return
152  }
153  else {
154  console.error("Could not find Plugindescription for property " + name)
155  }
156  }
157 
158  commandCallback(function(options) {
159  return component.base_properties.createProperty(description, value, link, options)
160  })
161  }
162 
168  function removeAllPropertiesWithPrefix(component, prefix)
169  {
170  var properties = getProperties(component)
171  var count = properties.rowCount();
172 
173  for(var i = count-1; i >= 0; --i)
174  {
175  var index = properties.index(i,0);
176  var prop = properties.data(index, 256);
177 
178  if(prop.name.startsWith(prefix))
179  {
180  commandCallback(function(options)
181  {
182  return component.base_properties.removeProperty(prop, options)
183  })
184  }
185  }
186  }
187 
194  function setProperty(component, name, value) {
195  console.assert(commandCallback, "Command callback not found")
196  console.assert(component, "Component not set")
197 
198  var property = findProperty(component, name)
199  if(property) {
200  commandCallback(function(options) {
201  return property.changeValue(value, options)
202  })
203  }
204  else {
205  createProperty(component, name, value)
206  }
207  }
208 
215  function getPropertyValue(component, name) {
216  console.assert(component, "Component not set")
217 
218  const property = findProperty(component, name)
219  if(property) {
220  return property.value
221  }
222 
223  const description = findPropertyDescription(component, name)
224  if(description) {
225  return description.value
226  }
227 
228  console.error("Property not found")
229  return ""
230  }
231 
237  function findInputPin(name) {
238  let result = null
239  Utils.each(targetModel.input_pins, function(pin) {
240  if (pin.name === name) {
241  result = pin
242  }
243  })
244  return result
245  }
246 
251  function createInputPin(name) {
252  const pin = findInputPin(name)
253  if(!pin) {
254  commandCallback(function(options) {
255  return targetModel.addInputPin(name, options)
256  })
257  }
258  }
259 
264  function listInputPins() {
265  let names = []
266  Utils.each(targetModel.input_pins, function(pin) {
267  names.push(pin.name)
268  })
269  return names
270  }
271 
276  function removeInputPin(pin) {
277  commandCallback(function(options) {
278  return targetModel.removeInputPin(pin, options)
279  })
280  }
281 
287  function findOutputPin(name) {
288  let result = null
289  Utils.each(targetModel.output_pins, function(pin) {
290  if (pin.name === name) {
291  result = pin
292  }
293  })
294  return result
295  }
296 
301  function createOutputPin(name) {
302  const pin = findOutputPin(name)
303  if(!pin) {
304  commandCallback(function(options) {
305  return targetModel.addOutputPin(name, options)
306  })
307  }
308  }
309 
314  function listOutputPins() {
315  let names = []
316  Utils.each(targetModel.output_pins, function(pin) {
317  names.push(pin.name)
318  })
319  return names
320  }
321 
326  function removeOutputPin(pin) {
327  commandCallback(function(options) {
328  return targetModel.removeOutputPin(pin, options)
329  })
330  }
331 
336  function listRunner()
337  {
338  let names = []
339  Utils.each(targetModel.runners, function(runner) {
340  names.push(runner.name)
341  })
342  return names
343  }
344 
350  function findRunner(runnerName) {
351  let result = null
352  Utils.each(targetModel.runners, function(runner) {
353  if (runner.name === runnerName) {
354  result = runner
355  }
356  })
357  return result
358  }
359 
364  function createRunner(name) {
365  const runner = findRunner(name)
366  if(!runner) {
367  commandCallback(function(options) {
368  return targetModel.addRunner(name, options)
369  })
370  }
371  }
372 
377  function removeRunner(runner) {
378  commandCallback(function(options) {
379  return targetModel.removeRunner(runner, options)
380  })
381  }
382 
391  function resolveAndGetAbsoluteUrl(path, basePath) {
392  if(!basePath){
393  basePath = "."
394  }
395  return editorDescription.GetAbsoluteUrl(path, basePath)
396  }
397 
402  function getPropertyFile() {
403  return targetModel.base_properties.getPropertyFileUrl()
404  }
405 
414  function launch(command, args, workingDirectory) {
415  return editorDescription.Launch(command, args, workingDirectory)
416  }
417 
425  function parseJsonResult(result)
426  {
427  console.log("result: " + result)
428  var regex = /json_result:([\s\S]*):end/i
429 
430  var match = regex.exec(result);
431  if(match.length < 2) {
432  throw "Couldn't find Json string"
433  }
434 
435  var json_result = match[1]
436  json_result = json_result.replace(/(\r\n|\n|\r)/gm,"")
437 
438  console.log("json_result: " + json_result)
439 
440  return JSON.parse(json_result)
441  }
442 
448  function forEach(array, callback) {
449  Utils.each(array, callback)
450  }
451 
457  function removeFromArray(array, callback) {
458  var newArray = []
459  forEach(array, function(item) {
460  if(!callback(item))
461  {
462  newArray.push(item)
463  }
464  });
465  return newArray
466  }
467 
475  function contains(array, value, compare_callback) {
476  var found = false;
477  forEach(array, function(item) {
478  if(compare_callback && compare_callback(item, value)) {
479  found = true
480  }
481  else if(item === value) {
482  found = true
483  }
484  })
485  return found;
486  }
487 
500  function getContext(model) {
501  // If no model is provided, use the default targetModel.
502  if (!model) model = targetModel;
503 
504  // Retrieve the context using the ModelManager.
505  return modelmanager.getContext(targetModel);
506  }
507 
512  function getEnvironmentFiles() {
513  let context = root.getContext();
514  let result = [];
515  Utils.each(context["environment"].environmentfiles, item => result.push(item));
516  return result;
517  }
518 
523  function getPlugindescriptionFiles() {
524  let context = root.getContext();
525  let result = [];
526  Utils.each(context["plugindescription"].plugindescriptions, item => result.push(item));
527  return result;
528  }
529 
534  function getGraph() {
535  let context = root.getContext();
536  return context["graph"];
537  }
538 
543  function getSystem() {
544  let context = root.getContext();
545  return context["system"];
546  }
547 
552  function getSession() {
553  let context = root.getContext();
554  return context["session"];
555  }
556 
557 
558  Component.onCompleted: {
559  execute()
560  }
561 }
562