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

mcallegari / qlcplus / 42371974533

07 Nov 2026 12:19PM UTC coverage: 36.826% (-0.1%) from 36.925%
42371974533

push

github

mcallegari
Back to 5.4.0 debug

19248 of 52268 relevant lines covered (36.83%)

41917.74 hits per line

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

56.82
/engine/src/rgbmatrix.cpp
1
/*
2
  Q Light Controller Plus
3
  rgbmatrix.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 <QCoreApplication>
24
#include <QElapsedTimer>
25
#include <QDebug>
26
#include <cmath>
27
#include <QDir>
28

29
#include "rgbscriptscache.h"
30
#include "qlcfixturehead.h"
31
#include "fixturegroup.h"
32
#include "genericfader.h"
33
#include "fadechannel.h"
34
#include "rgbmatrix.h"
35
#include "rgbimage.h"
36
#include "doc.h"
37

38
#define KXMLQLCRGBMatrixStartColor      QStringLiteral("MonoColor")
39
#define KXMLQLCRGBMatrixEndColor        QStringLiteral("EndColor")
40
#define KXMLQLCRGBMatrixColor           QStringLiteral("Color")
41
#define KXMLQLCRGBMatrixColorIndex      QStringLiteral("Index")
42

43
#define KXMLQLCRGBMatrixFixtureGroup    QStringLiteral("FixtureGroup")
44
#define KXMLQLCRGBMatrixDimmerControl   QStringLiteral("DimmerControl")
45

46
#define KXMLQLCRGBMatrixProperty        QStringLiteral("Property")
47
#define KXMLQLCRGBMatrixPropertyName    QStringLiteral("Name")
48
#define KXMLQLCRGBMatrixPropertyValue   QStringLiteral("Value")
49

50
#define KXMLQLCRGBMatrixControlMode         QStringLiteral("ControlMode")
51
#define KXMLQLCRGBMatrixControlModeRgb      QStringLiteral("RGB")
52
#define KXMLQLCRGBMatrixControlModeAmber    QStringLiteral("Amber")
53
#define KXMLQLCRGBMatrixControlModeWhite    QStringLiteral("White")
54
#define KXMLQLCRGBMatrixControlModeUV       QStringLiteral("UV")
55
#define KXMLQLCRGBMatrixControlModeDimmer   QStringLiteral("Dimmer")
56
#define KXMLQLCRGBMatrixControlModeShutter  QStringLiteral("Shutter")
57

58
static const int RGBMatrixColorMask = 0x00FFFFFF;
59

60
/****************************************************************************
61
 * Initialization
62
 ****************************************************************************/
63

64
RGBMatrix::RGBMatrix(Doc *doc)
11 ✔
65
    : Function(doc, Function::RGBMatrixType)
66
    , m_dimmerControl(false)
11 ✔
67
    , m_fixtureGroupID(FixtureGroup::invalidId())
22 ✔
68
    , m_group(NULL)
11 ✔
69
    , m_requestEngineCreation(true)
11 ✔
70
    , m_runAlgorithm(NULL)
11 ✔
71
    , m_algorithm(NULL)
11 ✔
72
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
73
    , m_algorithmMutex(QMutex::Recursive)
74
#endif
75
    , m_stepHandler(new RGBMatrixStep())
11 ✔
76
    , m_stepsCount(0)
11 ✔
77
    , m_stepBeatDuration(0)
11 ✔
78
    , m_continuousPhase(0.0)
11 ✔
79
    , m_applyingStyleAttributes(false)
11 ✔
80
    , m_controlMode(RGBMatrix::ControlModeRgb)
22 ✔
81
{
82
    setName(tr("New RGB Matrix"));
11 ✔
83
    setDuration(500);
11 ✔
84

85
    m_rgbColors.fill(QColor(), RGBAlgorithmColorDisplayCount);
11 ✔
86
    setColor(0, Qt::red);
11 ✔
87

88
    /** Register the fixed attributes before loading an algorithm, so that
89
     *  the attributes exposed by a Script are always appended after them */
90
    for (int i = 0; i < ColorAttributeCount; ++i)
66 ✔
91
    {
92
        registerAttribute(tr("Color %1").arg(i + 1), LastWins | Single, -1.0, 16777215.0,
66 ✔
93
                          getColor(i).isValid() ? int(getColor(i).rgb() & RGBMatrixColorMask) : -1);
121 ✔
94
    }
95

96
    int algoCount = RGBAlgorithm::algorithms(doc).count();
11 ✔
97
    registerAttribute(tr("Pattern"), LastWins | Single, 0.0, algoCount > 0 ? algoCount - 1 : 0, 0);
11 ✔
98

99
    setAlgorithm(RGBAlgorithm::algorithm(doc, "Stripes"));
11 ✔
100

101
    connect(doc, SIGNAL(fixtureGroupRemoved(quint32)),
11 ✔
102
            this, SLOT(slotFixtureGroupRemoved(quint32)));
103
}
11 ✔
104

105
RGBMatrix::~RGBMatrix()
14 ✔
106
{
107
    //if (m_runAlgorithm != NULL)
108
    //    delete m_runAlgorithm;
109
    delete m_algorithm;
11 ✔
110
    delete m_stepHandler;
11 ✔
111
}
14 ✔
112

113
QIcon RGBMatrix::getIcon() const
×
114
{
115
    return QIcon(":/rgbmatrix.png");
×
116
}
117

118
int RGBMatrix::algorithmIndex() const
16 ✔
119
{
120
    if (m_algorithm == NULL || doc() == NULL)
16 ✔
121
        return 0;
×
122

123
    QStringList algoList = RGBAlgorithm::algorithms(doc());
16 ✔
124
    int idx = algoList.indexOf(m_algorithm->name());
16 ✔
125
    return idx >= 0 ? idx : 0;
16 ✔
126
}
16 ✔
127

128
void RGBMatrix::setTotalDuration(quint32 msec)
2 ✔
129
{
130
    QMutexLocker algorithmLocker(&m_algorithmMutex);
2 ✔
131

132
    if (m_algorithm == NULL)
2 ✔
133
        return;
×
134

135
    FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
2 ✔
136
    if (grp == NULL)
2 ✔
137
        return;
×
138

139
    // an algorithm might have no steps (e.g. an image not loaded yet)
140
    // or fail to report them (-1)
141
    int steps = m_algorithm->rgbMapStepCount(grp->size());
2 ✔
142
    setDuration(msec / uint(qMax(1, steps)));
2 ✔
143
}
2 ✔
144

145
quint32 RGBMatrix::totalDuration()
4 ✔
146
{
147
    QMutexLocker algorithmLocker(&m_algorithmMutex);
4 ✔
148

149
    if (m_algorithm == NULL)
4 ✔
150
        return 0;
×
151

152
    FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
4 ✔
153
    if (grp == NULL)
4 ✔
154
        return 0;
1 ✔
155

156
    //qDebug () << "Algorithm steps:" << m_algorithm->rgbMapStepCount(grp->size());
157
    return qMax(0, m_algorithm->rgbMapStepCount(grp->size())) * duration();
3 ✔
158
}
4 ✔
159

160
void RGBMatrix::setDimmerControl(bool dimmerControl)
1 ✔
161
{
162
    m_dimmerControl = dimmerControl;
1 ✔
163
}
1 ✔
164

165
bool RGBMatrix::dimmerControl() const
2 ✔
166
{
167
    return m_dimmerControl;
2 ✔
168
}
169

170
/****************************************************************************
171
 * Copying
172
 ****************************************************************************/
173

174
Function* RGBMatrix::createCopy(Doc* doc, bool addToDoc)
1 ✔
175
{
176
    Q_ASSERT(doc != NULL);
1 ✔
177

178
    Function *copy = new RGBMatrix(doc);
1 ✔
179
    if (copy->copyFrom(this) == false)
1 ✔
180
    {
181
        delete copy;
×
182
        copy = NULL;
×
183
    }
184
    if (addToDoc == true && doc->addFunction(copy) == false)
1 ✔
185
    {
186
        delete copy;
×
187
        copy = NULL;
×
188
    }
189

190
    return copy;
1 ✔
191
}
192

