programblockeditor.cpp 29.8 KB
Newer Older
1
2
3
#include "programblockeditor.h"
#include <QQmlEngine>
#include <QPropertyAnimation>
Leander Schulten's avatar
Leander Schulten committed
4
#include "programms/property.hpp"
5
#include "errornotifier.h"
6
7
8
9
10
11

QQmlEngine*  ProgramBlockEditor::engine = nullptr;

ProgramBlockEditor::ProgramBlockEditor():programBlockEntry(engine,QUrl("qrc:/ProgramBlockEntry.qml")),
    programBlockConnection(engine,QUrl("qrc:/ProgramBlockConnection.qml"))
{
Leander Schulten's avatar
Leander Schulten committed
12
    setAcceptedMouseButtons(Qt::AllButtons);
13
    /*QQuickItem *c1 ;
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
    if(programBlockEntry.isReady()){
        QQuickItem*  component = qobject_cast<QQuickItem*>(programBlockEntry.create());
        component->setParentItem(this);
        QQmlEngine::setObjectOwnership(component,QQmlEngine::JavaScriptOwnership);
        component->setProperty("text","testTest");
        component->setX(5);
        component->setY(20);
        component->setWidth(100);
        c1 = component;


    }else{
        qDebug() << " Component not loaded!";
    }

    QQuickItem*  component = qobject_cast<QQuickItem*>(programBlockConnection.create());
    component->setParentItem(this);
    QQmlEngine::setObjectOwnership(component,QQmlEngine::JavaScriptOwnership);
    component->setProperty("targetStartIndex",0);
    component->setProperty("targetLength",50);
    component->setProperty("sourceStartIndex",10);
    component->setProperty("sourceLength",50);
    component->setProperty("targetBaseline",100);
    component->setProperty("sourceBaseline",160);
    component->setProperty("color",QColor(23,255,23));{
        QPropertyAnimation  *animation = new QPropertyAnimation(component, "sourceStartIndex");
        animation->setDuration(1000);
        animation->setStartValue(10);
        animation->setEndValue(800);
        animation->setLoopCount(-1);
        animation->setEasingCurve(QEasingCurve::OutCubic);
        animation->setParent(this);
        animation->start();}
    {
        QPropertyAnimation  *animation = new QPropertyAnimation(component, "sourceBaseline");
        animation->setDuration(1000);
        animation->setStartValue(160);
        animation->setEndValue(800);
        animation->setLoopCount(-1);
        animation->setEasingCurve(QEasingCurve::OutCubic);
        animation->setParent(this);
        animation->start();}

    QObject::connect(c1,&QQuickItem::xChanged,[=](){
        component->setProperty("targetStartIndex",c1->x());
    });
    QObject::connect(c1,&QQuickItem::yChanged,[=](){
        component->setProperty("targetBaseline",c1->y()+c1->height());
62
    });*/
63
64
65
66
67
68
69
70
    /*
    property alias targetStartIndex: path.startX
    property alias targetLength: topRight.relativeX
    property alias sourceStartIndex: bottomLeft.x
    property real sourceLength
    property real targetBaseline
    property real sourceBaseline*/
}
Leander Schulten's avatar
Leander Schulten committed
71
Q_DECLARE_METATYPE(Modules::PropertyBase *)
72

Leander Schulten's avatar
Leander Schulten committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
void ProgramBlockEditor::updatePossibleEntries(){
    QStringList l;
    using namespace Modules;
    for(const auto & m : ModuleManager::singletone()->getProgrammModules()){
        l << QString::fromStdString(m.name()) + "(Program)";
    }
    for(const auto & m : ModuleManager::singletone()->getFilterModules()){
        l << QString::fromStdString(m.name()) + "(Filter)";
    }
    for(const auto & m : ModuleManager::singletone()->getConsumerModules()){
        l << QString::fromStdString(m.name()) + "(Consumer)";
    }
    possibleEntryModel.setStringList(l);
}

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
void ProgramBlockEditor::updateInputOutputModels(){
    QStringList in, out;
    using namespace Modules;
    for(const auto & p : programBlock->getPrograms()){
        out << QString::fromLatin1(p->getName()) + "(Program)";
    }
    for(const auto & p : programBlock->getFilter()){
        out << QString::fromLatin1(static_cast<Filter*>(p.second.source.get())->getName()) + "(Filter)";
        in << QString::fromLatin1(static_cast<Filter*>(p.second.source.get())->getName()) + "(Filter)";
    }
    for(const auto & p : programBlock->getConsumer()){
        in << QString::fromLatin1(static_cast<Consumer*>(p.source.get())->getName()) + "(Consumer)";
    }
    inputDataConsumerModel.setStringList(in);
    outputDataProducerModel.setStringList(out);
}

