• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mcallegari / qlcplus / 44419295440

18 Nov 2026 11:16PM UTC coverage: 61.964% (+25.2%) from 36.744%
44419295440

push

github

mcallegari
engine/test: add missing file

18252 of 29456 relevant lines covered (61.96%)

62667.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.49
/engine/src/function.cpp
1
/*
2
  Q Light Controller Plus
3
  function.cpp
4

5
  Copyright (c) Heikki Junnila
6
                Massimo Callegari
7

8
  Licensed under the Apache License, Version 2.0 (the "License");
9
  you may not use this file except in compliance with the License.
10
  You may obtain a copy of the License at
11

12
      http://www.apache.org/licenses/LICENSE-2.0.txt
13

14
  Unless required by applicable law or agreed to in writing, software
15
  distributed under the License is distributed on an "AS IS" BASIS,
16
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
  See the License for the specific language governing permissions and
18
  limitations under the License.
19
*/
20

21
#include <QXmlStreamReader>
22
#include <QXmlStreamWriter>
23
#include <QElapsedTimer>
24
#include <QString>
25
#include <QDebug>
26
#include <math.h>
27

28
#include "qlcmacros.h"
29
#include "qlcfile.h"
30

31
#include "scriptwrapper.h"
32
#include "mastertimer.h"
33
#include "collection.h"
34
#include "rgbmatrix.h"
35
#include "function.h"
36
#include "sequence.h"
37
#include "chaser.h"
38
#include "audio.h"
39
#include "video.h"
40
#include "scene.h"
41
#include "show.h"
42
#include "efx.h"
43
#include "doc.h"
44

45
#define OVERRIDE_ATTRIBUTE_START_ID     128
46

47
const QString KSceneString      (      "Scene" );
48
const QString KChaserString     (     "Chaser" );
49
const QString KEFXString        (        "EFX" );
50
const QString KCollectionString ( "Collection" );
51
const QString KScriptString     (     "Script" );
52
const QString KRGBMatrixString  (  "RGBMatrix" );
53
const QString KShowString       (       "Show" );
54
const QString KSequenceString   (   "Sequence" );
55
const QString KAudioString      (      "Audio" );
56
const QString KVideoString      (      "Video" );
57
const QString KUndefinedString  (  "Undefined" );
58

59
const QString KLoopString       (       "Loop" );
60
const QString KPingPongString   (   "PingPong" );
61
const QString KSingleShotString ( "SingleShot" );
62
const QString KRandomString     (     "Random" );
63

64
const QString KBackwardString   (   "Backward" );
65
const QString KForwardString    (    "Forward" );
66

67
const QString KTimeTypeString   (       "Time" );
68
const QString KBeatsTypeString  (      "Beats" );
69

70
/*****************************************************************************
71
 * Initialization
72
 *****************************************************************************/
73

74
Function::Function(QObject *parent)
×
75
    : QObject(parent)
76
    , m_id(Function::invalidId())
×
77
    , m_type(Undefined)
×
78
    , m_path(QString())
×
79
    , m_visible(true)
×
80
    , m_runOrder(Loop)
×
81
    , m_direction(Forward)
×
82
    , m_tempoType(Time)
×
83
    , m_overrideTempoType(Original)
×
84
    , m_beatResyncNeeded(false)
×
85
    , m_fadeInSpeed(0)
×
86
    , m_fadeOutSpeed(0)
×
87
    , m_duration(0)
×
88
    , m_overrideFadeInSpeed(defaultSpeed())
×
89
    , m_overrideFadeOutSpeed(defaultSpeed())
×
90
    , m_overrideDuration(defaultSpeed())
×
91
    , m_flashing(false)
×
92
    , m_elapsed(0)
×
93
    , m_elapsedBeats(0)
×
94
    , m_stop(true)
×
95
    , m_running(false)
×
96
    , m_paused(false)
×
97
    , m_lastOverrideAttributeId(OVERRIDE_ATTRIBUTE_START_ID)
×
98
    , m_preserveAttributes(false)
×
99
    , m_blendMode(Universe::NormalBlend)
×
100
{
101
}
×
102

103
Function::Function(Doc* doc, Type t)
486 ✔
104
    : QObject(doc)
105
    , m_id(Function::invalidId())
972 ✔
106
    , m_type(t)
486 ✔
107
    , m_path(QString())
486 ✔
108
    , m_visible(true)
486 ✔
109
    , m_runOrder(Loop)
486 ✔
110
    , m_direction(Forward)
486 ✔
111
    , m_tempoType(Time)
486 ✔
112
    , m_overrideTempoType(Original)
486 ✔
113
    , m_beatResyncNeeded(false)
486 ✔
114
    , m_fadeInSpeed(0)
486 ✔
115
    , m_fadeOutSpeed(0)
486 ✔
116
    , m_duration(0)
486 ✔
117
    , m_overrideFadeInSpeed(defaultSpeed())
486 ✔
118
    , m_overrideFadeOutSpeed(defaultSpeed())
486 ✔
119
    , m_overrideDuration(defaultSpeed())
486 ✔
120
    , m_flashing(false)
486 ✔
121
    , m_elapsed(0)
486 ✔
122
    , m_elapsedBeats(0)
486 ✔
123
    , m_stop(true)
486 ✔
124
    , m_running(false)
486 ✔
125
    , m_paused(false)
486 ✔
126
    , m_lastOverrideAttributeId(OVERRIDE_ATTRIBUTE_START_ID)
486 ✔
127
    , m_preserveAttributes(false)
486 ✔
128
    , m_blendMode(Universe::NormalBlend)
972 ✔
129
{
130
    Q_ASSERT(doc != NULL);
486 ✔
131
    registerAttribute(tr("Intensity"), Multiply | Single);
486 ✔
132
}
486 ✔
133

134
Function::~Function()
486 ✔
135
{
136
}
486 ✔
137

138
Doc* Function::doc() const
2,236 ✔
139
{
140
    return qobject_cast<Doc*>(parent());
2,236 ✔
141
}
142

143
/*****************************************************************************
144
 * Copying
145
 *****************************************************************************/
146
Function *Function::createCopy(Doc *doc, bool addToDoc)
×
147
{
148
    Q_ASSERT(doc != NULL);
×
149

150
    Function* copy = new Function(doc, type());
×
151
    if (copy->copyFrom(this) == false)
×
152
    {
153
        delete copy;
×
154
        copy = NULL;
×
155
    }
156
    if (addToDoc == true && doc->addFunction(copy) == false)
×
157
    {
158
        delete copy;
×
159
        copy = NULL;
×
160
    }
161

162
    return copy;
×
163
}
164

165
bool Function::copyFrom(const Function* function)
19 ✔
166
{
167
    if (function == NULL)
19 ✔
168
        return false;
1 ✔
169

170
    m_name = function->name();
18 ✔
171
    m_runOrder = function->runOrder();
18 ✔
172
    m_direction = function->direction();
18 ✔
173
    m_tempoType = function->tempoType();
18 ✔
174
    m_fadeInSpeed = function->fadeInSpeed();
18 ✔
175
    m_fadeOutSpeed = function->fadeOutSpeed();
18 ✔
176
    m_duration = function->duration();
18 ✔
177
    m_path = function->path(true);
18 ✔
178
    m_visible = function->isVisible();
18 ✔
179
    m_blendMode = function->blendMode();
18 ✔
180
    m_uiState = function->uiStateMap();
18 ✔
181

182
    emit changed(m_id);
18 ✔
183

184
    return true;
18 ✔
185
}
186

