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

mcallegari / qlcplus / 43708031602

15 Nov 2026 11:15AM UTC coverage: 61.667% (+0.09%) from 61.581%
43708031602

push

github

mcallegari
engine: fix Doc test unit

18048 of 29267 relevant lines covered (61.67%)

63071.28 hits per line

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

90.45
/engine/src/doc.cpp
1
/*
2
  Q Light Controller Plus
3
  doc.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 <algorithm>
23
#include <QXmlStreamWriter>
24
#include <QStringList>
25
#include <QString>
26
#include <QDebug>
27
#include <QList>
28
#include <QTime>
29
#include <QDir>
30
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
31
#include <QRandomGenerator>
32
#endif
33

34
#include "qlcfixturemode.h"
35
#include "qlcfixturedef.h"
36
#include "qlcrdmservice.h"
37

38
#include "monitorproperties.h"
39
#include "audioplugincache.h"
40
#include "rgbscriptscache.h"
41
#include "serviceloader.h"
42
#include "channelsgroup.h"
43
#include "scriptwrapper.h"
44
#include "collection.h"
45
#include "function.h"
46
#include "universe.h"
47
#include "sequence.h"
48
#include "fixture.h"
49
#include "scene.h"
50
#include "chaser.h"
51
#include "show.h"
52
#include "doc.h"
53
#include "bus.h"
54

55
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
56
 #include "audiocapture_qt5.h"
57
#else
58
 #include "audiocapture_qt6.h"
59
#endif
60

61
#define AUTOSAVE_TIMEOUT    30 // seconds
62

63
Doc::Doc(QObject* parent, int universes)
242 ✔
64
    : QObject(parent)
65
    , m_workspacePath("")
242 ✔
66
    , m_fixtureDefCache(new QLCFixtureDefCache)
242 ✔
67
    , m_modifiersCache(new QLCModifiersCache)
242 ✔
68
    , m_rgbScriptsCache(new RGBScriptsCache(this))
242 ✔
69
    , m_ioPluginCache(new IOPluginCache(this))
242 ✔
70
    , m_audioPluginCache(new AudioPluginCache(this))
242 ✔
71
    , m_serviceLoader(new ServiceLoader(this))
242 ✔
72
    , m_masterTimer(new MasterTimer(this))
242 ✔
73
    , m_ioMap(new InputOutputMap(this, universes))
242 ✔
74
    , m_monitorProps(NULL)
242 ✔
75
    , m_mode(Design)
242 ✔
76
    , m_kiosk(false)
242 ✔
77
    , m_loadStatus(Cleared)
242 ✔
78
    , m_clipboard(new QLCClipboard(this))
242 ✔
79
    , m_fixturesListCacheUpToDate(false)
242 ✔
80
    , m_latestFixtureId(0)
242 ✔
81
    , m_latestFixtureGroupId(0)
242 ✔
82
    , m_latestChannelsGroupId(0)
242 ✔
83
    , m_latestPaletteId(0)
242 ✔
84
    , m_latestFunctionId(0)
242 ✔
85
    , m_startupFunctionId(Function::invalidId())
726 ✔
86
{
87
    Bus::init(this);
242 ✔
88
    resetModified();
242 ✔
89
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
90
    qsrand(QTime::currentTime().msec());
91
#endif
92

93
    m_autosaveTimer.setInterval(AUTOSAVE_TIMEOUT * 1000);
242 ✔
94
    m_autosaveTimer.setSingleShot(true);
242 ✔
95

96
    connect(&m_autosaveTimer, SIGNAL(timeout()), this, SIGNAL(needAutosave()));
242 ✔
97
    connect(m_serviceLoader, SIGNAL(serviceAvailable(QString)),
242 ✔
98
            this, SLOT(slotServiceAvailable(QString)));
99
}
242 ✔
100

101
Doc::~Doc()
427 ✔
102
{
103
    delete m_masterTimer;
242 ✔
104
    m_masterTimer = NULL;
242 ✔
105

106
    clearContents();
242 ✔
107

108
    if (isKiosk() == false)
242 ✔
109
    {
110
        // TODO: is this still needed ??
111
        //m_ioMap->saveDefaults();
112
    }
113
    /* Services use the I/O plugins: unload them first */
114
    delete m_serviceLoader;
242 ✔
115
    m_serviceLoader = NULL;
242 ✔
116

117
    delete m_ioMap;
242 ✔
118
    m_ioMap = NULL;
242 ✔
119

120
    delete m_ioPluginCache;
242 ✔
121
    m_ioPluginCache = NULL;
242 ✔
122

123
    delete m_modifiersCache;
242 ✔
124
    m_modifiersCache = NULL;
242 ✔
125

126
    delete m_fixtureDefCache;
242 ✔
127
    m_fixtureDefCache = NULL;
242 ✔
128

129
    delete m_rgbScriptsCache;
242 ✔
130
    m_rgbScriptsCache = NULL;
242 ✔
131
}
427 ✔
132

133
void Doc::clearContents()
516 ✔
134
{
135
    emit clearing();
516 ✔
136

137
    m_clipboard->resetContents();
516 ✔
138

139
    if (m_monitorProps != NULL)
516 ✔
140
        m_monitorProps->reset();
23 ✔
141

142
    destroyAudioCapture();
516 ✔
143

144
    // Delete all function instances
145
    QListIterator <quint32> funcit(m_functions.keys());
516 ✔
146
    while (funcit.hasNext() == true)
786 ✔
147
    {
148
        Function* func = m_functions.take(funcit.next());
270 ✔
149
        if (func == NULL)
270 ✔
150
            continue;
×
151
        emit functionRemoved(func->id());
270 ✔
152
        delete func;
270 ✔
153
    }
154

155
    // Delete all palettes
156
    QListIterator <quint32> palIt(m_palettes.keys());
516 ✔
157
    while (palIt.hasNext() == true)
518 ✔
158
    {
159
        QLCPalette *palette = m_palettes.take(palIt.next());
2 ✔
160
        emit paletteRemoved(palette->id());
2 ✔
161
        delete palette;
2 ✔
162
    }
163

164
    // Delete all channel groups
165
    QListIterator <quint32> grpchans(m_channelsGroups.keys());
516 ✔
166
    while (grpchans.hasNext() == true)
521 ✔
167
    {
168
        ChannelsGroup* grp = m_channelsGroups.take(grpchans.next());
5 ✔
169
        emit channelsGroupRemoved(grp->id());
5 ✔
170
        delete grp;
5 ✔
171
    }
172

173
    // Delete all fixture groups
174
    QListIterator <quint32> grpit(m_fixtureGroups.keys());
516 ✔
175
    while (grpit.hasNext() == true)
528 ✔
176
    {
177
        FixtureGroup* grp = m_fixtureGroups.take(grpit.next());
12 ✔
178
        quint32 grpID = grp->id();
12 ✔
179
        delete grp;
12 ✔
180
        emit fixtureGroupRemoved(grpID);
12 ✔
181
    }
182

183
    // Delete all fixture instances
184
    QListIterator <quint32> fxit(m_fixtures.keys());
516 ✔
185
    while (fxit.hasNext() == true)
17,282 ✔
186
    {
187
        Fixture* fxi = m_fixtures.take(fxit.next());
16,766 ✔
188
        quint32 fxID = fxi->id();
16,766 ✔
189
        delete fxi;
16,766 ✔
190
        emit fixtureRemoved(fxID);
16,766 ✔
191
    }
192
    m_fixturesListCacheUpToDate = false;
516 ✔
193

194
    m_orderedGroups.clear();
516 ✔
195

196
    m_latestFunctionId = 0;
516 ✔
197
    m_startupFunctionId = Function::invalidId();
516 ✔
198
    m_latestFixtureId = 0;
516 ✔
199
    m_latestFixtureGroupId = 0;
516 ✔
200
    m_latestChannelsGroupId = 0;
516 ✔
201
    m_latestPaletteId = 0;
516 ✔
202
    m_addresses.clear();
516 ✔
203
    m_loadStatus = Cleared;
516 ✔
204

205
    emit cleared();
516 ✔
206
}
516 ✔
207