193
bool RGBMatrix::copyFrom(const Function* function)
1 ✔
194
{
195
    const RGBMatrix *mtx = qobject_cast<const RGBMatrix*> (function);
1 ✔
196
    if (mtx == NULL)
1 ✔
197
        return false;
×
198

199
    setDimmerControl(mtx->dimmerControl());
1 ✔
200
    setFixtureGroup(mtx->fixtureGroup());
1 ✔
201

202
    m_rgbColors.clear();
1 ✔
203
    foreach (QColor col, mtx->getColors())
7 ✔
204
        m_rgbColors.append(col);
6 ✔
205

206
    if (mtx->algorithm() != NULL)
1 ✔
207
        setAlgorithm(mtx->algorithm()->clone());
1 ✔
208
    else
209
        setAlgorithm(NULL);
×
210

211
    setControlMode(mtx->controlMode());
1 ✔
212

213
    return Function::copyFrom(function);
1 ✔
214
}
215

216
/****************************************************************************
217
 * Fixtures
218
 ****************************************************************************/
219

220
quint32 RGBMatrix::fixtureGroup() const
42 ✔
221
{
222
    return m_fixtureGroupID;
42 ✔
223
}
224

225
void RGBMatrix::setFixtureGroup(quint32 id)
10 ✔
226
{
227
    m_fixtureGroupID = id;
10 ✔
228
    {
229
        QMutexLocker algoLocker(&m_algorithmMutex);
10 ✔
230
        m_group = doc()->fixtureGroup(m_fixtureGroupID);
10 ✔
231
    }
10 ✔
232
    m_stepsCount = algorithmStepsCount();
10 ✔
233
}
10 ✔
234

235
QList<quint32> RGBMatrix::components() const
2 ✔
236
{
237
    if (m_group != NULL)
2 ✔
238
        return m_group->fixtureList();
1 ✔
239

240
    return QList<quint32>();
1 ✔
241
}
242

243
void RGBMatrix::slotFixtureGroupRemoved(quint32 id)
3 ✔
244
{
245
    if (id != m_fixtureGroupID)
3 ✔
246
        return;
2 ✔
247

248
    // the group is deleted right after this signal: drop the cached
249
    // pointer, so that a running matrix stops instead of using it
250
    QMutexLocker algorithmLocker(&m_algorithmMutex);
1 ✔
251
    m_group = NULL;
1 ✔
252
}
1 ✔
253

254
/****************************************************************************
255
 * Algorithm
256
 ****************************************************************************/
257

258
void RGBMatrix::setAlgorithm(RGBAlgorithm *algo)
16 ✔
259
{
260
    {
261
        QMutexLocker algorithmLocker(&m_algorithmMutex);
16 ✔
262

263
        /** Unregister the attributes of the outgoing algorithm while it is
264
         *  still around, since their names come from its properties */
265
        unregisterScriptPropertyAttributes();
16 ✔
266

267
        delete m_algorithm;
16 ✔
268
        m_algorithm = algo;
16 ✔
269

270
        m_requestEngineCreation = true;
16 ✔
271

272
        /** If there's been a change of Script algorithm "on the fly",
273
         *  then re-apply the properties currently set in this RGBMatrix */
274
        if (m_algorithm != NULL && m_algorithm->type() == RGBAlgorithm::Script)
16 ✔
275
        {
276
            RGBScript *script = static_cast<RGBScript*> (m_algorithm);
15 ✔
277
            QMapIterator<QString, QString> it(m_properties);
15 ✔
278
            while (it.hasNext())
15 ✔
279
            {
280
                it.next();
×
281
                if (script->setProperty(it.key(), it.value()) == false)
×
282
                {
283
                    /** If the new algorithm doesn't expose a property,
284
                     *  then remove it from the cached list, otherwise
285
                     *  it would be carried around forever (and saved on XML) */
286
                    m_properties.take(it.key());
×
287
                }
288
            }
289

290
            QVector<uint> colors = script->rgbMapGetColors();
15 ✔
291
            for (int i = 0; i < colors.count(); i++)
15 ✔
292
                m_rgbColors.replace(i, QColor::fromRgb(colors.at(i)));
×
293
        }
15 ✔
294
    }
16 ✔
295
    m_stepsCount = algorithmStepsCount();
16 ✔
296

297
    /** Expose the properties of the new algorithm as Function attributes */
298
    registerScriptPropertyAttributes();
16 ✔
299

300
    if (m_applyingStyleAttributes == false)
16 ✔
301
        Function::adjustAttribute(algorithmIndex(), PatternAttr);
16 ✔
302

303
    emit changed(id());
16 ✔
304
}
16 ✔
305

306
RGBAlgorithm *RGBMatrix::algorithm() const
18 ✔
307
{
308
    return m_algorithm;
18 ✔
309
}
310

311
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
312
QMutex& RGBMatrix::algorithmMutex()
313
{
314
    return m_algorithmMutex;
315
}
316
#else
317
QRecursiveMutex& RGBMatrix::algorithmMutex()
×
318
{
319
    return m_algorithmMutex;
×
320
}
321
#endif
322

323

324
int RGBMatrix::stepsCount() const
2 ✔
325
{
326
    return m_stepsCount;
2 ✔
327
}
328

329
int RGBMatrix::algorithmStepsCount()
27 ✔
330
{
331
    QMutexLocker algorithmLocker(&m_algorithmMutex);
27 ✔
332

333
    if (m_algorithm == NULL)
27 ✔
334
        return 0;
×
335

336
    FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
27 ✔
337
    if (grp != NULL)
27 ✔
338
        return m_algorithm->rgbMapStepCount(grp->size());
9 ✔
339

340
    return 0;
18 ✔
341
}
27 ✔
342

343
void RGBMatrix::previewMap(int step, RGBMatrixStep *handler)
7 ✔
344
{
345
    QMutexLocker algorithmLocker(&m_algorithmMutex);
7 ✔
346
    if (m_algorithm == NULL || handler == NULL)
7 ✔
347
        return;
×
348

349
    if (m_group == NULL)
7 ✔
350
        m_group = doc()->fixtureGroup(fixtureGroup());
1 ✔
351

352
    if (m_group != NULL)
7 ✔
353
    {
354
        setMapColors(m_algorithm);
6 ✔
355
        m_algorithm->rgbMap(m_group->size(), handler->stepColor().rgb(), step, handler->m_map);
6 ✔
356
    }
357
}
7 ✔
358

359
/****************************************************************************
360
 * Color
361
 ****************************************************************************/
362

363
void RGBMatrix::setColor(int i, QColor c)
27 ✔
364
{
365
    if (i < 0)
27 ✔
366
        return;
×
367

368
    if (i >= m_rgbColors.count())
27 ✔
369
        m_rgbColors.resize(i + 1);
×
370

371
    m_rgbColors.replace(i, c);
27 ✔
372
    {
373
        QMutexLocker algorithmLocker(&m_algorithmMutex);
27 ✔
374
        if (m_algorithm != NULL)
27 ✔
375
        {
376
            m_algorithm->setColors(m_rgbColors);
16 ✔
377
            updateColorDelta();
16 ✔
378
        }
379
    }
27 ✔
380
    setMapColors(m_algorithm);
27 ✔
381

382
    if (m_applyingStyleAttributes == false && i >= 0 && i < ColorAttributeCount)
27 ✔
383
        Function::adjustAttribute(c.isValid() ? int(c.rgb() & RGBMatrixColorMask) : -1, Color1Attr + i);
27 ✔
384

385
    emit changed(id());
27 ✔
386
}
387