187
/*****************************************************************************
188
 * ID
189
 *****************************************************************************/
190

191
void Function::setID(quint32 id)
295 ✔
192
{
193
    /* Don't set doc modified status or emit changed signal, because this
194
       function is called only once during function creation. */
195
    m_id = id;
295 ✔
196
}
295 ✔
197

198
quint32 Function::id() const
3,733 ✔
199
{
200
    return m_id;
3,733 ✔
201
}
202

203
quint32 Function::invalidId()
6,199 ✔
204
{
205
    return UINT_MAX;
6,199 ✔
206
}
207

208
/*****************************************************************************
209
 * Name
210
 *****************************************************************************/
211

212
void Function::setName(const QString& name)
624 ✔
213
{
214
    if (m_name == name)
624 ✔
215
        return;
×
216

217
    m_name = QString(name);
624 ✔
218

219
    emit nameChanged(m_id);
624 ✔
220
}
221

222
QString Function::name() const
934 ✔
223
{
224
    return m_name;
934 ✔
225
}
226

227
/*****************************************************************************
228
 * Type
229
 *****************************************************************************/
230

231
Function::Type Function::type() const
1,068 ✔
232
{
233
    return m_type;
1,068 ✔
234
}
235

236
QString Function::typeString() const
3 ✔
237
{
238
    return Function::typeToString(type());
3 ✔
239
}
240

241
QString Function::typeToString(Type type)
233 ✔
242
{
243
    switch (type)
233 ✔
244
    {
245
        case SceneType:      return KSceneString;
125 ✔
246
        case ChaserType:     return KChaserString;
18 ✔
247
        case EFXType:        return KEFXString;
14 ✔
248
        case CollectionType: return KCollectionString;
19 ✔
249
        case ScriptType:     return KScriptString;
2 ✔
250
        case RGBMatrixType:  return KRGBMatrixString;
5 ✔
251
        case ShowType:       return KShowString;
9 ✔
252
        case SequenceType:   return KSequenceString;
5 ✔
253
        case AudioType:      return KAudioString;
1 ✔
254
        case VideoType:      return KVideoString;
3 ✔
255
        case Undefined:
32 ✔
256
        default:
257
            return KUndefinedString;
32 ✔
258
    }
259
}
260

261
Function::Type Function::stringToType(const QString& string)
24 ✔
262
{
263
    if (string == KSceneString)
24 ✔
264
        return SceneType;
2 ✔
265
    else if (string == KChaserString)
22 ✔
266
        return ChaserType;
2 ✔
267
    else if (string == KEFXString)
20 ✔
268
        return EFXType;
2 ✔
269
    else if (string == KCollectionString)
18 ✔
270
        return CollectionType;
6 ✔
271
    else if (string == KScriptString)
12 ✔
272
        return ScriptType;
1 ✔
273
    else if (string == KRGBMatrixString)
11 ✔
274
        return RGBMatrixType;
1 ✔
275
    else if (string == KShowString)
10 ✔
276
        return ShowType;
2 ✔
277
    else if (string == KSequenceString)
8 ✔
278
        return SequenceType;
1 ✔
279
    else if (string == KAudioString)
7 ✔
280
        return AudioType;
1 ✔
281
    else if (string == KVideoString)
6 ✔
282
        return VideoType;
1 ✔
283
    else
284
        return Undefined;
5 ✔
285
}
286

287
QIcon Function::getIcon() const
×
288
{
289
    return QIcon(":/function.png");
×
290
}
291

292
/*********************************************************************
293
 * Path
294
 *********************************************************************/
295
void Function::setPath(QString path)
9 ✔
296
{
297
    if (path.contains(typeToString(type())))
9 ✔
298
        path.remove(typeToString(type()) + "/");
×
299
    //qDebug() << "Function " << name() << "path set to:" << path;
300
    m_path = path;
9 ✔
301
}
9 ✔
302

303
QString Function::path(bool simplified) const
30 ✔
304
{
305
    if (simplified == true)
30 ✔
306
        return m_path;
30 ✔
307
    else
308
        return QString("%1/%2").arg(typeToString(type())).arg(m_path);
×
309
}
310

311
/*********************************************************************
312
 * Visibility
313
 *********************************************************************/
314

315
void Function::setVisible(bool visible)
10 ✔
316
{
317
    m_visible = visible;
10 ✔
318
}
10 ✔
319

320
bool Function::isVisible() const
36 ✔
321
{
322
    return m_visible;
36 ✔
323
}
324

325
/*********************************************************************
326
 * Common
327
 *********************************************************************/
328

329
bool Function::saveXMLCommon(QXmlStreamWriter *doc) const
12 ✔
330
{
331
    Q_ASSERT(doc != NULL);
12 ✔
332

333
    doc->writeAttribute(KXMLQLCFunctionID, QString::number(id()));
24 ✔
334
    doc->writeAttribute(KXMLQLCFunctionType, Function::typeToString(type()));
24 ✔
335
    doc->writeAttribute(KXMLQLCFunctionName, name());
24 ✔
336
    if (isVisible() == false)
12 ✔
337
        doc->writeAttribute(KXMLQLCFunctionHidden, KXMLQLCTrue);
×
338
    if (path(true).isEmpty() == false)
12 ✔
339
        doc->writeAttribute(KXMLQLCFunctionPath, path(true));
×
340
    if (blendMode() != Universe::NormalBlend)
12 ✔
341
        doc->writeAttribute(KXMLQLCFunctionBlendMode, Universe::blendModeToString(blendMode()));
×
342

343
    return true;
12 ✔
344
}
345

346
/*****************************************************************************
347
 * Running order
348
 *****************************************************************************/
349

350
void Function::setRunOrder(const Function::RunOrder& order)
75 ✔
351
{
352
    if (order == Loop || order == SingleShot || order == PingPong || order == Random)
75 ✔
353
        m_runOrder = order;
74 ✔
354
    else
355
        m_runOrder = Loop;
1 ✔
356
    emit runOrderChanged();
75 ✔
357
    emit changed(m_id);
75 ✔
358
}
75 ✔
359

360
Function::RunOrder Function::runOrder() const
1,845 ✔
361
{
362
    return m_runOrder;
1,845 ✔
363
}
364

365
QString Function::runOrderToString(const RunOrder& order)
16 ✔
366
{
367
    switch (order)
16 ✔
368
    {
369
    default:
6 ✔
370
    case Loop:
371
        return KLoopString;
6 ✔
372
    case PingPong:
3 ✔
373
        return KPingPongString;
3 ✔
374
    case SingleShot:
6 ✔
375
        return KSingleShotString;
6 ✔
376
    case Random:
1 ✔
377
        return KRandomString;
1 ✔
378
    }
379
}
380