208
void Doc::setWorkspacePath(QString path)
2 ✔
209
{
210
    m_workspacePath = path;
2 ✔
211
}
2 ✔
212

213
QString Doc::workspacePath() const
102 ✔
214
{
215
    return m_workspacePath;
102 ✔
216
}
217

218
QString Doc::normalizeComponentPath(const QString& filePath) const
5 ✔
219
{
220
    if (filePath.isEmpty())
5 ✔
221
        return filePath;
1 ✔
222

223
    QFileInfo f(filePath);
4 ✔
224

225
    if (f.absolutePath().startsWith(workspacePath()))
4 ✔
226
    {
227
        return QDir(workspacePath()).relativeFilePath(f.absoluteFilePath());
4 ✔
228
    }
229
    else
230
    {
231
        return f.absoluteFilePath();
2 ✔
232
    }
233
}
4 ✔
234

235
QString Doc::denormalizeComponentPath(const QString& filePath) const
4 ✔
236
{
237
    if (filePath.isEmpty())
4 ✔
238
        return filePath;
1 ✔
239

240
    return QFileInfo(QDir(workspacePath()), filePath).absoluteFilePath();
6 ✔
241
}
242

243
/*****************************************************************************
244
 * Engine components
245
 *****************************************************************************/
246

247
QLCFixtureDefCache* Doc::fixtureDefCache() const
218 ✔
248
{
249
    return m_fixtureDefCache;
218 ✔
250
}
251

252
void Doc::setFixtureDefinitionCache(QLCFixtureDefCache *cache)
×
253
{
254
    m_fixtureDefCache = cache;
×
255
}
×
256

257
QLCModifiersCache* Doc::modifiersCache() const
1 ✔
258
{
259
    return m_modifiersCache;
1 ✔
260
}
261

262
RGBScriptsCache* Doc::rgbScriptsCache() const
140 ✔
263
{
264
    return m_rgbScriptsCache;
140 ✔
265
}
266

267
IOPluginCache* Doc::ioPluginCache() const
353 ✔
268
{
269
    return m_ioPluginCache;
353 ✔
270
}
271

272
AudioPluginCache *Doc::audioPluginCache() const
1 ✔
273
{
274
    return m_audioPluginCache;
1 ✔
275
}
276

277
ServiceLoader *Doc::serviceLoader() const
×
278
{
279
    return m_serviceLoader;
×
280
}
281

282
void Doc::slotServiceAvailable(const QString &id)
×
283
{
284
    if (id == QLCRDM_SERVICE_ID)
×
285
    {
286
        QLCRDMService *rdm = m_serviceLoader->service<QLCRDMService>(id);
×
287
        if (rdm == NULL)
×
288
        {
289
            qWarning() << "The" << id << "service doesn't implement the expected interface";
×
290
            return;
×
291
        }
292
        rdm->setTransportProvider(m_ioMap);
×
293
    }
294
}
295

296
InputOutputMap* Doc::inputOutputMap() const
2,788 ✔
297
{
298
    return m_ioMap;
2,788 ✔
299
}
300

301
MasterTimer* Doc::masterTimer() const
1,507 ✔
302
{
303
    return m_masterTimer;
1,507 ✔
304
}
305

306
QSharedPointer<AudioCapture> Doc::audioInputCapture() const
86 ✔
307
{
308
    if (!m_inputCapture)
86 ✔
309
    {
310
        qDebug() << "Creating new audio capture";
13 ✔
311
        m_inputCapture = QSharedPointer<AudioCapture>(
26 ✔
312
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
313
            new AudioCaptureQt5()
314
#else
315
            new AudioCaptureQt6()
13 ✔
316
#endif
317
            );
13 ✔
318
    }
319
    return m_inputCapture;
86 ✔
320
}
321

322
void Doc::destroyAudioCapture()
516 ✔
323
{
324
    if (m_inputCapture.isNull() == false)
516 ✔
325
    {
326
        qDebug() << "Destroying audio capture";
13 ✔
327
        m_inputCapture.clear();
13 ✔
328
    }
329
}
516 ✔
330

331
/*****************************************************************************
332
 * Modified status
333
 *****************************************************************************/
334
Doc::LoadStatus Doc::loadStatus() const
2 ✔
335
{
336
    return m_loadStatus;
2 ✔
337
}
338

339
bool Doc::isModified() const
27 ✔
340
{
341
    return m_modified;
27 ✔
342
}
343

344
void Doc::setModified()
3,454 ✔
345
{
346
    m_modified = true;
3,454 ✔
347
    m_autosaveTimer.start();
3,454 ✔
348
    emit modified(true);
3,454 ✔
349
}
3,454 ✔
350

351
void Doc::resetModified()
255 ✔
352
{
353
    m_modified = false;
255 ✔
354
    m_autosaveTimer.stop();
255 ✔
355
    emit modified(false);
255 ✔
356
}
255 ✔
357

358
/*****************************************************************************
359
 * Main operating mode
360
 *****************************************************************************/
361

362
void Doc::setMode(Doc::Mode mode)
6 ✔
363
{
364
    /* Don't do mode switching twice */
365
    if (m_mode == mode)
6 ✔
366
        return;
3 ✔
367

368
    m_mode = mode;
3 ✔
369

370
    if (mode == Operate)
3 ✔
371
        m_autosaveTimer.stop();
2 ✔
372
    else if (m_modified)
1 ✔
373
        m_autosaveTimer.start();
×
374

375
    // Run startup function
376
    if (m_mode == Operate && m_startupFunctionId != Function::invalidId())
3 ✔
377
    {
378
        Function *func = function(m_startupFunctionId);
×
379
        if (func != NULL)
×
380
        {
381
            qDebug() << Q_FUNC_INFO << "Starting startup function. (" << m_startupFunctionId << ")";
×
382
            func->start(masterTimer(), FunctionParent::master());
×
383
        }
384
        else
385
        {
386
            qWarning() << Q_FUNC_INFO << "Startup function does not exist, erasing. (" << m_startupFunctionId << ")";
×
387
            m_startupFunctionId = Function::invalidId();
×
388
        }
389
    }
390

391
    emit modeChanged(m_mode);
3 ✔
392
}
393

394
Doc::Mode Doc::mode() const
4 ✔
395
{
396
    return m_mode;
4 ✔
397
}
398

399
void Doc::setKiosk(bool state)
1 ✔
400
{
401
    m_kiosk = state;
1 ✔
402
}
1 ✔
403

404
bool Doc::isKiosk() const
244 ✔
405
{
406
    return m_kiosk;
244 ✔
407
}
408

409
/*********************************************************************
410
 * Clipboard
411
 *********************************************************************/
412

413
QLCClipboard *Doc::clipboard()
1 ✔
414
{
415
    return m_clipboard;
1 ✔
416
}
417

418
/*****************************************************************************
419
 * Fixtures
420
 *****************************************************************************/
421

