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

mcallegari / qlcplus / 40027806002

23 Oct 2026 09:34PM UTC coverage: 36.076% (+0.2%) from 35.891%
40027806002

push

github

mcallegari
engine: add test units for universe split

18801 of 52115 relevant lines covered (36.08%)

42105.94 hits per line

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

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

85
    m_rgbColors.fill(QColor(), RGBAlgorithmColorDisplayCount);
10 ✔
86
    setColor(0, Qt::red);
10 ✔
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)
60 ✔
91
    {
92
        registerAttribute(tr("Color %1").arg(i + 1), LastWins | Single, -1.0, 16777215.0,
60 ✔
93
                          getColor(i).isValid() ? int(getColor(i).rgb() & RGBMatrixColorMask) : -1);
110 ✔
94
    }
95

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

99
    setAlgorithm(RGBAlgorithm::algorithm(doc, "Stripes"));
10 ✔
100
}
10 ✔
101

102
RGBMatrix::~RGBMatrix()
12 ✔
103
{
104
    //if (m_runAlgorithm != NULL)
105
    //    delete m_runAlgorithm;
106
    delete m_algorithm;
10 ✔
107
    delete m_stepHandler;
10 ✔
108
}
12 ✔
109

110
QIcon RGBMatrix::getIcon() const
×
111
{
112
    return QIcon(":/rgbmatrix.png");
×
113
}
114

115
int RGBMatrix::algorithmIndex() const
15 ✔
116
{
117
    if (m_algorithm == NULL || doc() == NULL)
15 ✔
118
        return 0;
×
119

120
    QStringList algoList = RGBAlgorithm::algorithms(doc());
15 ✔
121
    int idx = algoList.indexOf(m_algorithm->name());
15 ✔
122
    return idx >= 0 ? idx : 0;
15 ✔
123
}
15 ✔
124

125
void RGBMatrix::setTotalDuration(quint32 msec)
2 ✔
126
{
127
    QMutexLocker algorithmLocker(&m_algorithmMutex);
2 ✔
128

129
    if (m_algorithm == NULL)
2 ✔
130
        return;
×
131

132
    FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
2 ✔
133
    if (grp == NULL)
2 ✔
134
        return;
×
135

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

142
quint32 RGBMatrix::totalDuration()
4 ✔
143
{
144
    QMutexLocker algorithmLocker(&m_algorithmMutex);
4 ✔
145

146
    if (m_algorithm == NULL)
4 ✔
147
        return 0;
×
148

149
    FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
4 ✔
150
    if (grp == NULL)
4 ✔
151
        return 0;
1 ✔
152

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

157
void RGBMatrix::setDimmerControl(bool dimmerControl)
1 ✔
158
{
159
    m_dimmerControl = dimmerControl;
1 ✔
160
}
1 ✔
161

162
bool RGBMatrix::dimmerControl() const
2 ✔
163
{
164
    return m_dimmerControl;
2 ✔
165
}
166

167
/****************************************************************************
168
 * Copying
169
 ****************************************************************************/
170

171
Function* RGBMatrix::createCopy(Doc* doc, bool addToDoc)
1 ✔
172
{
173
    Q_ASSERT(doc != NULL);
1 ✔
174

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

187
    return copy;
1 ✔
188
}
189

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

196
    setDimmerControl(mtx->dimmerControl());
1 ✔
197
    setFixtureGroup(mtx->fixtureGroup());
1 ✔
198

199
    m_rgbColors.clear();
1 ✔
200
    foreach (QColor col, mtx->getColors())
7 ✔
201
        m_rgbColors.append(col);
6 ✔
202

203
    if (mtx->algorithm() != NULL)
1 ✔
204
        setAlgorithm(mtx->algorithm()->clone());
1 ✔
205
    else
206
        setAlgorithm(NULL);
×
207

208
    setControlMode(mtx->controlMode());
1 ✔
209

210
    return Function::copyFrom(function);
1 ✔
211
}
212

213
/****************************************************************************
214
 * Fixtures
215
 ****************************************************************************/
216

217
quint32 RGBMatrix::fixtureGroup() const
40 ✔
218
{
219
    return m_fixtureGroupID;
40 ✔
220
}
221

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

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

237
    return QList<quint32>();
1 ✔
238
}
239

240
/****************************************************************************
241
 * Algorithm
242
 ****************************************************************************/
243

244
void RGBMatrix::setAlgorithm(RGBAlgorithm *algo)
15 ✔
245
{
246
    {
247
        QMutexLocker algorithmLocker(&m_algorithmMutex);
15 ✔
248

249
        /** Unregister the attributes of the outgoing algorithm while it is
250
         *  still around, since their names come from its properties */
251
        unregisterScriptPropertyAttributes();
15 ✔
252

253
        delete m_algorithm;
15 ✔
254
        m_algorithm = algo;
15 ✔
255

256
        m_requestEngineCreation = true;
15 ✔
257

258
        /** If there's been a change of Script algorithm "on the fly",
259
         *  then re-apply the properties currently set in this RGBMatrix */
260
        if (m_algorithm != NULL && m_algorithm->type() == RGBAlgorithm::Script)
15 ✔
261
        {
262
            RGBScript *script = static_cast<RGBScript*> (m_algorithm);
14 ✔
263
            QMapIterator<QString, QString> it(m_properties);
14 ✔
264
            while (it.hasNext())
14 ✔
265
            {
266
                it.next();
×
267
                if (script->setProperty(it.key(), it.value()) == false)
×
268
                {
269
                    /** If the new algorithm doesn't expose a property,
270
                     *  then remove it from the cached list, otherwise
271
                     *  it would be carried around forever (and saved on XML) */
272
                    m_properties.take(it.key());
×
273
                }
274
            }
275

276
            QVector<uint> colors = script->rgbMapGetColors();
14 ✔
277
            for (int i = 0; i < colors.count(); i++)
14 ✔
278
                m_rgbColors.replace(i, QColor::fromRgb(colors.at(i)));
×
279
        }
14 ✔
280
    }
15 ✔
281
    m_stepsCount = algorithmStepsCount();
15 ✔
282

283
    /** Expose the properties of the new algorithm as Function attributes */
284
    registerScriptPropertyAttributes();
15 ✔
285

286
    if (m_applyingStyleAttributes == false)
15 ✔
287
        Function::adjustAttribute(algorithmIndex(), PatternAttr);
15 ✔
288

289
    emit changed(id());
15 ✔
290
}
15 ✔
291

292
RGBAlgorithm *RGBMatrix::algorithm() const
18 ✔
293
{
294
    return m_algorithm;
18 ✔
295
}
296