381
Function::RunOrder Function::stringToRunOrder(const QString& str)
20 ✔
382
{
383
    if (str == KPingPongString)
20 ✔
384
        return PingPong;
3 ✔
385
    else if (str == KSingleShotString)
17 ✔
386
        return SingleShot;
9 ✔
387
    else if (str == KRandomString)
8 ✔
388
        return Random;
1 ✔
389
    else
390
        return Loop;
7 ✔
391
}
392

393
bool Function::saveXMLRunOrder(QXmlStreamWriter *doc) const
10 ✔
394
{
395
    Q_ASSERT(doc != NULL);
10 ✔
396

397
    doc->writeTextElement(KXMLQLCFunctionRunOrder, runOrderToString(runOrder()));
20 ✔
398

399
    return true;
10 ✔
400
}
401

402
bool Function::loadXMLRunOrder(QXmlStreamReader &root)
14 ✔
403
{
404
    if (root.name() != KXMLQLCFunctionRunOrder)
14 ✔
405
    {
406
        qWarning() << Q_FUNC_INFO << "RunOrder node not found";
×
407
        return false;
×
408
    }
409
    QString str = root.readElementText();
14 ✔
410
    if (str.isEmpty())
14 ✔
411
        return false;
×
412

413
    setRunOrder(stringToRunOrder(str));
14 ✔
414

415
    return true;
14 ✔
416
}
14 ✔
417

418
/*****************************************************************************
419
 * Direction
420
 *****************************************************************************/
421

422
void Function::setDirection(const Function::Direction& dir)
71 ✔
423
{
424
    if (dir == Forward || dir == Backward)
71 ✔
425
        m_direction = dir;
70 ✔
426
    else
427
        m_direction = Forward;
1 ✔
428
    emit changed(m_id);
71 ✔
429
}
71 ✔
430

431
Function::Direction Function::direction() const
18,303 ✔
432
{
433
    return m_direction;
18,303 ✔
434
}
435

436
QString Function::directionToString(const Direction& dir)
16 ✔
437
{
438
    switch (dir)
16 ✔
439
    {
440
        default:
8 ✔
441
        case Forward:
442
            return KForwardString;
8 ✔
443
        case Backward:
8 ✔
444
            return KBackwardString;
8 ✔
445
    }
446
}
447

448
Function::Direction Function::stringToDirection(const QString& str)
28 ✔
449
{
450
    if (str == KBackwardString)
28 ✔
451
        return Backward;
16 ✔
452
    else
453
        return Forward;
12 ✔
454
}
455

456
bool Function::saveXMLDirection(QXmlStreamWriter *doc) const
8 ✔
457
{
458
    Q_ASSERT(doc != NULL);
8 ✔
459

460
    doc->writeTextElement(KXMLQLCFunctionDirection, directionToString(direction()));
16 ✔
461

462
    return true;
8 ✔
463
}
464

465
bool Function::loadXMLDirection(QXmlStreamReader &root)
12 ✔
466
{
467
    if (root.name() != KXMLQLCFunctionDirection)
12 ✔
468
    {
469
        qWarning() << Q_FUNC_INFO << "Direction node not found";
×
470
        return false;
×
471
    }
472

473
    QString str = root.readElementText();
12 ✔
474
    if (str.isEmpty())
12 ✔
475
        return false;
×
476

477
    setDirection(stringToDirection(str));
12 ✔
478

479
    return true;
12 ✔
480
}
12 ✔
481

482
/*********************************************************************
483
 * Speed type
484
 *********************************************************************/
485

486
void Function::setTempoType(const Function::TempoType &type)
12 ✔
487
{
488
    if (type == m_tempoType)
12 ✔
489
        return;
1 ✔
490

491
    m_tempoType = type;
11 ✔
492

493
    /* Retrieve the current BPM value known by the Master Timer */
494
    float bpmNum = doc()->masterTimer()->bpmNumber();
11 ✔
495
    /* Calculate the duration in ms of a single beat */
496
    float beatTime = 60000.0 / bpmNum;
11 ✔
497

498
    switch (type)
11 ✔
499
    {
500
        /* Beats -> Time */
501
        case Time:
1 ✔
502
            setFadeInSpeed(beatsToTime(fadeInSpeed(), beatTime));
1 ✔
503
            setDuration(beatsToTime(duration(), beatTime));
1 ✔
504
            setFadeOutSpeed(beatsToTime(fadeOutSpeed(), beatTime));
1 ✔
505
            disconnect(doc()->masterTimer(), SIGNAL(bpmNumberChanged(int)),
1 ✔
506
                       this, SLOT(slotBPMChanged(int)));
507
        break;
1 ✔
508

509
        /* Time -> Beats */
510
        case Beats:
10 ✔
511
            setFadeInSpeed(timeToBeats(fadeInSpeed(), beatTime));
10 ✔
512
            setDuration(timeToBeats(duration(), beatTime));
10 ✔
513
            setFadeOutSpeed(timeToBeats(fadeOutSpeed(), beatTime));
10 ✔
514
            connect(doc()->masterTimer(), SIGNAL(bpmNumberChanged(int)),
10 ✔
515
                    this, SLOT(slotBPMChanged(int)));
516
        break;
10 ✔
517
        default:
×
518
            qDebug() << "Error. Unhandled tempo type" << type;
×
519
        break;
×
520
    }
521

522
    emit changed(m_id);
11 ✔
523
    emit tempoTypeChanged();
11 ✔
524
}
525

526
Function::TempoType Function::tempoType() const
2,539 ✔
527
{
528
    return m_tempoType;
2,539 ✔
529
}
530

531
QString Function::tempoTypeToString(const Function::TempoType &type)
2 ✔
532
{
533
    switch (type)
2 ✔
534
    {
535
        default:
1 ✔
536
        case Time:
537
            return KTimeTypeString;
1 ✔
538
        case Beats:
1 ✔
539
            return KBeatsTypeString;
1 ✔
540
    }
541
}
542

543
Function::TempoType Function::stringToTempoType(const QString &str)
2 ✔
544
{
545
    if (str == KTimeTypeString)
2 ✔
546
        return Time;
1 ✔
547
    else
548
        return Beats;
1 ✔
549
}
550

551
uint Function::timeToBeats(uint time, int beatDuration)
33 ✔
552
{
553
    if (time == 0 || time == infiniteSpeed())
33 ✔
554
        return time;
28 ✔
555

556
    uint value = 0;
5 ✔
557

558
    float beats = (float)time / (float)beatDuration;
5 ✔
559
    value = floor(beats) * 1000;
5 ✔
560

561
    beats -= floor(beats);
5 ✔
562
    beats = floor((beats * 1000) / 125) * 125;
5 ✔
563

564
    return value + beats;
5 ✔
565
}
566

567
uint Function::beatsToTime(uint beats, int beatDuration)
5 ✔
568
{
569
    if (beats == 0 || beats == infiniteSpeed())
5 ✔
570
        return beats;
1 ✔
571

572
    return ((float)beats / 1000.0) * beatDuration;
4 ✔
573
}
574

575
Function::TempoType Function::overrideTempoType() const
2 ✔
576
{
577
    return m_overrideTempoType;
2 ✔
578
}
579