388
QColor RGBMatrix::getColor(int i) const
76 ✔
389
{
390
    if (i < 0 || i >= m_rgbColors.count())
76 ✔
391
        return QColor();
×
392

393
    return m_rgbColors.at(i);
76 ✔
394
}
395

396
QVector<QColor> RGBMatrix::getColors() const
1 ✔
397
{
398
    return m_rgbColors;
1 ✔
399
}
400

401
void RGBMatrix::updateColorDelta()
16 ✔
402
{
403
    if (m_rgbColors.count() > 1)
16 ✔
404
        m_stepHandler->calculateColorDelta(m_rgbColors[0], m_rgbColors[1], m_algorithm);
16 ✔
405
}
16 ✔
406

407
void RGBMatrix::setMapColors(RGBAlgorithm *algorithm)
33 ✔
408
{
409
    QMutexLocker algorithmLocker(&m_algorithmMutex);
33 ✔
410
    if (algorithm == NULL)
33 ✔
411
        return;
11 ✔
412

413
    if (algorithm->apiVersion() < 3)
22 ✔
414
        return;
22 ✔
415

416
    if (m_group == NULL)
×
417
        m_group = doc()->fixtureGroup(fixtureGroup());
×
418

419
    QVector<unsigned int> rawColors;
×
420
    const int acceptColors = algorithm->acceptColors();
×
421
    rawColors.reserve(acceptColors);
×
422
    for (int i = 0; i < acceptColors; i++)
×
423
    {
424
        if (m_rgbColors.count() > i)
×
425
        {
426
            QColor col = m_rgbColors.at(i);
×
427
            rawColors.append(col.isValid() ? col.rgb() : 0);
×
428
        }
429
        else
430
        {
431
            rawColors.append(0);
×
432
        }
433
    }
434

435
    algorithm->rgbMapSetColors(rawColors);
×
436
}
33 ✔
437

438
/************************************************************************
439
 * Properties
440
 ************************************************************************/
441

442
void RGBMatrix::setProperty(QString propName, QString value)
1 ✔
443
{
444
    QMutexLocker algoLocker(&m_algorithmMutex);
1 ✔
445

446
    // Remember the old step count before changing it (used to scale the step index)
447
    int oldStepsCount = m_stepsCount;
1 ✔
448

449
    m_properties[propName] = value;
1 ✔
450
    if (m_algorithm != NULL && m_algorithm->type() == RGBAlgorithm::Script)
1 ✔
451
    {
452
        RGBScript *script = static_cast<RGBScript*> (m_algorithm);
1 ✔
453
        script->setProperty(propName, value);
1 ✔
454

455
        QVector<uint> colors = script->rgbMapGetColors();
1 ✔
456
        for (int i = 0; i < colors.count(); i++)
1 ✔
457
            setColor(i, QColor::fromRgb(colors.at(i)));
×
458
    }
1 ✔
459
    m_stepsCount = algorithmStepsCount();
1 ✔
460

461
    // Scale currentStepIndex to the new step count to preserve the phase.
462
    // We use m_continuousPhase (a continuous 0.0-1.0 value) instead of the discrete
463
    // stepIndex to avoid accumulating rounding errors over multiple speed changes.
464
    // Analogous to EFXFixture::durationChanged() using m_currentAngle.
465
    if (m_stepHandler != NULL && m_stepsCount > 0 && oldStepsCount != m_stepsCount)
1 ✔
466
    {
467
        // If m_continuousPhase is not up to date yet (first change),
468
        // compute it from the current stepIndex
469
        if (oldStepsCount > 0)
×
470
        {
471
            int currentStepIndex = m_stepHandler->currentStepIndex();
×
472
            m_continuousPhase = double(currentStepIndex) / double(oldStepsCount);
×
473
        }
474

475
        // Rescale the phase to the new step count (preserve the continuous phase,
476
        // not the discrete index)
477
        int newStepIndex = int(round(m_continuousPhase * double(m_stepsCount)));
×
478
        // Make sure it stays within range
479
        if (newStepIndex >= m_stepsCount)
×
480
            newStepIndex = m_stepsCount - 1;
×
481
        if (newStepIndex < 0)
×
482
            newStepIndex = 0;
×
483
        m_stepHandler->setCurrentStepIndex(newStepIndex);
×
484
    }
485
}
1 ✔
486

487
QString RGBMatrix::property(QString propName)
18 ✔
488
{
489
    QMutexLocker algoLocker(&m_algorithmMutex);
18 ✔
490

491
    /** If the property is cached, then return it right away */
492
    QMap<QString, QString>::iterator it = m_properties.find(propName);
18 ✔
493
    if (it != m_properties.end())
18 ✔
494
        return it.value();
1 ✔
495

496
    /** Otherwise, let's retrieve it from the Script */
497
    if (m_algorithm != NULL && m_algorithm->type() == RGBAlgorithm::Script)
17 ✔
498
    {
499
        RGBScript *script = static_cast<RGBScript*> (m_algorithm);
17 ✔
500
        return script->property(propName);
17 ✔
501
    }
502

503
    return QString();
×
504
}
18 ✔
505

506
/****************************************************************************
507
 * Load & Save
508
 ****************************************************************************/
509

510
bool RGBMatrix::loadXML(QXmlStreamReader &root)
3 ✔
511
{
512
    if (root.name() != KXMLQLCFunction)
3 ✔
513
    {
514
        qWarning() << Q_FUNC_INFO << "Function node not found";
1 ✔
515
        return false;
1 ✔
516
    }
517

518
    if (root.attributes().value(KXMLQLCFunctionType).toString() != typeToString(Function::RGBMatrixType))
4 ✔
519
    {
520
        qWarning() << Q_FUNC_INFO << "Function is not an RGB matrix";
1 ✔
521
        return false;
1 ✔
522
    }
523

524
    /* Load matrix contents */
525
    while (root.readNextStartElement())
12 ✔
526
    {
527
        if (root.name() == KXMLQLCFunctionSpeed)
11 ✔
528
        {
529
            loadXMLSpeed(root);
1 ✔
530
        }
531
        else if (root.name() == KXMLQLCFunctionTempoType)
10 ✔
532
        {
533
            loadXMLTempoType(root);
×
534
        }
535
        else if (root.name() == KXMLQLCRGBAlgorithm)
10 ✔
536
        {
537
            setAlgorithm(RGBAlgorithm::loader(doc(), root));
1 ✔
538
        }
539
        else if (root.name() == KXMLQLCRGBMatrixFixtureGroup)
9 ✔
540
        {
541
            setFixtureGroup(root.readElementText().toUInt());
1 ✔
542
        }
543
        else if (root.name() == KXMLQLCFunctionDirection)
8 ✔
544
        {
545
            loadXMLDirection(root);
1 ✔
546
        }
547
        else if (root.name() == KXMLQLCFunctionRunOrder)
7 ✔
548
        {
549
            loadXMLRunOrder(root);
1 ✔
550
        }
551
        // Legacy support
552
        else if (root.name() == KXMLQLCRGBMatrixStartColor)
6 ✔
553
        {
554
            setColor(0, QColor::fromRgb(QRgb(root.readElementText().toUInt())));
×
555
        }
556
        else if (root.name() == KXMLQLCRGBMatrixEndColor)
6 ✔
557
        {
558
            setColor(1, QColor::fromRgb(QRgb(root.readElementText().toUInt())));
×
559
        }
560
        else if (root.name() == KXMLQLCRGBMatrixColor)
6 ✔
561
        {
562
            int colorIdx = root.attributes().value(KXMLQLCRGBMatrixColorIndex).toInt();
10 ✔
563
            setColor(colorIdx, QColor::fromRgb(QRgb(root.readElementText().toUInt())));
5 ✔
564
        }
565
        else if (root.name() == KXMLQLCRGBMatrixControlMode)
1 ✔
566
        {
567
            setControlMode(stringToControlMode(root.readElementText()));
1 ✔
568
        }
569
        else if (root.name() == KXMLQLCRGBMatrixProperty)
×
570
        {
571
            QString name = root.attributes().value(KXMLQLCRGBMatrixPropertyName).toString();
×
572
            QString value = root.attributes().value(KXMLQLCRGBMatrixPropertyValue).toString();
×
573
            setProperty(name, value);
×
574
            root.skipCurrentElement();
×
575
        }
×
576
        else if (root.name() == KXMLQLCRGBMatrixDimmerControl)
×
577
        {
578
            setDimmerControl(root.readElementText().toInt());
×
579
        }
580
        else
581
        {
582
            qWarning() << Q_FUNC_INFO << "Unknown RGB matrix tag:" << root.name();
×
583
            root.skipCurrentElement();
×
584
        }
585
    }
586

587
    return true;
1 ✔
588
}
589

590
bool RGBMatrix::saveXML(QXmlStreamWriter *doc) const
1 ✔
591
{
592
    Q_ASSERT(doc != NULL);
1 ✔
593

594
    /* Function tag */
595
    doc->writeStartElement(KXMLQLCFunction);
2 ✔
596

597
    /* Common attributes */
598
    saveXMLCommon(doc);
1 ✔
599

600
    /* Tempo type */
601
    saveXMLTempoType(doc);
1 ✔
602

603
    /* Speeds */
604
    saveXMLSpeed(doc);
1 ✔
605

606
    /* Direction */
607
    saveXMLDirection(doc);
1 ✔
608

609
    /* Run order */
610
    saveXMLRunOrder(doc);
1 ✔
611

612
    /* Algorithm */
613
    if (m_algorithm != NULL)
1 ✔
614
        m_algorithm->saveXML(doc);
1 ✔
615

616
    /* LEGACY - Dimmer Control */
617
    if (dimmerControl())
1 ✔
618
        doc->writeTextElement(KXMLQLCRGBMatrixDimmerControl, QString::number(dimmerControl()));
×
619

620
    /* Colors */
621
    for (int i = 0; i < m_rgbColors.count(); i++)
6 ✔
622
    {
623
        if (m_rgbColors.at(i).isValid() == false)
5 ✔
624
            continue;
×
625

626
        doc->writeStartElement(KXMLQLCRGBMatrixColor);
10 ✔
627
        doc->writeAttribute(KXMLQLCRGBMatrixColorIndex, QString::number(i));
10 ✔
628
        doc->writeCharacters(QString::number(m_rgbColors.at(i).rgb()));
5 ✔
629
        doc->writeEndElement();
5 ✔
630
    }
631

632
    /* Control Mode */
633
    doc->writeTextElement(KXMLQLCRGBMatrixControlMode, RGBMatrix::controlModeToString(m_controlMode));
2 ✔
634

635
    /* Fixture Group */
636
    doc->writeTextElement(KXMLQLCRGBMatrixFixtureGroup, QString::number(fixtureGroup()));
2 ✔
637

638
    /* Properties */
639
    QMapIterator<QString, QString> it(m_properties);
1 ✔
640
    while (it.hasNext())
1 ✔
641
    {
642
        it.next();
×
643
        doc->writeStartElement(KXMLQLCRGBMatrixProperty);
×
644
        doc->writeAttribute(KXMLQLCRGBMatrixPropertyName, it.key());
×
645
        doc->writeAttribute(KXMLQLCRGBMatrixPropertyValue, it.value());
×
646
        doc->writeEndElement();
×
647
    }
648

649
    /* End the <Function> tag */
650
    doc->writeEndElement();
1 ✔
651

652
    return true;
1 ✔
653
}
1 ✔
654

655
/****************************************************************************
656
 * Running
657
 ****************************************************************************/
658

659
void RGBMatrix::tap()
×
660
{
661
    if (stopped() == false)
×
662
    {
663
        FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
×
664
        // Filter out taps that are too close to each other
665
        if (grp != NULL && uint(m_roundTime.elapsed()) >= (duration() / 4))
×
666
        {
667
            roundCheck();
×
668
            resetElapsed();
×
669
        }
670
    }
671
}
×
672

673
void RGBMatrix::checkEngineCreation()
2 ✔
674
{
675
    m_runAlgorithm = m_algorithm;
2 ✔
676
    m_requestEngineCreation = false;
2 ✔
677
}
2 ✔
678

679
void RGBMatrix::preRun(MasterTimer *timer)
1 ✔
680
{
681
    {
682
        QMutexLocker algorithmLocker(&m_algorithmMutex);
1 ✔
683

684
        m_group = doc()->fixtureGroup(m_fixtureGroupID);
1 ✔
685
        if (m_group == NULL)
1 ✔
686
        {
687
            // No fixture group to control
688
            stop(FunctionParent::master());
×
689
            return;
×
690
        }
691

692
        if (m_algorithm != NULL)
1 ✔
693
        {
694
            checkEngineCreation();
1 ✔
695

696
            // Copy direction from parent class direction
697
            m_stepHandler->initializeDirection(direction(), m_rgbColors[0], m_rgbColors[1], m_stepsCount, m_runAlgorithm);
1 ✔
698

699
            // Update continuous phase when starting playback
700
            if (m_stepsCount > 0)
1 ✔
701
                m_continuousPhase = double(m_stepHandler->currentStepIndex()) / double(m_stepsCount);
1 ✔
702

703
            if (m_runAlgorithm->type() == RGBAlgorithm::Script)
1 ✔
704
            {
705
                RGBScript *script = static_cast<RGBScript*> (m_runAlgorithm);
1 ✔
706
                QMapIterator<QString, QString> it(m_properties);
1 ✔
707
                while (it.hasNext())
1 ✔
708
                {
709
                    it.next();
×
710
                    script->setProperty(it.key(), it.value());
×
711
                }
712
            }
1 ✔
713
            else if (m_runAlgorithm->type() == RGBAlgorithm::Image)
×
714
            {
715
                RGBImage *image = static_cast<RGBImage*> (m_runAlgorithm);
×
716
                if (image->animatedSource())
×
717
                    image->rewindAnimation();
×
718
            }
719
        }
720
    }
1 ✔
721

722
    m_roundTime.restart();
1 ✔
723

724
    Function::preRun(timer);
1 ✔
725
}
726

727
void RGBMatrix::write(MasterTimer *timer, QList<Universe *> universes)
3 ✔
728
{
729
    Q_UNUSED(timer);
730

731
    {
732
        QMutexLocker algorithmLocker(&m_algorithmMutex);
3 ✔
733
        if (m_group == NULL)
3 ✔
734
        {
735
            // No fixture group to control
736
            stop(FunctionParent::master());
1 ✔
737
            return;
1 ✔
738
        }
739

740
        // No time to do anything.
741
        if (duration() == 0)
2 ✔
742
            return;
×
743

744
        if (m_algorithm != NULL && m_requestEngineCreation)
2 ✔
745
            checkEngineCreation();
×
746

747
        // Invalid/nonexistent script
748
        if (m_runAlgorithm == NULL || m_runAlgorithm->apiVersion() == 0)
2 ✔
749
            return;
×
750

751
        if (isPaused() == false)
2 ✔
752
        {
753
            // Get a new map every time elapsed is reset to zero
754
            if (elapsed() < MasterTimer::tick())
2 ✔
755
            {
756
                if (tempoType() == Beats)
1 ✔
757
                    m_stepBeatDuration = beatsToTime(duration(), timer->beatTimeDuration());
×
758

759
                //qDebug() << "RGBMatrix step" << m_stepHandler->currentStepIndex() << ", color:" << QString::number(m_stepHandler->stepColor().rgb(), 16);
760
                m_runAlgorithm->rgbMap(m_group->size(), m_stepHandler->stepColor().rgb(),
1 ✔
761
                                       m_stepHandler->currentStepIndex(), m_stepHandler->m_map);
1 ✔
762
                updateMapChannels(m_stepHandler->m_map, m_group, universes);
1 ✔
763
            }
764
        }
765
    }
3 ✔
766

767
    if (isPaused() == false)
2 ✔
768
    {
769
        // Increment the ms elapsed time
770
        incrementElapsed();
2 ✔
771

772
        /* Check if we need to change direction, stop completely or go to next step
773
         * The cases are:
774
         * 1- time tempo type: act normally, on ms elapsed time
775
         * 2- beat tempo type, beat occurred: check if the elapsed beats is a multiple of
776
         *    the step beat duration. If so, proceed to the next step
777
         * 3- beat tempo type, not beat: if the ms elapsed time reached the step beat
778
         *    duration in ms, and the ms time to the next beat is not less than 1/16 of
779
         *    the step beat duration in ms, then proceed to the next step. If the ms time to the
780
         *    next beat is less than 1/16 of the step beat duration in ms, then defer the step
781
         *    change to case #2, to resync the matrix to the next beat
782
         */
783
        if (tempoType() == Time && elapsed() >= duration())
2 ✔
784
        {
785
            roundCheck();
×
786
        }
787
        else if (tempoType() == Beats)
2 ✔
788
        {
789
            if (timer->isBeat())
×
790
            {
791
                incrementElapsedBeats();
×
792
                qDebug() << "Elapsed beats:" << elapsedBeats() << ", time elapsed:" << elapsed() << ", step time:" << m_stepBeatDuration;
×
793
                if (elapsedBeats() % duration() == 0)
×
794
                {
795
                    roundCheck();
×
796
                    resetElapsed();
×
797
                }
798
            }
799
            else if (elapsed() >= m_stepBeatDuration && (uint)timer->timeToNextBeat() > m_stepBeatDuration / 16)
×
800
            {
801
                qDebug() << "Elapsed exceeded";
×
802
                roundCheck();
×
803
            }
804
        }
805
    }
806
}
807

808
void RGBMatrix::postRun(MasterTimer *timer, QList<Universe *> universes)
1 ✔
809
{
810
    uint fadeout = overrideFadeOutSpeed() == defaultSpeed() ? fadeOutSpeed() : overrideFadeOutSpeed();
1 ✔
811

812
    /* If no fade out is needed, dismiss all the requested faders.
813
     * Otherwise, set all the faders to fade out and let Universe dismiss them
814
     * when done */
815
    if (fadeout == 0)
1 ✔
816
    {
817
        dismissAllFaders();
1 ✔
818
    }
819
    else
820
    {
821
        if (tempoType() == Beats)
×
822
            fadeout = beatsToTime(fadeout, timer->beatTimeDuration());
×
823

824
        foreach (QSharedPointer<GenericFader> fader, m_fadersMap)
×
825
        {
826
            if (!fader.isNull())
×
827
                fader->setFadeOut(true, fadeout);
×
828
        }
×
829
    }
830

831
    m_fadersMap.clear();
1 ✔
832

833
    {
834
        QMutexLocker algorithmLocker(&m_algorithmMutex);
1 ✔
835
        checkEngineCreation();
1 ✔
836
        if (m_runAlgorithm != NULL)
1 ✔
837
            m_runAlgorithm->postRun();
1 ✔
838
    }
1 ✔
839

840
    Function::postRun(timer, universes);
1 ✔
841
}
1 ✔
842

843
void RGBMatrix::roundCheck()
×
844
{
845
    QMutexLocker algorithmLocker(&m_algorithmMutex);
×
846
    if (m_algorithm == NULL)
×
847
        return;
×
848

849
    if (m_stepHandler->checkNextStep(runOrder(), m_rgbColors[0], m_rgbColors[1], m_stepsCount) == false)
×
850
        stop(FunctionParent::master());
×
851

852
    // Update continuous phase based on current step index (prevents cumulative rounding errors)
853
    // This is analogous to how EFX uses m_currentAngle for phase scaling
854
    if (m_stepsCount > 0)
×
855
        m_continuousPhase = double(m_stepHandler->currentStepIndex()) / double(m_stepsCount);
×
856

857
    m_roundTime.restart();
×
858

859
    if (tempoType() == Beats)
×
860
        roundElapsed(m_stepBeatDuration);
×
861
    else
862
        roundElapsed(duration());
×
863
}
×
864

865
QSharedPointer<GenericFader> RGBMatrix::getFader(Universe *universe)
3 ✔
866
{
867
    // get the universe Fader first. If doesn't exist, create it
868
    if (universe == NULL)
3 ✔
869
        return QSharedPointer<GenericFader>();
×
870

871
    QSharedPointer<GenericFader> fader = m_fadersMap.value(universe->id(), QSharedPointer<GenericFader>());
3 ✔
872
    if (fader.isNull())
3 ✔
873
    {
874
        fader = universe->requestFader();
1 ✔
875
        fader->adjustIntensity(getAttributeValue(Intensity));
1 ✔
876
        fader->setBlendMode(blendMode());
1 ✔
877
        fader->setName(name());
1 ✔
878
        fader->setParentFunctionID(id());
1 ✔
879
        m_fadersMap[universe->id()] = fader;
1 ✔
880
    }
881

882
    return fader;
3 ✔
883
}
3 ✔
884

885
void RGBMatrix::updateFaderValues(FadeChannel &fc, uchar value, uint fadeTime)
3 ✔
886
{
887
    // If the channel is already fading towards the requested value, let it
888
    // continue undisturbed. Restarting start/elapsed on every step (even when
889
    // the target doesn't change) would keep resetting the fade before it can
890
    // ever reach its target, which is especially noticeable when the fade out
891
    // time is longer than the step duration
892
    if (fc.target() == value)
3 ✔
893
        return;
2 ✔
894

895
    fc.setStart(fc.current());
1 ✔
896
    fc.setTarget(value);
1 ✔
897
    fc.setElapsed(0);
1 ✔
898
    fc.setReady(false);
1 ✔
899
    // fade in/out depends on target value
900
    if (value == 0)
1 ✔
901
        fc.setFadeTime(fadeOutSpeed());
×
902
    else
903
        fc.setFadeTime(fadeTime);
1 ✔
904
}
905

906
void RGBMatrix::updateMapChannels(const RGBMap& map, const FixtureGroup *grp, QList<Universe *> universes)
1 ✔
907
{
908
    uint fadeTime = (overrideFadeInSpeed() == defaultSpeed()) ? fadeInSpeed() : overrideFadeInSpeed();
1 ✔
909

910
    // Create/modify fade channels for ALL heads in the group
911
    QMapIterator<QLCPoint, GroupHead> it(grp->headsMap());
1 ✔
912
    while (it.hasNext())
2 ✔
913
    {
914
        it.next();
1 ✔
915
        QLCPoint pt = it.key();
1 ✔
916
        GroupHead grpHead = it.value();
1 ✔
917
        Fixture *fxi = doc()->fixture(grpHead.fxi);
1 ✔
918
        if (fxi == NULL)
1 ✔
919
            continue;
×
920

921
        QLCFixtureHead head = fxi->head(grpHead.head);
1 ✔
922

923
        if (pt.y() >= map.count() || pt.x() >= map[pt.y()].count())
1 ✔
924
            continue;
×
925

926
        uint col = map[pt.y()][pt.x()];
1 ✔
927
        QVector<quint32> channelList;
1 ✔
928
        QVector<uchar> valueList;
1 ✔
929

930
        if (m_controlMode == ControlModeRgb)
1 ✔
931
        {
932
            channelList = head.rgbChannels();
1 ✔
933

934
            if (channelList.size() == 3)
1 ✔
935
            {
936
                valueList.append(qRed(col));
1 ✔
937
                valueList.append(qGreen(col));
1 ✔
938
                valueList.append(qBlue(col));
1 ✔
939
            }
940
            else
941
            {
942
                channelList = head.cmyChannels();
×
943

944
                if (channelList.size() == 3)
×
945
                {
946
                    // CMY color mixing
947
                    QColor cmyCol(col);
×
948
                    valueList.append(cmyCol.cyan());
×
949
                    valueList.append(cmyCol.magenta());
×
950
                    valueList.append(cmyCol.yellow());
×
951
                }
952
            }
953
        }
954
        else if (m_controlMode == ControlModeShutter)
×
955
        {
956
            channelList = head.shutterChannels();
×
957

958
            if (channelList.size())
×
959
            {
960
                // make sure only one channel is in the list
961
                channelList.resize(1);
×
962
                valueList.append(rgbToGrey(col));
×
963
            }
964
        }
965
        else if (m_controlMode == ControlModeDimmer || m_dimmerControl)
×
966
        {
967
            // Collect all dimmers that affect current head:
968
            // They are the master dimmer (affects whole fixture)
969
            // and per-head dimmer.
970
            //
971
            // If there are no RGB or CMY channels, the least important* dimmer channel
972
            // is used to create grayscale image.
973
            //
974
            // The rest of the dimmer channels are set to full if dimmer control is
975
            // enabled and target color is > 0 (see
976
            // https://www.qlcplus.org/forum/viewtopic.php?f=29&t=11090)
977
            //
978
            // Note: If there is only one head, and only one dimmer channel,
979
            // make it a master dimmer in fixture definition.
980
            //
981
            // *least important - per head dimmer if present,
982
            // otherwise per fixture dimmer if present
983

984
            quint32 masterDim = fxi->masterIntensityChannel();
×
985
            quint32 headDim = head.channelNumber(QLCChannel::Intensity, QLCChannel::MSB);
×
986

987
            if (masterDim != QLCChannel::invalid())
×
988
            {
989
                channelList.append(masterDim);
×
990
                valueList.append(rgbToGrey(col));
×
991
            }
992

993
            if (headDim != QLCChannel::invalid() && headDim != masterDim)
×
994
            {
995
                channelList.append(headDim);
×
996
                // If a master dimmer is present, it carries the greyscale fade and the
997
                // per-head dimmer is just opened fully (on/off). With no master dimmer
998
                // (e.g. generic single-channel dimmers), the head dimmer must carry the
999
                // greyscale value itself, otherwise it would only ever output 0 or 255.
1000
                if (masterDim != QLCChannel::invalid())
×
1001
                    valueList.append(rgbToGrey(col) == 0 ? 0 : 255);
×
1002
                else
1003
                    valueList.append(rgbToGrey(col));
×
1004
            }
1005
        }
×
1006
        else
1007
        {
1008
            if (m_controlMode == ControlModeWhite)
×
1009
                channelList.append(head.channelNumber(QLCChannel::White, QLCChannel::MSB));
×
1010
            else if (m_controlMode == ControlModeAmber)
×
1011
                channelList.append(head.channelNumber(QLCChannel::Amber, QLCChannel::MSB));
×
1012
            else if (m_controlMode == ControlModeUV)
×
1013
                channelList.append(head.channelNumber(QLCChannel::UV, QLCChannel::MSB));
×
1014

1015
            valueList.append(rgbToGrey(col));
×
1016
        }
1017

1018
        for (int i = 0; i < channelList.count(); i++)
4 ✔
1019
        {
1020
            if (channelList.at(i) == QLCChannel::invalid())
3 ✔
1021
                continue;
×
1022

1023
            // a cross-universe fixture channel may belong to a following universe
1024
            quint32 absAddress = fxi->channelAddress(channelList.at(i));
3 ✔
1025
            if (absAddress == QLCChannel::invalid())
3 ✔
1026
                continue;
×
1027

1028
            int universeIndex = absAddress >> 9;
3 ✔
1029
            if (universeIndex >= universes.count())
3 ✔
1030
                continue;
×
1031

1032
            Universe *universe = universes.at(universeIndex);
3 ✔
1033
            QSharedPointer<GenericFader> fader = getFader(universe);
3 ✔
1034
            if (fader.isNull())
3 ✔
1035
                continue;
×
1036

1037
            const quint32 fixtureID = grpHead.fxi;
3 ✔
1038
            const quint32 channel = channelList.at(i);
3 ✔
1039
            const uchar value = valueList.at(i);
3 ✔
1040
            fader->updateChannel(doc(), universe, fixtureID, channel, [this, value, fadeTime](FadeChannel &fc)
3 ✔
1041
            {
1042
                updateFaderValues(fc, value, fadeTime);
3 ✔
1043
            });
3 ✔
1044
        }
3 ✔
1045
    }
1 ✔
1046
}
1 ✔
1047

1048
uchar RGBMatrix::rgbToGrey(uint col)
×
1049
{
1050
    uchar r = qRed(col);
×
1051
    uchar g = qGreen(col);
×
1052
    uchar b = qBlue(col);
×
1053

1054
    // Special case: if R=G=B (grayscale), return the value directly.
1055
    // This avoids floating-point precision issues.
1056
    if (r == g && g == b)
×
1057
        return r;
×
1058

1059
    // the weights are taken from
1060
    // https://en.wikipedia.org/wiki/YUV#SDTV_with_BT.601
1061
    return uchar(round(0.299 * r + 0.587 * g + 0.114 * b));
×
1062
}
1063

1064
/*********************************************************************
1065
 * Attributes
1066
 *********************************************************************/
1067

1068
int RGBMatrix::adjustAttribute(qreal fraction, int attributeId)
×
1069
{
1070
    int attrIndex = Function::adjustAttribute(fraction, attributeId);
×
1071

1072
    if (attrIndex == Intensity)
×
1073
    {
1074
        foreach (QSharedPointer<GenericFader> fader, m_fadersMap)
×
1075
        {
1076
            if (!fader.isNull())
×
1077
                fader->adjustIntensity(getAttributeValue(Function::Intensity));
×
1078
        }
×
1079
    }
1080
    else if (attrIndex >= Color1Attr && attrIndex <= ColorLastAttr)
×
1081
    {
1082
        applyColorAttribute(attrIndex - Color1Attr, getAttributeValue(attrIndex));
×
1083
    }
1084
    else if (attrIndex == PatternAttr)
×
1085
    {
1086
        applyPatternAttribute(getAttributeValue(PatternAttr));
×
1087
    }
1088
    else if (attrIndex >= ScriptPropertyAttr)
×
1089
    {
1090
        applyScriptPropertyAttribute(attrIndex - ScriptPropertyAttr, getAttributeValue(attrIndex));
×
1091
    }
1092

1093
    return attrIndex;
×
1094
}
1095

1096
void RGBMatrix::applyStyleAttributes()
×
1097
{
1098
    for (int i = 0; i < ColorAttributeCount; ++i)
×
1099
        applyColorAttribute(i, getAttributeValue(Color1Attr + i));
×
1100

1101
    applyPatternAttribute(getAttributeValue(PatternAttr));
×
1102

1103
    for (int i = 0; i < scriptPropertyAttributes().count(); i++)
×
1104
        applyScriptPropertyAttribute(i, getAttributeValue(ScriptPropertyAttr + i));
×
1105
}
×
1106

1107
QList<RGBScriptProperty> RGBMatrix::scriptPropertyAttributes() const
32 ✔
1108
{
1109
    QList<RGBScriptProperty> list;
32 ✔
1110

1111
    if (m_algorithm == NULL || m_algorithm->type() != RGBAlgorithm::Script)
32 ✔
1112
        return list;
12 ✔
1113

1114
    RGBScript *script = static_cast<RGBScript*> (m_algorithm);
20 ✔
1115

1116
    foreach (RGBScriptProperty prop, script->properties())
60 ✔
1117
    {
1118
        /** Only the properties that a slider can drive are exposed:
1119
         *  a list needs at least two values to pick from, a range needs
1120
         *  a meaningful span and a string cannot be controlled at all */
1121
        if (prop.m_type == RGBScriptProperty::List)
20 ✔
1122
        {
1123
            if (prop.m_listValues.count() > 1)
20 ✔
1124
                list.append(prop);
20 ✔
1125
        }
1126
        else if (prop.m_type == RGBScriptProperty::Range)
×
1127
        {
1128
            if (prop.m_rangeMaxValue > prop.m_rangeMinValue)
×
1129
                list.append(prop);
×
1130
        }
1131
        else if (prop.m_type == RGBScriptProperty::Float)
×
1132
        {
1133
            list.append(prop);
×
1134
        }
1135
    }
40 ✔
1136

1137
    return list;
20 ✔
1138
}
×
1139

1140
QString RGBMatrix::scriptPropertyAttributeName(const RGBScriptProperty &prop)
20 ✔
1141
{
1142
    return prop.m_displayName.isEmpty() ? prop.m_name : prop.m_displayName;
20 ✔
1143
}
1144

1145
void RGBMatrix::unregisterScriptPropertyAttributes()
16 ✔
1146
{
1147
    foreach (RGBScriptProperty prop, scriptPropertyAttributes())
37 ✔
1148
        unregisterAttribute(scriptPropertyAttributeName(prop));
21 ✔
1149
}
16 ✔
1150

1151
void RGBMatrix::registerScriptPropertyAttributes()
16 ✔
1152
{
1153
    QMutexLocker algorithmLocker(&m_algorithmMutex);
16 ✔
1154

1155
    foreach (RGBScriptProperty prop, scriptPropertyAttributes())
47 ✔
1156
    {
1157
        qreal min = 0.0;
15 ✔
1158
        qreal max = 1.0;
15 ✔
1159

1160
        if (prop.m_type == RGBScriptProperty::List)
15 ✔
1161
            max = prop.m_listValues.count() - 1;
15 ✔
1162
        else if (prop.m_type == RGBScriptProperty::Range)
×
1163
        {
1164
            min = prop.m_rangeMinValue;
×
1165
            max = prop.m_rangeMaxValue;
×
1166
        }
1167
        /** Scripts don't declare a range for float properties,
1168
         *  so 0.0 - 1.0 is assumed */
1169

1170
        /** A list property holds a string, so the attribute value is the
1171
         *  index of the current value in the values list */
1172
        QString current = property(prop.m_name);
15 ✔
1173
        qreal value = prop.m_type == RGBScriptProperty::List ?
30 ✔
1174
                      qMax(0, prop.m_listValues.indexOf(current)) : current.toDouble();
15 ✔
1175

1176
        registerAttribute(scriptPropertyAttributeName(prop), LastWins | Single,
15 ✔
1177
                          min, max, qBound(min, value, max));
15 ✔
1178
    }
31 ✔
1179
}
16 ✔
1180

1181
void RGBMatrix::applyScriptPropertyAttribute(int attrIndex, qreal value)
×
1182
{
1183
    QList<RGBScriptProperty> props = scriptPropertyAttributes();
×
1184
    if (attrIndex < 0 || attrIndex >= props.count())
×
1185
        return;
×
1186

1187
    const RGBScriptProperty prop = props.at(attrIndex);
×
1188
    QString strValue;
×
1189

1190
    switch (prop.m_type)
×
1191
    {
1192
        case RGBScriptProperty::List:
×
1193
            strValue = prop.m_listValues.at(qBound(0, int(qRound(value)), prop.m_listValues.count() - 1));
×
1194
        break;
×
1195
        case RGBScriptProperty::Float:
×
1196
            strValue = QString::number(value);
×
1197
        break;
×
1198
        default:
×
1199
            strValue = QString::number(qRound(value));
×
1200
        break;
×
1201
    }
1202

1203
    if (property(prop.m_name) != strValue)
×
1204
        setProperty(prop.m_name, strValue);
×
1205
}
×
1206

1207
void RGBMatrix::applyColorAttribute(int colorIndex, qreal packedColor)
×
1208
{
1209
    if (colorIndex < 0 || colorIndex >= ColorAttributeCount)
×
1210
        return;
×
1211

1212
    int packed = qRound(packedColor);
×
1213
    QColor targetColor = packed < 0 ? QColor() :
1214
                                      QColor::fromRgb(static_cast<QRgb>((packed & RGBMatrixColorMask) | 0xFF000000));
×
1215
    if (getColor(colorIndex) == targetColor)
×
1216
        return;
×
1217

1218
    bool previous = m_applyingStyleAttributes;
×
1219
    m_applyingStyleAttributes = true;
×
1220
    setColor(colorIndex, targetColor);
×
1221
    m_applyingStyleAttributes = previous;
×
1222
}
1223

1224
void RGBMatrix::applyPatternAttribute(qreal patternIndex)
×
1225
{
1226
    if (doc() == NULL)
×
1227
        return;
×
1228

1229
    QStringList algoList = RGBAlgorithm::algorithms(doc());
×
1230
    if (algoList.isEmpty())
×
1231
        return;
×
1232

1233
    int idx = qRound(patternIndex);
×
1234
    if (idx < 0)
×
1235
        idx = 0;
×
1236
    else if (idx >= algoList.count())
×
1237
        idx = algoList.count() - 1;
×
1238

1239
    RGBAlgorithm *algo = RGBAlgorithm::algorithm(doc(), algoList.at(idx));
×
1240
    if (algo == NULL)
×
1241
        return;
×
1242

1243
    if (m_algorithm != NULL && m_algorithm->name() == algo->name())
×
1244
    {
1245
        delete algo;
×
1246
        return;
×
1247
    }
1248

1249
    algo->setColors(getColors());
×
1250

1251
    bool previous = m_applyingStyleAttributes;
×
1252
    m_applyingStyleAttributes = true;
×
1253
    setAlgorithm(algo);
×
1254
    m_applyingStyleAttributes = previous;
×
1255
}
×
1256

1257
/*************************************************************************
1258
 * Blend mode
1259
 *************************************************************************/
1260

1261
void RGBMatrix::setBlendMode(Universe::BlendMode mode)
×
1262
{
1263
    if (mode == blendMode())
×
1264
        return;
×
1265

1266
    foreach (QSharedPointer<GenericFader> fader, m_fadersMap)
×
1267
    {
1268
        if (!fader.isNull())
×
1269
            fader->setBlendMode(mode);
×
1270
    }
×
1271

1272
    Function::setBlendMode(mode);
×
1273
    emit changed(id());
×
1274
}
1275

1276
/*************************************************************************
1277
 * Control Mode
1278
 *************************************************************************/
1279

1280
RGBMatrix::ControlMode RGBMatrix::controlMode() const
2 ✔
1281
{
1282
    return m_controlMode;
2 ✔
1283
}
1284

1285
void RGBMatrix::setControlMode(RGBMatrix::ControlMode mode)
3 ✔
1286
{
1287
    m_controlMode = mode;
3 ✔
1288
    emit changed(id());
3 ✔
1289
}
3 ✔
1290

1291
RGBMatrix::ControlMode RGBMatrix::stringToControlMode(QString mode)
1 ✔
1292
{
1293
    if (mode == KXMLQLCRGBMatrixControlModeRgb)
1 ✔
1294
        return ControlModeRgb;
1 ✔
1295
    else if (mode == KXMLQLCRGBMatrixControlModeAmber)
×
1296
        return ControlModeAmber;
×
1297
    else if (mode == KXMLQLCRGBMatrixControlModeWhite)
×
1298
        return ControlModeWhite;
×
1299
    else if (mode == KXMLQLCRGBMatrixControlModeUV)
×
1300
        return ControlModeUV;
×
1301
    else if (mode == KXMLQLCRGBMatrixControlModeDimmer)
×
1302
        return ControlModeDimmer;
×
1303
    else if (mode == KXMLQLCRGBMatrixControlModeShutter)
×
1304
        return ControlModeShutter;
×
1305

1306
    return ControlModeRgb;
×
1307
}
1308

1309
QString RGBMatrix::controlModeToString(RGBMatrix::ControlMode mode)
1 ✔
1310
{
1311
    switch(mode)
1 ✔
1312
    {
1313
        default:
1 ✔
1314
        case ControlModeRgb:
1315
            return QString(KXMLQLCRGBMatrixControlModeRgb);
1 ✔
1316
        break;
1317
        case ControlModeAmber:
×
1318
            return QString(KXMLQLCRGBMatrixControlModeAmber);
×
1319
        break;
1320
        case ControlModeWhite:
×
1321
            return QString(KXMLQLCRGBMatrixControlModeWhite);
×
1322
        break;
1323
        case ControlModeUV:
×
1324
            return QString(KXMLQLCRGBMatrixControlModeUV);
×
1325
        break;
1326
        case ControlModeDimmer:
×
1327
            return QString(KXMLQLCRGBMatrixControlModeDimmer);
×
1328
        break;
1329
        case ControlModeShutter:
×
1330
            return QString(KXMLQLCRGBMatrixControlModeShutter);
×
1331
        break;
1332
    }
1333
}
1334

1335

1336
/*************************************************************************
1337
 *************************************************************************
1338
 *                          RGBMatrixStep class
1339
 *************************************************************************
1340
 *************************************************************************/
1341

1342
RGBMatrixStep::RGBMatrixStep()
12 ✔
1343
    : m_direction(Function::Forward)
12 ✔
1344
    , m_currentStepIndex(0)
12 ✔
1345
    , m_stepColor(QColor())
12 ✔
1346
    , m_crDelta(0)
12 ✔
1347
    , m_cgDelta(0)
12 ✔
1348
    , m_cbDelta(0)
12 ✔
1349
{
1350

1351
}
12 ✔
1352

1353
void RGBMatrixStep::setCurrentStepIndex(int index)
1 ✔
1354
{
1355
    m_currentStepIndex = index;
1 ✔
1356
}
1 ✔
1357

1358
int RGBMatrixStep::currentStepIndex() const
3 ✔
1359
{
1360
    return m_currentStepIndex;
3 ✔
1361
}
1362

1363
void RGBMatrixStep::calculateColorDelta(const QColor& startColor, const QColor& endColor, const RGBAlgorithm *algorithm)
17 ✔
1364
{
1365
    m_crDelta = 0;
17 ✔
1366
    m_cgDelta = 0;
17 ✔
1367
    m_cbDelta = 0;
17 ✔
1368

1369
    if (endColor.isValid() && algorithm != NULL && algorithm->acceptColors() > 1)
17 ✔
1370
    {
1371
        m_crDelta = endColor.red() - startColor.red();
10 ✔
1372
        m_cgDelta = endColor.green() - startColor.green();
10 ✔
1373
        m_cbDelta = endColor.blue() - startColor.blue();
10 ✔
1374

1375
        //qDebug() << "Color deltas:" << m_crDelta << m_cgDelta << m_cbDelta;
1376
    }
1377
}
17 ✔
1378

1379
void RGBMatrixStep::setStepColor(QColor color)
1 ✔
1380
{
1381
    m_stepColor = color;
1 ✔
1382
}
1 ✔
1383

1384
QColor RGBMatrixStep::stepColor() const
7 ✔
1385
{
1386
    return m_stepColor;
7 ✔
1387
}
1388

1389
void RGBMatrixStep::updateStepColor(int stepIndex, QColor startColor, int stepsCount)
×
1390
{
1391
    if (stepsCount <= 0)
×
1392
        return;
×
1393

1394
    if (stepsCount == 1)
×
1395
    {
1396
        m_stepColor = startColor;
×
1397
    }
1398
    else
1399
    {
1400
        m_stepColor.setRed(startColor.red() + (m_crDelta * stepIndex / (stepsCount - 1)));
×
1401
        m_stepColor.setGreen(startColor.green() + (m_cgDelta * stepIndex / (stepsCount - 1)));
×
1402
        m_stepColor.setBlue(startColor.blue() + (m_cbDelta * stepIndex / (stepsCount - 1)));
×
1403
    }
1404

1405
    //qDebug() << "RGBMatrix step" << stepIndex << ", color:" << QString::number(m_stepColor.rgb(), 16);
1406
}
1407

1408
void RGBMatrixStep::initializeDirection(Function::Direction direction, const QColor& startColor, const QColor& endColor, int stepsCount, const RGBAlgorithm *algorithm)
1 ✔
1409
{
1410
    m_direction = direction;
1 ✔
1411

1412
    if (m_direction == Function::Forward)
1 ✔
1413
    {
1414
        setCurrentStepIndex(0);
1 ✔
1415
        setStepColor(startColor);
1 ✔
1416
    }
1417
    else
1418
    {
1419
        setCurrentStepIndex(stepsCount - 1);
×
1420

1421
        if (endColor.isValid())
×
1422
            setStepColor(endColor);
×
1423
        else
1424
            setStepColor(startColor);
×
1425
    }
1426

1427
    calculateColorDelta(startColor, endColor, algorithm);
1 ✔
1428
}
1 ✔
1429

1430
bool RGBMatrixStep::checkNextStep(Function::RunOrder order,
×
1431
                                  QColor startColor, QColor endColor, int stepsNumber)
1432
{
1433
    if (order == Function::PingPong)
×
1434
    {
1435
        if (m_direction == Function::Forward && (m_currentStepIndex + 1) == stepsNumber)
×
1436
        {
1437
            m_direction = Function::Backward;
×
1438
            m_currentStepIndex = stepsNumber - 2;
×
1439
            if (endColor.isValid())
×
1440
                m_stepColor = endColor;
×
1441

1442
            updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1443
        }
1444
        else if (m_direction == Function::Backward && (m_currentStepIndex - 1) < 0)
×
1445
        {
1446
            m_direction = Function::Forward;
×
1447
            m_currentStepIndex = 1;
×
1448
            m_stepColor = startColor;
×
1449
            updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1450
        }
1451
        else
1452
        {
1453
            if (m_direction == Function::Forward)
×
1454
                m_currentStepIndex++;
×
1455
            else
1456
                m_currentStepIndex--;
×
1457
            updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1458
        }
1459
    }
1460
    else if (order == Function::SingleShot)
×
1461
    {
1462
        if (m_direction == Function::Forward)
×
1463
        {
1464
            if (m_currentStepIndex >= stepsNumber - 1)
×
1465
                return false;
×
1466
            else
1467
            {
1468
                m_currentStepIndex++;
×
1469
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1470
            }
1471
        }
1472
        else
1473
        {
1474
            if (m_currentStepIndex <= 0)
×
1475
                return false;
×
1476
            else
1477
            {
1478
                m_currentStepIndex--;
×
1479
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1480
            }
1481
        }
1482
    }
1483
    else
1484
    {
1485
        if (m_direction == Function::Forward)
×
1486
        {
1487
            if (m_currentStepIndex >= stepsNumber - 1)
×
1488
            {
1489
                m_currentStepIndex = 0;
×
1490
                m_stepColor = startColor;
×
1491
            }
1492
            else
1493
            {
1494
                m_currentStepIndex++;
×
1495
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1496
            }
1497
        }
1498
        else
1499
        {
1500
            if (m_currentStepIndex <= 0)
×
1501
            {
1502
                m_currentStepIndex = stepsNumber - 1;
×
1503
                if (endColor.isValid())
×
1504
                    m_stepColor = endColor;
×
1505
            }
1506
            else
1507
            {
1508
                m_currentStepIndex--;
×
1509
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1510
            }
1511
        }
1512
    }
1513

1514
    return true;
×
1515
}
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