422
quint32 Doc::createFixtureId()
16,766 ✔
423
{
424
    /* This results in an endless loop if there are UINT_MAX-1 fixtures. That,
425
       however, seems a bit unlikely. Are there even 4294967295-1 fixtures in
426
       total in the whole world? */
427
    while (m_fixtures.contains(m_latestFixtureId) == true ||
50,165 ✔
428
           m_latestFixtureId == Fixture::invalidId())
16,766 ✔
429
    {
430
        m_latestFixtureId++;
16,633 ✔
431
    }
432

433
    return m_latestFixtureId;
16,766 ✔
434
}
435

436
bool Doc::addFixture(Fixture* fixture, quint32 id)
395 ✔
437
{
438
    Q_ASSERT(fixture != NULL);
395 ✔
439

440
    quint32 i;
441
    quint32 uni = fixture->lastUniverse();
395 ✔
442

443
    // No ID given, this method can assign one
444
    if (id == Fixture::invalidId())
395 ✔
445
        id = createFixtureId();
382 ✔
446

447
    if (m_fixtures.contains(id) == true || id == Fixture::invalidId())
395 ✔
448
    {
449
        qWarning() << Q_FUNC_INFO << "a fixture with ID" << id << "already exists!";
1 ✔
450
        return false;
1 ✔
451
    }
452

453
    /* Check for overlapping address */
454
    for (i = 0; i < fixture->channels(); i++)
6,751 ✔
455
    {
456
        quint32 absAddress = fixture->channelAddress(i);
6,360 ✔
457
        if (m_addresses.contains(absAddress))
6,360 ✔
458
        {
459
            qWarning() << Q_FUNC_INFO << "fixture" << id << "overlapping with fixture" << m_addresses[absAddress] << "@ channel" << absAddress;
3 ✔
460
            return false;
3 ✔
461
        }
462
    }
463

464
    fixture->setID(id);
391 ✔
465
    m_fixtures.insert(id, fixture);
391 ✔
466
    m_fixturesListCacheUpToDate = false;
391 ✔
467

468
    /* Patch fixture change signals thru Doc */
469
    connect(fixture, SIGNAL(changed(quint32)),
391 ✔
470
            this, SLOT(slotFixtureChanged(quint32)));
471

472
    /* Keep track of fixture addresses */
473
    for (i = 0; i < fixture->channels(); i++)
6,598 ✔
474
        m_addresses[fixture->channelAddress(i)] = id;
6,207 ✔
475

476
    if (uni >= inputOutputMap()->universesCount())
391 ✔
477
    {
478
        for (i = inputOutputMap()->universesCount(); i <= uni; i++)
2 ✔
479
            inputOutputMap()->addUniverse(i);
1 ✔
480
        inputOutputMap()->startUniverses();
1 ✔
481
    }
482

483
    // Add the fixture channels capabilities to the universe they belong
484
    patchFixtureChannels(fixture);
391 ✔
485

486
    emit fixtureAdded(id);
391 ✔
487
    setModified();
391 ✔
488

489
    return true;
391 ✔
490
}
491

492
void Doc::patchFixtureChannels(Fixture *fixture)
395 ✔
493
{
494
    QList<Universe *> universes = inputOutputMap()->claimUniverses();
395 ✔
495

496
    QList<int> forcedHTP = fixture->forcedHTPChannels();
395 ✔
497
    QList<int> forcedLTP = fixture->forcedLTPChannels();
395 ✔
498

499
    for (quint32 i = 0; i < fixture->channels(); i++)
6,615 ✔
500
    {
501
        const QLCChannel *channel(fixture->channel(i));
6,220 ✔
502
        quint32 absAddress = fixture->channelAddress(i);
6,220 ✔
503

504
        // don't touch the channels of another fixture
505
        if (m_addresses.value(absAddress, fixture->id()) != fixture->id())
6,220 ✔
506
            continue;
4 ✔
507

508
        quint32 uni = absAddress >> 9;
6,216 ✔
509
        quint32 addr = absAddress & 0x01FF;
6,216 ✔
510

511
        // the fixture may not fit in the universe (e.g. address too high)
512
        if (uni >= quint32(universes.count()) || addr >= 512)
6,216 ✔
513
            continue;
×
514

515
        // Inform Universe of any HTP/LTP forcing
516
        if (forcedHTP.contains(int(i)))
6,216 ✔
517
            universes.at(uni)->setChannelCapability(addr, channel->group(), Universe::HTP);
×
518
        else if (forcedLTP.contains(int(i)))
6,216 ✔
519
            universes.at(uni)->setChannelCapability(addr, channel->group(), Universe::LTP);
3 ✔
520
        else
521
            universes.at(uni)->setChannelCapability(addr, channel->group());
6,213 ✔
522

523
        // Apply the default value BEFORE modifiers
524
        universes.at(uni)->setChannelDefaultValue(addr, channel->defaultValue());
6,216 ✔
525

526
        // Apply a channel modifier, if defined
527
        ChannelModifier *mod = fixture->channelModifier(i);
6,216 ✔
528
        universes.at(uni)->setChannelModifier(addr, mod);
6,216 ✔
529
    }
530

531
    inputOutputMap()->releaseUniverses(true);
395 ✔
532
}
395 ✔
533

534
void Doc::unpatchChannels(const QList<quint32> &addresses)
13 ✔
535
{
536
    if (addresses.isEmpty())
13 ✔
537
        return;
2 ✔
538

539
    QList<Universe *> universes = inputOutputMap()->claimUniverses();
11 ✔
540

541
    foreach (quint32 address, addresses)
404 ✔
542
    {
543
        // still (or again) used by a fixture
544
        if (m_addresses.contains(address))
393 ✔
545
            continue;
×
546

547
        quint32 uni = address / 512;
393 ✔
548
        if (uni < quint32(universes.count()))
393 ✔
549
            universes.at(uni)->resetChannelCapability(address % 512);
393 ✔
550
    }
11 ✔
551

552
    inputOutputMap()->releaseUniverses(true);
11 ✔
553
}
11 ✔
554

555
QList<quint32> Doc::fixtureAddresses(quint32 id) const
21 ✔
556
{
557
    QList<quint32> addresses;
21 ✔
558

559
    QHashIterator <quint32, quint32> it(m_addresses);
21 ✔
560
    while (it.hasNext() == true)
528 ✔
561
    {
562
        it.next();
507 ✔
563
        if (it.value() == id)
507 ✔
564
            addresses.append(it.key());
414 ✔
565
    }
566

567
    return addresses;
42 ✔
568
}
21 ✔
569

570
bool Doc::deleteFixture(quint32 id)
15 ✔
571
{
572
    if (m_fixtures.contains(id) == true)
15 ✔
573
    {
574
        Fixture* fxi = m_fixtures.take(id);
9 ✔
575
        Q_ASSERT(fxi != NULL);
9 ✔
576
        m_fixturesListCacheUpToDate = false;
9 ✔
577

578
        /* Keep track of fixture addresses */
579
        QList<quint32> oldAddresses = fixtureAddresses(id);
9 ✔
580
        QMutableHashIterator <uint,uint> it(m_addresses);
9 ✔
581
        while (it.hasNext() == true)
450 ✔
582
        {
583
            it.next();
441 ✔
584
            if (it.value() == id)
441 ✔
585
                it.remove();
384 ✔
586
        }
587

588
        /* The channels are free now: remove their capabilities, default
589
         * values and modifiers, which would otherwise keep affecting the output */
590
        unpatchChannels(oldAddresses);
9 ✔
591
        if (m_monitorProps != NULL)
9 ✔
592
            m_monitorProps->removeFixture(id);
×
593

594
        emit fixtureRemoved(id);
9 ✔
595
        setModified();
9 ✔
596
        delete fxi;
9 ✔
597

598
        if (m_fixtures.count() == 0)
9 ✔
599
            m_latestFixtureId = 0;
3 ✔
600

601
        return true;
9 ✔
602
    }
9 ✔
603
    else
604
    {
605
        qWarning() << Q_FUNC_INFO << "No fixture with id" << id;
6 ✔
606
        return false;
6 ✔
607
    }
608
}
609