580
void Function::setOverrideTempoType(Function::TempoType type)
1 ✔
581
{
582
    m_overrideTempoType = type;
1 ✔
583
}
1 ✔
584

585
void Function::slotBPMChanged(int bpmNumber)
×
586
{
587
    Q_UNUSED(bpmNumber)
588
    m_beatResyncNeeded = true;
×
589
}
×
590

591
bool Function::saveXMLTempoType(QXmlStreamWriter *doc) const
8 ✔
592
{
593
    Q_ASSERT(doc != NULL);
8 ✔
594

595
    /* Make this optional to keep projects lighter */
596
    if (tempoType() == Beats)
8 ✔
597
        doc->writeTextElement(KXMLQLCFunctionTempoType, tempoTypeToString(tempoType()));
×
598

599
    return true;
8 ✔
600
}
601

602
bool Function::loadXMLTempoType(QXmlStreamReader &root)
×
603
{
604
    if (root.name() != KXMLQLCFunctionTempoType)
×
605
    {
606
        qWarning() << Q_FUNC_INFO << "Tempo type node not found";
×
607
        return false;
×
608
    }
609

610
    QString str = root.readElementText();
×
611
    if (str.isEmpty())
×
612
        return false;
×
613

614
    setTempoType(stringToTempoType(str));
×
615

616
    return true;
×
617
}
×
618

619
/****************************************************************************
620
 * Speed
621
 ****************************************************************************/
622

623
void Function::setFadeInSpeed(uint ms)
43 ✔
624
{
625
    if (m_fadeInSpeed == ms)
43 ✔
626
        return;
11 ✔
627

628
    m_fadeInSpeed = ms;
32 ✔
629
    emit changed(m_id);
32 ✔
630
}
631

632
uint Function::fadeInSpeed() const
1,212 ✔
633
{
634
    return m_fadeInSpeed;
1,212 ✔
635
}
636

637
void Function::setFadeOutSpeed(uint ms)
38 ✔
638
{
639
    if (m_fadeOutSpeed == ms)
38 ✔
640
        return;
10 ✔
641

642
    m_fadeOutSpeed = ms;
28 ✔
643
    emit changed(m_id);
28 ✔
644
}
645

646
uint Function::fadeOutSpeed() const
182 ✔
647
{
648
    return m_fadeOutSpeed;
182 ✔
649
}
650

651
void Function::setDuration(uint ms)
161 ✔
652
{
653
    if (m_duration == ms)
161 ✔
654
        return;
12 ✔
655

656
    m_duration = ms;
149 ✔
657
    emit changed(m_id);
149 ✔
658
}
659

660
uint Function::duration() const
2,048 ✔
661
{
662
    return m_duration;
2,048 ✔
663
}
664

665
quint32 Function::totalDuration()
1 ✔
666
{
667
    // fall back to duration in case a
668
    // subclass doesn't provide this method
669
    return duration();
1 ✔
670
}
671

672
void Function::setTotalDuration(quint32 msec)
×
673
{
674
    Q_UNUSED(msec)
675
}
×
676

677
void Function::setOverrideFadeInSpeed(uint ms)
1 ✔
678
{
679
    m_overrideFadeInSpeed = ms;
1 ✔
680
}
1 ✔
681

682
uint Function::overrideFadeInSpeed() const
1,332 ✔
683
{
684
    return m_overrideFadeInSpeed;
1,332 ✔
685
}
686

687
void Function::setOverrideFadeOutSpeed(uint ms)
33 ✔
688
{
689
    m_overrideFadeOutSpeed = ms;
33 ✔
690
}
33 ✔
691

692
uint Function::overrideFadeOutSpeed() const
320 ✔
693
{
694
    return m_overrideFadeOutSpeed;
320 ✔
695
}
696

697
void Function::setOverrideDuration(uint ms)
1 ✔
698
{
699
    m_overrideDuration = ms;
1 ✔
700
}
1 ✔
701

702
uint Function::overrideDuration() const
198 ✔
703
{
704
    return m_overrideDuration;
198 ✔
705
}
706

707
QString Function::speedToString(uint ms)
13 ✔
708
{
709
    QString str;
13 ✔
710
    if (ms == infiniteSpeed())
13 ✔
711
    {
712
        str = QChar(0x221E); // Infinity symbol
1 ✔
713
    }
714
    else
715
    {
716
        uint h, m, s;
717

718
        h = ms / MS_PER_HOUR;
12 ✔
719
        ms -= (h * MS_PER_HOUR);
12 ✔
720

721
        m = ms / MS_PER_MINUTE;
12 ✔
722
        ms -= (m * MS_PER_MINUTE);
12 ✔
723

724
        s = ms / MS_PER_SECOND;
12 ✔
725
        ms -= (s * MS_PER_SECOND);
12 ✔
726

727
        if (h != 0)
12 ✔
728
            str += QString("%1h").arg(h, 1, 10, QChar('0'));
4 ✔
729
        if (m != 0)
12 ✔
730
            str += QString("%1m").arg(m, str.size() ? 2 : 1, 10, QChar('0'));
5 ✔
731
        if (s != 0)
12 ✔
732
            str += QString("%1s").arg(s, str.size() ? 2 : 1, 10, QChar('0'));
6 ✔
733
        if (ms != 0 || str.size() == 0)
12 ✔
734
            str += QString("%1ms").arg(ms, str.size() ? 3 : 1, 10, QChar('0'));
9 ✔
735
    }
736

737
    return str;
13 ✔
738
}
×
739

740
static uint speedSplit(QString& speedString, QString splitNeedle)
78 ✔
741
{
742
    QStringList splitResult;
78 ✔
743
    // Filter out "ms" because "m" and "s" may wrongly use it
744
    splitResult = speedString.split("ms");
78 ✔
745
    if (splitResult.count() > 1)
78 ✔
746
        splitResult = splitResult.at(0).split(splitNeedle);
12 ✔
747
    else
748
        splitResult = speedString.split(splitNeedle);
66 ✔
749

750
    if (splitResult.count() > 1)
78 ✔
751
    {
752
        speedString.remove(0, speedString.indexOf(splitNeedle) + 1);
13 ✔
753
        return splitResult.at(0).toUInt();
13 ✔
754
    }
755
    return 0;
65 ✔
756
}
78 ✔
757

758
uint Function::stringToSpeed(QString speed)
27 ✔
759
{
760
    uint value = 0;
27 ✔
761

762
    if (speed == QChar(0x221E)) // Infinity symbol
27 ✔
763
        return infiniteSpeed();
1 ✔
764

765
    value += speedSplit(speed, "h") * 1000 * 60 * 60;
26 ✔
766
    value += speedSplit(speed, "m") * 1000 * 60;
26 ✔
767
    value += speedSplit(speed, "s") * 1000;
26 ✔
768

769
    if (speed.contains("."))
26 ✔
770
    {
771
        // lround avoids toDouble precison issues (.03 transforms to .029)
772
        value += lround(speed.toDouble() * 1000.0);
20 ✔
773
    }
774
    else
775
    {
776
        if (speed.contains("ms"))
6 ✔
777
            speed = speed.split("ms").at(0);
3 ✔
778
        value += speed.toUInt();
6 ✔
779
    }
780

781
    return speedNormalize(value);
26 ✔
782
}
783

