• 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

94.93
/engine/src/collection.cpp
1
/*
2
  Q Light Controller Plus
3
  collection.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 <QString>
22
#include <QDebug>
23
#include <QFile>
24
#include <QList>
25
#include <QMutexLocker>
26
#include <QXmlStreamReader>
27
#include <QXmlStreamWriter>
28

29
#include "mastertimer.h"
30
#include "collection.h"
31
#include "function.h"
32
#include "doc.h"
33

34
/*****************************************************************************
35
 * Initialization
36
 *****************************************************************************/
37

38
Collection::Collection(Doc* doc)
29 ✔
39
    : Function(doc, Function::CollectionType)
29 ✔
40
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
41
    , m_functionListMutex(QMutex::Recursive)
42
#endif
43
{
44
    setName(tr("New Collection"));
29 ✔
45

46
    // Listen to member Function removals
47
    connect(doc, SIGNAL(functionRemoved(quint32)),
29 ✔
48
            this, SLOT(slotFunctionRemoved(quint32)));
49
}
29 ✔
50

51
Collection::~Collection()
45 ✔
52
{
53
}
45 ✔
54

55
QIcon Collection::getIcon() const
×
56
{
57
    return QIcon(":/collection.png");
×
58
}
59

60
quint32 Collection::totalDuration()
2 ✔
61
{
62
    quint32 totalDuration = 0;
2 ✔
63

64
    // the members of a Collection run in parallel,
65
    // so the Collection lasts as long as the longest one
66
    foreach (QVariant fid, functions())
8 ✔
67
    {
68
        Function* function = doc()->function(fid.toUInt());
4 ✔
69
        if (function == nullptr)
4 ✔
70
            continue;
×
71
        totalDuration = qMax(totalDuration, function->totalDuration());
4 ✔
72
    }
6 ✔
73

74
    return totalDuration;
2 ✔
75
}
76

77
/*****************************************************************************
78
 * Copying
79
 *****************************************************************************/
80

81
Function* Collection::createCopy(Doc* doc, bool addToDoc)
1 ✔
82
{
83
    Q_ASSERT(doc != NULL);
1 ✔
84

85
    Function* copy = new Collection(doc);
1 ✔
86
    if (copy->copyFrom(this) == false)
1 ✔
87
    {
88
        delete copy;
×
89
        copy = NULL;
×
90
    }
91
    if (addToDoc == true && doc->addFunction(copy) == false)
1 ✔
92
    {
93
        delete copy;
×
94
        copy = NULL;
×
95
    }
96

97
    return copy;
1 ✔
98
}
99

100
bool Collection::copyFrom(const Function* function)
4 ✔
101
{
102
    const Collection* coll = qobject_cast<const Collection*> (function);
4 ✔
103
    if (coll == NULL)
4 ✔
104
        return false;
1 ✔
105

106
    m_functions.clear();
3 ✔
107
    m_functions = coll->m_functions;
3 ✔
108

109
    return Function::copyFrom(function);
3 ✔
110
}
111

112
/*****************************************************************************
113
 * Contents
114
 *****************************************************************************/
115

116
bool Collection::addFunction(quint32 fid, int insertIndex)
51 ✔
117
{
118
    if (fid != this->id() && m_functions.contains(fid) == false)
51 ✔
119
    {
120
        {
121
            QMutexLocker locker(&m_functionListMutex);
49 ✔
122
            if (insertIndex == -1)
49 ✔
123
                m_functions.append(fid);
49 ✔
124
            else
125
                m_functions.insert(insertIndex, fid);
×
126
        }
49 ✔
127

128
        emit changed(this->id());
49 ✔
129
        emit functionsChanged();
49 ✔
130
        return true;
49 ✔
131
    }
132
    else
133
    {
134
        return false;
2 ✔
135
    }
136
}
137

138
bool Collection::removeFunction(quint32 fid)
54 ✔
139
{
140
    int num = 0;
54 ✔
141
    {
142
        QMutexLocker locker(&m_functionListMutex);
54 ✔
143
        num = m_functions.removeAll(fid);
54 ✔
144
    }
54 ✔
145

146
    if (num > 0)
54 ✔
147
    {
148
        emit changed(this->id());
12 ✔
149
        emit functionsChanged();
12 ✔
150
        return true;
12 ✔
151
    }
152
    else
153
    {
154
        return false;
42 ✔
155
    }
156
}
157

