Developer documentation | Axl-2.5.1

axlDataDynamic.cpp
Go to the documentation of this file.
1 /* axlDataDynamic.cpp ---
2  *
3  * Author: Anais Ducoffe
4  * Copyright (C)
5  * Created:
6  * Version: $Id$
7  * Last-Updated:
8  * By:
9  * Update #:
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlDataDynamic.h"
22 #include <axlCore/axlCone.h>
23 #include <axlCore/axlCylinder.h>
24 #include <axlCore/axlCircleArc.h>
25 #include <axlCore/axlEllipsoid.h>
26 #include <axlCore/axlLine.h>
27 #include <axlCore/axlPlane.h>
28 #include <axlCore/axlPoint.h>
29 #include <axlCore/axlSphere.h>
30 #include <axlCore/axlTorus.h>
31 #include <axlCore/axlDouble.h>
32 #include <axlCore/axlInteger.h>
37 
38 #include <dtkCoreSupport/dtkGlobal.h>
39 #include <dtkCoreSupport/dtkAbstractProcessFactory.h>
40 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
41 #include <QtGui>
42 
43 
44 class axlDataDynamicPrivate
45 {
46 public:
47  QList<axlAbstractData *> inputs;
48  QList<axlAbstractData *> parameters;
49  axlAbstractProcess *process;
50  QList<axlAbstractData*> output;
51  int nbUp;
52  //determine if it is a dynamic object or just to remind a computation.
54 
55 };
56 
57 axlDataDynamic::axlDataDynamic(void) : axlAbstractDataComposite(), d(new axlDataDynamicPrivate)
58 {
59  this->setObjectName(this->identifier());
60  d->nbUp = 0;
61  d->process = NULL;
62  d->type = axlDataDynamic::Dynamic;
63 
64 }
65 
67 {
68 
69  if(d->process){
70  delete d->process;
71  d->process = NULL;
72  }
73 
74  for(int k = 0; k < d->inputs.size();k++){
75  disconnect(d->inputs.at(k), SIGNAL(modifiedGeometry()),this, SLOT(update()));
76  if(!dynamic_cast<axlDouble *>(d->inputs.at(k)) && !dynamic_cast<axlInteger *>(d->inputs.at(k))){
77  disconnect(d->inputs.at(k), SIGNAL(destroyed()),this, SLOT(onRemoved()));
78  }else{
79  delete d->inputs.at(k);
80  }
81  }
82  d->inputs.clear();
83  d->parameters.clear();
84 
85 
86  if(!d->output.isEmpty()){
87  for(int i = 0; i < d->output.size();i++){
88  if(d->output.at(i)){
89  delete d->output.at(i);
90  }
91  }
92  d->output.clear();
93  }
94 
95  delete d;
96  d = NULL;
97 
98 }
99 
101 
105  emit destroyed(this);
106  delete this;
107 
108 }
109 
111 
115  return d->type;
116 
117 }
118 
120 
124  d->type = type;
125 
126 }
127 
129 
133  return d->nbUp;
134 
135 }
136 
138 
142  if (d->output.size() > channel) {
143  return d->output.at(channel);
144  } else {
145  qDebug() << Q_FUNC_INFO << "no channel-th output available";
146  return NULL;
147  }
148 }
149 
151 
155  if (d->output.size() > 0) {
156  return d->output.at(0);
157  } else {
158  qDebug() << Q_FUNC_INFO << "no output computed" << d->process->identifier();
159  return NULL;
160  }
161 
162 }
163 
164 
166 
170  d->inputs.clear();
171 }
172 
174 
177 bool axlDataDynamic::isNotInputProcess(dtkAbstractData * data){
178  if(dynamic_cast<axlAbstractData *>(data)){
179  return !(d->inputs.contains(dynamic_cast<axlAbstractData *>(data)));
180  }
181 
182  return true;
183 }
184 
186 
189 bool axlDataDynamic::isNotOutputProcess(dtkAbstractData * data){
190  if(dynamic_cast<axlAbstractData *>(data)){
191  return !(d->output.contains(dynamic_cast<axlAbstractData *>(data)));
192  }
193 
194  return true;
195 }
196 
197 
199 
202 QList<axlAbstractData *> axlDataDynamic::inputs(void){
203  return d->inputs;
204 }
205 
207 
211  return d->process;
212 }
213 
215 
218 void axlDataDynamic::setInput(dtkAbstractData *data, int channel)
219 {
220  if (axlAbstractData *axlData = dynamic_cast<axlAbstractData *>(data)) {
221  d->inputs.append(axlData);
222 // qDebug() << "---------------------";
223 // qDebug() << "data :" << &data;
224 // for(int i = 0; i < d->inputs.count(); i++)
225 // qDebug() << &(d->inputs.at(i));
226 
227  if (d->type == axlDataDynamic::Dynamic) {
228  connect(axlData, SIGNAL(modifiedGeometry()), this, SLOT(update()));
229  }
230  if (!dynamic_cast<axlDouble *>(data) && !dynamic_cast<axlInteger *>(data)) {
231  // connect(this, SIGNAL(modifiedGeometry()), axlData, SIGNAL(modifiedGeometry()));
232  connect(axlData, SIGNAL(destroyed()), this, SLOT(onRemoved()));
233  }
234  }
235 }
236 
238 
242  if(d->process!= NULL)
243  d->process->setParameter(value);
244  else
245  dtkWarn()<<"axlDataDynamic: process NULL";
246 }
247 
249 
252 void axlDataDynamic::setParameter(int value, int channel){
253  if(d->process!= NULL)
254  d->process->setParameter(value,channel);
255  else
256  dtkWarn()<<"axlDataDynamic: process NULL";
257 }
258 
260 
263 void axlDataDynamic::setParameter(double value){
264  if(d->process!= NULL)
265  d->process->setParameter(value);
266  else
267  dtkWarn()<<"axlDataDynamic: process NULL";
268 }
269 
271 
274 void axlDataDynamic::setParameter(double value, int channel){
275  if(d->process!= NULL)
276  d->process->setParameter(value,channel);
277  else
278  dtkWarn()<<"axlDataDynamic: process NULL";
279 }
280 
282 
285 int axlDataDynamic::setProcess(QString processName){
286  d->process = dynamic_cast<axlAbstractProcess *>(dtkAbstractProcessFactory::instance()->create(processName));
287  dtkWarn() << "Process" << processName;
288  if(d->process)
289  return 1;
290  else {
291  dtkWarn() << "Process" << processName << "not found";
292  return 0;
293  }
294 }
295 
297 
301  d->process = dynamic_cast<axlAbstractProcess *>(dtkAbstractProcessFactory::instance()->create(process->identifier()));
302  d->process->copyProcess(process);
303 
304 }
305 
306 /* dtk overload */
308 
311 QString axlDataDynamic::description(void) const
312 {
313  if(d->output.empty()){
314  return " create a dynamic object";
315  }else{
316  if(!d->output.isEmpty()){
317 
318  axlAbstractData *output = dynamic_cast<axlAbstractData *>(d->output.at(0));
319  return " create a dynamic object \n" + output->description();
320  }else{
321  return "";
322  }
323  }
324 }
325 
326 
328 
331 QString axlDataDynamic::identifier(void) const
332 {
333  return "axlDataDynamic";
334 }
335 
336 
338 
342  return d->output.size();
343 }
344 
345 
347 
351 {
352  // static axlDataDynamic *last = NULL;
353 
354  if (!(d->output.empty())) {
355  for (int i = 0; i < d->output.size(); i++) {
356  this->remove(d->output.at(i));
357  }
358  d->output.clear();
359  }
360 
361  //Count number of parameters
362  int compteurParametre = 0;
363  for (int i = 0;i < d->inputs.size(); i++) {
364  if(dynamic_cast<axlDouble *>(d->inputs.at(i)) || dynamic_cast<axlInteger *>(d->inputs.at(i)) ) {
365  compteurParametre++;
366  }
367  }
368 
369  int countData = 0;
370  int countParametersDouble = 0;
371  int countParametersInt = 0;
372 
373  int result = 0;
374 
375  if (d->process) {
376  for (int i = 0; i < d->inputs.size(); i++) {
377  if (axlDouble *axldata = dynamic_cast<axlDouble *>(d->inputs.at(i))) {
378  if (compteurParametre == 1){
379  d->process->setParameter(axldata->value());
380  } else {
381  d->process->setParameter(axldata->value(), countParametersDouble);
382  }
383  countParametersDouble++;
384  } else if(axlInteger *axldata = dynamic_cast<axlInteger *>(d->inputs.at(i))) {
385  if (compteurParametre == 1) {
386  d->process->setParameter(axldata->value());
387  } else {
388  d->process->setParameter(axldata->value(), countParametersInt);
389  }
390  countParametersInt++;
391  } else {
392  if (d->nbUp == 0) {
393  d->process->setInput(d->inputs.at(i),countData);
394  countData++;
395  }
396  }
397  }
398  result = d->process->update();
399 
400  } else {
401  dtkWarn()<<Q_FUNC_INFO << "no process found";
402  return;
403  }
404 
405  if (result == 0) {
406  dtkWarn() << Q_FUNC_INFO << "problem in process, should return 1" << d->process->identifier() << "update";
407  return;
408  }
409 
410  if (d->process->channelCount() > 1) {
411  for (int i = 0; i< d->process->channelCount(); i++) {
412  //if(d->output.isEmpty()){
413  d->output.append(dynamic_cast<axlAbstractData *>(d->process->output(i)));
414  //}
415 
416  if(dynamic_cast<axlAbstractData *>(d->output.at(i))){
417  axlAbstractData *axlData = dynamic_cast<axlAbstractData *>(d->output.at(i));
418  axlData->setEditable(false);
419  // axlData->setColor(this->color());
420  // axlData->setOpacity(this->opacity());
421  // axlData->setShader(this->shader());
422  // axlData->setSize(this->size());
423  axlData->setObjectName(axlData->identifier());
424  //axlData->setParent(this);
425  axlData->touchGeometry();
426  }
427 
428  }
429 
430  } else {
431  d->output.append(dynamic_cast<axlAbstractData *>(d->process->output()));
432 
433  if(dynamic_cast<axlAbstractData *>(d->output.at(0))){
434  axlAbstractData *axlData =dynamic_cast<axlAbstractData *>(d->output.at(0));
435  axlData->setEditable(false);
436  // axlData->setColor(this->color());
437  // axlData->setOpacity(this->opacity());
438  // axlData->setShader(this->shader());
439  // axlData->setSize(this->size());
440  axlData->setObjectName(axlData->identifier());
441  //axlData->setParent(this);
442  axlData->touchGeometry();
443  }
444 
445  }
446 
447  for(int i = 0; i < d->inputs.size(); i++){
448  for (int j = 0; j< d->output.size();j++){
449  if(d->output.at(j) == d->inputs.at(i)){
450  d->inputs.at(i)->setEditable(true);
451  }
452  }
453  }
454 
455 
456  for(int i = 0; i < d->output.size();i++){
457  this->add(d->output.at(i));
458  }
459 
460  d->nbUp += 1;
461 
462  if(!this->fields().isEmpty())
463  this->touchField();
464 
465  this->touchGeometry();
466 }
467 
468 
469 QVariantList axlDataDynamic::convertDataToQVariant(void) const{
470  QVariantList list;
471  QVariant id = QVariant::fromValue(identifier());
472  list.append(id);
473  QVariant nbUp = QVariant::fromValue(d->nbUp);
474  list.append(nbUp);
475  QVariant process = QVariant::fromValue(d->process->identifier());
476  list.append(process);
477  if(d->type == axlDataDynamic::Dynamic){
478  QVariant type = QVariant::fromValue(1);
479  list.append(type);
480  }else{
481  QVariant type = QVariant::fromValue(2);
482  list.append(type);
483  }
484 
485 
486  //inputs
487  for(int i = 0; i < d->inputs.size();i++){
488  QVariantList in = d->inputs.at(i)->convertDataToQVariant();
489  int s = in.size();
490  list.append("input");
491  list.append(s);
492  list.append(in);
493 
494  }
495 
496  //parameters
497  for(int i = 0; i < d->parameters.size();i++){
498  QVariantList param = d->parameters.at(i)->convertDataToQVariant();
499  int s = param.size();
500  list.append("parameter");
501  list.append(s);
502  list.append(param);
503 
504  }
505 
506  //outputs
507  for(int i = 0; i < d->output.size();i++){
508  QVariantList out = d->output.at(i)->convertDataToQVariant();
509  int s = out.size();
510  list.append("output");
511  list.append(s);
512  list.append(out);
513 
514  }
515 
516  QVariant name = QVariant::fromValue(objectName());
517  list.append(name);
518  return list;
519 
520 }
521 
522 int axlDataDynamic::convertQVariantToData(const QVariantList &data){
523 
524  d->nbUp = data.at(1).toInt();
525  setProcess(data.at(2).toString());
526  int type = data.at(3).toInt();
527  if(type ==1){
528  d->type = axlDataDynamic::Dynamic;
529  }else{
530  d->type = axlDataDynamic::Static;
531  }
532 
533  int currentIndice = 4;
534  while(currentIndice < (data.size()-1)){
535  QString what = data.at(currentIndice).toString();
536  int count = data.at(currentIndice+1).toInt();
537  QString id = data.at(currentIndice+2).toString();
538  axlAbstractData *obj = dynamic_cast<axlAbstractData *>(dtkAbstractDataFactory::instance()->create(id));
539  QVariantList list;
540  for(int i = 0;i < count;i++){
541  list.append(data.at(currentIndice+2+i));
542  }
543  obj->convertQVariantToData(list);
544 
545  if(what=="input"){
546  d->inputs.append(obj);
547 
548 
549  }else if(what=="parameter"){
550  d->parameters.append(obj);
551 
552  }else{
553  d->output.append(obj);
554 
555  }
556  currentIndice = currentIndice+count+2;
557  }
558 
559  setObjectName(data.last().toString());
560 
561  return 1;
562 
563 }
564 
565 // /////////////////////////////////////////////////////////////////
566 // Type instanciation
567 // /////////////////////////////////////////////////////////////////
568 
569 dtkAbstractData *createAxlDataDynamic(void)
570 {
571  return new axlDataDynamic();
572 
573 }
574 
575 // /////////////////////////////////////////////////////////////////
576 // axlDataDynamic documentation
577 // /////////////////////////////////////////////////////////////////
578 
virtual void update(void)
Update the dynamic object.
void touchGeometry(void)
void add(dtkAbstractData *data)
axlDataDynamic::Type getType(void)
Return the type : dynamic if it is a dynamic object or static if it is just a computation node...
bool isNotInputProcess(dtkAbstractData *data)
Check whether the output of the process is a different object that one of its inputs.
virtual void touchField(void)
int setProcess(QString processName)
Create the dynamic object process only with the name of its latter.
virtual int numberOfChannels(void)
Return the number of output(s);.
QVariantList convertDataToQVariant(void) const
void setType(axlDataDynamic::Type type)
Sets the type of the object. Type could be dynamic or static.
Class axlDouble defines a double.
Definition: axlDouble.h:29
void onRemoved(void)
This slot is called whenever one of the dynamic object input is deleted.
void setEditable(bool edit)
virtual QString description(void) const
Return a description of the dynamic object.
virtual int convertQVariantToData(const QVariantList &data)
Modify properties and geometry variables of the axlAbstractData. Return 1 if the modification was suc...
axlAbstractData * outputs(void)
Return the first output of the list.
void modifiedGeometry(void)
dtkAbstractData * createAxlDataDynamic(void)
int convertQVariantToData(const QVariantList &data)
axlAbstractProcess * process(void)
Return the process.
Class axlDataDynamic defines a dynamic object.
void setInput(dtkAbstractData *data, int channel=0)
Add input;.
bool isNotOutputProcess(dtkAbstractData *data)
Check whether the data is an output of the process.
void setParameter(int parameter)
Set parameter.
Class axlInteger defines an integer.
Definition: axlInteger.h:28
QList< axlAbstractData * > inputs(void)
Return input list;.
int numberOfUpdate(void)
Return how many times the geometry of the dynamic object has been updated.
void clearInputs(void)
Clear input list.
virtual QString identifier(void) const
Return the identifier "axlDataDynamic".
virtual ~axlDataDynamic(void)
QList< axlAbstractField * > fields(void)
virtual QString identifier(void) const
Class axlAbstractData defines an API for all type of axel data.