channelprogrammeditor.cpp 22.8 KB
Newer Older
Leander Schulten's avatar
Leander Schulten committed
1
2
#include "channelprogrammeditor.h"
#include <QSGFlatColorMaterial>
3
#include <QSGGeometryNode>
4
#include <QSGVertexColorMaterial>
5
#include <cmath>
Leander Schulten's avatar
Leander Schulten committed
6

7
namespace GUI {
8

9
CurrentTimePointWrapper::CurrentTimePointWrapper(ChannelProgrammEditor *editor) : editor(editor) {}
Leander Schulten's avatar
Leander Schulten committed
10

11
void CurrentTimePointWrapper::setValue(unsigned char value) {
Leander Schulten's avatar
Leander Schulten committed
12
    auto cur = editor->getCurrentTimePoint();
13
14
    if (cur) {
        if (cur->value != value) {
Leander Schulten's avatar
Leander Schulten committed
15
16
17
18
19
20
21
            cur->value = value;
            emit valueChanged();
            editor->update();
        }
    }
}

22
unsigned char CurrentTimePointWrapper::getValue() const {
Leander Schulten's avatar
Leander Schulten committed
23
    auto cur = editor->getCurrentTimePoint();
24
    if (cur) return cur->value;
Leander Schulten's avatar
Leander Schulten committed
25
26
    return 0;
}
27
bool CurrentTimePointWrapper::hasCurrent() {
Leander Schulten's avatar
Leander Schulten committed
28
29
    return editor->haveCurrentTimePoint();
}
30
31
void CurrentTimePointWrapper::setTime(double time) {
    if (editor->channelProgramm) {
32
        auto cur = editor->currentTimePoint;
33
        if (cur == editor->channelProgramm->timeline.cend()) return;
34
        double min = 0.;
35
        if (cur != editor->channelProgramm->timeline.begin()) {
36
37
38
39
40
41
            --cur;
            min = cur->time;
            ++cur;
        }
        ++cur;
        double max = 99999.;
42
43
        if (cur != editor->channelProgramm->timeline.cend()) {
            max = cur->time;
44
        }
45
        time = std::max(min, std::min(max, time));
46
        --cur;
47
48
        if (cur->time != time) {
            editor->channelProgramm->changeTimeOfTimePoint(cur->time, time);
49
50
51
            editor->currentTimePoint = editor->channelProgramm->timeline.cend();
            emit hasCurrentChanged();
            emit timeChanged();
Leander Schulten's avatar
Leander Schulten committed
52
53
            editor->update();
        }
54
    }
Leander Schulten's avatar
Leander Schulten committed
55
56
}

57
double CurrentTimePointWrapper::getTime() const {
Leander Schulten's avatar
Leander Schulten committed
58
    auto cur = editor->getCurrentTimePoint();
59
    if (cur) return cur->time;
Leander Schulten's avatar
Leander Schulten committed
60
61
62
    return 0;
}

63
64
65
ChannelProgrammEditor::ChannelProgrammEditor() : currentTimePointWrapper(this), modifier(0) {
    setFlag(ItemHasContents, true);
    setFlag(ItemIsFocusScope, true);
66
67
68
69
70
71
72
    setAcceptedMouseButtons(Qt::AllButtons);
    setAcceptHoverEvents(true);
    setEnabled(true);
    setFocus(true);
    setActiveFocusOnTab(true);
    setKeepMouseGrab(true);
    setAntialiasing(true);
Leander Schulten's avatar
Leander Schulten committed
73
74
}

75
76
77
void ChannelProgrammEditor::setChannelProgramm(DMX::ChannelProgramm *p) {
    if (p != channelProgramm) {
        channelProgramm = p;
78
        currentTimePoint = channelProgramm->timeline.cend();
79
        currentSegment = channelProgramm->timeline.cend();
Leander Schulten's avatar
Leander Schulten committed
80
81
82
83
84
        update();
        emit channelProgrammChanged();
    }
}

85
QSGNode *ChannelProgrammEditor::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *nodeData) {
Leander Schulten's avatar
Leander Schulten committed
86
    Q_UNUSED(nodeData)
87
88
89
90
91
92
93
    ChannelProgrammEditor::updatePolish();
    QSGTransformNode *node = nullptr;
    QSGGeometryNode *lineNode = nullptr;
    QSGGeometry *lineGeometry = nullptr;
    QSGGeometryNode *pointNode = nullptr;
    QSGGeometry *pointGeometry = nullptr;
    const int segments = 128;
Leander Schulten's avatar
Leander Schulten committed
94
    // effective version: const int count = channelProgramm?channelProgramm->timeline.empty()?0:channelProgramm->timeline.crbegin()->time*16:0;
95
96
    const int count = channelProgramm ? channelProgramm->timeline.empty() ? 0 : channelProgramm->timeline.begin()->time < 0.01 ? (channelProgramm->timeline.size() - 1) * segments : channelProgramm->timeline.size() * segments : 0;
    const int pointCount = channelProgramm ? channelProgramm->timeline.empty() ? 0 : channelProgramm->timeline.size() : 0;
Leander Schulten's avatar
Leander Schulten committed
97
98

    if (!oldNode) {
99
        node = new QSGTransformNode;
100
101
102
103
104
105
106
        lineNode = new QSGGeometryNode;
        lineGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), count);
        lineGeometry->setLineWidth(1);
        lineGeometry->setDrawingMode(QSGGeometry::DrawLineStrip);
        lineNode->setGeometry(lineGeometry);
        lineNode->setFlag(QSGNode::OwnsGeometry);