784
uint Function::speedNormalize(uint speed)
81 ✔
785
{
786
    if ((int)speed < 0)
81 ✔
787
        return infiniteSpeed();
6 ✔
788
    return speed;
75 ✔
789
}
790

791
uint Function::speedAdd(uint left, uint right)
14 ✔
792
{
793
    if (speedNormalize(left) == infiniteSpeed()
14 ✔
794
        || speedNormalize(right) == infiniteSpeed())
14 ✔
795
        return infiniteSpeed();
3 ✔
796

797
    return speedNormalize(left + right);
11 ✔
798
}
799

800
uint Function::speedSubtract(uint left, uint right)
24 ✔
801
{
802
    if (right >= left)
24 ✔
803
        return 0;
21 ✔
804
    if (speedNormalize(right) == infiniteSpeed())
3 ✔
805
        return 0;
×
806
    if (speedNormalize(left) == infiniteSpeed())
3 ✔
807
        return infiniteSpeed();
1 ✔
808

809
    return speedNormalize(left - right);
2 ✔
810
}
811

812
void Function::tap()
×
813
{
814
}
×
815

816
bool Function::loadXMLSpeed(QXmlStreamReader &speedRoot)
8 ✔
817
{
818
    if (speedRoot.name() != KXMLQLCFunctionSpeed)
8 ✔
819
        return false;
×
820

821
    QXmlStreamAttributes attrs = speedRoot.attributes();
8 ✔
822

823
    m_fadeInSpeed = attrs.value(KXMLQLCFunctionSpeedFadeIn).toString().toUInt();
8 ✔
824
    m_fadeOutSpeed = attrs.value(KXMLQLCFunctionSpeedFadeOut).toString().toUInt();
8 ✔
825
    m_duration = attrs.value(KXMLQLCFunctionSpeedDuration).toString().toUInt();
8 ✔
826

827
    speedRoot.skipCurrentElement();
8 ✔
828

829
    return true;
8 ✔
830
}
8 ✔
831

832
bool Function::saveXMLSpeed(QXmlStreamWriter *doc) const
10 ✔
833
{
834
    doc->writeStartElement(KXMLQLCFunctionSpeed);
20 ✔
835
    doc->writeAttribute(KXMLQLCFunctionSpeedFadeIn, QString::number(fadeInSpeed()));
20 ✔
836
    doc->writeAttribute(KXMLQLCFunctionSpeedFadeOut, QString::number(fadeOutSpeed()));
20 ✔
837
    doc->writeAttribute(KXMLQLCFunctionSpeedDuration, QString::number(duration()));
20 ✔
838
    doc->writeEndElement();
10 ✔
839

840
    return true;
10 ✔
841
}
842

843
uint Function::infiniteSpeed()
1,531 ✔
844
{
845
    return (uint) -2;
1,531 ✔
846
}
847

848
uint Function::defaultSpeed()
3,932 ✔
849
{
850
    return (uint) -1;
3,932 ✔
851
}
852

853
/*****************************************************************************
854
 * UI State
855
 *****************************************************************************/
856

857
QVariant Function::uiStateValue(QString property) const
2 ✔
858
{
859
    return m_uiState.value(property, QVariant());
4 ✔
860
}
861

862
void Function::setUiStateValue(QString property, QVariant value)
1 ✔
863
{
864
    m_uiState[property] = value;
1 ✔
865
}
1 ✔
866

867
QMap<QString, QVariant> Function::uiStateMap() const
20 ✔
868
{
869
    return m_uiState;
20 ✔
870
}
871

872
/*****************************************************************************
873
 * Fixtures
874
 *****************************************************************************/
875

876
void Function::slotFixtureRemoved(quint32 fid)
1 ✔
877
{
878
    Q_UNUSED(fid);
879
}
1 ✔
880

881
/*****************************************************************************
882
 * Load & Save
883
 *****************************************************************************/
884
bool Function::saveXML(QXmlStreamWriter *doc) const
×
885
{
886
    Q_UNUSED(doc)
887
    return false;
×
888
}
889

890
bool Function::loadXML(QXmlStreamReader &root)
×
891
{
892
    Q_UNUSED(root)
893
    return false;
×
894
}
895

896
bool Function::loader(QXmlStreamReader &root, Doc* doc)
13 ✔
897
{
898
    if (root.name() != KXMLQLCFunction)
13 ✔
899
    {
900
        qWarning("Function node not found!");
2 ✔
901
        return false;
2 ✔
902
    }
903

904
    QXmlStreamAttributes attrs = root.attributes();
11 ✔
905

906
    /* Get common information from the tag's attributes */
907
    quint32 id = attrs.value(KXMLQLCFunctionID).toString().toUInt();
11 ✔
908
    QString name = attrs.value(KXMLQLCFunctionName).toString();
11 ✔
909
    Type type = Function::stringToType(attrs.value(KXMLQLCFunctionType).toString());
11 ✔
910
    QString path;
11 ✔
911
    bool visible = true;
11 ✔
912
    Universe::BlendMode blendMode = Universe::NormalBlend;
11 ✔
913

914
    if (attrs.hasAttribute(KXMLQLCFunctionPath))
11 ✔
915
        path = attrs.value(KXMLQLCFunctionPath).toString();
×
916
    if (attrs.hasAttribute(KXMLQLCFunctionHidden))
11 ✔
917
        visible = false;
×
918
    if (attrs.hasAttribute(KXMLQLCFunctionBlendMode))
11 ✔
919
        blendMode = Universe::stringToBlendMode(attrs.value(KXMLQLCFunctionBlendMode).toString());
×
920

921
    /* Check for ID validity before creating the function */
922
    if (id == Function::invalidId())
11 ✔
923
    {
924
        qWarning() << Q_FUNC_INFO << "Function ID" << id << "is not allowed.";
×
925
        return false;
×
926
    }
927

928
    /* Create a new function according to the type */
929
    Function* function = NULL;
11 ✔
930
    if (type == Function::SceneType)
11 ✔
931
        function = new class Scene(doc);
1 ✔
932
    else if (type == Function::ChaserType)
10 ✔
933
        function = new class Chaser(doc);
1 ✔
934
    else if (type == Function::CollectionType)
9 ✔
935
        function = new class Collection(doc);
5 ✔
936
    else if (type == Function::EFXType)
4 ✔
937
        function = new class EFX(doc);
1 ✔
938
    else if (type == Function::ScriptType)
3 ✔
939
        function = new class Script(doc);
×
940
    else if (type == Function::RGBMatrixType)
3 ✔
941
        function = new class RGBMatrix(doc);
×
942
    else if (type == Function::ShowType)
3 ✔
943
        function = new class Show(doc);
1 ✔
944
    else if (type == Function::SequenceType)
2 ✔
945
        function = new class Sequence(doc);
×
946
    else if (type == Function::AudioType)
2 ✔
947
        function = new class Audio(doc);
×
948
    else if (type == Function::VideoType)
2 ✔
949
        function = new class Video(doc);
×
950
    else
951
        return false;
2 ✔
952

953
    function->setName(name);
9 ✔
954
    function->setPath(path);
9 ✔
955
    function->setVisible(visible);
9 ✔
956
    function->setBlendMode(blendMode);
9 ✔
957
    /* Set the ID before loading, so that a Function can detect
958
       references to itself (e.g. a Show containing itself) */
959
    function->setID(id);
9 ✔
960
    if (function->loadXML(root) == true)
9 ✔
961
    {
962
        if (doc->addFunction(function, id) == true)
9 ✔
963
        {
964
            /* Success */
965
            return true;
9 ✔
966
        }
967
        else
968
        {
969
            qWarning() << "Function" << name << "cannot be created.";
×
970
            delete function;
×
971
            return false;
×
972
        }
973
    }
974
    else
975
    {
976
        qWarning() << "Function" << name << "cannot be loaded.";
×
977
        delete function;
×
978
        return false;
×
979
    }
980
}
11 ✔
981

