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

mcallegari / qlcplus / 44419295440

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

push

github

mcallegari
engine/test: add missing file

18252 of 29456 relevant lines covered (61.96%)

62667.01 hits per line

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

90.21
/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
#include "qlclinkservice.h"
38

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

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

62
#define AUTOSAVE_TIMEOUT    30 // seconds
63

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

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

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

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

107
    clearContents();
242 ✔
108

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

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

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

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

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

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

134
void Doc::clearContents()
528 ✔
135
{
136
    emit clearing();
528 ✔
137

138
    m_clipboard->resetContents();
528 ✔
139

140
    if (m_monitorProps != NULL)
528 ✔
141
        m_monitorProps->reset();
23 ✔
142

143
    destroyAudioCapture();
528 ✔
144

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

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

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

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

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

195
    m_orderedGroups.clear();
528 ✔
196

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

206
    emit cleared();
528 ✔
207
}
528 ✔
208

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

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

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

224
    QFileInfo f(filePath);
4 ✔
225

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

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

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

244
/*****************************************************************************
245
 * Engine components
246
 *****************************************************************************/
247

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

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

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

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

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

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

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

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

301
InputOutputMap* Doc::inputOutputMap() const
2,852 ✔
302
{
303
    return m_ioMap;
2,852 ✔
304
}
305

306
MasterTimer* Doc::masterTimer() const
1,814 ✔
307
{
308
    return m_masterTimer;
1,814 ✔
309
}
310

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

327
void Doc::destroyAudioCapture()
528 ✔
328
{
329
    if (m_inputCapture.isNull() == false)
528 ✔
330
    {
331
        qDebug() << "Destroying audio capture";
13 ✔
332
        m_inputCapture.clear();
13 ✔
333
    }
334
}
528 ✔
335

336
/*****************************************************************************
337
 * Modified status
338
 *****************************************************************************/
339
Doc::LoadStatus Doc::loadStatus() const
2 ✔
340
{
341
    return m_loadStatus;
2 ✔
342
}
343

344
bool Doc::isModified() const
27 ✔
345
{
346
    return m_modified;
27 ✔
347
}
348

349
void Doc::setModified()
3,454 ✔
350
{
351
    m_modified = true;
3,454 ✔
352
    m_autosaveTimer.start();
3,454 ✔
353
    emit modified(true);
3,454 ✔
354
}
3,454 ✔
355

356
void Doc::resetModified()
255 ✔
357
{
358
    m_modified = false;
255 ✔
359
    m_autosaveTimer.stop();
255 ✔
360
    emit modified(false);
255 ✔
361
}
255 ✔
362

363
/*****************************************************************************
364
 * Main operating mode
365
 *****************************************************************************/
366

367
void Doc::setMode(Doc::Mode mode)
6 ✔
368
{
369
    /* Don't do mode switching twice */
370
    if (m_mode == mode)
6 ✔
371
        return;
3 ✔
372

373
    m_mode = mode;
3 ✔
374

375
    if (mode == Operate)
3 ✔
376
        m_autosaveTimer.stop();
2 ✔
377
    else if (m_modified)
1 ✔
378
        m_autosaveTimer.start();
×
379

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

396
    emit modeChanged(m_mode);
3 ✔
397
}
398

399
Doc::Mode Doc::mode() const
4 ✔
400
{
401
    return m_mode;
4 ✔
402
}
403

404
void Doc::setKiosk(bool state)
1 ✔
405
{
406
    m_kiosk = state;
1 ✔
407
}
1 ✔
408

409
bool Doc::isKiosk() const
244 ✔
410
{
411
    return m_kiosk;
244 ✔
412
}
413

414
/*********************************************************************
415
 * Clipboard
416
 *********************************************************************/
417

418
QLCClipboard *Doc::clipboard()
1 ✔
419
{
420
    return m_clipboard;
1 ✔
421
}
422

423
/*****************************************************************************
424
 * Fixtures
425
 *****************************************************************************/
426

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

438
    return m_latestFixtureId;
16,766 ✔
439
}
440