Leander Schulten's avatar
Leander Schulten committed
107
108
        QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
        material->setColor(graphColor);
109
110
111
112
113
114
115
116
117
118
        lineNode->setMaterial(material);
        lineNode->setFlag(QSGNode::OwnsMaterial);
        node->appendChildNode(lineNode);

        pointNode = new QSGGeometryNode;
        pointGeometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), pointCount);
        pointGeometry->setLineWidth(5);
        pointGeometry->setDrawingMode(QSGGeometry::DrawPoints);
        pointNode->setGeometry(pointGeometry);
        pointNode->setFlag(QSGNode::OwnsGeometry);
Leander Schulten's avatar
Leander Schulten committed
119

120
121
122
123
        pointNode->setMaterial(new QSGVertexColorMaterial);
        pointNode->setFlag(QSGNode::OwnsMaterial);

        node->appendChildNode(pointNode);
Leander Schulten's avatar
Leander Schulten committed
124
    } else {
125
126
127
128
        node = static_cast<QSGTransformNode *>(oldNode);
        lineNode = static_cast<QSGGeometryNode *>(node->childAtIndex(0));
        lineGeometry = lineNode->geometry();
        lineGeometry->allocate(count);
129
        static_cast<QSGFlatColorMaterial *>(lineNode->material())->setColor(graphColor);
130
131
132
        pointNode = static_cast<QSGGeometryNode *>(node->childAtIndex(1));
        pointGeometry = pointNode->geometry();
        pointGeometry->allocate(pointCount);
Leander Schulten's avatar
Leander Schulten committed
133
134
    }

135
    QMatrix4x4 matrix;
136
137
138

    matrix.translate(totalXOffset, 0);
    matrix.scale(xScale, 1.0);
139
140
    node->setMatrix(matrix);

141
    if (count) {
Leander Schulten's avatar
Leander Schulten committed
142
143

        QRectF bounds = boundingRect();
144
        QSGGeometry::Point2D *vertices = lineGeometry->vertexDataAsPoint2D();
Leander Schulten's avatar
Leander Schulten committed
145

146
        // const qreal stepSize = 1./segments;
Leander Schulten's avatar
Leander Schulten committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        auto iter = channelProgramm->timeline.cbegin();
        /*
         * effective version :
         *
        int i=0;
        if(iter->time>0.001){
            const auto from = channelProgramm->timeline.crbegin()->value;
            const auto to = iter->value;
            for(;;++i){
                const auto progress = i*stepSize / iter->time;
                if(progress>0.99){
                    vertices[i].y = to;
                    break;
                }
                vertices[i].y = channelProgramm->timeline.crbegin()->easingCurveToNextPoint.valueForProgress(progress) * (to-from)+from;
            }
        }
        for(;iter!=channelProgramm->timeline.cend()&&i<count;++i) {

        }*/
167
        if (iter->time > 0.01) {
Leander Schulten's avatar
Leander Schulten committed
168
169
            const auto from = channelProgramm->timeline.crbegin()->value;
            const auto to = iter->value;
170
171
            for (int i = 0; i < segments; ++i, ++vertices) {
                const auto progress = i / (segments - 1.);
Leander Schulten's avatar
Leander Schulten committed
172
                vertices->x = progress * iter->time;
173
                vertices->y = 255 - (channelProgramm->timeline.crbegin()->easingCurveToNextPoint.valueForProgress(progress) * (to - from) + from);
Leander Schulten's avatar
Leander Schulten committed
174
                vertices->y /= 255.;
175
                vertices->y *= bounds.height();
Leander Schulten's avatar
Leander Schulten committed
176
177
            }
        }
178
        for (;;) {
Leander Schulten's avatar
Leander Schulten committed
179
180
            const auto from = iter->value;
            const auto offsetTime = iter->time;
181
            const auto &curve = iter->easingCurveToNextPoint;
Leander Schulten's avatar
Leander Schulten committed
182
            ++iter;
183
            if (iter == channelProgramm->timeline.cend()) {
184
185
                break;
            }
Leander Schulten's avatar
Leander Schulten committed
186
187
            const auto to = iter->value;
            const auto timeDiff = iter->time - offsetTime;
188
189
            for (int i = 0; i < segments; ++i, ++vertices) {
                const auto progress = i / (segments - 1.);
Leander Schulten's avatar
Leander Schulten committed
190
                vertices->x = progress * timeDiff + offsetTime;
191
                vertices->y = 255 - (curve.valueForProgress(progress) * (to - from) + from);
192
193
194
195
196
197
198
199
                vertices->y /= 255.;
                vertices->y *= bounds.height();
            }
        }
        {

            QRectF bounds = boundingRect();
            QSGGeometry::ColoredPoint2D *vertices = pointGeometry->vertexDataAsColoredPoint2D();
200
            for (auto iter = channelProgramm->timeline.cbegin(); iter != channelProgramm->timeline.cend(); ++iter) {
201
202
                vertices->x = iter->time;
                vertices->y = 255 - iter->value;
Leander Schulten's avatar
Leander Schulten committed
203
                vertices->y /= 255.;
204
                vertices->y *= bounds.height();
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
                if (iter == currentTimePoint) {
                    vertices->r = 0;
                    vertices->g = 255;
                    vertices->b = 0;
                    vertices->a = 255;
                } else {
                    if (lastMousePosition != INVALID_POS) {
                        const auto xDiff = (vertices->x - mapFromVisualX(lastMousePosition.x())) * (xScale);
                        const auto yDiff = vertices->y - lastMousePosition.y();
                        auto disstance = (int)std::sqrt(xDiff * xDiff + yDiff * yDiff) * 2;
                        if (disstance < 255) {
                            vertices->r = 255 - disstance;
                            vertices->g = 0;
                            vertices->b = disstance;
                            vertices->a = 255;
                        } else {
                            vertices->r = 0;
                            vertices->g = 0;
                            vertices->b = 255;
                            vertices->a = 255;
225
                        }
226
227
228
229
230
                    } else {
                        vertices->r = 0;
                        vertices->g = 0;
                        vertices->b = 255;
                        vertices->a = 255;
231
232
233
                    }
                }
                ++vertices;
Leander Schulten's avatar
Leander Schulten committed
234
235
236
237
            }
        }
    }

238
239
    lineNode->markDirty(QSGNode::DirtyGeometry);
    pointNode->markDirty(QSGNode::DirtyGeometry);
Leander Schulten's avatar
Leander Schulten committed
240
241
242
243

    return node;
}

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
void ChannelProgrammEditor::updateHoverSegment() {
    auto i = channelProgramm->timeline.upper_bound(mapFromVisualX(lastMousePosition.x()));
    double newX;
    double newLength;
    if (i == channelProgramm->timeline.end()) {
        newX = 0;
        newLength = 0;
    } else {
        // i is the first element
        if (i == channelProgramm->timeline.begin()) {
            newX = mapToVisualX(0);
            newLength = xScale * i->time;
        } else {
            auto previous = i;
            --previous;
            newX = mapToVisualX(previous->time);
            newLength = xScale * (i->time - previous->time);
        }
    }
    if (newX != hoverSegmentX) {
        hoverSegmentX = newX;
        emit hoverSegmentXChanged();
    }
    if (newLength != hoverSegmentLength) {
        hoverSegmentLength = newLength;
        emit hoverSegmentLengthChanged();
    }
}

void ChannelProgrammEditor::updateSelectedSegment() {
    double newX;
    double newLength;
    if (currentSegment == channelProgramm->timeline.end()) {
        newX = 0;
        newLength = 0;
    } else {
        auto next = currentSegment;
        ++next;
        // i is the first segment
        if (next == channelProgramm->timeline.end()) {
            newX = mapToVisualX(0);
            newLength = xScale * channelProgramm->timeline.begin()->time;
        } else {
            newX = mapToVisualX(currentSegment->time);
            newLength = xScale * (next->time - currentSegment->time);
        }
    }
    if (newX != selectedSegmentX) {
        selectedSegmentX = newX;
        emit selectedSegmentXChanged();
    }
    if (newLength != selectedSegmentLength) {
        selectedSegmentLength = newLength;
        emit selectedSegmentLengthChanged();
    }
}

decltype(DMX::ChannelProgramm::timeline)::iterator ChannelProgrammEditor::getTimePointForPosition(int x_, int y_) {
302
    auto x = mapFromVisualX(x_);
Leander Schulten's avatar
Leander Schulten committed
303
304
305
306
307
    auto y = getScaledY(y_);
    if (channelProgramm->timeline.empty()) {
        return channelProgramm->timeline.cend();
    }
    auto best = channelProgramm->timeline.end();
308
309
    auto minDist = clickRadius;
    auto begin = channelProgramm->timeline.cbegin();
310
311
    auto end = channelProgramm->timeline.cend();
    if (begin == channelProgramm->timeline.cend()) {
Leander Schulten's avatar
Leander Schulten committed
312
313
314
        begin = channelProgramm->timeline.end();
        --begin;
    }
315
316
317
318
319
    for (auto i = begin; i != end; ++i) {
        const auto xDiff = (i->time - x) * (xScale);
        const auto yDiff = i->value - y;
        auto dist = std::sqrt(xDiff * xDiff + yDiff * yDiff);        
        if (dist < minDist) {
Leander Schulten's avatar
Leander Schulten committed
320
321
322
323
324
325
326
            minDist = dist;
            best = i;
        }
    }
    return best;
}

327
328
329
QEasingCurve *ChannelProgrammEditor::getCurveForPoint(int x) {
    if (!channelProgramm) return nullptr;
    if (channelProgramm->timeline.empty()) return nullptr;
Leander Schulten's avatar
Leander Schulten committed
330
    auto lowerBound = channelProgramm->timeline.lower_bound(x);
331
    if (lowerBound == channelProgramm->timeline.cend()) {
Leander Schulten's avatar
Leander Schulten committed
332
        return &channelProgramm->timeline.crbegin()->easingCurveToNextPoint;
333
    } else {
Leander Schulten's avatar
Leander Schulten committed
334
335
336
337
        return &lowerBound->easingCurveToNextPoint;
    }
}

338
339
340
341
342
343
344
void ChannelProgrammEditor::setSelectedEasingCurve(QEasingCurve::Type type) {
    if (currentSegment != channelProgramm->timeline.end() && currentSegment->easingCurveToNextPoint.type() != type) {
        currentSegment->easingCurveToNextPoint.setType(type);
        emit selectedEasingCurveChanged();
        update();
    }
}
Leander Schulten's avatar
Leander Schulten committed
345

346
void ChannelProgrammEditor::mousePressEvent(QMouseEvent *event) {
347
348
349
350
    update();
    lastMousePosition = event->pos();
    forceActiveFocus(Qt::MouseFocusReason);
    event->accept();
Leander Schulten's avatar
Leander Schulten committed
351
    mousePressTimestamp = event->timestamp();
352

353
354
355
356
    if (!channelProgramm) return;    
    if (channelProgramm->timeline.empty()) {
        if (modifier.testFlag(N_PRESSED)) {
            currentTimePoint = channelProgramm->timeline.insert(DMX::TimePoint(mapFromVisualX(event->x()), getScaledY(event->y()), QEasingCurve::Linear)).first;
357
            update();
358
            updateHoverSegment();
359
        }
Leander Schulten's avatar
Leander Schulten committed
360
        return;
361
    }
362
363
364
365
366
367
368
369
    if (modifier.testFlag(D_PRESSED)) {
        auto iter = getTimePointForPosition(event->x(), event->y());
        if (iter != channelProgramm->timeline.end()) {
            if (iter == currentSegment) {
                currentSegment = channelProgramm->timeline.end();
                emit selectedEasingCurveChanged();
                updateSelectedSegment();
            }
Leander Schulten's avatar
Leander Schulten committed
370
            channelProgramm->timeline.erase(iter);
371
            update();
372
            updateHoverSegment();
Leander Schulten's avatar
Leander Schulten committed
373
374
375
        }
        currentTimePoint = channelProgramm->timeline.cend();
        emit currentTimePointWrapper.hasCurrentChanged();
376
377
378
379
    } else {
        if (modifier.testFlag(N_PRESSED)) {
            if (getTimePointForPosition(event->x(), event->y()) == channelProgramm->timeline.end()) {
                currentTimePoint = channelProgramm->timeline.insert(DMX::TimePoint(mapFromVisualX(event->x()), getScaledY(event->y()), QEasingCurve::Linear)).first;
380
                update();
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
                updateHoverSegment();
            }
        } else {
            auto oldIter = currentTimePoint;
            currentTimePoint = getTimePointForPosition(event->x(), event->y());
            if (currentTimePoint == channelProgramm->timeline.end()) {
                // no timepoints gets selected, lets select a segment
                auto i = channelProgramm->timeline.upper_bound(mapFromVisualX(event->localPos().x()));
                // the timepoint for the segment before the first timepoint is the last timepoint
                if (i == channelProgramm->timeline.begin()) {
                    i = channelProgramm->timeline.end();
                    // we want the left timepoint of an segment
                    --i;
                } else if (i != channelProgramm->timeline.end()) {
                    // we want the left timepoint of an segment
                    --i;
                }
                if (i != currentSegment) {
                    currentSegment = i;
                    emit selectedEasingCurveChanged();
                    updateSelectedSegment();
                }
            } else {
                if (currentSegment != channelProgramm->timeline.end()) {
                    currentSegment = channelProgramm->timeline.end();
                    emit selectedEasingCurveChanged();
                    updateSelectedSegment();
                }
Leander Schulten's avatar
Leander Schulten committed
409
410
            }
        }
411
        emit currentTimePointWrapper.hasCurrentChanged();
Leander Schulten's avatar
Leander Schulten committed
412
413
414
415
416
        emit currentTimePointWrapper.valueChanged();
        emit currentTimePointWrapper.timeChanged();
    }
}

417
418
void ChannelProgrammEditor::keyReleaseEvent(QKeyEvent *event) {
    if (!event->modifiers().testFlag(Qt::ShiftModifier)) {
Leander Schulten's avatar
Leander Schulten committed
419
        switch (event->key()) {
420
421
422
423
        case Qt::Key::Key_N: modifier.setFlag(Modifier::N_PRESSED, false); break;
        case Qt::Key::Key_D: modifier.setFlag(Modifier::D_PRESSED, false); break;
        case Qt::Key::Key_X: modifier.setFlag(Modifier::X_PRESSED, false); break;
        case Qt::Key::Key_Y: modifier.setFlag(Modifier::Y_PRESSED, false); break;
Leander Schulten's avatar
Leander Schulten committed
424
425
426
427
        }
    }
}

428
void ChannelProgrammEditor::keyPressEvent(QKeyEvent *event) {
429
    event->accept();
Leander Schulten's avatar
Leander Schulten committed
430
    switch (event->key()) {
431
    case Qt::Key::Key_N: modifier.setFlag(Modifier::N_PRESSED); break;
Leander Schulten's avatar
Leander Schulten committed
432
433
    case Qt::Key::Key_D:
        modifier.setFlag(Modifier::D_PRESSED);
434
435
436
437
438
439
        if (haveCurrentTimePoint()) {
            if (currentTimePoint == currentSegment) {
                currentSegment = channelProgramm->timeline.end();
                emit selectedEasingCurveChanged();
                updateSelectedSegment();
            }
440
441
442
443
            channelProgramm->timeline.erase(currentTimePoint);
            currentTimePoint = channelProgramm->timeline.end();
            emit currentTimePointWrapper.hasCurrentChanged();
            update();
444
            updateHoverSegment();
445
        }
Leander Schulten's avatar
Leander Schulten committed
446
        break;
447
448
    case Qt::Key::Key_X: modifier.setFlag(Modifier::X_PRESSED); break;
    case Qt::Key::Key_Y: modifier.setFlag(Modifier::Y_PRESSED); break;
Leander Schulten's avatar
Leander Schulten committed
449
450
451
    }
    if (haveCurrentTimePoint()) {
        switch (event->key()) {
452
        case Qt::Key::Key_Up: {
Leander Schulten's avatar
Leander Schulten committed
453
454
455
            auto oldValue = currentTimePoint->value;
            if (event->modifiers().testFlag(Qt::MetaModifier)) {
                currentTimePoint->value = 255;
456
457
458
459
460
461
            } else if (event->modifiers().testFlag(Qt::ControlModifier)) {
                currentTimePoint->value = std::min(255, currentTimePoint->value + 25);
            } else if (event->modifiers().testFlag(Qt::ShiftModifier)) {
                currentTimePoint->value = std::min(255, currentTimePoint->value + 1);
            } else {
                currentTimePoint->value = std::min(255, currentTimePoint->value + 5);
Leander Schulten's avatar
Leander Schulten committed
462
            }
463
            if (oldValue != currentTimePoint->value) {
Leander Schulten's avatar
Leander Schulten committed
464
465
466
                update();
                emit currentTimePointWrapper.valueChanged();
            }
467
468
        } break;
        case Qt::Key::Key_Down: {
Leander Schulten's avatar
Leander Schulten committed
469
470
471
            auto oldValue = currentTimePoint->value;
            if (event->modifiers().testFlag(Qt::MetaModifier)) {
                currentTimePoint->value = 0;
472
473
474
475
476
477
            } else if (event->modifiers().testFlag(Qt::ControlModifier)) {
                currentTimePoint->value = std::max(0, currentTimePoint->value - 25);
            } else if (event->modifiers().testFlag(Qt::ShiftModifier)) {
                currentTimePoint->value = std::max(0, currentTimePoint->value - 1);
            } else {
                currentTimePoint->value = std::max(0, currentTimePoint->value - 5);
Leander Schulten's avatar
Leander Schulten committed
478
            }
479
            if (oldValue != currentTimePoint->value) {
Leander Schulten's avatar
Leander Schulten committed
480
                update();
481
                emit currentTimePointWrapper.valueChanged();
Leander Schulten's avatar
Leander Schulten committed
482
            }
483
        } break;
Leander Schulten's avatar
Leander Schulten committed
484
        case Qt::Key::Key_Left:
485
            // TODO
Leander Schulten's avatar
Leander Schulten committed
486
487
            break;
        case Qt::Key::Key_Right:
488
            // TODO
Leander Schulten's avatar
Leander Schulten committed
489
490
491
492
493
            break;
        }
    }
}

494
void ChannelProgrammEditor::wheelEvent(QWheelEvent *event) {
Leander Schulten's avatar
Leander Schulten committed
495
#ifdef Q_OS_WIN
496
    auto xDiff = event->angleDelta().y();
Leander Schulten's avatar
Leander Schulten committed
497
#else
498
    auto xDiff = event->pixelDelta().x();
Leander Schulten's avatar
Leander Schulten committed
499
#endif
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
    if (modifier.testFlag(X_PRESSED)) {
        xScale += xDiff * 1 / 32.;
        if (xScale < 0.05) {
            xScale = 0.05;
        }
    } else {
        totalXOffset += xDiff;
        if (totalXOffset > 0) // nach links beschränken
            totalXOffset = 0;
        if (channelProgramm) { // nach rechts beschränken
            if (!channelProgramm->timeline.empty()) {
                if (channelProgramm->timeline.crbegin()->time * xScale < -totalXOffset) {
                    totalXOffset = -channelProgramm->timeline.crbegin()->time * xScale;
                }
            }
        }
    }
517
518
    event->accept();
    update();
519
520
    updateHoverSegment();
    updateSelectedSegment();
521
522
}

523
524
525
void ChannelProgrammEditor::hoverMoveEvent(QHoverEvent *event) {
    lastMousePosition = event->posF();
    update();
526
    event->accept();
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
    // update hover segment
    updateHoverSegment();
    emit mouseXChanged();
}

void ChannelProgrammEditor::hoverLeaveEvent(QHoverEvent *e) {
    lastMousePosition = INVALID_POS;
    emit mouseXChanged();
    update();
    e->accept();
    // no hover segment any more
    hoverSegmentLength = 0;
    emit hoverSegmentLengthChanged();
}

void ChannelProgrammEditor::mouseMoveEvent(QMouseEvent *event) {
    lastMousePosition = event->localPos();
    event->accept();
    if (haveCurrentTimePoint()) {
Leander Schulten's avatar
Leander Schulten committed
546
547
548
        const auto timeOffset = 0.05;
        auto before = currentTimePoint;
        auto next = currentTimePoint;
549
        if (before == channelProgramm->timeline.cbegin()) {
550
551
552
            // we are at the first time Point
            before = channelProgramm->timeline.cend();
        }
Leander Schulten's avatar
Leander Schulten committed
553
554
        --before;
        ++next;
555
        int newX = mapFromVisualX(event->x());
556
        int newY = std::max(0, std::min(255, getScaledY(event->y())));
Leander Schulten's avatar
Leander Schulten committed
557

558
559
560
561
        if (currentTimePoint == channelProgramm->timeline.cbegin()) {
            if (newX < 0) {
                newX = 0;
            } else if (newX > next->time - timeOffset) {
Leander Schulten's avatar
Leander Schulten committed
562
563
                newX = next->time - timeOffset;
            }
564
565
566
        } else if (next == channelProgramm->timeline.cend()) {
            if (newX < before->time + timeOffset) {
                newX = before->time + timeOffset;
Leander Schulten's avatar
Leander Schulten committed
567
            }
568
569
570
571
        } else {
            if (newX < before->time + timeOffset) {
                newX = before->time + timeOffset;
            } else if (newX > next->time - timeOffset) {
Leander Schulten's avatar
Leander Schulten committed
572
573
574
575
                newX = next->time - timeOffset;
            }
        }

576
        /*if (modifier.testFlag(X_PRESSED)&&!modifier.testFlag(Y_PRESSED)) {
Leander Schulten's avatar
Leander Schulten committed
577
578
579
            newY = currentTimePoint->value;
        }else if(!modifier.testFlag(X_PRESSED)&&modifier.testFlag(Y_PRESSED)){
            newX = currentTimePoint->time;
580
581
        }*/

582
583
        if (currentTimePoint->time != newX) {
            *const_cast<double *>(&currentTimePoint->time) = newX;
Leander Schulten's avatar
Leander Schulten committed
584
585
586
            emit currentTimePointWrapper.timeChanged();
        }

587
        if (currentTimePoint->value != newY) {
Leander Schulten's avatar
Leander Schulten committed
588
589
590
            currentTimePoint->value = newY;
            emit currentTimePointWrapper.valueChanged();
        }
591
        update();
Leander Schulten's avatar
Leander Schulten committed
592
    }
593
594
595
596
    emit mouseXChanged();
    // update hover segment
    updateHoverSegment();
    updateSelectedSegment();
Leander Schulten's avatar
Leander Schulten committed
597
598
}

599
void ChannelProgrammEditor::mouseReleaseEvent(QMouseEvent *event) {
600
601
    event->accept();
    lastMousePosition = INVALID_POS;
602
603
604
605
606
607
    emit mouseXChanged();
    if (event->timestamp() - mousePressTimestamp < 300) {
        if (event->button() == Qt::LeftButton) {
            emit click(event->x(), event->y());
        } else if (event->button() == Qt::RightButton) {
            emit rightClick(event->x(), event->y());
Leander Schulten's avatar
Leander Schulten committed
608
609
        }
    }
Leander Schulten's avatar
Leander Schulten committed
610
611
}

612
613
614
615
void ChannelProgrammEditor::hoverEnterEvent(QHoverEvent *e) {
    lastMousePosition = e->pos();
    e->accept();
    forceActiveFocus(Qt::MouseFocusReason);
Leander Schulten's avatar
Leander Schulten committed
616
617
}

618
619
620
621
622
DMX::TimePoint *ChannelProgrammEditor::getCurrentTimePoint() {
    if (haveCurrentTimePoint()) {
        return const_cast<DMX::TimePoint *>(&*currentTimePoint);
    }
    return nullptr;
Leander Schulten's avatar
Leander Schulten committed
623
}
624
625

} // namespace GUI