158
QList<quint32> Collection::functions() const
61 ✔
159
{
160
    QMutexLocker locker(&m_functionListMutex);
61 ✔
161
    return m_functions;
61 ✔
162
}
61 ✔
163

164
void Collection::slotFunctionRemoved(quint32 fid)
51 ✔
165
{
166
    removeFunction(fid);
51 ✔
167
}
51 ✔
168

169
/*****************************************************************************
170
 * Load & Save
171
 *****************************************************************************/
172

173
bool Collection::saveXML(QXmlStreamWriter *doc) const
2 ✔
174
{
175
    int i = 0;
2 ✔
176

177
    Q_ASSERT(doc != NULL);
2 ✔
178

179
    /* Function tag */
180
    doc->writeStartElement(KXMLQLCFunction);
4 ✔
181

182
    /* Common attributes */
183
    saveXMLCommon(doc);
2 ✔
184

185
    /* Steps */
186
    QListIterator <quint32> it(m_functions);
2 ✔
187
    while (it.hasNext() == true)
6 ✔
188
    {
189
        /* Step tag */
190
        doc->writeStartElement(KXMLQLCFunctionStep);
8 ✔
191

192
        /* Step number */
193
        doc->writeAttribute(KXMLQLCFunctionNumber, QString::number(i++));
8 ✔
194

195
        /* Step Function ID */
196
        doc->writeCharacters(QString::number(it.next()));
4 ✔
197
        doc->writeEndElement();
4 ✔
198
    }
199

200
    /* End the <Function> tag */
201
    doc->writeEndElement();
2 ✔
202

203
    return true;
2 ✔
204
}
2 ✔
205

206
bool Collection::loadXML(QXmlStreamReader &root)
10 ✔
207
{
208
    if (root.name() != KXMLQLCFunction)
10 ✔
209
    {
210
        qWarning() << Q_FUNC_INFO << "Function node not found";
1 ✔
211
        return false;
1 ✔
212
    }
213

214
    if (root.attributes().value(KXMLQLCFunctionType).toString() != typeToString(Function::CollectionType))
18 ✔
215
    {
216
        qWarning() << Q_FUNC_INFO << root.attributes().value(KXMLQLCFunctionType).toString()
3 ✔
217
                   << "is not a collection";
1 ✔
218
        return false;
1 ✔
219
    }
220

221
    /* Load collection contents */
222
    while (root.readNextStartElement())
28 ✔
223
    {
224
        if (root.name() == KXMLQLCFunctionStep)
20 ✔
225
            addFunction(root.readElementText().toUInt());
18 ✔
226
        else
227
        {
228
            qWarning() << Q_FUNC_INFO << "Unknown collection tag:" << root.name();
2 ✔
229
            root.skipCurrentElement();
2 ✔
230
        }
231
    }
232

233
    return true;
8 ✔
234
}
235

236
void Collection::postLoad()
5 ✔
237
{
238
    Doc* doc = qobject_cast <Doc*> (parent());
5 ✔
239
    Q_ASSERT(doc != NULL);
5 ✔
240

241
    /* Check that all member functions exist (nonexistent functions can
242
       be present only when a corrupted file has been loaded) */
243
    QMutableListIterator<quint32> it(m_functions);
5 ✔
244
    while (it.hasNext() == true)
18 ✔
245
    {
246
        /* Remove any nonexistent member functions */
247
        Function* function = doc->function(it.next());
13 ✔
248

249
        if (function == NULL || function->contains(id())) // forbid self-containment
13 ✔
250
            it.remove();
13 ✔
251
    }
252
}
5 ✔
253

254
bool Collection::contains(quint32 functionId) const
7 ✔
255
{
256
    Doc* doc = qobject_cast <Doc*> (parent());
7 ✔
257
    Q_ASSERT(doc != NULL);
7 ✔
258

259
    foreach (quint32 fid, m_functions)
15 ✔
260
    {
261
        Function* function = doc->function(fid);
12 ✔
262
        // contains() can be called during init, function may be NULL
263
        if (function == NULL)
12 ✔
264
            continue;
×
265

266
        if (function->id() == functionId)
12 ✔
267
            return true;
3 ✔
268
        if (function->contains(functionId))
9 ✔
269
            return true;
1 ✔
270
    }
7 ✔
271

272
    return false;
3 ✔
273
}
274