610
bool Doc::replaceFixtures(QList<Fixture*> newFixturesList)
6 ✔
611
{
612
    // Delete all fixture instances
613
    QListIterator <quint32> fxit(m_fixtures.keys());
6 ✔
614
    while (fxit.hasNext() == true)
14 ✔
615
    {
616
        Fixture* fxi = m_fixtures.take(fxit.next());
8 ✔
617
        disconnect(fxi, SIGNAL(changed(quint32)),
8 ✔
618
                   this, SLOT(slotFixtureChanged(quint32)));
619
        delete fxi;
8 ✔
620
        m_fixturesListCacheUpToDate = false;
8 ✔
621
    }
622
    m_latestFixtureId = 0;
6 ✔
623
    m_addresses.clear();
6 ✔
624

625
    foreach (Fixture *fixture, newFixturesList)
14 ✔
626
    {
627
        quint32 id = fixture->id();
8 ✔
628
        // create a copy of the original cause remapping will
629
        // destroy it later
630
        Fixture *newFixture = new Fixture(this);
8 ✔
631
        newFixture->setID(id);
8 ✔
632
        newFixture->setName(fixture->name());
8 ✔
633
        newFixture->setAddress(fixture->address());
8 ✔
634
        newFixture->setUniverse(fixture->universe());
8 ✔
635
        newFixture->setCrossUniverse(fixture->crossUniverse());
8 ✔
636

637
        if (fixture->fixtureDef() == NULL ||
18 ✔
638
            (fixture->fixtureDef()->manufacturer() == KXMLFixtureGeneric &&
18 ✔
639
             fixture->fixtureDef()->model() == KXMLFixtureGeneric))
10 ✔
640
        {
641
            // Generic dimmers just need to know the number of channels
642
            newFixture->setChannels(fixture->channels());
2 ✔
643
        }
644
        else if (fixture->fixtureDef() == NULL ||
12 ✔
645
            (fixture->fixtureDef()->manufacturer() == KXMLFixtureGeneric &&
12 ✔
646
             fixture->fixtureDef()->model() == KXMLFixtureRGBPanel))
6 ✔
647
        {
648
            // RGB Panels definitions are not cached or shared, so
649
            // let's make a deep copy of them
650
            QLCFixtureDef *fixtureDef = new QLCFixtureDef();
×
651
            *fixtureDef = *fixture->fixtureDef();
×
652
            QLCFixtureMode *mode = new QLCFixtureMode(fixtureDef);
×
653
            *mode = *fixture->fixtureMode();
×
654
            newFixture->setFixtureDefinition(fixtureDef, mode);
×
655
        }
656
        else
657
        {
658
            QLCFixtureDef *def = fixtureDefCache()->fixtureDef(fixture->fixtureDef()->manufacturer(),
12 ✔
659
                                                               fixture->fixtureDef()->model());
12 ✔
660
            QLCFixtureMode *mode = NULL;
6 ✔
661
            if (def != NULL)
6 ✔
662
                mode = def->mode(fixture->fixtureMode()->name());
1 ✔
663
            newFixture->setFixtureDefinition(def, mode);
6 ✔
664
        }
665

666
        newFixture->setExcludeFadeChannels(fixture->excludeFadeChannels());
8 ✔
667
        newFixture->setForcedHTPChannels(fixture->forcedHTPChannels());
8 ✔
668
        newFixture->setForcedLTPChannels(fixture->forcedLTPChannels());
8 ✔
669

670
        for (quint32 s = 0; s < fixture->channels(); s++)
35 ✔
671
        {
672
            ChannelModifier *chMod = fixture->channelModifier(s);
27 ✔
673
            if (chMod != NULL)
27 ✔
674
                newFixture->setChannelModifier(s, chMod);
×
675
        }
676

677
        m_fixtures.insert(id, newFixture);
8 ✔
678
        m_fixturesListCacheUpToDate = false;
8 ✔
679

680
        /* Patch fixture change signals thru Doc */
681
        connect(newFixture, SIGNAL(changed(quint32)),
8 ✔
682
                this, SLOT(slotFixtureChanged(quint32)));
683

684
        /* Keep track of fixture addresses */
685
        for (quint32 i = 0; i < newFixture->channels(); i++)
20 ✔
686
            m_addresses[newFixture->channelAddress(i)] = id;
12 ✔
687
        m_latestFixtureId = id;
8 ✔
688
    }
6 ✔
689
    return true;
6 ✔
690
}
6 ✔
691

692
bool Doc::updateFixtureChannelCapabilities(quint32 id, QList<int> forcedHTP, QList<int> forcedLTP)
2 ✔
693
{
694
    if (m_fixtures.contains(id) == false)
2 ✔
695
        return false;
1 ✔
696

697
    Fixture* fixture = m_fixtures[id];
1 ✔
698
    // get exclusive access to the universes list
699
    QList<Universe *> universes = inputOutputMap()->claimUniverses();
1 ✔
700

701
    // Set forced HTP channels
702
    fixture->setForcedHTPChannels(forcedHTP);
1 ✔
703

704
    // Set forced LTP channels
705
    fixture->setForcedLTPChannels(forcedLTP);
1 ✔
706

707
    // Update the Fixture Universe with the current channel states
708
    for (quint32 i = 0 ; i < fixture->channels(); i++)
7 ✔
709
    {
710
        const QLCChannel *channel(fixture->channel(i));
6 ✔
711

712
        // a cross-universe fixture may span multiple universes
713
        quint32 absAddress = fixture->channelAddress(i);
6 ✔
714
        quint32 uniIdx = absAddress >> 9;
6 ✔
715
        if (int(uniIdx) >= universes.count())
6 ✔
716
            continue;
×
717

718
        Universe *universe = universes.at(uniIdx);
6 ✔
719
        quint32 addr = absAddress & 0x01FF;
6 ✔
720

721
        // Inform Universe of any HTP/LTP forcing
722
        if (forcedHTP.contains(int(i)))
6 ✔
723
            universe->setChannelCapability(addr, channel->group(), Universe::HTP);
2 ✔
724
        else if (forcedLTP.contains(int(i)))
4 ✔
725
            universe->setChannelCapability(addr, channel->group(), Universe::LTP);
1 ✔
726
        else
727
            universe->setChannelCapability(addr, channel->group());
3 ✔
728

729
        // Apply the default value BEFORE modifiers
730
        universe->setChannelDefaultValue(addr, channel->defaultValue());
6 ✔
731

732
        // Apply a channel modifier, if defined
733
        ChannelModifier *mod = fixture->channelModifier(i);
6 ✔
734
        universe->setChannelModifier(addr, mod);
6 ✔
735
    }
736

737
    inputOutputMap()->releaseUniverses(true);
1 ✔
738

739
    return true;
1 ✔
740
}
1 ✔
741