441
bool Doc::addFixture(Fixture* fixture, quint32 id)
395 ✔
442
{
443
    Q_ASSERT(fixture != NULL);
395 ✔
444

445
    quint32 i;
446
    quint32 uni = fixture->lastUniverse();
395 ✔
447

448
    // No ID given, this method can assign one
449
    if (id == Fixture::invalidId())
395 ✔
450
        id = createFixtureId();
382 ✔
451

452
    if (m_fixtures.contains(id) == true || id == Fixture::invalidId())
395 ✔
453
    {
454
        qWarning() << Q_FUNC_INFO << "a fixture with ID" << id << "already exists!";
1 ✔
455
        return false;
1 ✔
456
    }
457

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

469
    fixture->setID(id);
391 ✔
470
    m_fixtures.insert(id, fixture);
391 ✔
471
    m_fixturesListCacheUpToDate = false;
391 ✔
472

473
    /* Patch fixture change signals thru Doc */
474
    connect(fixture, SIGNAL(changed(quint32)),
391 ✔
475
            this, SLOT(slotFixtureChanged(quint32)));
476

477
    /* Keep track of fixture addresses */
478
    for (i = 0; i < fixture->channels(); i++)
6,598 ✔
479
        m_addresses[fixture->channelAddress(i)] = id;
6,207 ✔
480

481
    if (uni >= inputOutputMap()->universesCount())
391 ✔
482
    {
483
        for (i = inputOutputMap()->universesCount(); i <= uni; i++)
2 ✔
484
            inputOutputMap()->addUniverse(i);
1 ✔
485
        inputOutputMap()->startUniverses();
1 ✔
486
    }
487

488
    // Add the fixture channels capabilities to the universe they belong
489
    patchFixtureChannels(fixture);
391 ✔
490

491
    emit fixtureAdded(id);
391 ✔
492
    setModified();
391 ✔
493

494
    return true;
391 ✔
495
}
496

497
void Doc::patchFixtureChannels(Fixture *fixture)
395 ✔
498
{
499
    QList<Universe *> universes = inputOutputMap()->claimUniverses();
395 ✔
500

501
    QList<int> forcedHTP = fixture->forcedHTPChannels();
395 ✔
502
    QList<int> forcedLTP = fixture->forcedLTPChannels();
395 ✔
503

504
    for (quint32 i = 0; i < fixture->channels(); i++)
6,615 ✔
505
    {
506
        const QLCChannel *channel(fixture->channel(i));
6,220 ✔
507
        quint32 absAddress = fixture->channelAddress(i);
6,220 ✔
508

509
        // don't touch the channels of another fixture
510
        if (m_addresses.value(absAddress, fixture->id()) != fixture->id())
6,220 ✔
511
            continue;
4 ✔
512

513
        quint32 uni = absAddress >> 9;
6,216 ✔
514
        quint32 addr = absAddress & 0x01FF;
6,216 ✔
515

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

520
        // Inform Universe of any HTP/LTP forcing
521
        if (forcedHTP.contains(int(i)))
6,216 ✔
522
            universes.at(uni)->setChannelCapability(addr, channel->group(), Universe::HTP);
×
523
        else if (forcedLTP.contains(int(i)))
6,216 ✔
524
            universes.at(uni)->setChannelCapability(addr, channel->group(), Universe::LTP);
3 ✔
525
        else
526
            universes.at(uni)->setChannelCapability(addr, channel->group());
6,213 ✔
527

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

531
        // Apply a channel modifier, if defined
532
        ChannelModifier *mod = fixture->channelModifier(i);
6,216 ✔
533
        universes.at(uni)->setChannelModifier(addr, mod);
6,216 ✔
534
    }
535

536
    inputOutputMap()->releaseUniverses(true);
395 ✔
537
}
395 ✔
538

539
void Doc::unpatchChannels(const QList<quint32> &addresses)
13 ✔
540
{
541
    if (addresses.isEmpty())
13 ✔
542
        return;
2 ✔
543

544
    QList<Universe *> universes = inputOutputMap()->claimUniverses();
11 ✔
545

546
    foreach (quint32 address, addresses)
404 ✔
547
    {
548
        // still (or again) used by a fixture
549
        if (m_addresses.contains(address))
393 ✔
550
            continue;
×
551

552
        quint32 uni = address / 512;
393 ✔
553
        if (uni < quint32(universes.count()))
393 ✔
554
            universes.at(uni)->resetChannelCapability(address % 512);
393 ✔
555
    }
11 ✔
556

557
    inputOutputMap()->releaseUniverses(true);
11 ✔
558
}
11 ✔
559

560
QList<quint32> Doc::fixtureAddresses(quint32 id) const
21 ✔
561
{
562
    QList<quint32> addresses;
21 ✔
563

564
    QHashIterator <quint32, quint32> it(m_addresses);
21 ✔
565
    while (it.hasNext() == true)
528 ✔
566
    {
567
        it.next();
507 ✔
568
        if (it.value() == id)
507 ✔
569
            addresses.append(it.key());
414 ✔
570
    }
571

572
    return addresses;
42 ✔
573
}
21 ✔
574

575
bool Doc::deleteFixture(quint32 id)
15 ✔
576
{
577
    if (m_fixtures.contains(id) == true)
15 ✔
578
    {
579
        Fixture* fxi = m_fixtures.take(id);
9 ✔
580
        Q_ASSERT(fxi != NULL);
9 ✔
581
        m_fixturesListCacheUpToDate = false;
9 ✔
582

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

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

599
        emit fixtureRemoved(id);
9 ✔
600
        setModified();
9 ✔
601
        delete fxi;
9 ✔
602

603
        if (m_fixtures.count() == 0)
9 ✔
604
            m_latestFixtureId = 0;
3 ✔
605

606
        return true;
9 ✔
607
    }
9 ✔
608
    else
609
    {
610
        qWarning() << Q_FUNC_INFO << "No fixture with id" << id;
6 ✔
611
        return false;
6 ✔
612
    }
613
}
614

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

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

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

671
        newFixture->setExcludeFadeChannels(fixture->excludeFadeChannels());
8 ✔
672
        newFixture->setForcedHTPChannels(fixture->forcedHTPChannels());
8 ✔
673
        newFixture->setForcedLTPChannels(fixture->forcedLTPChannels());
8 ✔
674

675
        for (quint32 s = 0; s < fixture->channels(); s++)
35 ✔
676
        {
677
            ChannelModifier *chMod = fixture->channelModifier(s);
27 ✔
678
            if (chMod != NULL)
27 ✔
679
                newFixture->setChannelModifier(s, chMod);
×
680
        }
681

682
        m_fixtures.insert(id, newFixture);
8 ✔
683
        m_fixturesListCacheUpToDate = false;
8 ✔
684

685
        /* Patch fixture change signals thru Doc */
686
        connect(newFixture, SIGNAL(changed(quint32)),
8 ✔
687
                this, SLOT(slotFixtureChanged(quint32)));
688

689
        /* Keep track of fixture addresses */
690
        for (quint32 i = 0; i < newFixture->channels(); i++)
20 ✔
691
            m_addresses[newFixture->channelAddress(i)] = id;
12 ✔
692
        m_latestFixtureId = id;
8 ✔
693
    }
6 ✔
694
    return true;
6 ✔
695
}
6 ✔
696

697
bool Doc::updateFixtureChannelCapabilities(quint32 id, QList<int> forcedHTP, QList<int> forcedLTP)
2 ✔
698
{
699
    if (m_fixtures.contains(id) == false)
2 ✔
700
        return false;
1 ✔
701

702
    Fixture* fixture = m_fixtures[id];
1 ✔
703
    // get exclusive access to the universes list
704
    QList<Universe *> universes = inputOutputMap()->claimUniverses();
1 ✔
705

706
    // Set forced HTP channels
707
    fixture->setForcedHTPChannels(forcedHTP);
1 ✔
708

709
    // Set forced LTP channels
710
    fixture->setForcedLTPChannels(forcedLTP);
1 ✔
711

712
    // Update the Fixture Universe with the current channel states
713
    for (quint32 i = 0 ; i < fixture->channels(); i++)
7 ✔
714
    {
715
        const QLCChannel *channel(fixture->channel(i));
6 ✔
716

717
        // a cross-universe fixture may span multiple universes
718
        quint32 absAddress = fixture->channelAddress(i);
6 ✔
719
        quint32 uniIdx = absAddress >> 9;
6 ✔
720
        if (int(uniIdx) >= universes.count())
6 ✔
721
            continue;
×
722

723
        Universe *universe = universes.at(uniIdx);
6 ✔
724
        quint32 addr = absAddress & 0x01FF;
6 ✔
725

726
        // Inform Universe of any HTP/LTP forcing
727
        if (forcedHTP.contains(int(i)))
6 ✔
728
            universe->setChannelCapability(addr, channel->group(), Universe::HTP);
2 ✔
729
        else if (forcedLTP.contains(int(i)))
4 ✔
730
            universe->setChannelCapability(addr, channel->group(), Universe::LTP);
1 ✔
731
        else
732
            universe->setChannelCapability(addr, channel->group());
3 ✔
733

734
        // Apply the default value BEFORE modifiers
735
        universe->setChannelDefaultValue(addr, channel->defaultValue());
6 ✔
736

737
        // Apply a channel modifier, if defined
738
        ChannelModifier *mod = fixture->channelModifier(i);
6 ✔
739
        universe->setChannelModifier(addr, mod);
6 ✔
740
    }
741

742
    inputOutputMap()->releaseUniverses(true);
1 ✔
743

744
    return true;
1 ✔
745
}
1 ✔
746

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

765
int Doc::fixturesCount() const
×
766
{
767
    return m_fixtures.count();
×
768
}
769

770
Fixture* Doc::fixture(quint32 id) const
61,594 ✔
771
{
772
    return m_fixtures.value(id, NULL);
61,594 ✔
773
}
774

775
quint32 Doc::fixtureForAddress(quint32 universeAddress) const
60,047 ✔
776
{
777
    return m_addresses.value(universeAddress, Fixture::invalidId());
60,047 ✔
778
}
779

780
int Doc::totalPowerConsumption(int& fuzzy) const
4 ✔
781
{
782
    int totalPowerConsumption = 0;
4 ✔
783

784
    // Make sure fuzzy starts from zero
785
    fuzzy = 0;
4 ✔
786

787
    QListIterator <Fixture*> fxit(fixtures());
4 ✔
788
    while (fxit.hasNext() == true)
14 ✔
789
    {
790
        Fixture* fxi(fxit.next());
10 ✔
791
        Q_ASSERT(fxi != NULL);
10 ✔
792

793
        if (fxi->fixtureMode() != NULL)
10 ✔
794
        {
795
            QLCPhysical phys = fxi->fixtureMode()->physical();
10 ✔
796
            if (phys.powerConsumption() > 0)
10 ✔
797
                totalPowerConsumption += phys.powerConsumption();
7 ✔
798
            else
799
                fuzzy++;
3 ✔
800
        }
10 ✔
801
        else
802
        {
803
            fuzzy++;
×
804
        }
805
    }
806

807
    return totalPowerConsumption;
4 ✔
808
}
4 ✔
809

810
void Doc::slotFixtureChanged(quint32 id)
6 ✔
811
{
812
    /* Keep track of fixture addresses */
813
    Fixture* fxi = fixture(id);
6 ✔
814
    QList<quint32> oldAddresses = fixtureAddresses(id);
6 ✔
815

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

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

839
        m_addresses[i] = id;
15 ✔
840
    }
841

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

861
    setModified();
6 ✔
862
    emit fixtureChanged(id);
6 ✔
863
}
6 ✔
864

865
/*****************************************************************************
866
 * Fixture groups
867
 *****************************************************************************/
868

869
bool Doc::addFixtureGroup(FixtureGroup* grp, quint32 id)
17 ✔
870
{
871
    Q_ASSERT(grp != NULL);
17 ✔
872

873
    // No ID given, this method can assign one
874
    if (id == FixtureGroup::invalidId())
17 ✔
875
        id = createFixtureGroupId();
10 ✔
876

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

887
        /* Patch fixture group change signals thru Doc */
888
        connect(grp, SIGNAL(changed(quint32)),
15 ✔
889
                this, SLOT(slotFixtureGroupChanged(quint32)));
890

891
        emit fixtureGroupAdded(id);
15 ✔
892
        setModified();
15 ✔
893

894
        return true;
15 ✔
895
    }
896
}
897

898
bool Doc::deleteFixtureGroup(quint32 id)
5 ✔
899
{
900
    if (m_fixtureGroups.contains(id) == true)
5 ✔
901
    {
902
        FixtureGroup* grp = m_fixtureGroups.take(id);
3 ✔
903
        Q_ASSERT(grp != NULL);
3 ✔
904

905
        emit fixtureGroupRemoved(id);
3 ✔
906
        setModified();
3 ✔
907
        delete grp;
3 ✔
908

909
        return true;
3 ✔
910
    }
911
    else
912
    {
913
        qWarning() << Q_FUNC_INFO << "No fixture group with id" << id;
2 ✔
914
        return false;
2 ✔
915
    }
916
}
917

918
FixtureGroup* Doc::fixtureGroup(quint32 id) const
61 ✔
919
{
920
    return m_fixtureGroups.value(id, NULL);
61 ✔
921
}
922

923
QList <FixtureGroup*> Doc::fixtureGroups() const
47 ✔
924
{
925
    return m_fixtureGroups.values();
47 ✔
926
}
927

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

939
    return m_latestFixtureGroupId;
10 ✔
940
}
941

942
void Doc::slotFixtureGroupChanged(quint32 id)
28 ✔
943
{
944
    setModified();
28 ✔
945
    emit fixtureGroupChanged(id);
28 ✔
946
}
28 ✔
947

948
/*********************************************************************
949
 * Channels groups
950
 *********************************************************************/
951
bool Doc::addChannelsGroup(ChannelsGroup *grp, quint32 id)
6 ✔
952
{
953
    Q_ASSERT(grp != NULL);
6 ✔
954

955
    // No ID given, this method can assign one
956
    if (id == ChannelsGroup::invalidId())
6 ✔
957
        id = createChannelsGroupId();
5 ✔
958

959
     grp->setId(id);
6 ✔
960
     m_channelsGroups[id] = grp;
6 ✔
961
     if (m_orderedGroups.contains(id) == false)
6 ✔
962
        m_orderedGroups.append(id);
6 ✔
963

964
     emit channelsGroupAdded(id);
6 ✔
965
     setModified();
6 ✔
966

967
     return true;
6 ✔
968
}
969

970
bool Doc::deleteChannelsGroup(quint32 id)
2 ✔
971
{
972
    if (m_channelsGroups.contains(id) == true)
2 ✔
973
    {
974
        ChannelsGroup* grp = m_channelsGroups.take(id);
1 ✔
975
        Q_ASSERT(grp != NULL);
1 ✔
976

977
        emit channelsGroupRemoved(id);
1 ✔
978
        setModified();
1 ✔
979
        delete grp;
1 ✔
980

981
        int idx = m_orderedGroups.indexOf(id);
1 ✔
982
        if (idx != -1)
1 ✔
983
            m_orderedGroups.takeAt(idx);
1 ✔
984

985
        return true;
1 ✔
986
    }
987
    else
988
    {
989
        qWarning() << Q_FUNC_INFO << "No channels group with id" << id;
1 ✔
990
        return false;
1 ✔
991
    }
992
}
993

994
bool Doc::moveChannelGroup(quint32 id, int direction)
3 ✔
995
{
996
    if (direction == 0 || m_orderedGroups.contains(id) == false)
3 ✔
997
        return false;
1 ✔
998

999
    int idx = m_orderedGroups.indexOf(id);
2 ✔
1000

1001
    if (idx + direction < 0 || idx + direction >= m_orderedGroups.count())
2 ✔
1002
        return false;
1 ✔
1003

1004
    qDebug() << Q_FUNC_INFO << m_orderedGroups;
1 ✔
1005
    m_orderedGroups.takeAt(idx);
1 ✔
1006
    m_orderedGroups.insert(idx + direction, id);
1 ✔
1007
    qDebug() << Q_FUNC_INFO << m_orderedGroups;
1 ✔
1008

1009
    setModified();
1 ✔
1010
    return true;
1 ✔
1011
}
1012

1013
ChannelsGroup* Doc::channelsGroup(quint32 id) const
2 ✔
1014
{
1015
    return m_channelsGroups.value(id, NULL);
2 ✔
1016
}
1017

1018
QList <ChannelsGroup*> Doc::channelsGroups() const
23 ✔
1019
{
1020
    QList <ChannelsGroup*> orderedList;
23 ✔
1021

1022
    for (int i = 0; i < m_orderedGroups.count(); i++)
48 ✔
1023
    {
1024
        orderedList.append(m_channelsGroups[m_orderedGroups.at(i)]);
25 ✔
1025
    }
1026
    return orderedList;
23 ✔
1027
}
×
1028

1029
quint32 Doc::createChannelsGroupId()
5 ✔
1030
{
1031
    while (m_channelsGroups.contains(m_latestChannelsGroupId) == true ||
12 ✔
1032
           m_latestChannelsGroupId == ChannelsGroup::invalidId())
5 ✔
1033
    {
1034
        m_latestChannelsGroupId++;
2 ✔
1035
    }
1036

1037
    return m_latestChannelsGroupId;
5 ✔
1038
}
1039

1040
/*********************************************************************
1041
 * Palettes
1042
 *********************************************************************/
1043

1044
bool Doc::addPalette(QLCPalette *palette, quint32 id)
5 ✔
1045
{
1046
    Q_ASSERT(palette != NULL);
5 ✔
1047

1048
    // No ID given, this method can assign one
1049
    if (id == QLCPalette::invalidId())
5 ✔
1050
        id = createPaletteId();
4 ✔
1051

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

1063
        emit paletteAdded(id);
5 ✔
1064
        setModified();
5 ✔
1065
    }
1066

1067
    return true;
5 ✔
1068
}
1069

1070
bool Doc::deletePalette(quint32 id)
4 ✔
1071
{
1072
    if (m_palettes.contains(id) == true)
4 ✔
1073
    {
1074
        QLCPalette *palette = m_palettes.take(id);
3 ✔
1075
        Q_ASSERT(palette != NULL);
3 ✔
1076

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

1086
        emit paletteRemoved(id);
3 ✔
1087
        setModified();
3 ✔
1088
        delete palette;
3 ✔
1089

1090
        return true;
3 ✔
1091
    }
1092
    else
1093
    {
1094
        qWarning() << Q_FUNC_INFO << "No palette with id" << id;
1 ✔
1095
        return false;
1 ✔
1096
    }
1097
}
1098

1099
QLCPalette *Doc::palette(quint32 id) const
4 ✔
1100
{
1101
    return m_palettes.value(id, NULL);
4 ✔
1102
}
1103

1104
QList<QLCPalette *> Doc::palettes() const
8 ✔
1105
{
1106
    return m_palettes.values();
8 ✔
1107
}
1108

1109
quint32 Doc::createPaletteId()
4 ✔
1110
{
1111
    while (m_palettes.contains(m_latestPaletteId) == true ||
10 ✔
1112
           m_latestPaletteId == FixtureGroup::invalidId())
4 ✔
1113
    {
1114
        m_latestPaletteId++;
2 ✔
1115
    }
1116

1117
    return m_latestPaletteId;
4 ✔
1118
}
1119

1120
/*****************************************************************************
1121
 * Functions
1122
 *****************************************************************************/
1123

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

1135
    return m_latestFunctionId;
262 ✔
1136
}
1137