Modules::OutputDataProducer * getOutputDataProducer(Modules::ProgramBlock * pb, QString name_){
    if(name_.endsWith("(Program)")){
        auto name = name_.left(name_.length() - 9).toStdString();
        for(const auto &p : pb->getPrograms()){
            if(p->getName() == name)
                return p.get();
        }
    }
    if(name_.endsWith("(Filter)")){
        auto name = name_.left(name_.length() - 8).toStdString();
        for(const auto &p : pb->getFilter()){
            if(static_cast<Modules::Filter*>(p.second.source.get())->getName() == name)
                return static_cast<Modules::Filter*>(p.second.source.get());
        }
    }
    return nullptr;
}

void ProgramBlockEditor::addConnection(int inputIndex, int length, int outputIndex, int startIndex){
    if(inputIndex< 0 || inputIndex >= inputDataConsumerModel.rowCount()){
        return;
    }
    if(outputIndex< 0 || outputIndex >= outputDataProducerModel.rowCount()){
        return;
    }
    using namespace Modules;
    auto inputName = inputDataConsumerModel.data(inputDataConsumerModel.index(inputIndex)).toString();
    auto outputName = outputDataProducerModel.data(outputDataProducerModel.index(outputIndex)).toString();
    if(inputName.endsWith("(Filter)")){
        auto name = inputName.left(inputName.length() - 8).toStdString();
        for(auto & f : programBlock->getFilter()){
            if(static_cast<Filter*>(f.second.source.get())->getName() == name){
                f.second.addTarget(length,getOutputDataProducer(programBlock,outputName),startIndex);
            }
        }
    }else{/*"(Consumer)"*/
        auto name = inputName.left(inputName.length() - 10).toStdString();
        for(auto & f : programBlock->getConsumer()){
            if(static_cast<Filter*>(f.source.get())->getName() == name){
                f.addTarget(length,getOutputDataProducer(programBlock,outputName),startIndex);
            }
        }
    }
    recreateView();
}

void ProgramBlockEditor::addConnection(int length, int startIndex){
    if(dragEndItem == nullptr || length < 0 || startIndex < 0 )
        return;
    using namespace Modules;
    OutputDataProducer * output = dynamic_cast<OutputDataProducer*>(dragStartItem->property("propertyBase").value<PropertyBase*>());
    InputDataConsumer  * input  = dynamic_cast<InputDataConsumer*>(dragEndItem->property("propertyBase").value<PropertyBase*>());
    if(!input||!output)
        return;
    for(auto & i : programBlock->getFilter()){
        if(i.second.source.get() == input){
            i.second.addTarget(length,output,startIndex);
            recreateView();
            return;
        }
    }
    for(auto & i : programBlock->getConsumer()){
        if(i.source.get() == input){
            i.addTarget(length,output,startIndex);
            recreateView();
            return;
        }
    }
}

void ProgramBlockEditor::removeEntry(){
    if(dragStartItem){
        using namespace Modules;
        auto pb = dragStartItem->property("propertyBase").value<PropertyBase*>();
        auto p = dynamic_cast<Program*>(pb);
        auto f = dynamic_cast<Filter*>(pb);
        auto c = dynamic_cast<Consumer*>(pb);
        if(p){
            programBlock->removeProgram(p);
        }else if(f){
            programBlock->removeFilter(f);
        }else if(c){
            programBlock->removeConsumer(c);
        }
        dragStartItem = nullptr;
        recreateView();
    }
}

void ProgramBlockEditor::removeIncomingConnections(){
    if(!dragStartItem)
        return;
    using namespace Modules;
    auto pb = dragStartItem->property("propertyBase").value<PropertyBase*>();
    for(auto & i : programBlock->getFilter()){
        if(static_cast<Filter*>(i.second.source.get())==pb){
            i.second.targeds.clear();
            recreateView();
            return;
        }
    }
    for( auto & i : programBlock->getConsumer()){
        if(static_cast<Consumer*>(i.source.get())==pb){
            i.targeds.clear();
            recreateView();
            return;
        }
    }
}

Leander Schulten's avatar
Leander Schulten committed
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
void ProgramBlockEditor::addEntry(int index, int size){
    if(index < 0 || index >= possibleEntryModel.rowCount()){
        return;
    }
    using namespace Modules;
    QString name = possibleEntryModel.data(possibleEntryModel.index(index)).toString();
    if(name.endsWith("(Program)")){
        auto p = ModuleManager::singletone()->createProgramm(name.left(name.length()-9).toStdString());
        if(p){
            p->setOutputLength(size);
            programBlock->addProgramm(p);
            qDebug() << "added";
            recreateView();
        }else{
            qDebug()<<"Invalid pointer for : "  << name;
        }
    } else if(name.endsWith("(Filter)")){
        auto p = ModuleManager::singletone()->createFilter(name.left(name.length()-8).toStdString());
        if(p){
           p->setInputLength(size);
           programBlock->addFilter(Modules::detail::Connection(p),1);
           qDebug() << "added";
           recreateView();
        }else{
           qDebug()<<"Invalid pointer for : "  << name;
        }
    } else if(name.endsWith("(Consumer)")){
        auto p = ModuleManager::singletone()->createConsumer(name.left(name.length()-10).toStdString());
        if(p){
            p->setInputLength(size);
            programBlock->addConsumer(Modules::detail::Connection(p));
            recreateView();
            qDebug() << "added";
        }else{
            qDebug()<<"Invalid pointer for : "  << name;
        }
    }else{
        qDebug() << "ERROR : " << name;
    }
}

256
257
void ProgramBlockEditor::recreateView(){
    for(auto i : this->childItems()){
Leander Schulten's avatar
Leander Schulten committed
258
        if(i->objectName()=="removeable")
259
260
        i->setParentItem(nullptr);
    }
261
262
    if(programBlockEntry.isError()){
        qDebug() << programBlockEntry.errorString();
263
        ErrorNotifier::showError(programBlockEntry.errorString());
264
265
266
267
268
        for(const auto & e : programBlockEntry.errors()){
            qDebug() << e;
        }
    }if(programBlockConnection.isError()){
        qDebug() << programBlockConnection.errorString();
269
        ErrorNotifier::showError(programBlockConnection.errorString());
270
271
272
273
274
        for(const auto & e : programBlockConnection.errors()){
            qDebug() << e;
        }
    }
    if(!programBlock||!programBlockEntry.isReady()||!programBlockConnection.isReady())
275
276
277
278
        return;
    using namespace Modules;
    std::map<Named*,QQuickItem*> components;
    int y = 0;
279
    int x = 0;
280
281
    const int LAYER_OFFSET = 15;
    int layerOffsetX = 0;
282
283
284
    for(const auto & p : programBlock->getPrograms()){
        QQuickItem*  component = qobject_cast<QQuickItem*>(programBlockEntry.create());
        component->setProperty("text",p.get()->getName());
285
        component->setX(x + layerOffsetX);
286
        component->setY(y);
Leander Schulten's avatar
Leander Schulten committed
287
        component->setWidth(p.get()->getOutputLength() * scale);
Leander Schulten's avatar
Leander Schulten committed
288
        component->setProperty("propertyBase",QVariant::fromValue(static_cast<PropertyBase*>(p.get())));
Leander Schulten's avatar
Leander Schulten committed
289
        component->setObjectName("removeable");
290
        x += component->width();
291
292
        components[p.get()] = component;
    }
293
294
    x=0;
    int lastLayer = -1;
295
    for(const auto & p : programBlock->getFilter()){
296
297
298
        if(lastLayer!=p.first){
            x = 0;
            y += spaceBetweenLayers;
299
            layerOffsetX += LAYER_OFFSET;
300
        }
301
302
        QQuickItem*  component = qobject_cast<QQuickItem*>(programBlockEntry.create());
        component->setProperty("text",dynamic_cast<Named*>(p.second.source.get())->getName());
303
        component->setX(x + layerOffsetX);
304
        component->setY(y);
Leander Schulten's avatar
Leander Schulten committed
305
        component->setWidth(p.second.source.get()->getInputLength() * scale);
Leander Schulten's avatar
Leander Schulten committed
306
        component->setProperty("propertyBase",QVariant::fromValue(dynamic_cast<PropertyBase*>(p.second.source.get())));
Leander Schulten's avatar
Leander Schulten committed
307
        component->setObjectName("removeable");
308
        components[dynamic_cast<Named*>(p.second.source.get())] = component;
309
310
        x+= component->width();
        lastLayer = p.first;
311
    }
312
    y+=spaceBetweenLayers;
313
    layerOffsetX += LAYER_OFFSET;
314
    x = 0;
315
316
317
    for(const auto & p : programBlock->getConsumer()){
        QQuickItem*  component = qobject_cast<QQuickItem*>(programBlockEntry.create());
        component->setProperty("text",dynamic_cast<Named*>(p.source.get())->getName());
318
        component->setX(x + layerOffsetX);
319
        component->setY(y);
Leander Schulten's avatar
Leander Schulten committed
320
        component->setWidth(p.source.get()->getInputLength() * scale);
Leander Schulten's avatar
Leander Schulten committed
321
        component->setProperty("propertyBase",QVariant::fromValue(dynamic_cast<PropertyBase*>(p.source.get())));
Leander Schulten's avatar
Leander Schulten committed
322
        component->setObjectName("removeable");
323
        x += component->width();
324
325
326
327
328
329
330
331
332
        components[dynamic_cast<Named*>(p.source.get())] = component;
    }


    for(const auto & p : programBlock->getFilter()){
        int index = 0;
        for(const auto & c : p.second.targeds){
            QQuickItem*  component = qobject_cast<QQuickItem*>(programBlockConnection.create());
            component->setParentItem(this);
333
            QQmlEngine::setObjectOwnership(component,QQmlEngine::JavaScriptOwnership);            
Leander Schulten's avatar
Leander Schulten committed
334
            component->setProperty("targetLength",c.first*scale);
335
            component->setProperty("sourceStartIndex",index*scale);
Leander Schulten's avatar
Leander Schulten committed
336
            component->setProperty("sourceLength",c.first*scale);
337
338
            const auto elem1 = components[dynamic_cast<Named*>(c.second.targed)];
            component->setProperty("targetBaseline",elem1->y() + elem1->height());
339
            component->setProperty("targetStartIndex",elem1->x() + c.second.startIndex*scale);
340
            const auto elem2 = components[dynamic_cast<Named*>(p.second.source.get())];
341
            component->setProperty("sourceStartIndex",index*scale + elem2->x());
342
343
            component->setProperty("sourceBaseline",elem2->y());
            component->setProperty("color",QColor(23,255,23));
Leander Schulten's avatar
Leander Schulten committed
344
            component->setObjectName("removeable");
345
346
347
348
349
350
351
352
353

            QObject::connect(elem1,&QQuickItem::xChanged,[=](){
                component->setProperty("targetStartIndex",elem1->x());
            });
            QObject::connect(elem1,&QQuickItem::yChanged,[=](){
                component->setProperty("targetBaseline",elem1->y()+elem1->height());
            });

            QObject::connect(elem2,&QQuickItem::xChanged,[=](){
354
                component->setProperty("sourceStartIndex",elem2->x() + index*scale);
355
356
357
358
            });
            QObject::connect(elem2,&QQuickItem::yChanged,[=](){
                component->setProperty("sourceBaseline",elem2->y());
            });
359
            index += c.first;
360
361
362
363
364
365
366
367
368
        }
    }

    for(const auto & p : programBlock->getConsumer()){
        int index = 0;
        for(const auto & c : p.targeds){
            QQuickItem*  component = qobject_cast<QQuickItem*>(programBlockConnection.create());
            component->setParentItem(this);
            QQmlEngine::setObjectOwnership(component,QQmlEngine::JavaScriptOwnership);
Leander Schulten's avatar
Leander Schulten committed
369
            component->setProperty("targetLength",c.first*scale);
370
            component->setProperty("sourceStartIndex",index*scale);
Leander Schulten's avatar
Leander Schulten committed
371
            component->setProperty("sourceLength",c.first*scale);
372
373
            const auto elem1 = components[dynamic_cast<Named*>(c.second.targed)];
            component->setProperty("targetBaseline",elem1->y() + elem1->height());
374
            component->setProperty("targetStartIndex",elem1->x() + c.second.startIndex*scale);
375
            const auto elem2 = components[dynamic_cast<Named*>(p.source.get())];
376
            component->setProperty("sourceStartIndex",elem2->x()+index*scale);
377
378
            component->setProperty("sourceBaseline",elem2->y());
            component->setProperty("color",QColor(23,255,23));
Leander Schulten's avatar
Leander Schulten committed
379
            component->setObjectName("removeable");
380
381

            QObject::connect(elem1,&QQuickItem::xChanged,[=](){
382
                component->setProperty("targetStartIndex",elem1->x()+c.second.startIndex*scale);
383
384
385
386
387
388
            });
            QObject::connect(elem1,&QQuickItem::yChanged,[=](){
                component->setProperty("targetBaseline",elem1->y()+elem1->height());
            });

            QObject::connect(elem2,&QQuickItem::xChanged,[=](){
389
                component->setProperty("sourceStartIndex",elem2->x() + index*scale);
390
391
392
393
            });
            QObject::connect(elem2,&QQuickItem::yChanged,[=](){
                component->setProperty("sourceBaseline",elem2->y());
            });
394
395

            index += c.first;
396
397
398
        }
    }

Leander Schulten's avatar
Leander Schulten committed
399
400
401
402
403
404
405
    // add components after the shapes, so that the childAt finds the components and not the shapes
    for(const auto & p : components){
        p.second->setParentItem(this);
        QQmlEngine::setObjectOwnership(p.second,QQmlEngine::JavaScriptOwnership);
    }
}

Leander Schulten's avatar
Leander Schulten committed
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
void print(PropertyInformationModel * p){
    for(const auto &o:*p){
        qDebug()<<o->getName() << o->getValue();
    }
}

template<typename TargetType>
void transferData(::detail::PropertyInformation *pi,Modules::Property * p){
    p->asNumeric<TargetType>()->setValue(pi->getValue().value<TargetType>());
}


void detail::PropertyInformation::updateValue(){
    qDebug()<<"set value";
    using namespace Modules;
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
    if(named){
        auto p = dynamic_cast<Program*>(named);
        auto f = dynamic_cast<Filter*>(named);
        auto c = dynamic_cast<Consumer*>(named);
        if(p){
            p->setOutputLength(value.toInt());
        }else if(f){
            f->setInputLength(value.toInt());
        }else if(c){
            c->setInputLength(value.toInt());
        }
        if(updateCallback)
            updateCallback();
        return;
    }
Leander Schulten's avatar
Leander Schulten committed
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
    switch (getType()) {
        case Property::Double: transferData<double>(this,property);
            break;
        case Property::Float: transferData<float>(this,property);
            break;
        case Property::Int: transferData<int>(this,property);
            break;
        case Property::Long: transferData<long>(this,property);
            break;
        case Property::Bool:
            property->asBool()->setValue(getValue().toBool());
            break;
        case Property::String:
            property->asString()->setValue(getValue().toString().toStdString());
    }
}

Leander Schulten's avatar
Leander Schulten committed
453
454
455
456
457
458
459
460
461
462
463
464
465
template<typename SourceType>
void transferData(Modules::Property & p, ::detail::PropertyInformation &pi){
    pi.setValue(p.asNumeric<SourceType>()->getValue());
    pi.setMinValue(p.asNumeric<SourceType>()->getMin());
    pi.setMaxValue(p.asNumeric<SourceType>()->getMax());
}
template<>
void transferData<long>(Modules::Property & p, ::detail::PropertyInformation &pi){
    pi.setValue(static_cast<qlonglong>(p.asNumeric<long>()->getValue()));
    pi.setMinValue(static_cast<qlonglong>(p.asNumeric<long>()->getMin()));
    pi.setMaxValue(static_cast<qlonglong>(p.asNumeric<long>()->getMax()));
}

466
QQuickItem * ProgramBlockEditor::getItemWithPropertyBase(QMouseEvent *event){
Leander Schulten's avatar
Leander Schulten committed
467
468
    auto comp = childAt(event->x(),event->y());
    if(!comp){
469
        return nullptr;
Leander Schulten's avatar
Leander Schulten committed
470
471
472
473
474
    }

    while (comp != this && !comp->property("propertyBase").isValid()) {
        comp = comp->parentItem();
    }
475
476
477
478
479
480
481
482
483
484
485
486
    if(comp->property("propertyBase").isValid())
        return comp;
    return nullptr;
}

void ProgramBlockEditor::mousePressEvent(QMouseEvent *event){
    dragStartItem = getItemWithPropertyBase(event);
    if(dragStartItem){
        if(event->modifiers().testFlag(Qt::ShiftModifier)){
            // only filter can be moved
            if(dynamic_cast<Modules::Filter*>(dragStartItem->property("propertyBase").value<Modules::PropertyBase*>())!= nullptr){
                dragType = MovePermanent;
Leander Schulten's avatar
Leander Schulten committed
487
            }
488
489
490
491
492
493
494
495
496
        }else if(event->modifiers().testFlag(Qt::AltModifier)){
            dragType = AddReverseConnection;
#ifdef Q_OS_MAC
        }else if(event->modifiers().testFlag(Qt::MetaModifier)){
#else
        }else if(event->modifiers().testFlag(Qt::ControlModifier)){
#endif
            dragType = MoveTemporarily;
            dragOffsetInItem = event->pos() - dragStartItem->position();
Leander Schulten's avatar
Leander Schulten committed
497
        }else{
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
            dragType = AddConnection;
        }
    }else{
        dragType = None;
    }
    qDebug() << "pressed "<<event->pos();
    qDebug() << "Type : " << dragType;
}

void ProgramBlockEditor::mouseMoveEvent(QMouseEvent *event){
    //qDebug() << "moved "<<event->pos();
    using namespace Modules;
    if(dragType==MoveTemporarily){
        dragStartItem->setPosition(event->pos()-dragOffsetInItem);
    }else if(dragType == MovePermanent){
        bool larger;
        if((larger = event->y()-dragStartItem->y()>spaceBetweenLayers/2)||dragStartItem->y()-event->y()>spaceBetweenLayers/2){
            //move element to the next layer
            PropertyBase * pb = dragStartItem->property("propertyBase").value<PropertyBase*>();
            if(larger){
                for(auto i = programBlock->getFilter().cbegin();i!=programBlock->getFilter().cend();++i){
                    if(static_cast<Filter*>(i->second.source.get())==static_cast<Filter*>(pb)){
                        const auto saveI = i;
                        Modules::detail::Connection c = i->second;
                        int layer = i->first;
                        ++i;
                        // check if ne should move the component:
                        if(i==programBlock->getFilter().cend() && dragStartItem->x()<=0.5) // we are the last component and alone in a row
                            break;
                        while (i!=programBlock->getFilter().cend() && i->first == layer) { // find the next component in a other layer
                            ++i;
                        }
                        if(i==programBlock->getFilter().cend()){ // we was in the last row with another component
                            layer++;
                        }else{ // we go to the next layer
                            layer = i->first;
                        }
                        programBlock->getFilter().erase(saveI);
                        programBlock->addFilter(std::move(c),layer);
                        break;
                    }
                }
            }else{
                // smaller: same as larger with reserve iterators
                for(auto i = programBlock->getFilter().crbegin();i!=programBlock->getFilter().crend();++i){
                    if(static_cast<Filter*>(i->second.source.get())==static_cast<Filter*>(pb)){
                        Modules::detail::Connection c = i->second;
                        int layer = i->first;
                        ++i;
                        const auto saveI = i;
                        // check if ne should move the component:
                        if(i==programBlock->getFilter().crend() && dragStartItem->x()<=0.5) // we are the last component and alone in a row
                            break;
                        while (i!=programBlock->getFilter().crend() && i->first == layer) { // find the next component in a other layer
                            ++i;
                        }
                        if(i==programBlock->getFilter().crend()){ // we was in the last row with another component
                            layer--;
                        }else{ // we go to the next layer
                            layer = i->first;
                        }
                        programBlock->getFilter().erase(saveI.base());
                        programBlock->addFilter(std::move(c),layer);
                        break;
                    }
                }
Leander Schulten's avatar
Leander Schulten committed
564
            }
565
566
567
568
569
570
571
572
573
574
575
            dragStartItem = nullptr;
            recreateView();
            for(const auto & c : childItems()){
                if(c->property("propertyBase").isValid()){
                    if(c->property("propertyBase").value<PropertyBase*>()==pb){
                        dragStartItem = c;
                        break;
                    }
                }
            }
            Q_ASSERT(dragStartItem != nullptr);
Leander Schulten's avatar
Leander Schulten committed
576
        }
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
    }
}