742
QList<Fixture*> const& Doc::fixtures() const
57 ✔
743
{
744
    if (!m_fixturesListCacheUpToDate)
57 ✔
745
    {
746
        // Sort fixtures by id
747
        QMap <quint32, Fixture*> fixturesMap;
50 ✔
748
        QHashIterator <quint32, Fixture*> hashIt(m_fixtures);
50 ✔
749
        while (hashIt.hasNext())
113 ✔
750
        {
751
            hashIt.next();
63 ✔
752
            fixturesMap.insert(hashIt.key(), hashIt.value());
63 ✔
753
        }
754
        const_cast<QList<Fixture*>&>(m_fixturesListCache) = fixturesMap.values();
50 ✔
755
        const_cast<bool&>(m_fixturesListCacheUpToDate) = true;
50 ✔
756
    }
50 ✔
757
    return m_fixturesListCache;
57 ✔
758
}
759

760
int Doc::fixturesCount() const
×
761
{
762
    return m_fixtures.count();
×
763
}
764

765
Fixture* Doc::fixture(quint32 id) const
61,594 ✔
766
{
767
    return m_fixtures.value(id, NULL);
61,594 ✔
768
}
769

770
quint32 Doc::fixtureForAddress(quint32 universeAddress) const
60,047 ✔
771
{
772
    return m_addresses.value(universeAddress, Fixture::invalidId());
60,047 ✔
773
}
774

775
int Doc::totalPowerConsumption(int& fuzzy) const
4 ✔
776
{
777
    int totalPowerConsumption = 0;
4 ✔
778

779
    // Make sure fuzzy starts from zero
780
    fuzzy = 0;
4 ✔
781

782
    QListIterator <Fixture*> fxit(fixtures());
4 ✔
783
    while (fxit.hasNext() == true)
14 ✔
784
    {
785
        Fixture* fxi(fxit.next());
10 ✔
786
        Q_ASSERT(fxi != NULL);
10 ✔
787

788
        if (fxi->fixtureMode() != NULL)
10 ✔
789
        {
790
            QLCPhysical phys = fxi->fixtureMode()->physical();
10 ✔
791
            if (phys.powerConsumption() > 0)
10 ✔
792
                totalPowerConsumption += phys.powerConsumption();
7 ✔
793
            else
794
                fuzzy++;
3 ✔
795
        }
10 ✔
796
        else
797
        {
798
            fuzzy++;
×
799
        }
800
    }
801

802
    return totalPowerConsumption;
4 ✔
803
}
4 ✔
804

805
void Doc::slotFixtureChanged(quint32 id)
6 ✔
806
{
807
    /* Keep track of fixture addresses */
808
    Fixture* fxi = fixture(id);
6 ✔
809
    QList<quint32> oldAddresses = fixtureAddresses(id);
6 ✔
810

811
    // remove it
812
    QMutableHashIterator <uint,uint> it(m_addresses);
6 ✔
813
    while (it.hasNext() == true)
39 ✔
814
    {
815
        it.next();
33 ✔
816
        if (it.value() == id)
33 ✔
817
        {
818
            qDebug() << Q_FUNC_INFO << " remove: " << it.key() << " val: " << it.value();
15 ✔
819
            it.remove();
15 ✔
820
        }
821
    }
822

823
    for (quint32 ch = 0; ch < fxi->channels(); ch++)
25 ✔
824
    {
825
        /*
826
         * setting new universe and address calls this twice,
827
         * with an tmp wrong address after the first call (old address() + new universe()).
828
         * we only add if the channel is free, to prevent messing up things
829
         */
830
        quint32 i = fxi->channelAddress(ch);
19 ✔
831
        if (m_addresses.contains(i))
19 ✔
832
            continue;
4 ✔
833

834
        m_addresses[i] = id;
15 ✔
835
    }
836

837
    /* If the fixture has been moved (or its channels count changed), the
838
     * universes must follow: free the channels it left and set up the ones
839
     * it now occupies. Other changes (e.g. the name) must not touch them,
840
     * since setting up the channels also resets them to their default value */
841
    QList<quint32> newAddresses = fixtureAddresses(id);
6 ✔
842
    std::sort(oldAddresses.begin(), oldAddresses.end());
6 ✔
843
    std::sort(newAddresses.begin(), newAddresses.end());
6 ✔
844
    if (oldAddresses != newAddresses)
6 ✔
845
    {
846
        QList<quint32> freedAddresses;
4 ✔
847
        foreach (quint32 address, oldAddresses)
13 ✔
848
        {
849
            if (newAddresses.contains(address) == false)
9 ✔
850
                freedAddresses.append(address);
9 ✔
851
        }
4 ✔
852
        unpatchChannels(freedAddresses);
4 ✔
853
        patchFixtureChannels(fxi);
4 ✔
854
    }
4 ✔
855

856
    setModified();
6 ✔
857
    emit fixtureChanged(id);
6 ✔
858
}
6 ✔
859

860
/*****************************************************************************
861
 * Fixture groups
862
 *****************************************************************************/
863

864
bool Doc::addFixtureGroup(FixtureGroup* grp, quint32 id)
17 ✔
865
{
866
    Q_ASSERT(grp != NULL);
17 ✔
867

868
    // No ID given, this method can assign one
869
    if (id == FixtureGroup::invalidId())
17 ✔
870
        id = createFixtureGroupId();
10 ✔
871

872
    if (m_fixtureGroups.contains(id) == true || id == FixtureGroup::invalidId())
17 ✔
873
    {
874
        qWarning() << Q_FUNC_INFO << "a fixture group with ID" << id << "already exists!";
2 ✔
875
        return false;
2 ✔
876
    }
877
    else
878
    {
879
        grp->setId(id);
15 ✔
880
        m_fixtureGroups[id] = grp;
15 ✔
881

882
        /* Patch fixture group change signals thru Doc */
883
        connect(grp, SIGNAL(changed(quint32)),
15 ✔
884
                this, SLOT(slotFixtureGroupChanged(quint32)));
885

886
        emit fixtureGroupAdded(id);
15 ✔
887
        setModified();
15 ✔
888

889
        return true;
15 ✔
890
    }
891
}
892

893
bool Doc::deleteFixtureGroup(quint32 id)
5 ✔
894
{
895
    if (m_fixtureGroups.contains(id) == true)
5 ✔
896
    {
897
        FixtureGroup* grp = m_fixtureGroups.take(id);
3 ✔
898
        Q_ASSERT(grp != NULL);
3 ✔
899

900
        emit fixtureGroupRemoved(id);
3 ✔
901
        setModified();
3 ✔
902
        delete grp;
3 ✔
903

904
        return true;
3 ✔
905
    }
906
    else
907
    {
908
        qWarning() << Q_FUNC_INFO << "No fixture group with id" << id;
2 ✔
909
        return false;
2 ✔
910
    }
911
}
912

913
FixtureGroup* Doc::fixtureGroup(quint32 id) const
61 ✔
914
{
915
    return m_fixtureGroups.value(id, NULL);
61 ✔
916
}
917

918
QList <FixtureGroup*> Doc::fixtureGroups() const
47 ✔
919
{
920
    return m_fixtureGroups.values();
47 ✔
921
}
922

923
quint32 Doc::createFixtureGroupId()
10 ✔
924
{
925
    /* This results in an endless loop if there are UINT_MAX-1 fixture groups. That,
926
       however, seems a bit unlikely. Are there even 4294967295-1 fixtures in
927
       total in the whole world? */
928
    while (m_fixtureGroups.contains(m_latestFixtureGroupId) == true ||
25 ✔
929
           m_latestFixtureGroupId == FixtureGroup::invalidId())
10 ✔
930
    {
931
        m_latestFixtureGroupId++;
5 ✔
932
    }
933

934
    return m_latestFixtureGroupId;
10 ✔
935
}
936

937
void Doc::slotFixtureGroupChanged(quint32 id)
28 ✔
938
{
939
    setModified();
28 ✔
940
    emit fixtureGroupChanged(id);
28 ✔
941
}
28 ✔
942

943
/*********************************************************************
944
 * Channels groups
945
 *********************************************************************/
946
bool Doc::addChannelsGroup(ChannelsGroup *grp, quint32 id)
6 ✔
947
{
948
    Q_ASSERT(grp != NULL);
6 ✔
949

950
    // No ID given, this method can assign one
951
    if (id == ChannelsGroup::invalidId())
6 ✔
952
        id = createChannelsGroupId();
5 ✔
953

954
     grp->setId(id);
6 ✔
955
     m_channelsGroups[id] = grp;
6 ✔
956
     if (m_orderedGroups.contains(id) == false)
6 ✔
957
        m_orderedGroups.append(id);
6 ✔
958

959
     emit channelsGroupAdded(id);
6 ✔
960
     setModified();
6 ✔
961

962
     return true;
6 ✔
963
}
964

965
bool Doc::deleteChannelsGroup(quint32 id)
2 ✔
966
{
967
    if (m_channelsGroups.contains(id) == true)
2 ✔
968
    {
969
        ChannelsGroup* grp = m_channelsGroups.take(id);
1 ✔
970
        Q_ASSERT(grp != NULL);
1 ✔
971

972
        emit channelsGroupRemoved(id);
1 ✔
973
        setModified();
1 ✔
974
        delete grp;
1 ✔
975

976
        int idx = m_orderedGroups.indexOf(id);
1 ✔
977
        if (idx != -1)
1 ✔
978
            m_orderedGroups.takeAt(idx);
1 ✔
979

980
        return true;
1 ✔
981
    }
982
    else
983
    {
984
        qWarning() << Q_FUNC_INFO << "No channels group with id" << id;
1 ✔
985
        return false;
1 ✔
986
    }
987
}
988

989
bool Doc::moveChannelGroup(quint32 id, int direction)
3 ✔
990
{
991
    if (direction == 0 || m_orderedGroups.contains(id) == false)
3 ✔
992
        return false;
1 ✔
993

994
    int idx = m_orderedGroups.indexOf(id);
2 ✔
995

996
    if (idx + direction < 0 || idx + direction >= m_orderedGroups.count())
2 ✔
997
        return false;
1 ✔
998

999
    qDebug() << Q_FUNC_INFO << m_orderedGroups;
1 ✔
1000
    m_orderedGroups.takeAt(idx);
1 ✔
1001
    m_orderedGroups.insert(idx + direction, id);
1 ✔
1002
    qDebug() << Q_FUNC_INFO << m_orderedGroups;
1 ✔
1003

1004
    setModified();
1 ✔
1005
    return true;
1 ✔
1006
}
1007

1008
ChannelsGroup* Doc::channelsGroup(quint32 id) const
2 ✔
1009
{
1010
    return m_channelsGroups.value(id, NULL);
2 ✔
1011
}
1012

1013
QList <ChannelsGroup*> Doc::channelsGroups() const
23 ✔
1014
{
1015
    QList <ChannelsGroup*> orderedList;
23 ✔
1016

1017
    for (int i = 0; i < m_orderedGroups.count(); i++)
48 ✔
1018
    {
1019
        orderedList.append(m_channelsGroups[m_orderedGroups.at(i)]);
25 ✔
1020
    }
1021
    return orderedList;
23 ✔
1022
}
×
1023

1024
quint32 Doc::createChannelsGroupId()
5 ✔
1025
{
1026
    while (m_channelsGroups.contains(m_latestChannelsGroupId) == true ||
12 ✔
1027
           m_latestChannelsGroupId == ChannelsGroup::invalidId())
5 ✔
1028
    {
1029
        m_latestChannelsGroupId++;
2 ✔
1030
    }
1031

1032
    return m_latestChannelsGroupId;
5 ✔
1033
}
1034

1035
/*********************************************************************
1036
 * Palettes
1037
 *********************************************************************/
1038

1039
bool Doc::addPalette(QLCPalette *palette, quint32 id)
5 ✔
1040
{
1041
    Q_ASSERT(palette != NULL);
5 ✔
1042

1043
    // No ID given, this method can assign one
1044
    if (id == QLCPalette::invalidId())
5 ✔
1045
        id = createPaletteId();
4 ✔
1046

1047
    if (m_palettes.contains(id) == true || id == QLCPalette::invalidId())
5 ✔
1048
    {
1049
        qWarning() << Q_FUNC_INFO << "a palette with ID" << id << "already exists!";
×
1050
        return false;
×
1051
    }
1052
    else
1053
    {
1054
        palette->setID(id);
5 ✔
1055
        palette->setTemporary(false);
5 ✔
1056
        m_palettes[id] = palette;
5 ✔
1057

1058
        emit paletteAdded(id);
5 ✔
1059
        setModified();
5 ✔
1060
    }
1061

1062
    return true;
5 ✔
1063
}
1064

1065
bool Doc::deletePalette(quint32 id)
4 ✔
1066
{
1067
    if (m_palettes.contains(id) == true)
4 ✔
1068
    {
1069
        QLCPalette *palette = m_palettes.take(id);
3 ✔
1070
        Q_ASSERT(palette != NULL);
3 ✔
1071

1072
        // remove the palette from the Scenes referencing it,
1073
        // otherwise a new palette might reuse its ID
1074
        foreach (Function *function, m_functions)
5 ✔
1075
        {
1076
            Scene *scene = qobject_cast<Scene *>(function);
2 ✔
1077
            if (scene != NULL)
2 ✔
1078
                scene->removePalette(id);
2 ✔
1079
        }
3 ✔
1080

1081
        emit paletteRemoved(id);
3 ✔
1082
        setModified();
3 ✔
1083
        delete palette;
3 ✔
1084

1085
        return true;
3 ✔
1086
    }
1087
    else
1088
    {
1089
        qWarning() << Q_FUNC_INFO << "No palette with id" << id;
1 ✔
1090
        return false;
1 ✔
1091
    }
1092
}
1093

1094
QLCPalette *Doc::palette(quint32 id) const
4 ✔
1095
{
1096
    return m_palettes.value(id, NULL);
4 ✔
1097
}
1098

1099
QList<QLCPalette *> Doc::palettes() const
8 ✔
1100
{
1101
    return m_palettes.values();
8 ✔
1102
}
1103

1104
quint32 Doc::createPaletteId()
4 ✔
1105
{
1106
    while (m_palettes.contains(m_latestPaletteId) == true ||
10 ✔
1107
           m_latestPaletteId == FixtureGroup::invalidId())
4 ✔
1108
    {
1109
        m_latestPaletteId++;
2 ✔
1110
    }
1111

1112
    return m_latestPaletteId;
4 ✔
1113
}
1114

1115
/*****************************************************************************
1116
 * Functions
1117
 *****************************************************************************/
1118

1119
quint32 Doc::createFunctionId()
262 ✔
1120
{
1121
    /* This results in an endless loop if there are UINT_MAX-1 functions. That,
1122
       however, seems a bit unlikely. Are there even 4294967295-1 functions in
1123
       total in the whole world? */
1124
    while (m_functions.contains(m_latestFunctionId) == true ||
681 ✔
1125
           m_latestFunctionId == Fixture::invalidId())
262 ✔
1126
    {
1127
        m_latestFunctionId++;
157 ✔
1128
    }
1129

1130
    return m_latestFunctionId;
262 ✔
1131
}
1132

1133
bool Doc::addFunction(Function* func, quint32 id)
278 ✔
1134
{
1135
    Q_ASSERT(func != NULL);
278 ✔
1136

1137
    if (id == Function::invalidId())
278 ✔
1138
        id = createFunctionId();
262 ✔
1139

1140
    if (m_functions.contains(id) == true || id == Fixture::invalidId())
278 ✔
1141
    {
1142
        qWarning() << Q_FUNC_INFO << "a function with ID" << id << "already exists!";
2 ✔
1143
        return false;
2 ✔
1144
    }
1145
    else
1146
    {
1147
        // Listen to function changes
1148
        connect(func, SIGNAL(changed(quint32)),
276 ✔
1149
                this, SLOT(slotFunctionChanged(quint32)));
1150

1151
        // Listen to function name changes
1152
        connect(func, SIGNAL(nameChanged(quint32)),
276 ✔
1153
                this, SLOT(slotFunctionNameChanged(quint32)));
1154

1155
        // Make the function listen to fixture removals
1156
        connect(this, SIGNAL(fixtureRemoved(quint32)),
276 ✔
1157
                func, SLOT(slotFixtureRemoved(quint32)));
1158

1159
        // Place the function in the map and assign it the new ID
1160
        m_functions[id] = func;
276 ✔
1161
        func->setID(id);
276 ✔
1162
        emit functionAdded(id);
276 ✔
1163
        setModified();
276 ✔
1164

1165
        return true;
276 ✔
1166
    }
1167
}
1168

1169
QList <Function*> Doc::functions() const
58 ✔
1170
{
1171
    return m_functions.values();
58 ✔
1172
}
1173

1174
QList<Function *> Doc::functionsByType(Function::Type type) const
1 ✔
1175
{
1176
    QList <Function*> list;
1 ✔
1177
    foreach (Function *f, m_functions)
10 ✔
1178
    {
1179
        if (f != NULL && f->type() == type)
9 ✔
1180
            list.append(f);
5 ✔
1181
    }
1 ✔
1182
    return list;
1 ✔
1183
}
×
1184

1185
Function *Doc::functionByName(QString name)
×
1186
{
1187
    foreach (Function *f, m_functions)
×
1188
    {
1189
        if (f != NULL && f->name() == name)
×
1190
            return f;
×
1191
    }
×
1192
    return NULL;
×
1193
}
1194

1195
bool Doc::deleteFunction(quint32 id)
8 ✔
1196
{
1197
    if (m_functions.contains(id) == true)
8 ✔
1198
    {
1199
        Function* func = m_functions.take(id);
6 ✔
1200
        Q_ASSERT(func != NULL);
6 ✔
1201

1202
        if (m_startupFunctionId == id)
6 ✔
1203
            m_startupFunctionId = Function::invalidId();
×
1204

1205
        emit functionRemoved(id);
6 ✔
1206
        setModified();
6 ✔
1207
        delete func;
6 ✔
1208

1209
        return true;
6 ✔
1210
    }
1211
    else
1212
    {
1213
        qWarning() << Q_FUNC_INFO << "No function with id" << id;
2 ✔
1214
        return false;
2 ✔
1215
    }
1216
}
1217

1218
Function* Doc::function(quint32 id) const
927 ✔
1219
{
1220
    return m_functions.value(id, NULL);
927 ✔
1221
}
1222

1223
quint32 Doc::nextFunctionID() const
2 ✔
1224
{
1225
    quint32 tmpFID = m_latestFunctionId;
2 ✔
1226
    while (m_functions.contains(tmpFID) == true ||
5 ✔
1227
           tmpFID == Fixture::invalidId())
2 ✔
1228
    {
1229
        tmpFID++;
1 ✔
1230
    }
1231

1232
    return tmpFID;
2 ✔
1233
}
1234

1235
void Doc::setStartupFunction(quint32 fid)
1 ✔
1236
{
1237
    m_startupFunctionId = fid;
1 ✔
1238
}
1 ✔
1239

1240
quint32 Doc::startupFunction() const
2 ✔
1241
{
1242
    return m_startupFunctionId;
2 ✔
1243
}
1244

1245
QList<quint32> Doc::getUsage(quint32 fid)
7 ✔
1246
{
1247
    QList<quint32> usageList;
7 ✔
1248

1249
    foreach (Function *f, m_functions)
70 ✔
1250
    {
1251
        if (f->id() == fid)
63 ✔
1252
            continue;
6 ✔
1253

1254
        switch(f->type())
57 ✔
1255
        {
1256
            case Function::CollectionType:
7 ✔
1257
            {
1258
                Collection *c = qobject_cast<Collection *>(f);
7 ✔
1259
                int pos = c->functions().indexOf(fid);
7 ✔
1260
                if (pos != -1)
7 ✔
1261
                {
1262
                    usageList.append(f->id());
2 ✔
1263
                    usageList.append(pos);
2 ✔
1264
                }
1265
            }
1266
            break;
7 ✔
1267
            case Function::ChaserType:
6 ✔
1268

1269
            {
1270
                Chaser *c = qobject_cast<Chaser *>(f);
6 ✔
1271
                for (int i = 0; i < c->stepsCount(); i++)
18 ✔
1272
                {
1273
                    ChaserStep *cs = c->stepAt(i);
12 ✔
1274
                    if (cs->fid == fid)
12 ✔
1275
                    {
1276
                        usageList.append(f->id());
2 ✔
1277
                        usageList.append(i);
2 ✔
1278
                    }
1279
                }
1280
            }
1281
            break;
6 ✔
1282
            case Function::SequenceType:
7 ✔
1283
            {
1284
                Sequence *s = qobject_cast<Sequence *>(f);
7 ✔
1285
                if (s->boundSceneID() == fid)
7 ✔
1286
                {
1287
                    usageList.append(f->id());
1 ✔
1288
                    usageList.append(0);
1 ✔
1289
                }
1290
            }
1291
            break;
7 ✔
1292
            case Function::ScriptType:
7 ✔
1293
            {
1294
                Script *s = qobject_cast<Script *>(f);
7 ✔
1295
                QList<quint32> l = s->functionList();
7 ✔
1296
                for (int i = 0; i < l.count(); i+=2)
14 ✔
1297
                {
1298
                    if (l.at(i) == fid)
7 ✔
1299
                    {
1300
                        if (i + 1 >= l.count()) {
1 ✔
1301
                            qDebug() << "Doc::getUsage: Index entry missing on " << f->name();
×
1302
                            break;
×
1303
                        }
1304
                        usageList.append(s->id());
1 ✔
1305
                        usageList.append(l.at(i + 1)); // line number
1 ✔
1306
                    }
1307
                }
1308
            }
7 ✔
1309
            break;
7 ✔
1310
            case Function::ShowType:
×
1311
            {
1312
                Show *s = qobject_cast<Show *>(f);
×
1313
                foreach (Track *t, s->tracks())
×
1314
                {
1315
                    foreach (ShowFunction *sf, t->showFunctions())
×
1316
                    {
1317
                        if (sf->functionID() == fid)
×
1318
                        {
1319
                            usageList.append(f->id());
×
1320
                            usageList.append(t->id());
×
1321
                        }
1322
                    }
×
1323
                }
×
1324
            }
1325
            break;
×
1326
            default:
30 ✔
1327
            break;
30 ✔
1328
        }
1329
    }
7 ✔
1330

1331
    return usageList;
7 ✔
1332
}
×
1333

1334
void Doc::slotFunctionChanged(quint32 fid)
67 ✔
1335
{
1336
    setModified();
67 ✔
1337
    emit functionChanged(fid);
67 ✔
1338
}
67 ✔
1339