275
QList<quint32> Collection::components() const
1 ✔
276
{
277
    return m_functions;
1 ✔
278
}
279

280
/*****************************************************************************
281
 * Running
282
 *****************************************************************************/
283

284
FunctionParent Collection::functionParent() const
6 ✔
285
{
286
    return FunctionParent(FunctionParent::Function, id());
6 ✔
287
}
288

289
void Collection::preRun(MasterTimer *timer)
2 ✔
290
{
291
    Doc *doc = this->doc();
2 ✔
292
    Q_ASSERT(doc != NULL);
2 ✔
293
    {
294
        QMutexLocker locker(&m_functionListMutex);
2 ✔
295
        m_runningChildren.clear();
2 ✔
296
        foreach (quint32 fid, m_functions)
6 ✔
297
        {
298
            Function *function = doc->function(fid);
4 ✔
299
            Q_ASSERT(function != NULL);
4 ✔
300

301
            m_intensityOverrideIds << function->requestAttributeOverride(Function::Intensity, getAttributeValue(Function::Intensity));
4 ✔
302

303
            // Append the IDs of all functions started by this collection
304
            // to a set so that we can track which of them are still controlled
305
            // by this collection which are not.
306
            m_runningChildren << function->id();
4 ✔
307

308
            // Listen to the children's stopped signals so that this Collection
309
            // can give up its rights to stop the function later.
310
            connect(function, SIGNAL(stopped(quint32)),
4 ✔
311
                    this, SLOT(slotChildStopped(quint32)));
312

313
            // Listen to the children's stopped signals so that this collection
314
            // can give up its rights to stop the function later.
315
            connect(function, SIGNAL(running(quint32)),
4 ✔
316
                    this, SLOT(slotChildStarted(quint32)));
317

318
            //function->adjustAttribute(getAttributeValue(Function::Intensity), Function::Intensity);
319
            function->start(timer, functionParent(), 0, overrideFadeInSpeed(), overrideFadeOutSpeed(), overrideDuration());
4 ✔
320
        }
2 ✔
321
        m_tick = 1;
2 ✔
322
    }
2 ✔
323
    Function::preRun(timer);
2 ✔
324
}
2 ✔
325

326
void Collection::setPause(bool enable)
2 ✔
327
{
328
    Doc *doc = this->doc();
2 ✔
329
    Q_ASSERT(doc != NULL);
2 ✔
330
    foreach (quint32 fid, m_runningChildren)
6 ✔
331
    {
332
        Function *function = doc->function(fid);
4 ✔
333
        Q_ASSERT(function != NULL);
4 ✔
334
        function->setPause(enable);
4 ✔
335
    }
2 ✔
336
    Function::setPause(enable);
2 ✔
337
}
2 ✔
338

339
void Collection::write(MasterTimer *timer, QList<Universe *> universes)
8 ✔
340
{
341
    Q_UNUSED(timer);
342
    Q_UNUSED(universes);
343

344
    if (isPaused())
8 ✔
345
        return;
×
346

347
    // During first tick, children may be stopped & started.
348
    if (m_tick == 1)
8 ✔
349
        m_tick = 2;
2 ✔
350
    else if (m_tick == 2)
6 ✔
351
    {
352
        m_tick = 0;
2 ✔
353
        Doc *doc = this->doc();
2 ✔
354
        Q_ASSERT(doc != NULL);
2 ✔
355

356
        QMutexLocker locker(&m_functionListMutex);
2 ✔
357
        foreach (quint32 fid, m_runningChildren)
6 ✔
358
        {
359
            Function *function = doc->function(fid);
4 ✔
360
            Q_ASSERT(function != NULL);
4 ✔
361

362
            // First tick may correspond to this collection starting the function
363
            // Now that first tick is over, stop listening to running signal
364
            disconnect(function, SIGNAL(running(quint32)),
4 ✔
365
                    this, SLOT(slotChildStarted(quint32)));
366
        }
2 ✔
367
    }
2 ✔
368

369
    incrementElapsed();
8 ✔
370

371
    {
372
        QMutexLocker locker(&m_functionListMutex);
8 ✔
373
        if (m_runningChildren.size() > 0)
8 ✔
374
            return;
7 ✔
375
    }
8 ✔
376

377
    stop(functionParent());
1 ✔
378
}
379

380
void Collection::postRun(MasterTimer* timer, QList<Universe *> universes)
1 ✔
381
{
382
    Doc* doc = qobject_cast <Doc*> (parent());
1 ✔
383
    Q_ASSERT(doc != NULL);
1 ✔
384

385
    {
386
        QMutexLocker locker(&m_functionListMutex);
1 ✔
387
        /** Stop the member functions only if they have been started by this
388
            collection. */
389
        QSetIterator <quint32> it(m_runningChildren);
1 ✔
390
        while (it.hasNext() == true)
2 ✔
391
        {
392
            Function* function = doc->function(it.next());
1 ✔
393
            Q_ASSERT(function != NULL);
1 ✔
394
            function->stop(functionParent());
1 ✔
395
        }
396

397
        m_runningChildren.clear();
1 ✔
398

399
        for (int i = 0; i < m_functions.count(); i++)
3 ✔
400
        {
401
            Function* function = doc->function(m_functions.at(i));
2 ✔
402
            Q_ASSERT(function != NULL);
2 ✔
403

404
            disconnect(function, SIGNAL(stopped(quint32)),
2 ✔
405
                    this, SLOT(slotChildStopped(quint32)));
406
            if (m_tick == 2)
2 ✔
407
            {
408
                disconnect(function, SIGNAL(running(quint32)),
×
409
                        this, SLOT(slotChildStarted(quint32)));
410
            }
411
        }
412

413
        m_intensityOverrideIds.clear();
1 ✔
414
    }
1 ✔
415

416
    Function::postRun(timer, universes);
1 ✔
417
}
1 ✔
418

419
void Collection::slotChildStopped(quint32 fid)
3 ✔
420
{
421
    QMutexLocker locker(&m_functionListMutex);
3 ✔
422
    m_runningChildren.remove(fid);
3 ✔
423
}
3 ✔
424

425
void Collection::slotChildStarted(quint32 fid)
4 ✔
426
{
427
    QMutexLocker locker(&m_functionListMutex);
4 ✔
428
    m_runningChildren << fid;
4 ✔
429
}
4 ✔
430

431
int Collection::adjustAttribute(qreal fraction, int attributeId)
1 ✔
432
{
433
    int attrIndex = Function::adjustAttribute(fraction, attributeId);
1 ✔
434

435
    if (isRunning() && attrIndex == Intensity)
1 ✔
436
    {
437
        Doc* document = doc();
1 ✔
438
        Q_ASSERT(document != NULL);
1 ✔
439

440
        QMutexLocker locker(&m_functionListMutex);
1 ✔
441

442
        for (int i = 0; i < m_functions.count(); i++)
3 ✔
443
        {
444
            Function* function = document->function(m_functions.at(i));
2 ✔
445
            Q_ASSERT(function != NULL);
2 ✔
446
            function->adjustAttribute(getAttributeValue(Function::Intensity), m_intensityOverrideIds.at(i));
2 ✔
447
        }
448
    }
1 ✔
449

450
    return attrIndex;
1 ✔
451
}
452

453
void Collection::setBlendMode(Universe::BlendMode mode)
6 ✔
454
{
455
    if (mode == blendMode())
6 ✔
456
        return;
5 ✔
457

458
    qDebug() << "Collection" << name() << "blend mode set to" << Universe::blendModeToString(mode);
1 ✔
459

460
    if (isRunning())
1 ✔
461
    {
462
        for (int i = 0; i < m_functions.count(); i++)
3 ✔
463
        {
464
            Function* function = doc()->function(m_functions.at(i));
2 ✔
465
            Q_ASSERT(function != NULL);
2 ✔
466
            function->setBlendMode(mode);
2 ✔
467
        }
468
    }
469

470
    Function::setBlendMode(mode);
1 ✔
471
}
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