297
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
298
QMutex& RGBMatrix::algorithmMutex()
299
{
300
    return m_algorithmMutex;
301
}
302
#else
303
QRecursiveMutex& RGBMatrix::algorithmMutex()
×
304
{
305
    return m_algorithmMutex;
×
306
}
307
#endif
308

309

310
int RGBMatrix::stepsCount() const
2 ✔
311
{
312
    return m_stepsCount;
2 ✔
313
}
314

315
int RGBMatrix::algorithmStepsCount()
25 ✔
316
{
317
    QMutexLocker algorithmLocker(&m_algorithmMutex);
25 ✔
318

319
    if (m_algorithm == NULL)
25 ✔
320
        return 0;
×
321

322
    FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
25 ✔
323
    if (grp != NULL)
25 ✔
324
        return m_algorithm->rgbMapStepCount(grp->size());
8 ✔
325

326
    return 0;
17 ✔
327
}
25 ✔
328

329
void RGBMatrix::previewMap(int step, RGBMatrixStep *handler)
7 ✔
330
{
331
    QMutexLocker algorithmLocker(&m_algorithmMutex);
7 ✔
332
    if (m_algorithm == NULL || handler == NULL)
7 ✔
333
        return;
×
334

335
    if (m_group == NULL)
7 ✔
336
        m_group = doc()->fixtureGroup(fixtureGroup());
1 ✔
337

338
    if (m_group != NULL)
7 ✔
339
    {
340
        setMapColors(m_algorithm);
6 ✔
341
        m_algorithm->rgbMap(m_group->size(), handler->stepColor().rgb(), step, handler->m_map);
6 ✔
342
    }
343
}
7 ✔
344

345
/****************************************************************************
346
 * Color
347
 ****************************************************************************/
348

349
void RGBMatrix::setColor(int i, QColor c)
26 ✔
350
{
351
    if (i < 0)
26 ✔
352
        return;
×
353

354
    if (i >= m_rgbColors.count())
26 ✔
355
        m_rgbColors.resize(i + 1);
×
356

357
    m_rgbColors.replace(i, c);
26 ✔
358
    {
359
        QMutexLocker algorithmLocker(&m_algorithmMutex);
26 ✔
360
        if (m_algorithm != NULL)
26 ✔
361
        {
362
            m_algorithm->setColors(m_rgbColors);
16 ✔
363
            updateColorDelta();
16 ✔
364
        }
365
    }
26 ✔
366
    setMapColors(m_algorithm);
26 ✔
367

368
    if (m_applyingStyleAttributes == false && i >= 0 && i < ColorAttributeCount)
26 ✔
369
        Function::adjustAttribute(c.isValid() ? int(c.rgb() & RGBMatrixColorMask) : -1, Color1Attr + i);
26 ✔
370

371
    emit changed(id());
26 ✔
372
}
373

374
QColor RGBMatrix::getColor(int i) const
70 ✔
375
{
376
    if (i < 0 || i >= m_rgbColors.count())
70 ✔
377
        return QColor();
×
378

379
    return m_rgbColors.at(i);
70 ✔
380
}
381

382
QVector<QColor> RGBMatrix::getColors() const
1 ✔
383
{
384
    return m_rgbColors;
1 ✔
385
}
386

387
void RGBMatrix::updateColorDelta()
16 ✔
388
{
389
    if (m_rgbColors.count() > 1)
16 ✔
390
        m_stepHandler->calculateColorDelta(m_rgbColors[0], m_rgbColors[1], m_algorithm);
16 ✔
391
}
16 ✔
392

393
void RGBMatrix::setMapColors(RGBAlgorithm *algorithm)
32 ✔
394
{
395
    QMutexLocker algorithmLocker(&m_algorithmMutex);
32 ✔
396
    if (algorithm == NULL)
32 ✔
397
        return;
10 ✔
398

399
    if (algorithm->apiVersion() < 3)
22 ✔
400
        return;
22 ✔
401

402
    if (m_group == NULL)
×
403
        m_group = doc()->fixtureGroup(fixtureGroup());
×
404

405
    QVector<unsigned int> rawColors;
×
406
    const int acceptColors = algorithm->acceptColors();
×
407
    rawColors.reserve(acceptColors);
×
408
    for (int i = 0; i < acceptColors; i++)
×
409
    {
410
        if (m_rgbColors.count() > i)
×
411
        {
412
            QColor col = m_rgbColors.at(i);
×
413
            rawColors.append(col.isValid() ? col.rgb() : 0);
×
414
        }
415
        else
416
        {
417
            rawColors.append(0);
×
418
        }
419
    }
420

421
    algorithm->rgbMapSetColors(rawColors);
×
422
}
32 ✔
423

424
/************************************************************************
425
 * Properties
426
 ************************************************************************/
427

428
void RGBMatrix::setProperty(QString propName, QString value)
1 ✔
429
{
430
    QMutexLocker algoLocker(&m_algorithmMutex);
1 ✔
431

432
    // Remember the old step count before changing it (used to scale the step index)
433
    int oldStepsCount = m_stepsCount;
1 ✔
434

435
    m_properties[propName] = value;
1 ✔
436
    if (m_algorithm != NULL && m_algorithm->type() == RGBAlgorithm::Script)
1 ✔
437
    {
438
        RGBScript *script = static_cast<RGBScript*> (m_algorithm);
1 ✔
439
        script->setProperty(propName, value);
1 ✔
440

441
        QVector<uint> colors = script->rgbMapGetColors();
1 ✔
442
        for (int i = 0; i < colors.count(); i++)
1 ✔
443
            setColor(i, QColor::fromRgb(colors.at(i)));
×
444
    }
1 ✔
445
    m_stepsCount = algorithmStepsCount();
1 ✔
446

447
    // Scale currentStepIndex to the new step count to preserve the phase.
448
    // We use m_continuousPhase (a continuous 0.0-1.0 value) instead of the discrete
449
    // stepIndex to avoid accumulating rounding errors over multiple speed changes.
450
    // Analogous to EFXFixture::durationChanged() using m_currentAngle.
451
    if (m_stepHandler != NULL && m_stepsCount > 0 && oldStepsCount != m_stepsCount)
1 ✔
452
    {
453
        // If m_continuousPhase is not up to date yet (first change),
454
        // compute it from the current stepIndex
455
        if (oldStepsCount > 0)
×
456
        {
457
            int currentStepIndex = m_stepHandler->currentStepIndex();
×
458
            m_continuousPhase = double(currentStepIndex) / double(oldStepsCount);
×
459
        }
460

461
        // Rescale the phase to the new step count (preserve the continuous phase,
462
        // not the discrete index)
463
        int newStepIndex = int(round(m_continuousPhase * double(m_stepsCount)));
×
464
        // Make sure it stays within range
465
        if (newStepIndex >= m_stepsCount)
×
466
            newStepIndex = m_stepsCount - 1;
×
467
        if (newStepIndex < 0)
×
468
            newStepIndex = 0;
×
469
        m_stepHandler->setCurrentStepIndex(newStepIndex);
×
470
    }
471
}
1 ✔
472