void ProgramBlockEditor::mouseReleaseEvent(QMouseEvent *event){
    auto comp = getItemWithPropertyBase(event);
    if(dragStartItem == comp && event->button() == Qt::LeftButton){
        // find the item with the right property if exists
        using namespace Modules;
        if(comp){
            print(getPropertyInformationModel());
            Modules::PropertyBase * pb = comp->property("propertyBase").value<Modules::PropertyBase*>();
            if(propertyInformationModel.size()>pb->getProperties().size()){
                for(auto i = propertyInformationModel.cbegin()+pb->getProperties().size();i!=propertyInformationModel.cend();++i){
                    delete *i;
                }
                propertyInformationModel.erase(propertyInformationModel.cbegin()+pb->getProperties().size(),propertyInformationModel.cend());
            }else{
                while(propertyInformationModel.size() < pb->getProperties().size()){
                    propertyInformationModel.push_back(new ::detail::PropertyInformation);
                }
Leander Schulten's avatar
Leander Schulten committed
598
            }
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
            propertyInformationModel.push_back(new ::detail::PropertyInformation);
            auto s = pb->getProperties().cbegin();
            for(auto t = propertyInformationModel.cbegin() + 1;t!=propertyInformationModel.cend();++t,++s){
                auto & tp = **t;
                auto & sp = **s;
                tp.property = *s;
                tp.setDescription(QString::fromStdString(sp.getDescription()));
                tp.setName(QString::fromStdString(sp.getName()));
                tp.setType(sp.type);
                switch (sp.type) {
                    case Property::Double: transferData<double>(sp,tp);
                        break;
                    case Property::Float: transferData<float>(sp,tp);
                        break;
                    case Property::Int: transferData<int>(sp,tp);
                        break;
                    case Property::Long: transferData<long>(sp,tp);
                        break;
                    case Property::Bool:
                        tp.setMinValue(0);
                        tp.setMaxValue(1);
                        tp.setValue(sp.asBool()->getValue());
                        break;
                    case Property::String:
                        tp.setValue(QString::fromStdString(sp.asString()->getString()));
                }
Leander Schulten's avatar
Leander Schulten committed
625

626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
            }
            auto prop = *propertyInformationModel.begin();
            prop->setType(::detail::PropertyInformation::Type::Int);
            prop->setMinValue(1);
            prop->setMaxValue(1000);
            prop->setForwardProperty(false);
            prop->named = pb;
            if(dynamic_cast<InputDataConsumer*>(pb)){
                prop->setName("InputLength");
                prop->setDescription("The legth of the array that the entry consumes");
                prop->setValue(static_cast<int>(dynamic_cast<InputDataConsumer*>(pb)->getInputLength()));
            }else{
                prop->setName("OutputLength");
                prop->setDescription("The legth of the array that the program emits");
                prop->setValue(static_cast<int>(dynamic_cast<OutputDataProducer*>(pb)->getOutputLength()));
            }
            prop->updateCallback = [this,prop](){
                dragStartItem->setWidth(prop->getValue().value<int>()*this->scale);
            };
Leander Schulten's avatar
Leander Schulten committed
645

646
647
648
649
650
651
652
653
            print(getPropertyInformationModel());
            setShowProperties(true);

        }else{
            setShowProperties(false);
        }
    }else if(dragStartItem == comp && event->button() == Qt::RightButton && comp && !event->modifiers() && dragType == AddConnection){
        emit openRightClickEntry(event->x(),event->y());
Leander Schulten's avatar
Leander Schulten committed
654
    }
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
    using namespace Modules;
    if(dragType == AddConnection && comp){
        if(comp->y()>dragStartItem->y()){
            dragEndItem = comp;
            // prüfe, ob überhaupt noch eine Connection hinzugefügt werden kann
            auto pb = dragEndItem->property("propertyBase").value<PropertyBase*>();
            for(const auto & i : programBlock->getFilter()){
                if(static_cast<Filter*>(i.second.source.get())==pb){
                    const auto sum = std::accumulate(i.second.targeds.cbegin(),i.second.targeds.cend(),0,[](int sum,const auto & i){ return sum + i.first;});
                    if(sum >= i.second.source->getInputLength()){
                        dragType = None;
                        return;
                    }
                }
            }
            for(const auto & i : programBlock->getConsumer()){
                if(static_cast<Consumer*>(i.source.get())==pb){
                    const auto sum = std::accumulate(i.targeds.cbegin(),i.targeds.cend(),0,[](int sum,const auto & i){ return sum + i.first;});
                    if(sum >= i.source->getInputLength()){
                        dragType = None;
                        return;
                    }
                }
            }
            askToAddConnection(dragStartItem->property("text").toString(),dragEndItem->property("text").toString());
        }
    }else if(dragType == AddReverseConnection && comp){
        if(comp->y()<dragStartItem->y()){
            dragEndItem = comp;
            // prüfe, ob überhaupt noch eine Connection hinzugefügt werden kann
            auto pb = dragEndItem->property("propertyBase").value<PropertyBase*>();
            for(const auto & i : programBlock->getFilter()){
                if(static_cast<Filter*>(i.second.source.get())==pb){
                    const auto sum = std::accumulate(i.second.targeds.cbegin(),i.second.targeds.cend(),0,[](int sum,const auto & i){ return sum + i.first;});
                    if(sum >= i.second.source->getInputLength()){
                        dragType = None;
                        return;
                    }
                }
            }
            for(const auto & i : programBlock->getConsumer()){
                if(static_cast<Consumer*>(i.source.get())==pb){
                    const auto sum = std::accumulate(i.targeds.cbegin(),i.targeds.cend(),0,[](int sum,const auto & i){ return sum + i.first;});
                    if(sum >= i.source->getInputLength()){
                        dragType = None;
                        return;
                    }
                }
            }
            askToAddConnection(dragStartItem->property("text").toString(),dragEndItem->property("text").toString());
        }
    }
    dragType = None;
Leander Schulten's avatar
Leander Schulten committed
708
709
710

}

711