1340
void Doc::slotFunctionNameChanged(quint32 fid)
2 ✔
1341
{
1342
    setModified();
2 ✔
1343
    emit functionNameChanged(fid);
2 ✔
1344
}
2 ✔
1345

1346
/*********************************************************************
1347
 * Monitor Properties
1348
 *********************************************************************/
1349

1350
MonitorProperties *Doc::monitorProperties()
44 ✔
1351
{
1352
    if (m_monitorProps == NULL)
44 ✔
1353
        m_monitorProps = new MonitorProperties();
10 ✔
1354

1355
    return m_monitorProps;
44 ✔
1356
}
1357

1358
/*****************************************************************************
1359
 * Load & Save
1360
 *****************************************************************************/
1361

1362
bool Doc::loadXML(QXmlStreamReader &doc, bool loadIO)
2 ✔
1363
{
1364
    clearErrorLog();
2 ✔
1365

1366
    if (doc.name() != KXMLQLCEngine)
2 ✔
1367
    {
1368
        qWarning() << Q_FUNC_INFO << "Engine node not found";
1 ✔
1369
        return false;
1 ✔
1370
    }
1371

1372
    m_loadStatus = Loading;
1 ✔
1373
    emit loading();
1 ✔
1374

1375
    if (doc.attributes().hasAttribute(KXMLQLCStartupFunction))
2 ✔
1376
    {
1377
        quint32 sID = doc.attributes().value(KXMLQLCStartupFunction).toString().toUInt();
×
1378
        if (sID != Function::invalidId())
×
1379
            setStartupFunction(sID);
×
1380
    }
1381

1382
    while (doc.readNextStartElement())
17 ✔
1383
    {
1384
        //qDebug() << "Doc tag:" << doc.name();
1385
        if (doc.name() == KXMLFixture)
16 ✔
1386
        {
1387
            Fixture::loader(doc, this);
3 ✔
1388
        }
1389
        else if (doc.name() == KXMLQLCFixtureGroup)
13 ✔
1390
        {
1391
            FixtureGroup::loader(doc, this);
3 ✔
1392
        }
1393
        else if (doc.name() == KXMLQLCChannelsGroup)
10 ✔
1394
        {
1395
            ChannelsGroup::loader(doc, this);
×
1396
        }
1397
        else if (doc.name() == KXMLQLCPalette)
10 ✔
1398
        {
1399
            QLCPalette::loader(doc, this);
×
1400
            doc.skipCurrentElement();
×
1401
        }
1402
        else if (doc.name() == KXMLQLCFunction)
10 ✔
1403
        {
1404
            //qDebug() << doc.attributes().value("Name").toString();
1405
            Function::loader(doc, this);
4 ✔
1406
        }
1407
        else if (doc.name() == KXMLQLCBus)
6 ✔
1408
        {
1409
            /* LEGACY */
1410
            Bus::instance()->loadXML(doc);
5 ✔
1411
        }
1412
        else if (doc.name() == KXMLIOMap && loadIO)
2 ✔
1413
        {
1414
            m_ioMap->loadXML(doc);
×
1415
        }
1416
        else if (doc.name() == KXMLQLCMonitorProperties)
1 ✔
1417
        {
1418
            monitorProperties()->loadXML(doc, this);
×
1419
        }
1420
        else
1421
        {
1422
            qWarning() << Q_FUNC_INFO << "Unknown engine tag:" << doc.name();
1 ✔
1423
            doc.skipCurrentElement();
1 ✔
1424
        }
1425
    }
1426

1427
    postLoad();
1 ✔
1428

1429
    m_loadStatus = Loaded;
1 ✔
1430
    emit loaded();
1 ✔
1431

1432
    return true;
1 ✔
1433
}
1434

1435
bool Doc::saveXML(QXmlStreamWriter *doc) const
1 ✔
1436
{
1437
    Q_ASSERT(doc != NULL);
1 ✔
1438

1439
    /* Create the master Engine node */
1440
    doc->writeStartElement(KXMLQLCEngine);
2 ✔
1441

1442
    if (startupFunction() != Function::invalidId())
1 ✔
1443
        doc->writeAttribute(KXMLQLCStartupFunction, QString::number(startupFunction()));
×
1444

1445
    m_ioMap->saveXML(doc);
1 ✔
1446

1447
    /* Write fixtures into an XML document */
1448
    QListIterator <Fixture*> fxit(fixtures());
1 ✔
1449
    while (fxit.hasNext() == true)
4 ✔
1450
    {
1451
        Fixture *fxi(fxit.next());
3 ✔
1452
        if (fxi != NULL)
3 ✔
1453
            fxi->saveXML(doc);
3 ✔
1454
    }
1455

1456
    /* Write fixture groups into an XML document */
1457
    QListIterator <FixtureGroup*> grpit(fixtureGroups());
1 ✔
1458
    while (grpit.hasNext() == true)
3 ✔
1459
    {
1460
        FixtureGroup *grp(grpit.next());
2 ✔
1461
        if (grp != NULL)
2 ✔
1462
            grp->saveXML(doc);
2 ✔
1463
    }
1464

1465
    /* Write channel groups into an XML document */
1466
    QListIterator <ChannelsGroup*> chanGroups(channelsGroups());
1 ✔
1467
    while (chanGroups.hasNext() == true)
1 ✔
1468
    {
1469
        ChannelsGroup *grp(chanGroups.next());
×
1470
        if (grp != NULL)
×
1471
            grp->saveXML(doc);
×
1472
    }
1473

1474
    /* Write palettes into an XML document */
1475
    QListIterator <QLCPalette*> paletteIt(palettes());
1 ✔
1476
    while (paletteIt.hasNext() == true)
1 ✔
1477
    {
1478
        QLCPalette *palette(paletteIt.next());
×
1479
        if (palette != NULL)
×
1480
            palette->saveXML(doc);
×
1481
    }
1482

1483
    /* Write functions into an XML document */
1484
    QListIterator <Function*> funcit(functions());
1 ✔
1485
    while (funcit.hasNext() == true)
5 ✔
1486
    {
1487
        Function *func(funcit.next());
4 ✔
1488
        if (func != NULL)
4 ✔
1489
            func->saveXML(doc);
4 ✔
1490
    }
1491

1492
    if (m_monitorProps != NULL)
1 ✔
1493
        m_monitorProps->saveXML(doc, this);
1 ✔
1494

1495
    /* End the <Engine> tag */
1496
    doc->writeEndElement();
1 ✔
1497

1498
    return true;
1 ✔
1499
}
1 ✔
1500

1501
void Doc::appendToErrorLog(QString error)
8 ✔
1502
{
1503
    if (m_errorLog.contains(error))
8 ✔
1504
        return;
5 ✔
1505

1506
    m_errorLog.append(error);
3 ✔
1507
    m_errorLog.append("<br>");
3 ✔
1508
}
1509

1510
void Doc::clearErrorLog()
2 ✔
1511
{
1512
    m_errorLog = "";
2 ✔
1513
}
2 ✔
1514

1515
QString Doc::errorLog() const
1 ✔
1516
{
1517
    return m_errorLog;
1 ✔
1518
}
1519

1520
void Doc::postLoad()
1 ✔
1521
{
1522
    QListIterator <Function*> functionit(functions());
1 ✔
1523
    while (functionit.hasNext() == true)
5 ✔
1524
    {
1525
        Function* function(functionit.next());
4 ✔
1526
        Q_ASSERT(function != NULL);
4 ✔
1527
        function->postLoad();
4 ✔
1528
    }
1529
}
1 ✔
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