982
void Function::postLoad()
×
983
{
984
    /* NOP */
985
}
×
986

987
bool Function::contains(quint32 functionId) const
19 ✔
988
{
989
    Q_UNUSED(functionId);
990
    return false;
19 ✔
991
}
992

993
QList<quint32> Function::components() const
×
994
{
995
    return QList<quint32>();
×
996
}
997

998
/*****************************************************************************
999
 * Flash
1000
 *****************************************************************************/
1001

1002
void Function::flash(MasterTimer *timer, bool shouldOverride, bool forceLTP)
6 ✔
1003
{
1004
    Q_UNUSED(timer);
1005
    Q_UNUSED(shouldOverride);
1006
    Q_UNUSED(forceLTP);
1007

1008
    if (m_flashing == false)
6 ✔
1009
    {
1010
        emit flashing(m_id, true);
5 ✔
1011
    }
1012

1013
    m_flashing = true;
6 ✔
1014
}
6 ✔
1015

1016
void Function::unFlash(MasterTimer *timer)
5 ✔
1017
{
1018
    Q_UNUSED(timer);
1019

1020
    if (m_flashing == true)
5 ✔
1021
        emit flashing(m_id, false);
5 ✔
1022

1023
    m_flashing = false;
5 ✔
1024
}
5 ✔
1025

1026
bool Function::flashing() const
22 ✔
1027
{
1028
    return m_flashing;
22 ✔
1029
}
1030

1031
/*****************************************************************************
1032
 * Running
1033
 *****************************************************************************/
1034

1035
void Function::preRun(MasterTimer *timer)
164 ✔
1036
{
1037
    Q_UNUSED(timer);
1038

1039
    qDebug() << "Function preRun. Name:" << m_name << "ID:" << m_id << "type:" << typeToString(type());
164 ✔
1040
    m_running = true;
164 ✔
1041

1042
    emit running(m_id);
164 ✔
1043
}
164 ✔
1044

1045
void Function::write(MasterTimer *timer, QList<Universe *> universes)
×
1046
{
1047
    Q_UNUSED(timer);
1048
    Q_UNUSED(universes);
1049
}
×
1050

1051
void Function::postRun(MasterTimer *timer, QList<Universe *> universes)
140 ✔
1052
{
1053
    Q_UNUSED(timer);
1054
    Q_UNUSED(universes);
1055

1056
    qDebug() << "Function postRun. Name:" << m_name << "ID: " << m_id;
140 ✔
1057

1058
    {
1059
        QMutexLocker locker(&m_stopMutex);
140 ✔
1060

1061
        resetElapsed();
140 ✔
1062
        if (m_preserveAttributes == false)
140 ✔
1063
            resetAttributes();
140 ✔
1064

1065
        m_functionStopped.wakeAll();
140 ✔
1066
    }
140 ✔
1067

1068
    m_paused = false;
140 ✔
1069
    m_running = false;
140 ✔
1070
    emit stopped(m_id);
140 ✔
1071
}
140 ✔
1072

1073
void Function::dismissAllFaders()
103 ✔
1074
{
1075
    QMapIterator <quint32, QSharedPointer<GenericFader> > it(m_fadersMap);
103 ✔
1076
    while (it.hasNext() == true)
198 ✔
1077
    {
1078
        it.next();
95 ✔
1079
        QSharedPointer<GenericFader> fader = it.value();
95 ✔
1080
        if (!fader.isNull())
95 ✔
1081
            fader->requestDelete();
95 ✔
1082
    }
95 ✔
1083

1084
    m_fadersMap.clear();
103 ✔
1085
}
103 ✔
1086

1087
bool Function::isRunning() const
18,574 ✔
1088
{
1089
    return m_running;
18,574 ✔
1090
}
1091

1092
bool Function::isPaused() const
591 ✔
1093
{
1094
    return m_paused;
591 ✔
1095
}
1096

1097
/*****************************************************************************
1098
 * Elapsed ticks while running
1099
 *****************************************************************************/
1100

1101
quint32 Function::elapsed() const
24 ✔
1102
{
1103
    return m_elapsed;
24 ✔
1104
}
1105

1106
quint32 Function::elapsedBeats() const
1 ✔
1107
{
1108
    return m_elapsedBeats;
1 ✔
1109
}
1110

1111
void Function::resetElapsed()
141 ✔
1112
{
1113
    qDebug() << Q_FUNC_INFO;
141 ✔
1114
    m_elapsed = 0;
141 ✔
1115
    m_elapsedBeats = 0;
141 ✔
1116
}
141 ✔
1117

1118
void Function::incrementElapsed()
640 ✔
1119
{
1120
    // Don't wrap around. UINT_MAX is the maximum fade/hold time.
1121
    if (m_elapsed < UINT_MAX - MasterTimer::tick())
640 ✔
1122
        m_elapsed += MasterTimer::tick();
638 ✔
1123
    else
1124
        m_elapsed = UINT_MAX;
2 ✔
1125
}
640 ✔
1126

1127
void Function::incrementElapsedBeats()
×
1128
{
1129
    m_elapsedBeats += 1000;
×
1130
}
×
1131

1132
void Function::roundElapsed(quint32 roundTime)
×
1133
{
1134
    if (roundTime == 0)
×
1135
        m_elapsed = 0;
×
1136
    else
1137
        m_elapsed %= roundTime;
×
1138
}
×
1139

1140
/*****************************************************************************
1141
 * Start & Stop
1142
 *****************************************************************************/
1143

1144
void Function::start(MasterTimer* timer, FunctionParent source, quint32 startTime,
218 ✔
1145
                     uint overrideFadeIn, uint overrideFadeOut, uint overrideDuration, TempoType overrideTempoType)