473
QString RGBMatrix::property(QString propName)
17 ✔
474
{
475
    QMutexLocker algoLocker(&m_algorithmMutex);
17 ✔
476

477
    /** If the property is cached, then return it right away */
478
    QMap<QString, QString>::iterator it = m_properties.find(propName);
17 ✔
479
    if (it != m_properties.end())
17 ✔
480
        return it.value();
1 ✔
481

482
    /** Otherwise, let's retrieve it from the Script */
483
    if (m_algorithm != NULL && m_algorithm->type() == RGBAlgorithm::Script)
16 ✔
484
    {
485
        RGBScript *script = static_cast<RGBScript*> (m_algorithm);
16 ✔
486
        return script->property(propName);
16 ✔
487
    }
488

489
    return QString();
×
490
}
17 ✔
491

492
/****************************************************************************
493
 * Load & Save
494
 ****************************************************************************/
495

496
bool RGBMatrix::loadXML(QXmlStreamReader &root)
3 ✔
497
{
498
    if (root.name() != KXMLQLCFunction)
3 ✔
499
    {
500
        qWarning() << Q_FUNC_INFO << "Function node not found";
1 ✔
501
        return false;
1 ✔
502
    }
503

504
    if (root.attributes().value(KXMLQLCFunctionType).toString() != typeToString(Function::RGBMatrixType))
4 ✔
505
    {
506
        qWarning() << Q_FUNC_INFO << "Function is not an RGB matrix";
1 ✔
507
        return false;
1 ✔
508
    }
509

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

573
    return true;
1 ✔
574
}
575

576
bool RGBMatrix::saveXML(QXmlStreamWriter *doc) const
1 ✔
577
{
578
    Q_ASSERT(doc != NULL);
1 ✔
579

580
    /* Function tag */
581
    doc->writeStartElement(KXMLQLCFunction);
2 ✔
582

583
    /* Common attributes */
584
    saveXMLCommon(doc);
1 ✔
585

586
    /* Tempo type */
587
    saveXMLTempoType(doc);
1 ✔
588

589
    /* Speeds */
590
    saveXMLSpeed(doc);
1 ✔
591

592
    /* Direction */
593
    saveXMLDirection(doc);
1 ✔
594

595
    /* Run order */
596
    saveXMLRunOrder(doc);
1 ✔
597

598
    /* Algorithm */
599
    if (m_algorithm != NULL)
1 ✔
600
        m_algorithm->saveXML(doc);
1 ✔
601

602
    /* LEGACY - Dimmer Control */
603
    if (dimmerControl())
1 ✔
604
        doc->writeTextElement(KXMLQLCRGBMatrixDimmerControl, QString::number(dimmerControl()));
×
605

606
    /* Colors */
607
    for (int i = 0; i < m_rgbColors.count(); i++)
6 ✔
608
    {
609
        if (m_rgbColors.at(i).isValid() == false)
5 ✔
610
            continue;
×
611

612
        doc->writeStartElement(KXMLQLCRGBMatrixColor);
10 ✔
613
        doc->writeAttribute(KXMLQLCRGBMatrixColorIndex, QString::number(i));
10 ✔
614
        doc->writeCharacters(QString::number(m_rgbColors.at(i).rgb()));
5 ✔
615
        doc->writeEndElement();
5 ✔
616
    }
617

618
    /* Control Mode */
619
    doc->writeTextElement(KXMLQLCRGBMatrixControlMode, RGBMatrix::controlModeToString(m_controlMode));
2 ✔
620

621
    /* Fixture Group */
622
    doc->writeTextElement(KXMLQLCRGBMatrixFixtureGroup, QString::number(fixtureGroup()));
2 ✔
623

624
    /* Properties */
625
    QMapIterator<QString, QString> it(m_properties);
1 ✔
626
    while (it.hasNext())
1 ✔
627
    {
628
        it.next();
×
629
        doc->writeStartElement(KXMLQLCRGBMatrixProperty);
×
630
        doc->writeAttribute(KXMLQLCRGBMatrixPropertyName, it.key());
×
631
        doc->writeAttribute(KXMLQLCRGBMatrixPropertyValue, it.value());
×
632
        doc->writeEndElement();
×
633
    }
634

635
    /* End the <Function> tag */
636
    doc->writeEndElement();
1 ✔
637

638
    return true;
1 ✔
639
}
1 ✔
640

641
/****************************************************************************
642
 * Running
643
 ****************************************************************************/
644

645
void RGBMatrix::tap()
×
646
{
647
    if (stopped() == false)
×
648
    {
649
        FixtureGroup *grp = doc()->fixtureGroup(fixtureGroup());
×
650
        // Filter out taps that are too close to each other
651
        if (grp != NULL && uint(m_roundTime.elapsed()) >= (duration() / 4))
×
652
        {
653
            roundCheck();
×
654
            resetElapsed();
×
655
        }
656
    }
657
}
×
658

659
void RGBMatrix::checkEngineCreation()
×
660
{
661
    m_runAlgorithm = m_algorithm;
×
662
    m_requestEngineCreation = false;
×
663
}
×
664

665
void RGBMatrix::preRun(MasterTimer *timer)
×
666
{
667
    {
668
        QMutexLocker algorithmLocker(&m_algorithmMutex);
×
669

670
        m_group = doc()->fixtureGroup(m_fixtureGroupID);
×
671
        if (m_group == NULL)
×
672
        {
673
            // No fixture group to control
674
            stop(FunctionParent::master());
×
675
            return;
×
676
        }
677

678
        if (m_algorithm != NULL)
×
679
        {
680
            checkEngineCreation();
×
681

682
            // Copy direction from parent class direction
683
            m_stepHandler->initializeDirection(direction(), m_rgbColors[0], m_rgbColors[1], m_stepsCount, m_runAlgorithm);
×
684

685
            // Update continuous phase when starting playback
686
            if (m_stepsCount > 0)
×
687
                m_continuousPhase = double(m_stepHandler->currentStepIndex()) / double(m_stepsCount);
×
688

689
            if (m_runAlgorithm->type() == RGBAlgorithm::Script)
×
690
            {
691
                RGBScript *script = static_cast<RGBScript*> (m_runAlgorithm);
×
692
                QMapIterator<QString, QString> it(m_properties);
×
693
                while (it.hasNext())
×
694
                {
695
                    it.next();
×
696
                    script->setProperty(it.key(), it.value());
×
697
                }
698
            }
×
699
            else if (m_runAlgorithm->type() == RGBAlgorithm::Image)
×
700
            {
701
                RGBImage *image = static_cast<RGBImage*> (m_runAlgorithm);
×
702
                if (image->animatedSource())
×
703
                    image->rewindAnimation();
×
704
            }
705
        }
706
    }
×
707

708
    m_roundTime.restart();
×
709

710
    Function::preRun(timer);
×
711
}
712

