Developer documentation | Axl-2.5.1

axlInspectorToolGeneric.cpp
Go to the documentation of this file.
3 
6 
7 #include <dtkCoreSupport/dtkAbstractProcess.h>
8 #include <dtkCoreSupport/dtkAbstractProcessFactory.h>
9 
11 
12 #include <QtCore>
13 #include <QtGui>
14 
15 #include <QObject>
16 
17 #include <QDoubleSpinBox>
18 #include <QSpinBox>
19 #include <QLabel>
20 #include <QComboBox>
21 
22 
23 class axlInspectorToolGenericInputInteger : public QSpinBox
24 {
25  Q_OBJECT
26 
27 public :
28  axlInspectorToolGenericInputInteger(axlAbstractProcess *p,QWidget *parent = 0,int value = 0, QString chan = "") : QSpinBox(parent){
29  process = p;
30  if(!chan.isEmpty()) // add a check to know whether it is really a number.
31  channel = chan.toInt();
32  else
33  channel = -1;
34 
35  this->setParent(parent);
36 
37  //default value of the input
38  this->setValue(value);
39 
40  connect(this, SIGNAL(valueChanged(int)), this, SLOT(addParameterInt(int)));
41 
42 
43  }
44 
46 
47  disconnect(this, SIGNAL(valueChanged(int)), this, SLOT(addParameterInt(int)));
48  }
49 
50 
51 public slots :
52  void addParameterInt(int value){
53  if(channel == -1){
54  process->setParameter(value);
55  }else{
56  process->setParameter(value, channel);
57  }
58 
59  }
60 
61 public :
63  int channel;
64 };
65 
66 
67 
68 // Double parameters
69 class axlInspectorToolGenericInputDouble : public QDoubleSpinBox
70 {
71  Q_OBJECT
72 
73 public :
74  axlInspectorToolGenericInputDouble(axlAbstractProcess *p, QWidget *parent = 0, double value=0.0, QString chan = ""):QDoubleSpinBox(parent){
75  process = p;
76  if(!chan.isEmpty())// add a check to know whether it is really a number.
77  channel = chan.toInt();
78  else
79  channel = -1;
80 
81  this->setParent(parent);
82  //default value for this parameter
83  this->setValue(value);
84  this->setSingleStep(0.10);
85  this->setDecimals(5);
86  connect(this, SIGNAL(valueChanged(double)), this, SLOT(addParameter(double)));
87 
88 
89  }
90 
92 
93  disconnect(this, SIGNAL(valueChanged(double)), this, SLOT(addParameter(double)));
94  }
95 
96 public :
98  int channel;
99 
100 
101 public slots :
102  void addParameter(double value);
103 
104 };
105 
107  if(channel == -1){
108  process->setParameter(value);
109  }else{
110  process->setParameter(value, channel);
111  }
112 
113 }
114 
115 // Data Inputs
116 class axlInspectorToolGenericInputData : public QComboBox
117 {
118  Q_OBJECT
119 
120 public :
121  axlInspectorToolGenericInputData(axlAbstractProcess *p, axlInspectorObjectController *control, QWidget *parent = 0,QString chan =""):QComboBox(parent)
122  {
123  process = p;
124  controller = control;
125  if(!chan.isEmpty())
126  channel = chan.toInt();
127  else
128  channel=-1;
129 
130 
131  if(!controller->items().isEmpty()){
133 
134  this->addItem(item->text(0));
135  }
136  }
137 
138  QString name = this->currentText();
140  QString nameList = item->text(0);
141 
142 
143  if(nameList == name){
144  process->setInput(controller->data(item), channel);
145  }
146 
147 
148  }
149 
150 
151  connect(this, SIGNAL(activated(QString)), this, SLOT(OnFindingInput(const QString&)));
152  }
153 
155  {
156 
157  disconnect(this, SIGNAL(activated(QString)), this, SLOT(OnFindingInput(const QString&)));
158  }
159 
160 public :
163  int channel;
164 
165 
166 public slots :
167  // we search for the right input in the inspector Object Tree, we want to select.
168  void OnFindingInput(const QString& name){
169 
170 
171  foreach(axlInspectorObjectManagerTreeItem *item, controller->items()) {
172  QString nameList = item->text(0);
173 
174  if(nameList == name){
175  if(channel ==-1)
176  process->setInput(controller->data(item));
177  else
178  process->setInput(controller->data(item), channel);
179 
180  }
181  }
182  }
183 };
184 
185 
186 //Data Outputs
188 {
189 
190 public :
191  axlInspectorToolGenericOutputData(QString channel, QString label)
192  {
193  this->setText("Output:" + channel + " "+label);
194  }
195 
197 
198  }
199 
200 };
201 
202 
203 class axlInspectorToolGenericInputString : public QComboBox
204 {
205  Q_OBJECT
206 
207 public :
208  axlInspectorToolGenericInputString(axlAbstractProcess *p,QWidget *parent = 0, QStringList list = QStringList(), QString chan = "") : QComboBox(parent){
209  process = p;
210  if(!chan.isEmpty()) // add a check to know whether it is really a number.
211  channel = chan.toInt();
212  else
213  channel = -1;
214 
215  this->setParent(parent);
216 
217  //fill the QCombox with possible values. By default display first value in the list.
218  this->addItems(list);
219 
220  connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(addParameter(int)));
221 
222 
223  }
224 
226 
227  disconnect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(addParameter(int)));
228  }
229 
230 
231 public slots :
232  void addParameter(int currentIndex){
233  if(channel == -1){
234  process->setParameter(currentIndex);
235  }else{
236  process->setParameter(currentIndex, channel);
237  }
238 
239  }
240 
241 public :
243  int channel;
244 };
245 
246 
248 {
249  Q_OBJECT
250 
251 public :
252  axlInspectorToolGenericInputDataList(axlAbstractProcess *p,axlInspectorObjectController *control,QWidget *parent = 0, QString chan = "") : QWidget(parent){
253  process = p;
254  controller = control;
255 
256  if(chan.isEmpty())
257  channel = chan.toInt();
258  else
259  channel = -1;
260 
261  QList<axlAbstractData *>dataList;
262 
263  //select all data written selected or editable in the object inspector.
265  {
266  if( (item->text(2) == "Selected" || item->text(2) == "Editable"))
267  {
268  dataList << dynamic_cast<axlAbstractData *>(controller->data(item));
269  }
270  }
271 
272 
273  //setInput
274  if(channel == -1){
275  foreach (axlAbstractData *data, dataList) {
276  process->setInput(data);
277  }
278 
279  }else{
280  foreach (axlAbstractData *data, dataList) {
281  process->setInput(data, channel);
282  }
283  }
284 
285  }
286 
288  }
289 
290 public :
293  int channel;
294 };
295 
296 
297 
298 class axlInspectorToolGenericPrivate
299 {
300 public:
301  void setup(void);
302 
303 public:
304  axlAbstractProcess *process;
305 
306 public:
307  axlInspectorObjectController *controller;
308 
309 public:
311 
312 public:
313  QFormLayout *layout;
314  QPushButton *button;
315 
316 public :
317  bool emitData;
318 };
319 
320 axlInspectorToolGeneric::axlInspectorToolGeneric(QString implementation,axlInspectorObjectController*control, QWidget *parent) : QFrame(parent), d(new axlInspectorToolGenericPrivate)
321 {
322  d->emitData = false;
323  d->q = this;
324  d->process = qobject_cast<axlAbstractProcess *>(dtkAbstractProcessFactory::instance()->create(implementation));
325  if(d->process){
326  d->process->setParent(this);
327  connect(d->process, SIGNAL(dataSetFieldsChanged(QList<axlAbstractData *>, QString)), this, SIGNAL(dataSetFieldsChanged(QList<axlAbstractData *>, QString)));
328  d->controller = control;
329  d->setup();
330 
331  d->button = new QPushButton("Run", this);
332  d->layout->addWidget(d->button);
333  //connect(d->button, SIGNAL(clicked()), d->process, SLOT(update()));
334  connect(d->button, SIGNAL(clicked()),this, SLOT(runDataInserted()));
335  }else{
336  dtkWarn() << "no process available";
337  }
338  this->setAttribute(Qt::WA_DeleteOnClose);
339 }
340 
341 
342 
344 {
345 
346  disconnect(d->button, SIGNAL(clicked()), d->process, SLOT(update()));
347 
348  disconnect(d->process, SIGNAL(dataSetFieldsChanged(QList<axlAbstractData *>, QString)), this, SIGNAL(dataSetFieldsChanged(QList<axlAbstractData *>, QString)));
349  delete d->process;
350  delete d;
351 }
352 
353 
354 
356  //QString implementation = d->process->identifier();
357  //d->process = NULL;
358  //d->process = qobject_cast<axlAbstractProcess *>(dtkAbstractProcessFactory::instance()->create(implementation));
359  //d->setup();
360  if(d->process->update()){
361  if(d->emitData){
362  if(d->process->output()){
363  emit dataInserted(dynamic_cast<axlAbstractData *>(d->process->output()));
364  }
365  }
366  }
367 }
368 
369 
370 void axlInspectorToolGenericPrivate::setup(void)
371 {
372  this->layout = new QFormLayout(q);
373 
374  if(!this->process) {
375  this->layout->addRow(new QLabel("Invalid process", q));
376  } else {
377  this->layout->addRow(new QLabel(this->process->identifier(), q));
378  }
379 
380  QString form = this->process->form();
381 
382  if(form.isEmpty()){
383  // Default behavior will send to the process all selected data from the inspector
384  QList<dtkAbstractData*> dataSet;
385  int numberOfDataSelected =0;
386 
387  foreach(axlInspectorObjectManagerTreeItem *item, this->controller->items())
388  {
389  if(item->text(2) == "Selected")
390  {
391  dataSet.push_back (this->controller->data(item));
392  numberOfDataSelected++;
393  }
394  }
395 
396  // We don't care if the process implement really virtual functions called below.
397  if(dataSet.size() == 0)
398  qDebug() << " No input data selected for : "<< process->identifier();
399 
400  else if(dataSet.size() == 1)
401  process->setInput(dataSet.first());
402  else // (dataSet.size() > 1)
403  {
404  for (int i = 0 ; i < dataSet.size() ; i++)
405  process->setInput(dataSet[i], i);
406  }
407 
408 
409  //this->layout->addRow(new QLabel("No form description found", q));
410 
411  } else {
412 
413  QStringList lines = form.split("\n");
414  foreach(QString line, lines) {
415 
416  QStringList tokens = line.simplified().split(QRegExp("\\s"));
417  int l=tokens.at(0).size();
418 
419  if(tokens.first().startsWith("INPUT")) {
420 
421  Q_ASSERT(tokens.count() >3);
422 
423  QString type = tokens.at(1);
424  QString label = tokens.at(2);
425 
426  if(l > 6){
427 
428  QString channel = tokens.at(0).right(l-6);
429 
430  if(type == "int"){
431  int value = tokens.last().toInt();
432  this->layout->addRow(label, new axlInspectorToolGenericInputInteger(this->process,this->q, value, channel));
433  }
434 
435  if(type == "double"){
436  double value = tokens.last().toDouble();
437  this->layout->addRow(label, new axlInspectorToolGenericInputDouble(this->process,this->q, value, channel));
438  }
439 
440  if(type == "data")
441  this->layout->addRow(label, new axlInspectorToolGenericInputData(this->process, this->controller, this->q, channel));
442 
443 
444  if(type == "dataList")
445  this->layout->addRow(label, new axlInspectorToolGenericInputDataList(this->process, this->controller, this->q,channel));
446  }else{
447 
448  if(type == "int"){
449  int value = tokens.last().toInt();
450  this->layout->addRow(label, new axlInspectorToolGenericInputInteger(this->process,this->q,value));
451  }
452 
453  if(type == "double"){
454  double value = tokens.last().toDouble();
455  this->layout->addRow(label, new axlInspectorToolGenericInputDouble(this->process,this->q,value));
456  }
457 
458  if(type == "data")
459  this->layout->addRow(label, new axlInspectorToolGenericInputData(this->process, this->controller,this->q));
460 
461 
462  if(type == "dataList")
463  this->layout->addRow(label, new axlInspectorToolGenericInputDataList(this->process, this->controller,this->q));
464 
465  }
466 
467  }
468 
469  if(tokens.first().startsWith("PARAMETER")) {
470 
471 
472 
473  if(l > 9) {
474 
475  QString channel = tokens.at(0).right(l-10);
476  QString type = tokens.at(1);
477  QString label = tokens.at(2);
478  //qDebug()<<"parameter"<<channel;
479 
480  if(type == "int"){
481  int value = tokens.last().toInt();
482  this->layout->addRow(label, new axlInspectorToolGenericInputInteger(this->process, this->q,value, channel));
483  }
484 
485  if(type == "double"){
486  double value = tokens.last().toDouble();
487  this->layout->addRow(label, new axlInspectorToolGenericInputDouble(this->process, this->q,value, channel));
488  }
489 
490  }
491  }
492 
493 
494  if(tokens.first().startsWith("OUTPUT")){
495 
496  Q_ASSERT(tokens.count() > 2);
497  emitData = true;
498  QString channel = "";
499  if(l>7)
500  channel = tokens.at(0).right(l-8);
501  QString label = tokens.at(2);
502  this->layout->addRow(new axlInspectorToolGenericOutputData(channel, label));
503  }
504  }
505  }
506 }
507 
508 #include "axlInspectorToolGeneric.moc"
axlInspectorToolGenericInputDouble(axlAbstractProcess *p, QWidget *parent=0, double value=0.0, QString chan="")
axlInspectorObjectController * controller
axlInspectorToolGenericInputInteger(axlAbstractProcess *p, QWidget *parent=0, int value=0, QString chan="")
axlInspectorToolGeneric(QString implementation, axlInspectorObjectController *control, QWidget *parent=0)
dtkAbstractData * data(axlInspectorObjectManagerTreeItem *item)
axlInspectorToolGenericInputData(axlAbstractProcess *p, axlInspectorObjectController *control, QWidget *parent=0, QString chan="")
axlInspectorToolGenericInputDataList(axlAbstractProcess *p, axlInspectorObjectController *control, QWidget *parent=0, QString chan="")
axlInspectorObjectController * controller
axlInspectorToolGenericOutputData(QString channel, QString label)
void dataInserted(axlAbstractData *data)
QList< axlInspectorObjectManagerTreeItem * > items(void)
Class axlAbstractData defines an API for all type of axel data.
void dataSetFieldsChanged(QList< axlAbstractData * > dataSet, QString fieldName)
axlInspectorToolGenericInputString(axlAbstractProcess *p, QWidget *parent=0, QStringList list=QStringList(), QString chan="")