1138
bool Doc::addFunction(Function* func, quint32 id)
278 ✔
1139
{
1140
    Q_ASSERT(func != NULL);
278 ✔
1141

1142
    if (id == Function::invalidId())
278 ✔
1143
        id = createFunctionId();
262 ✔
1144

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

1156
        // Listen to function name changes
1157
        connect(func, SIGNAL(nameChanged(quint32)),
276 ✔
1158
                this, SLOT(slotFunctionNameChanged(quint32)));
1159

1160
        // Make the function listen to fixture removals
1161
        connect(this, SIGNAL(fixtureRemoved(quint32)),
276 ✔
1162
                func, SLOT(slotFixtureRemoved(quint32)));
1163

1164
        // Place the function in the map and assign it the new ID
1165
        m_functions[id] = func;
276 ✔
1166
        func->setID(id);
276 ✔
1167
        emit functionAdded(id);
276 ✔
1168
        setModified();
276 ✔
1169

1170
        return true;
276 ✔
1171
    }
1172
}
1173

1174
QList <Function*> Doc::functions() const
58 ✔
1175
{
1176
    return m_functions.values();
58 ✔
1177
}
1178

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

1190
Function *Doc::functionByName(QString name)
×
1191
{
1192
    foreach (Function *f, m_functions)
×
1193
    {
1194
        if (f != NULL && f->name() == name)
×
1195
            return f;
×
1196
    }
×
1197
    return NULL;
×
1198
}
1199

1200
bool Doc::deleteFunction(quint32 id)
8 ✔
1201
{
1202
    if (m_functions.contains(id) == true)
8 ✔
1203
    {
1204
        Function* func = m_functions.take(id);
6 ✔
1205
        Q_ASSERT(func != NULL);
6 ✔
1206

1207
        if (m_startupFunctionId == id)
6 ✔
1208
            m_startupFunctionId = Function::invalidId();
×
1209

1210
        emit functionRemoved(id);
6 ✔
1211
        setModified();
6 ✔
1212
        delete func;
6 ✔
1213

1214
        return true;
6 ✔
1215
    }
1216
    else
1217
    {
1218
        qWarning() << Q_FUNC_INFO << "No function with id" << id;
2 ✔
1219
        return false;
2 ✔
1220
    }
1221
}
1222

1223
Function* Doc::function(quint32 id) const
927 ✔
1224
{
1225
    return m_functions.value(id, NULL);
927 ✔
1226
}
1227

1228
quint32 Doc::nextFunctionID() const
2 ✔
1229
{
1230
    quint32 tmpFID = m_latestFunctionId;
2 ✔
1231
    while (m_functions.contains(tmpFID) == true ||
5 ✔
1232
           tmpFID == Fixture::invalidId())
2 ✔
1233
    {
1234
        tmpFID++;
1 ✔
1235
    }
1236

1237
    return tmpFID;
2 ✔
1238
}
1239

1240
void Doc::setStartupFunction(quint32 fid)
1 ✔
1241
{
1242
    m_startupFunctionId = fid;
1 ✔
1243
}
1 ✔
1244

1245
quint32 Doc::startupFunction() const
2 ✔
1246
{
1247
    return m_startupFunctionId;
2 ✔
1248
}
1249

1250
QList<quint32> Doc::getUsage(quint32 fid)
7 ✔
1251
{
1252
    QList<quint32> usageList;
7 ✔
1253

1254
    foreach (Function *f, m_functions)
70 ✔
1255
    {
1256
        if (f->id() == fid)
63 ✔
1257
            continue;
6 ✔
1258

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

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

1336
    return usageList;
7 ✔
1337
}
×
1338

1339
void Doc::slotFunctionChanged(quint32 fid)
67 ✔
1340
{
1341
    setModified();
67 ✔
1342
    emit functionChanged(fid);
67 ✔
1343
}
67 ✔
1344

1345
void Doc::slotFunctionNameChanged(quint32 fid)
2 ✔
1346
{
1347
    setModified();
2 ✔
1348
    emit functionNameChanged(fid);
2 ✔
1349
}
2 ✔
1350

1351
/*********************************************************************
1352
 * Monitor Properties
1353
 *********************************************************************/
1354

1355
MonitorProperties *Doc::monitorProperties()
44 ✔
1356
{
1357
    if (m_monitorProps == NULL)
44 ✔
1358
        m_monitorProps = new MonitorProperties();
10 ✔
1359

1360
    return m_monitorProps;
44 ✔
1361
}
1362

1363
/*****************************************************************************
1364
 * Load & Save
1365
 *****************************************************************************/
1366

1367
bool Doc::loadXML(QXmlStreamReader &doc, bool loadIO)
2 ✔
1368
{
1369
    clearErrorLog();
2 ✔
1370

1371
    if (doc.name() != KXMLQLCEngine)
2 ✔
1372
    {
1373
        qWarning() << Q_FUNC_INFO << "Engine node not found";
1 ✔
1374
        return false;
1 ✔
1375
    }
1376

1377
    m_loadStatus = Loading;
1 ✔
1378
    emit loading();
1 ✔
1379

1380
    if (doc.attributes().hasAttribute(KXMLQLCStartupFunction))
2 ✔
1381
    {
1382
        quint32 sID = doc.attributes().value(KXMLQLCStartupFunction).toString().toUInt();
×
1383
        if (sID != Function::invalidId())
×
1384
            setStartupFunction(sID);
×
1385
    }
1386

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

1432
    postLoad();
1 ✔
1433

1434
    m_loadStatus = Loaded;
1 ✔
1435
    emit loaded();
1 ✔
1436

1437
    return true;
1 ✔
1438
}
1439

1440
bool Doc::saveXML(QXmlStreamWriter *doc) const
1 ✔
1441
{
1442
    Q_ASSERT(doc != NULL);
1 ✔
1443

1444
    /* Create the master Engine node */
1445
    doc->writeStartElement(KXMLQLCEngine);
2 ✔
1446

1447
    if (startupFunction() != Function::invalidId())
1 ✔
1448
        doc->writeAttribute(KXMLQLCStartupFunction, QString::number(startupFunction()));
×
1449

1450
    m_ioMap->saveXML(doc);
1 ✔
1451

1452
    /* Write fixtures into an XML document */
1453
    QListIterator <Fixture*> fxit(fixtures());
1 ✔
1454
    while (fxit.hasNext() == true)
4 ✔
1455
    {
1456
        Fixture *fxi(fxit.next());
3 ✔
1457
        if (fxi != NULL)
3 ✔
1458
            fxi->saveXML(doc);
3 ✔
1459
    }
1460

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

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

1479
    /* Write palettes into an XML document */
1480
    QListIterator <QLCPalette*> paletteIt(palettes());
1 ✔
1481
    while (paletteIt.hasNext() == true)
1 ✔
1482
    {
1483
        QLCPalette *palette(paletteIt.next());
×
1484
        if (palette != NULL)
×
1485
            palette->saveXML(doc);
×
1486
    }
1487

1488
    /* Write functions into an XML document */
1489
    QListIterator <Function*> funcit(functions());
1 ✔
1490
    while (funcit.hasNext() == true)
5 ✔
1491
    {
1492
        Function *func(funcit.next());
4 ✔
1493
        if (func != NULL)
4 ✔
1494
            func->saveXML(doc);
4 ✔
1495
    }
1496

1497
    if (m_monitorProps != NULL)
1 ✔
1498
        m_monitorProps->saveXML(doc, this);
1 ✔
1499

1500
    /* End the <Engine> tag */
1501
    doc->writeEndElement();
1 ✔
1502

1503
    return true;
1 ✔
1504
}
1 ✔
1505

1506
void Doc::appendToErrorLog(QString error)
8 ✔
1507
{
1508
    if (m_errorLog.contains(error))
8 ✔
1509
        return;
5 ✔
1510

1511
    m_errorLog.append(error);
3 ✔
1512
    m_errorLog.append("<br>");
3 ✔
1513
}
1514

1515
void Doc::clearErrorLog()
2 ✔
1516
{
1517
    m_errorLog = "";
2 ✔
1518
}
2 ✔
1519

1520
QString Doc::errorLog() const
1 ✔
1521
{
1522
    return m_errorLog;
1 ✔
1523
}
1524

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