713
void RGBMatrix::write(MasterTimer *timer, QList<Universe *> universes)
×
714
{
715
    Q_UNUSED(timer);
716

717
    {
718
        QMutexLocker algorithmLocker(&m_algorithmMutex);
×
719
        if (m_group == NULL)
×
720
        {
721
            // No fixture group to control
722
            stop(FunctionParent::master());
×
723
            return;
×
724
        }
725

726
        // No time to do anything.
727
        if (duration() == 0)
×
728
            return;
×
729

730
        if (m_algorithm != NULL && m_requestEngineCreation)
×
731
            checkEngineCreation();
×
732

733
        // Invalid/nonexistent script
734
        if (m_runAlgorithm == NULL || m_runAlgorithm->apiVersion() == 0)
×
735
            return;
×
736

737
        if (isPaused() == false)
×
738
        {
739
            // Get a new map every time elapsed is reset to zero
740
            if (elapsed() < MasterTimer::tick())
×
741
            {
742
                if (tempoType() == Beats)
×
743
                    m_stepBeatDuration = beatsToTime(duration(), timer->beatTimeDuration());
×
744

745
                //qDebug() << "RGBMatrix step" << m_stepHandler->currentStepIndex() << ", color:" << QString::number(m_stepHandler->stepColor().rgb(), 16);
746
                m_runAlgorithm->rgbMap(m_group->size(), m_stepHandler->stepColor().rgb(),
×
747
                                       m_stepHandler->currentStepIndex(), m_stepHandler->m_map);
×
748
                updateMapChannels(m_stepHandler->m_map, m_group, universes);
×
749
            }
750
        }
751
    }
×
752

753
    if (isPaused() == false)
×
754
    {
755
        // Increment the ms elapsed time
756
        incrementElapsed();
×
757

758
        /* Check if we need to change direction, stop completely or go to next step
759
         * The cases are:
760
         * 1- time tempo type: act normally, on ms elapsed time
761
         * 2- beat tempo type, beat occurred: check if the elapsed beats is a multiple of
762
         *    the step beat duration. If so, proceed to the next step
763
         * 3- beat tempo type, not beat: if the ms elapsed time reached the step beat
764
         *    duration in ms, and the ms time to the next beat is not less than 1/16 of
765
         *    the step beat duration in ms, then proceed to the next step. If the ms time to the
766
         *    next beat is less than 1/16 of the step beat duration in ms, then defer the step
767
         *    change to case #2, to resync the matrix to the next beat
768
         */
769
        if (tempoType() == Time && elapsed() >= duration())
×
770
        {
771
            roundCheck();
×
772
        }
773
        else if (tempoType() == Beats)
×
774
        {
775
            if (timer->isBeat())
×
776
            {
777
                incrementElapsedBeats();
×
778
                qDebug() << "Elapsed beats:" << elapsedBeats() << ", time elapsed:" << elapsed() << ", step time:" << m_stepBeatDuration;
×
779
                if (elapsedBeats() % duration() == 0)
×
780
                {
781
                    roundCheck();
×
782
                    resetElapsed();
×
783
                }
784
            }
785
            else if (elapsed() >= m_stepBeatDuration && (uint)timer->timeToNextBeat() > m_stepBeatDuration / 16)
×
786
            {
787
                qDebug() << "Elapsed exceeded";
×
788
                roundCheck();
×
789
            }
790
        }
791
    }
792
}
793

794
void RGBMatrix::postRun(MasterTimer *timer, QList<Universe *> universes)
×
795
{
796
    uint fadeout = overrideFadeOutSpeed() == defaultSpeed() ? fadeOutSpeed() : overrideFadeOutSpeed();
×
797

798
    /* If no fade out is needed, dismiss all the requested faders.
799
     * Otherwise, set all the faders to fade out and let Universe dismiss them
800
     * when done */
801
    if (fadeout == 0)
×
802
    {
803
        dismissAllFaders();
×
804
    }
805
    else
806
    {
807
        if (tempoType() == Beats)
×
808
            fadeout = beatsToTime(fadeout, timer->beatTimeDuration());
×
809

810
        foreach (QSharedPointer<GenericFader> fader, m_fadersMap)
×
811
        {
812
            if (!fader.isNull())
×
813
                fader->setFadeOut(true, fadeout);
×
814
        }
×
815
    }
816

817
    m_fadersMap.clear();
×
818

819
    {
820
        QMutexLocker algorithmLocker(&m_algorithmMutex);
×
821
        checkEngineCreation();
×
822
        if (m_runAlgorithm != NULL)
×
823
            m_runAlgorithm->postRun();
×
824
    }
×
825

826
    Function::postRun(timer, universes);
×
827
}
×
828

829
void RGBMatrix::roundCheck()
×
830
{
831
    QMutexLocker algorithmLocker(&m_algorithmMutex);
×
832
    if (m_algorithm == NULL)
×
833
        return;
×
834

835
    if (m_stepHandler->checkNextStep(runOrder(), m_rgbColors[0], m_rgbColors[1], m_stepsCount) == false)
×
836
        stop(FunctionParent::master());
×
837

838
    // Update continuous phase based on current step index (prevents cumulative rounding errors)
839
    // This is analogous to how EFX uses m_currentAngle for phase scaling
840
    if (m_stepsCount > 0)
×
841
        m_continuousPhase = double(m_stepHandler->currentStepIndex()) / double(m_stepsCount);
×
842

843
    m_roundTime.restart();
×
844

845
    if (tempoType() == Beats)
×
846
        roundElapsed(m_stepBeatDuration);
×
847
    else
848
        roundElapsed(duration());
×
849
}
×
850

851
QSharedPointer<GenericFader> RGBMatrix::getFader(Universe *universe)
×
852
{
853
    // get the universe Fader first. If doesn't exist, create it
854
    if (universe == NULL)
×
855
        return QSharedPointer<GenericFader>();
×
856

857
    QSharedPointer<GenericFader> fader = m_fadersMap.value(universe->id(), QSharedPointer<GenericFader>());
×
858
    if (fader.isNull())
×
859
    {
860
        fader = universe->requestFader();
×
861
        fader->adjustIntensity(getAttributeValue(Intensity));
×
862
        fader->setBlendMode(blendMode());
×
863
        fader->setName(name());
×
864
        fader->setParentFunctionID(id());
×
865
        m_fadersMap[universe->id()] = fader;
×
866
    }
867

868
    return fader;
×
869
}
×
870

871
void RGBMatrix::updateFaderValues(FadeChannel &fc, uchar value, uint fadeTime)
×
872
{
873
    // If the channel is already fading towards the requested value, let it
874
    // continue undisturbed. Restarting start/elapsed on every step (even when
875
    // the target doesn't change) would keep resetting the fade before it can
876
    // ever reach its target, which is especially noticeable when the fade out
877
    // time is longer than the step duration
878
    if (fc.target() == value)
×
879
        return;
×
880

881
    fc.setStart(fc.current());
×
882
    fc.setTarget(value);
×
883
    fc.setElapsed(0);
×
884
    fc.setReady(false);
×
885
    // fade in/out depends on target value
886
    if (value == 0)
×
887
        fc.setFadeTime(fadeOutSpeed());
×
888
    else
889
        fc.setFadeTime(fadeTime);
×
890
}
891

892
void RGBMatrix::updateMapChannels(const RGBMap& map, const FixtureGroup *grp, QList<Universe *> universes)
×
893
{
894
    uint fadeTime = (overrideFadeInSpeed() == defaultSpeed()) ? fadeInSpeed() : overrideFadeInSpeed();
×
895

896
    // Create/modify fade channels for ALL heads in the group
897
    QMapIterator<QLCPoint, GroupHead> it(grp->headsMap());
×
898
    while (it.hasNext())
×
899
    {
900
        it.next();
×
901
        QLCPoint pt = it.key();
×
902
        GroupHead grpHead = it.value();
×
903
        Fixture *fxi = doc()->fixture(grpHead.fxi);
×
904
        if (fxi == NULL)
×
905
            continue;
×
906

907
        QLCFixtureHead head = fxi->head(grpHead.head);
×
908

909
        if (pt.y() >= map.count() || pt.x() >= map[pt.y()].count())
×
910
            continue;
×
911

912
        uint col = map[pt.y()][pt.x()];
×
913
        QVector<quint32> channelList;
×
914
        QVector<uchar> valueList;
×
915

916
        if (m_controlMode == ControlModeRgb)
×
917
        {
918
            channelList = head.rgbChannels();
×
919

920
            if (channelList.size() == 3)
×
921
            {
922
                valueList.append(qRed(col));
×
923
                valueList.append(qGreen(col));
×
924
                valueList.append(qBlue(col));
×
925
            }
926
            else
927
            {
928
                channelList = head.cmyChannels();
×
929

930
                if (channelList.size() == 3)
×
931
                {
932
                    // CMY color mixing
933
                    QColor cmyCol(col);
×
934
                    valueList.append(cmyCol.cyan());
×
935
                    valueList.append(cmyCol.magenta());
×
936
                    valueList.append(cmyCol.yellow());
×
937
                }
938
            }
939
        }
940
        else if (m_controlMode == ControlModeShutter)
×
941
        {
942
            channelList = head.shutterChannels();
×
943

944
            if (channelList.size())
×
945
            {
946
                // make sure only one channel is in the list
947
                channelList.resize(1);
×
948
                valueList.append(rgbToGrey(col));
×
949
            }
950
        }
951
        else if (m_controlMode == ControlModeDimmer || m_dimmerControl)
×
952
        {
953
            // Collect all dimmers that affect current head:
954
            // They are the master dimmer (affects whole fixture)
955
            // and per-head dimmer.
956
            //
957
            // If there are no RGB or CMY channels, the least important* dimmer channel
958
            // is used to create grayscale image.
959
            //
960
            // The rest of the dimmer channels are set to full if dimmer control is
961
            // enabled and target color is > 0 (see
962
            // https://www.qlcplus.org/forum/viewtopic.php?f=29&t=11090)
963
            //
964
            // Note: If there is only one head, and only one dimmer channel,
965
            // make it a master dimmer in fixture definition.
966
            //
967
            // *least important - per head dimmer if present,
968
            // otherwise per fixture dimmer if present
969

970
            quint32 masterDim = fxi->masterIntensityChannel();
×
971
            quint32 headDim = head.channelNumber(QLCChannel::Intensity, QLCChannel::MSB);
×
972

973
            if (masterDim != QLCChannel::invalid())
×
974
            {
975
                channelList.append(masterDim);
×
976
                valueList.append(rgbToGrey(col));
×
977
            }
978

979
            if (headDim != QLCChannel::invalid() && headDim != masterDim)
×
980
            {
981
                channelList.append(headDim);
×
982
                // If a master dimmer is present, it carries the greyscale fade and the
983
                // per-head dimmer is just opened fully (on/off). With no master dimmer
984
                // (e.g. generic single-channel dimmers), the head dimmer must carry the
985
                // greyscale value itself, otherwise it would only ever output 0 or 255.
986
                if (masterDim != QLCChannel::invalid())
×
987
                    valueList.append(rgbToGrey(col) == 0 ? 0 : 255);
×
988
                else
989
                    valueList.append(rgbToGrey(col));
×
990
            }
991
        }
×
992
        else
993
        {
994
            if (m_controlMode == ControlModeWhite)
×
995
                channelList.append(head.channelNumber(QLCChannel::White, QLCChannel::MSB));
×
996
            else if (m_controlMode == ControlModeAmber)
×
997
                channelList.append(head.channelNumber(QLCChannel::Amber, QLCChannel::MSB));
×
998
            else if (m_controlMode == ControlModeUV)
×
999
                channelList.append(head.channelNumber(QLCChannel::UV, QLCChannel::MSB));
×
1000

1001
            valueList.append(rgbToGrey(col));
×
1002
        }
1003

1004
        for (int i = 0; i < channelList.count(); i++)
×
1005
        {
1006
            if (channelList.at(i) == QLCChannel::invalid())
×
1007
                continue;
×
1008

1009
            // a cross-universe fixture channel may belong to a following universe
1010
            quint32 absAddress = fxi->channelAddress(channelList.at(i));
×
1011
            if (absAddress == QLCChannel::invalid())
×
1012
                continue;
×
1013

1014
            int universeIndex = absAddress >> 9;
×
1015
            if (universeIndex >= universes.count())
×
1016
                continue;
×
1017

1018
            Universe *universe = universes.at(universeIndex);
×
1019
            QSharedPointer<GenericFader> fader = getFader(universe);
×
1020
            if (fader.isNull())
×
1021
                continue;
×
1022

1023
            const quint32 fixtureID = grpHead.fxi;
×
1024
            const quint32 channel = channelList.at(i);
×
1025
            const uchar value = valueList.at(i);
×
1026
            fader->updateChannel(doc(), universe, fixtureID, channel, [this, value, fadeTime](FadeChannel &fc)
×
1027
            {
1028
                updateFaderValues(fc, value, fadeTime);
×
1029
            });
×
1030
        }
×
1031
    }
×
1032
}
×
1033

1034
uchar RGBMatrix::rgbToGrey(uint col)
×
1035
{
1036
    uchar r = qRed(col);
×
1037
    uchar g = qGreen(col);
×
1038
    uchar b = qBlue(col);
×
1039

1040
    // Special case: if R=G=B (grayscale), return the value directly.
1041
    // This avoids floating-point precision issues.
1042
    if (r == g && g == b)
×
1043
        return r;
×
1044

1045
    // the weights are taken from
1046
    // https://en.wikipedia.org/wiki/YUV#SDTV_with_BT.601
1047
    return uchar(round(0.299 * r + 0.587 * g + 0.114 * b));
×
1048
}
1049

1050
/*********************************************************************
1051
 * Attributes
1052
 *********************************************************************/
1053

1054
int RGBMatrix::adjustAttribute(qreal fraction, int attributeId)
×
1055
{
1056
    int attrIndex = Function::adjustAttribute(fraction, attributeId);
×
1057

1058
    if (attrIndex == Intensity)
×
1059
    {
1060
        foreach (QSharedPointer<GenericFader> fader, m_fadersMap)
×
1061
        {
1062
            if (!fader.isNull())
×
1063
                fader->adjustIntensity(getAttributeValue(Function::Intensity));
×
1064
        }
×
1065
    }
1066
    else if (attrIndex >= Color1Attr && attrIndex <= ColorLastAttr)
×
1067
    {
1068
        applyColorAttribute(attrIndex - Color1Attr, getAttributeValue(attrIndex));
×
1069
    }
1070
    else if (attrIndex == PatternAttr)
×
1071
    {
1072
        applyPatternAttribute(getAttributeValue(PatternAttr));
×
1073
    }
1074
    else if (attrIndex >= ScriptPropertyAttr)
×
1075
    {
1076
        applyScriptPropertyAttribute(attrIndex - ScriptPropertyAttr, getAttributeValue(attrIndex));
×
1077
    }
1078

1079
    return attrIndex;
×
1080
}
1081

1082
void RGBMatrix::applyStyleAttributes()
×
1083
{
1084
    for (int i = 0; i < ColorAttributeCount; ++i)
×
1085
        applyColorAttribute(i, getAttributeValue(Color1Attr + i));
×
1086

1087
    applyPatternAttribute(getAttributeValue(PatternAttr));
×
1088

1089
    for (int i = 0; i < scriptPropertyAttributes().count(); i++)
×
1090
        applyScriptPropertyAttribute(i, getAttributeValue(ScriptPropertyAttr + i));
×
1091
}
×
1092

1093
QList<RGBScriptProperty> RGBMatrix::scriptPropertyAttributes() const
30 ✔
1094
{
1095
    QList<RGBScriptProperty> list;
30 ✔
1096

1097
    if (m_algorithm == NULL || m_algorithm->type() != RGBAlgorithm::Script)
30 ✔
1098
        return list;
11 ✔
1099

1100
    RGBScript *script = static_cast<RGBScript*> (m_algorithm);
19 ✔
1101

1102
    foreach (RGBScriptProperty prop, script->properties())
57 ✔
1103
    {
1104
        /** Only the properties that a slider can drive are exposed:
1105
         *  a list needs at least two values to pick from, a range needs
1106
         *  a meaningful span and a string cannot be controlled at all */
1107
        if (prop.m_type == RGBScriptProperty::List)
19 ✔
1108
        {
1109
            if (prop.m_listValues.count() > 1)
19 ✔
1110
                list.append(prop);
19 ✔
1111
        }
1112
        else if (prop.m_type == RGBScriptProperty::Range)
×
1113
        {
1114
            if (prop.m_rangeMaxValue > prop.m_rangeMinValue)
×
1115
                list.append(prop);
×
1116
        }
1117
        else if (prop.m_type == RGBScriptProperty::Float)
×
1118
        {
1119
            list.append(prop);
×
1120
        }
1121
    }
38 ✔
1122

1123
    return list;
19 ✔
1124
}
×
1125

1126
QString RGBMatrix::scriptPropertyAttributeName(const RGBScriptProperty &prop)
19 ✔
1127
{
1128
    return prop.m_displayName.isEmpty() ? prop.m_name : prop.m_displayName;
19 ✔
1129
}
1130

1131
void RGBMatrix::unregisterScriptPropertyAttributes()
15 ✔
1132
{
1133
    foreach (RGBScriptProperty prop, scriptPropertyAttributes())
35 ✔
1134
        unregisterAttribute(scriptPropertyAttributeName(prop));
20 ✔
1135
}
15 ✔
1136

1137
void RGBMatrix::registerScriptPropertyAttributes()
15 ✔
1138
{
1139
    QMutexLocker algorithmLocker(&m_algorithmMutex);
15 ✔
1140

1141
    foreach (RGBScriptProperty prop, scriptPropertyAttributes())
44 ✔
1142
    {
1143
        qreal min = 0.0;
14 ✔
1144
        qreal max = 1.0;
14 ✔
1145

1146
        if (prop.m_type == RGBScriptProperty::List)
14 ✔
1147
            max = prop.m_listValues.count() - 1;
14 ✔
1148
        else if (prop.m_type == RGBScriptProperty::Range)
×
1149
        {
1150
            min = prop.m_rangeMinValue;
×
1151
            max = prop.m_rangeMaxValue;
×
1152
        }
1153
        /** Scripts don't declare a range for float properties,
1154
         *  so 0.0 - 1.0 is assumed */
1155

1156
        /** A list property holds a string, so the attribute value is the
1157
         *  index of the current value in the values list */
1158
        QString current = property(prop.m_name);
14 ✔
1159
        qreal value = prop.m_type == RGBScriptProperty::List ?
28 ✔
1160
                      qMax(0, prop.m_listValues.indexOf(current)) : current.toDouble();
14 ✔
1161

1162
        registerAttribute(scriptPropertyAttributeName(prop), LastWins | Single,
14 ✔
1163
                          min, max, qBound(min, value, max));
14 ✔
1164
    }
29 ✔
1165
}
15 ✔
1166

1167
void RGBMatrix::applyScriptPropertyAttribute(int attrIndex, qreal value)
×
1168
{
1169
    QList<RGBScriptProperty> props = scriptPropertyAttributes();
×
1170
    if (attrIndex < 0 || attrIndex >= props.count())
×
1171
        return;
×
1172

1173
    const RGBScriptProperty prop = props.at(attrIndex);
×
1174
    QString strValue;
×
1175

1176
    switch (prop.m_type)
×
1177
    {
1178
        case RGBScriptProperty::List:
×
1179
            strValue = prop.m_listValues.at(qBound(0, int(qRound(value)), prop.m_listValues.count() - 1));
×
1180
        break;
×
1181
        case RGBScriptProperty::Float:
×
1182
            strValue = QString::number(value);
×
1183
        break;
×
1184
        default:
×
1185
            strValue = QString::number(qRound(value));
×
1186
        break;
×
1187
    }
1188

1189
    if (property(prop.m_name) != strValue)
×
1190
        setProperty(prop.m_name, strValue);
×
1191
}
×
1192

1193
void RGBMatrix::applyColorAttribute(int colorIndex, qreal packedColor)
×
1194
{
1195
    if (colorIndex < 0 || colorIndex >= ColorAttributeCount)
×
1196
        return;
×
1197

1198
    int packed = qRound(packedColor);
×
1199
    QColor targetColor = packed < 0 ? QColor() :
1200
                                      QColor::fromRgb(static_cast<QRgb>((packed & RGBMatrixColorMask) | 0xFF000000));
×
1201
    if (getColor(colorIndex) == targetColor)
×
1202
        return;
×
1203

1204
    bool previous = m_applyingStyleAttributes;
×
1205
    m_applyingStyleAttributes = true;
×
1206
    setColor(colorIndex, targetColor);
×
1207
    m_applyingStyleAttributes = previous;
×
1208
}
1209

1210
void RGBMatrix::applyPatternAttribute(qreal patternIndex)
×
1211
{
1212
    if (doc() == NULL)
×
1213
        return;
×
1214

1215
    QStringList algoList = RGBAlgorithm::algorithms(doc());
×
1216
    if (algoList.isEmpty())
×
1217
        return;
×
1218

1219
    int idx = qRound(patternIndex);
×
1220
    if (idx < 0)
×
1221
        idx = 0;
×
1222
    else if (idx >= algoList.count())
×
1223
        idx = algoList.count() - 1;
×
1224

1225
    RGBAlgorithm *algo = RGBAlgorithm::algorithm(doc(), algoList.at(idx));
×
1226
    if (algo == NULL)
×
1227
        return;
×
1228

1229
    if (m_algorithm != NULL && m_algorithm->name() == algo->name())
×
1230
    {
1231
        delete algo;
×
1232
        return;
×
1233
    }
1234

1235
    algo->setColors(getColors());
×
1236

1237
    bool previous = m_applyingStyleAttributes;
×
1238
    m_applyingStyleAttributes = true;
×
1239
    setAlgorithm(algo);
×
1240
    m_applyingStyleAttributes = previous;
×
1241
}
×
1242

1243
/*************************************************************************
1244
 * Blend mode
1245
 *************************************************************************/
1246

1247
void RGBMatrix::setBlendMode(Universe::BlendMode mode)
×
1248
{
1249
    if (mode == blendMode())
×
1250
        return;
×
1251

1252
    foreach (QSharedPointer<GenericFader> fader, m_fadersMap)
×
1253
    {
1254
        if (!fader.isNull())
×
1255
            fader->setBlendMode(mode);
×
1256
    }
×
1257

1258
    Function::setBlendMode(mode);
×
1259
    emit changed(id());
×
1260
}
1261

1262
/*************************************************************************
1263
 * Control Mode
1264
 *************************************************************************/
1265

1266
RGBMatrix::ControlMode RGBMatrix::controlMode() const
2 ✔
1267
{
1268
    return m_controlMode;
2 ✔
1269
}
1270

1271
void RGBMatrix::setControlMode(RGBMatrix::ControlMode mode)
3 ✔
1272
{
1273
    m_controlMode = mode;
3 ✔
1274
    emit changed(id());
3 ✔
1275
}
3 ✔
1276

1277
RGBMatrix::ControlMode RGBMatrix::stringToControlMode(QString mode)
1 ✔
1278
{
1279
    if (mode == KXMLQLCRGBMatrixControlModeRgb)
1 ✔
1280
        return ControlModeRgb;
1 ✔
1281
    else if (mode == KXMLQLCRGBMatrixControlModeAmber)
×
1282
        return ControlModeAmber;
×
1283
    else if (mode == KXMLQLCRGBMatrixControlModeWhite)
×
1284
        return ControlModeWhite;
×
1285
    else if (mode == KXMLQLCRGBMatrixControlModeUV)
×
1286
        return ControlModeUV;
×
1287
    else if (mode == KXMLQLCRGBMatrixControlModeDimmer)
×
1288
        return ControlModeDimmer;
×
1289
    else if (mode == KXMLQLCRGBMatrixControlModeShutter)
×
1290
        return ControlModeShutter;
×
1291

1292
    return ControlModeRgb;
×
1293
}
1294

1295
QString RGBMatrix::controlModeToString(RGBMatrix::ControlMode mode)
1 ✔
1296
{
1297
    switch(mode)
1 ✔
1298
    {
1299
        default:
1 ✔
1300
        case ControlModeRgb:
1301
            return QString(KXMLQLCRGBMatrixControlModeRgb);
1 ✔
1302
        break;
1303
        case ControlModeAmber:
×
1304
            return QString(KXMLQLCRGBMatrixControlModeAmber);
×
1305
        break;
1306
        case ControlModeWhite:
×
1307
            return QString(KXMLQLCRGBMatrixControlModeWhite);
×
1308
        break;
1309
        case ControlModeUV:
×
1310
            return QString(KXMLQLCRGBMatrixControlModeUV);
×
1311
        break;
1312
        case ControlModeDimmer:
×
1313
            return QString(KXMLQLCRGBMatrixControlModeDimmer);
×
1314
        break;
1315
        case ControlModeShutter:
×
1316
            return QString(KXMLQLCRGBMatrixControlModeShutter);
×
1317
        break;
1318
    }
1319
}
1320

1321

1322
/*************************************************************************
1323
 *************************************************************************
1324
 *                          RGBMatrixStep class
1325
 *************************************************************************
1326
 *************************************************************************/
1327

1328
RGBMatrixStep::RGBMatrixStep()
11 ✔
1329
    : m_direction(Function::Forward)
11 ✔
1330
    , m_currentStepIndex(0)
11 ✔
1331
    , m_stepColor(QColor())
11 ✔
1332
    , m_crDelta(0)
11 ✔
1333
    , m_cgDelta(0)
11 ✔
1334
    , m_cbDelta(0)
11 ✔
1335
{
1336

1337
}
11 ✔
1338

1339
void RGBMatrixStep::setCurrentStepIndex(int index)
×
1340
{
1341
    m_currentStepIndex = index;
×
1342
}
×
1343

1344
int RGBMatrixStep::currentStepIndex() const
1 ✔
1345
{
1346
    return m_currentStepIndex;
1 ✔
1347
}
1348

1349
void RGBMatrixStep::calculateColorDelta(const QColor& startColor, const QColor& endColor, const RGBAlgorithm *algorithm)
16 ✔
1350
{
1351
    m_crDelta = 0;
16 ✔
1352
    m_cgDelta = 0;
16 ✔
1353
    m_cbDelta = 0;
16 ✔
1354

1355
    if (endColor.isValid() && algorithm != NULL && algorithm->acceptColors() > 1)
16 ✔
1356
    {
1357
        m_crDelta = endColor.red() - startColor.red();
10 ✔
1358
        m_cgDelta = endColor.green() - startColor.green();
10 ✔
1359
        m_cbDelta = endColor.blue() - startColor.blue();
10 ✔
1360

1361
        //qDebug() << "Color deltas:" << m_crDelta << m_cgDelta << m_cbDelta;
1362
    }
1363
}
16 ✔
1364

1365
void RGBMatrixStep::setStepColor(QColor color)
×
1366
{
1367
    m_stepColor = color;
×
1368
}
×
1369

1370
QColor RGBMatrixStep::stepColor() const
6 ✔
1371
{
1372
    return m_stepColor;
6 ✔
1373
}
1374

1375
void RGBMatrixStep::updateStepColor(int stepIndex, QColor startColor, int stepsCount)
×
1376
{
1377
    if (stepsCount <= 0)
×
1378
        return;
×
1379

1380
    if (stepsCount == 1)
×
1381
    {
1382
        m_stepColor = startColor;
×
1383
    }
1384
    else
1385
    {
1386
        m_stepColor.setRed(startColor.red() + (m_crDelta * stepIndex / (stepsCount - 1)));
×
1387
        m_stepColor.setGreen(startColor.green() + (m_cgDelta * stepIndex / (stepsCount - 1)));
×
1388
        m_stepColor.setBlue(startColor.blue() + (m_cbDelta * stepIndex / (stepsCount - 1)));
×
1389
    }
1390

1391
    //qDebug() << "RGBMatrix step" << stepIndex << ", color:" << QString::number(m_stepColor.rgb(), 16);
1392
}
1393

1394
void RGBMatrixStep::initializeDirection(Function::Direction direction, const QColor& startColor, const QColor& endColor, int stepsCount, const RGBAlgorithm *algorithm)
×
1395
{
1396
    m_direction = direction;
×
1397

1398
    if (m_direction == Function::Forward)
×
1399
    {
1400
        setCurrentStepIndex(0);
×
1401
        setStepColor(startColor);
×
1402
    }
1403
    else
1404
    {
1405
        setCurrentStepIndex(stepsCount - 1);
×
1406

1407
        if (endColor.isValid())
×
1408
            setStepColor(endColor);
×
1409
        else
1410
            setStepColor(startColor);
×
1411
    }
1412

1413
    calculateColorDelta(startColor, endColor, algorithm);
×
1414
}
×
1415

1416
bool RGBMatrixStep::checkNextStep(Function::RunOrder order,
×
1417
                                  QColor startColor, QColor endColor, int stepsNumber)
1418
{
1419
    if (order == Function::PingPong)
×
1420
    {
1421
        if (m_direction == Function::Forward && (m_currentStepIndex + 1) == stepsNumber)
×
1422
        {
1423
            m_direction = Function::Backward;
×
1424
            m_currentStepIndex = stepsNumber - 2;
×
1425
            if (endColor.isValid())
×
1426
                m_stepColor = endColor;
×
1427

1428
            updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1429
        }
1430
        else if (m_direction == Function::Backward && (m_currentStepIndex - 1) < 0)
×
1431
        {
1432
            m_direction = Function::Forward;
×
1433
            m_currentStepIndex = 1;
×
1434
            m_stepColor = startColor;
×
1435
            updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1436
        }
1437
        else
1438
        {
1439
            if (m_direction == Function::Forward)
×
1440
                m_currentStepIndex++;
×
1441
            else
1442
                m_currentStepIndex--;
×
1443
            updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1444
        }
1445
    }
1446
    else if (order == Function::SingleShot)
×
1447
    {
1448
        if (m_direction == Function::Forward)
×
1449
        {
1450
            if (m_currentStepIndex >= stepsNumber - 1)
×
1451
                return false;
×
1452
            else
1453
            {
1454
                m_currentStepIndex++;
×
1455
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1456
            }
1457
        }
1458
        else
1459
        {
1460
            if (m_currentStepIndex <= 0)
×
1461
                return false;
×
1462
            else
1463
            {
1464
                m_currentStepIndex--;
×
1465
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1466
            }
1467
        }
1468
    }
1469
    else
1470
    {
1471
        if (m_direction == Function::Forward)
×
1472
        {
1473
            if (m_currentStepIndex >= stepsNumber - 1)
×
1474
            {
1475
                m_currentStepIndex = 0;
×
1476
                m_stepColor = startColor;
×
1477
            }
1478
            else
1479
            {
1480
                m_currentStepIndex++;
×
1481
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1482
            }
1483
        }
1484
        else
1485
        {
1486
            if (m_currentStepIndex <= 0)
×
1487
            {
1488
                m_currentStepIndex = stepsNumber - 1;
×
1489
                if (endColor.isValid())
×
1490
                    m_stepColor = endColor;
×
1491
            }
1492
            else
1493
            {
1494
                m_currentStepIndex--;
×
1495
                updateStepColor(m_currentStepIndex, startColor, stepsNumber);
×
1496
            }
1497
        }
1498
    }
1499

1500
    return true;
×
1501
}
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