1146
{
1147
    qDebug() << "Function start(). Name:" << m_name << "ID: " << m_id << "source:" << source.type() << source.id() << ", startTime:" << startTime;
218 ✔
1148

1149
    Q_ASSERT(timer != NULL);
218 ✔
1150

1151
    {
1152
        QMutexLocker sourcesLocker(&m_sourcesMutex);
218 ✔
1153
        if (m_sources.contains(source))
218 ✔
1154
            return;
×
1155
        m_sources.append(source);
218 ✔
1156
        if (m_sources.size() > 1)
218 ✔
1157
            return;
×
1158
    }
218 ✔
1159

1160
    /** If we're in a paused state, then just return to the running state
1161
     *  to let subclasses resuming what they were doing. */
1162
    if (m_paused == true)
218 ✔
1163
    {
1164
        m_paused = false;
×
1165
        return;
×
1166
    }
1167

1168
    m_elapsed = startTime;
218 ✔
1169
    m_elapsedBeats = 0;
218 ✔
1170
    m_overrideFadeInSpeed = overrideFadeIn;
218 ✔
1171
    m_overrideFadeOutSpeed = overrideFadeOut;
218 ✔
1172
    m_overrideDuration = overrideDuration;
218 ✔
1173
    m_overrideTempoType = overrideTempoType == Original ? tempoType() : overrideTempoType;
218 ✔
1174

1175
    m_stop = false;
218 ✔
1176
    timer->startFunction(this);
218 ✔
1177
}
1178

1179
void Function::setPause(bool enable)
7 ✔
1180
{
1181
    if (enable && isRunning() == false)
7 ✔
1182
        return;
×
1183

1184
    m_paused = enable;
7 ✔
1185
}
1186

1187
void Function::stop(FunctionParent source, bool preserveAttributes)
225 ✔
1188
{
1189
    qDebug() << "Function stop(). Name:" << m_name << "ID: " << m_id << "source:" << source.type() << source.id();
225 ✔
1190

1191
    QMutexLocker sourcesLocker(&m_sourcesMutex);
225 ✔
1192

1193
    if ((source.id() == id() && source.type() == FunctionParent::Function) ||
461 ✔
1194
        (source.type() == FunctionParent::Master) ||
461 ✔
1195
        (source.type() == FunctionParent::ManualVCWidget))
183 ✔
1196
    {
1197
        m_sources.clear();
50 ✔
1198
    }
1199
    else
1200
    {
1201
        m_sources.removeAll(source);
175 ✔
1202
    }
1203

1204
    if (m_sources.size() == 0)
225 ✔
1205
    {
1206
        m_stop = true;
225 ✔
1207
        m_preserveAttributes = preserveAttributes;
225 ✔
1208
    }
1209
}
225 ✔
1210

1211
bool Function::stopped() const
826 ✔
1212
{
1213
    return m_stop;
826 ✔
1214
}
1215

1216
bool Function::startedAsChild() const
×
1217
{
1218
    QMutexLocker sourcesLocker(const_cast<QMutex*>(&m_sourcesMutex));
×
1219
    foreach (FunctionParent source, m_sources)
×
1220
    {
1221
        if (source.type() == FunctionParent::Function && source.id() != id())
×
1222
            return true;
×
1223
    }
×
1224
    return false;
×
1225
}
×
1226

1227
int Function::invalidAttributeId()
2,058 ✔
1228
{
1229
    return -1;
2,058 ✔
1230
}
1231

1232
bool Function::stopAndWait()
1 ✔
1233
{
1234
    QMutexLocker locker(&m_stopMutex);
1 ✔
1235

1236
    stop(FunctionParent::master());
1 ✔
1237

1238
    QElapsedTimer watchdog;
1 ✔
1239
    watchdog.restart();
1 ✔
1240

1241
    // block thread for maximum 2 seconds
1242
    while (m_running == true)
21 ✔
1243
    {
1244
        if (watchdog.elapsed() > 2000)
21 ✔
1245
            return false;
1 ✔
1246

1247
        // wait until the function has stopped
1248
        m_functionStopped.wait(&m_stopMutex, 100);
20 ✔
1249
    }
1250

1251
    return true;
×
1252
}
1 ✔
1253

1254
/*****************************************************************************
1255
 * Attributes
1256
 *****************************************************************************/
1257

1258
int Function::registerAttribute(QString name, int flags, qreal min, qreal max, qreal value)
1,338 ✔
1259
{
1260
    for (int i = 0; i < m_attributes.count(); i++)
3,790 ✔
1261
    {
1262
        if (m_attributes[i].m_name == name)
2,452 ✔
1263
        {
1264
            m_attributes[i].m_min = min;
×
1265
            m_attributes[i].m_max = max;
×
1266
            m_attributes[i].m_value = value;
×
1267
            m_attributes[i].m_flags = flags;
×
1268
            m_attributes[i].m_isOverridden = false;
×
1269
            m_attributes[i].m_overrideValue = 0.0;
×
1270
            return i;
×
1271
        }
1272
    }
1273
    Attribute newAttr;
1,338 ✔
1274
    newAttr.m_name = name;
1,338 ✔
1275
    newAttr.m_min = min;
1,338 ✔
1276
    newAttr.m_max = max;
1,338 ✔
1277
    newAttr.m_value = value;
1,338 ✔
1278
    newAttr.m_flags = flags;
1,338 ✔
1279
    newAttr.m_isOverridden = false;
1,338 ✔
1280
    newAttr.m_overrideValue = 0.0;
1,338 ✔
1281
    m_attributes.append(newAttr);
1,338 ✔
1282

1283
    return m_attributes.count() - 1;
1,338 ✔
1284
}
1,338 ✔
1285

1286
int Function::requestAttributeOverride(int attributeIndex, qreal value)
341 ✔
1287
{
1288
    if (attributeIndex < 0 || attributeIndex >= m_attributes.count())
341 ✔
1289
        return -1;
×
1290

1291
    int attributeID = invalidAttributeId();
341 ✔
1292

1293
    if (m_attributes.at(attributeIndex).m_flags & Single)
341 ✔
1294
    {
1295
        QMap <int, AttributeOverride>::iterator it = m_overrideMap.begin();
339 ✔
1296
        for (; it != m_overrideMap.end(); it++)
500 ✔
1297
        {
1298
            if (it.value().m_attrIndex == attributeIndex)
230 ✔
1299
            {
1300
                attributeID = it.key();
69 ✔
1301
                break;
69 ✔
1302
            }
1303
        }
1304
    }
1305

1306
    if (attributeID == invalidAttributeId())
341 ✔
1307
    {
1308
        AttributeOverride override;
1309
        override.m_attrIndex = attributeIndex;
272 ✔
1310
        override.m_value = 0.0;
272 ✔
1311

1312
        attributeID = m_lastOverrideAttributeId;
272 ✔
1313
        m_overrideMap[attributeID] = override;
272 ✔
1314

1315
        qDebug() << name() << "Override requested for new attribute" << attributeIndex << "value" << value << "new ID" << attributeID;
272 ✔
1316

1317
        calculateOverrideValue(attributeIndex);
272 ✔
1318

1319
        m_lastOverrideAttributeId++;
272 ✔
1320
    }
1321
    else
1322
    {
1323
        qDebug() << name() << "Override requested for existing attribute" << attributeIndex << "value" << value << "single ID" << attributeID;
69 ✔
1324
    }
1325

1326
    // actually apply the new override value
1327
    adjustAttribute(value, attributeID);
341 ✔
1328

1329
    return attributeID;
341 ✔
1330
}
1331

1332
void Function::releaseAttributeOverride(int attributeId)
1 ✔
1333
{
1334
    if (m_overrideMap.contains(attributeId) == false)
1 ✔
1335
        return;
×
1336

1337
    int attributeIndex = m_overrideMap[attributeId].m_attrIndex;
1 ✔
1338

1339
    m_overrideMap.remove(attributeId);
1 ✔
1340

1341
    calculateOverrideValue(attributeIndex);
1 ✔
1342

1343
    qDebug() << name() << "Attribute override released" << attributeId;
1 ✔
1344
}
1345

1346
bool Function::unregisterAttribute(QString name)
25 ✔
1347
{
1348
    for (int i = 0; i < m_attributes.count(); i++)
63 ✔
1349
    {
1350
        if (m_attributes[i].m_name == name)
63 ✔
1351
        {
1352
            m_attributes.removeAt(i);
25 ✔
1353

1354
            /** Removing an attribute shifts down the index of every attribute
1355
             *  following it, so the registered overrides must be realigned:
1356
             *  drop those referencing the attribute being removed and decrease
1357
             *  the ones referencing the attributes after it. Otherwise they
1358
             *  would end up controlling the wrong attribute, or point outside
1359
             *  the attributes list boundaries */
1360
            QMutableMapIterator <int, AttributeOverride> it(m_overrideMap);
25 ✔
1361
            while (it.hasNext())
27 ✔
1362
            {
1363
                it.next();
2 ✔
1364
                if (it.value().m_attrIndex == i)
2 ✔
1365
                    it.remove();
1 ✔
1366
                else if (it.value().m_attrIndex > i)
1 ✔
1367
                    it.value().m_attrIndex--;
1 ✔
1368
            }
1369

1370
            return true;
25 ✔
1371
        }
1372
    }
1373
    return false;
×
1374
}
1375

1376
bool Function::renameAttribute(int idx, QString newName)
2 ✔
1377
{
1378
    if (idx < 0 || idx >= m_attributes.count())
2 ✔
1379
        return false;
1 ✔
1380
    m_attributes[idx].m_name = newName;
1 ✔
1381

1382
    return true;
1 ✔
1383
}
1384

1385
int Function::adjustAttribute(qreal value, int attributeId)
579 ✔
1386
{
1387
    if (attributeId < 0)
579 ✔
1388
        return -1;
×
1389

1390
    int attrIndex;
1391

1392
    //qDebug() << name() << "Attribute ID:" << attributeId << ", val:" << value;
1393

1394
    if (attributeId < OVERRIDE_ATTRIBUTE_START_ID)
579 ✔
1395
    {
1396
        if (attributeId >= m_attributes.count() || m_attributes[attributeId].m_value == value)
228 ✔
1397
            return -1;
81 ✔
1398

1399
        // Adjust the original value of an attribute. Only Function editors should do this!
1400
        m_attributes[attributeId].m_value = CLAMP(value, m_attributes[attributeId].m_min, m_attributes[attributeId].m_max);
147 ✔
1401
        attrIndex = attributeId;
147 ✔
1402
    }
1403
    else
1404
    {
1405
        if (m_overrideMap.contains(attributeId) == false)
351 ✔
1406
            return -1;
1 ✔
1407

1408
        // Adjust an attribute override value and recalculate the final overridden value
1409
        m_overrideMap[attributeId].m_value = value;
350 ✔
1410
        attrIndex = m_overrideMap[attributeId].m_attrIndex;
350 ✔
1411

1412
        if (attrIndex < 0 || attrIndex >= m_attributes.count())
350 ✔
1413
            return -1;
×
1414

1415
        calculateOverrideValue(attrIndex);
350 ✔
1416
    }
1417

1418
    emit attributeChanged(attrIndex, m_attributes[attrIndex].m_isOverridden ?
994 ✔
1419
                                     m_attributes[attrIndex].m_overrideValue :
350 ✔
1420
                                     m_attributes[attrIndex].m_value);
147 ✔
1421

1422
    return attrIndex;
497 ✔
1423
}
1424

1425
void Function::resetAttributes()
142 ✔
1426
{
1427
    for (int i = 0; i < m_attributes.count(); i++)
427 ✔
1428
    {
1429
        m_attributes[i].m_isOverridden = false;
285 ✔
1430
        m_attributes[i].m_overrideValue = 0.0;
285 ✔
1431
    }
1432
    m_overrideMap.clear();
142 ✔
1433
    m_lastOverrideAttributeId = OVERRIDE_ATTRIBUTE_START_ID;
142 ✔
1434
}
142 ✔
1435

1436
qreal Function::getAttributeValue(int attributeIndex) const
91,012 ✔
1437
{
1438
    if (attributeIndex >= m_attributes.count())
91,012 ✔
1439
        return 0.0;
81 ✔
1440

1441
    return m_attributes[attributeIndex].m_isOverridden ?
181,862 ✔
1442
                m_attributes[attributeIndex].m_overrideValue :
226 ✔
1443
                m_attributes[attributeIndex].m_value;
181,636 ✔
1444
}
1445

1446
int Function::getAttributeIndex(QString name) const
4 ✔
1447
{
1448
    for (int i = 0; i < m_attributes.count(); i++)
9 ✔
1449
    {
1450
        Attribute attr = m_attributes.at(i);
8 ✔
1451
        if (attr.m_name == name)
8 ✔
1452
            return i;
3 ✔
1453
    }
8 ✔
1454
    return -1;
1 ✔
1455
}
1456

1457
QList<Attribute> Function::attributes() const
128 ✔
1458
{
1459
    return m_attributes;
128 ✔
1460
}
1461

1462
void Function::calculateOverrideValue(int attributeIndex)
623 ✔
1463
{
1464
    if (attributeIndex >= m_attributes.count())
623 ✔
1465
        return;
×
1466

1467
    qreal finalValue = 0.0;
623 ✔
1468
    bool found = false;
623 ✔
1469
    Attribute origAttr = m_attributes.at(attributeIndex);
623 ✔
1470

1471
    if (origAttr.m_flags & Multiply)
623 ✔
1472
        finalValue = origAttr.m_value;
617 ✔
1473

1474
    foreach (AttributeOverride attr, m_overrideMap)
1,571 ✔
1475
    {
1476
        if (attr.m_attrIndex != attributeIndex)
948 ✔
1477
            continue;
326 ✔
1478

1479
        found = true;
622 ✔
1480

1481
        if (origAttr.m_flags & Multiply)
622 ✔
1482
            finalValue = finalValue * attr.m_value;
616 ✔
1483
        else if (origAttr.m_flags & LastWins)
6 ✔
1484
            finalValue = attr.m_value;
6 ✔
1485
    }
623 ✔
1486

1487
    m_attributes[attributeIndex].m_overrideValue = finalValue;
623 ✔
1488
    m_attributes[attributeIndex].m_isOverridden = found;
623 ✔
1489
}
623 ✔
1490

1491
/*************************************************************************
1492
 * Blend mode
1493
 *************************************************************************/
1494

1495
void Function::setBlendMode(Universe::BlendMode mode)
6 ✔
1496
{
1497
    m_blendMode = mode;
6 ✔
1498
}
6 ✔
1499

1500
Universe::BlendMode Function::blendMode() const
158 ✔
1501
{
1502
    return m_blendMode;
158 ✔
1503
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc