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

mcallegari / qlcplus / 42371974533

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

push

github

mcallegari
Back to 5.4.0 debug

19248 of 52268 relevant lines covered (36.83%)

41917.74 hits per line

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

0.0
/ui/src/app.cpp
1
/*
2
  Q Light Controller Plus
3
  app.cpp
4

5
  Copyright (c) Heikki Junnila,
6
                Christopher Staite
7
                Massimo Callegari
8

9
  Licensed under the Apache License, Version 2.0 (the "License");
10
  you may not use this file except in compliance with the License.
11
  You may obtain a copy of the License at
12

13
      http://www.apache.org/licenses/LICENSE-2.0.txt
14

15
  Unless required by applicable law or agreed to in writing, software
16
  distributed under the License is distributed on an "AS IS" BASIS,
17
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
  See the License for the specific language governing permissions and
19
  limitations under the License.
20
*/
21

22
#include <QToolButton>
23
#include <QtWidgets>
24
#include <unistd.h>
25
#include <QtCore>
26

27
#if defined(WIN32) || defined(Q_OS_WIN)
28
  #include <windows.h>
29
#endif
30

31
#include "functionliveeditdialog.h"
32
#include "inputoutputmanager.h"
33
#include "functionselection.h"
34
#include "functionmanager.h"
35
#include "inputoutputmap.h"
36
#include "virtualconsole.h"
37
#include "fixturemanager.h"
38
#include "dmxdumpfactory.h"
39
#include "showmanager.h"
40
#include "mastertimer.h"
41
#include "addresstool.h"
42
#include "simpledesk.h"
43
#include "aboutbox.h"
44
#include "monitor.h"
45
#include "vcframe.h"
46
#include "app.h"
47
#include "doc.h"
48

49
#include "qlcfixturedefcache.h"
50
#include "audioplugincache.h"
51
#include "serviceloader.h"
52
#include "rgbscriptscache.h"
53
#include "videoprovider.h"
54
#include "qlcconfig.h"
55
#include "qlcfile.h"
56
#include "apputil.h"
57

58
#if defined(WIN32) || defined(Q_OS_WIN)
59
#   include "hotplugmonitor.h"
60
#endif
61

62
#if defined(__APPLE__) || defined(Q_OS_MAC)
63
extern void qt_set_sequence_auto_mnemonic(bool b);
64
#endif
65

66
#if defined(WIN32) || defined(Q_OS_WIN)
67
// Defined in Windows 11 headers but not in earlier versions.
68
#ifndef PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION
69
#define PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION 0x4
70
#endif
71

72
typedef BOOL (WINAPI *SetProcessInformationType)(
73
    HANDLE hProcess,
74
    PROCESS_INFORMATION_CLASS ProcessInformationClass,
75
    LPVOID ProcessInformation,
76
    DWORD ProcessInformationSize
77
);
78
#endif
79

80
//#define DEBUG_SPEED
81

82
#ifdef DEBUG_SPEED
83
 #include <QTime>
84
 QTime speedTime;
85
#endif
86

87
#define SETTINGS_GEOMETRY      QStringLiteral("workspace/geometry")
88
#define SETTINGS_WORKINGPATH   QStringLiteral("workspace/workingpath")
89
#define SETTINGS_RECENTFILE    QStringLiteral("workspace/recent")
90
#define KXMLQLCWorkspaceWindow QStringLiteral("CurrentWindow")
91

92
#define MAX_RECENT_FILES    10
93

94
#define KModeTextOperate QObject::tr("Operate")
95
#define KModeTextDesign QObject::tr("Design")
96
#define KUniverseCount 4
97

98
/*****************************************************************************
99
 * Initialization
100
 *****************************************************************************/
101

102
App::App()
×
103
    : QMainWindow()
104
    , m_tab(NULL)
×
105
    , m_overscan(false)
×
106
    , m_noGui(false)
×
107
    , m_progressDialog(NULL)
×
108
    , m_doc(NULL)
×
109

110
    , m_fileNewAction(NULL)
×
111
    , m_fileOpenAction(NULL)
×
112
    , m_fileSaveAction(NULL)
×
113
    , m_fileSaveAsAction(NULL)
×
114

115
    , m_modeToggleAction(NULL)
×
116
    , m_controlMonitorAction(NULL)
×
117
    , m_addressToolAction(NULL)
×
118
    , m_controlFullScreenAction(NULL)
×
119
    , m_controlBlackoutAction(NULL)
×
120
    , m_controlPanicAction(NULL)
×
121
    , m_dumpDmxAction(NULL)
×
122
    , m_liveEditAction(NULL)
×
123
    , m_liveEditVirtualConsoleAction(NULL)
×
124

125
    , m_helpIndexAction(NULL)
×
126
    , m_helpAboutAction(NULL)
×
127
    , m_quitAction(NULL)
×
128
    , m_fileOpenMenu(NULL)
×
129
    , m_fadeAndStopMenu(NULL)
×
130

131
    , m_toolbar(NULL)
×
132

133
    , m_dumpProperties(NULL)
×
134
    , m_videoProvider(NULL)
×
135
{
136
    QCoreApplication::setOrganizationName("qlcplus");
×
137
    QCoreApplication::setOrganizationDomain("qlcplus.org");
×
138
    QCoreApplication::setApplicationName(APPNAME);
×
139
}
×
140

141
App::~App()
×
142
{
143
    QSettings settings;
×
144

145
    // Don't save kiosk-mode window geometry because that will screw things up
146
    if (m_doc->isKiosk() == false && QLCFile::hasWindowManager())
×
147
        settings.setValue(SETTINGS_GEOMETRY, saveGeometry());
×
148
    else
149
        settings.setValue(SETTINGS_GEOMETRY, QVariant());
×
150

151
    if (Monitor::instance() != NULL)
×
152
        delete Monitor::instance();
×
153

154
    if (FixtureManager::instance() != NULL)
×
155
        delete FixtureManager::instance();
×
156

157
    if (FunctionManager::instance() != NULL)
×
158
        delete FunctionManager::instance();
×
159

160
    if (ShowManager::instance() != NULL)
×
161
        delete ShowManager::instance();
×
162

163
    if (InputOutputManager::instance() != NULL)
×
164
        delete InputOutputManager::instance();
×
165

166
    if (VirtualConsole::instance() != NULL)
×
167
        delete VirtualConsole::instance();
×
168

169
    if (SimpleDesk::instance() != NULL)
×
170
        delete SimpleDesk::instance();
×
171

172
    if (m_dumpProperties != NULL)
×
173
        delete m_dumpProperties;
×
174

175
    if (m_videoProvider != NULL)
×
176
        delete m_videoProvider;
×
177

178
    if (m_doc != NULL)
×
179
        delete m_doc;
×
180

181
    m_doc = NULL;
×
182
}
×
183

184
void App::startup()
×
185
{
186
#if defined(__APPLE__) || defined(Q_OS_MAC)
187
    createProgressDialog();
188
#endif
189

190
    init();
×
191
    slotModeDesign();
×
192
    slotDocModified(false);
×
193

194
#if defined(__APPLE__) || defined(Q_OS_MAC)
195
    destroyProgressDialog();
196
#endif
197

198
    // Activate FixtureManager
199
    setActiveWindow(FixtureManager::staticMetaObject.className());
×
200
}
×
201

202
void App::enableOverscan()
×
203
{
204
    m_overscan = true;
×
205
}
×
206

207
void App::disableGUI()
×
208
{
209
    m_noGui = true;
×
210
}
×
211

212
void App::init()
×
213
{
214
    QSettings settings;
×
215

216
    setWindowIcon(QIcon(":/qlcplus.png"));
×
217

218
    m_tab = new QTabWidget(this);
×
219
    m_tab->setTabPosition(QTabWidget::South);
×
220
    setCentralWidget(m_tab);
×
221

222
#if defined(__APPLE__) || defined(Q_OS_MAC)
223
    m_tab->setElideMode(Qt::TextElideMode::ElideNone);
224
    qt_set_sequence_auto_mnemonic(true);
225
#endif
226

227
    QVariant var = settings.value(SETTINGS_GEOMETRY);
×
228
    if (var.isValid() == true)
×
229
    {
230
        this->restoreGeometry(var.toByteArray());
×
231
    }
232
    else
233
    {
234
        /* Application geometry and window state */
235
        QSize size = settings.value("/workspace/size").toSize();
×
236
        if (size.isValid() == true)
×
237
        {
238
            resize(size);
×
239
        }
240
        else
241
        {
242
            if (QLCFile::hasWindowManager() == false)
×
243
            {
244
                QScreen *screen = QGuiApplication::screens().first();
×
245
                QRect geometry = screen->geometry();
×
246
                if (m_noGui == true)
×
247
                {
248
                    setGeometry(geometry.width(), geometry.height(), 1, 1);
×
249
                }
250
                else
251
                {
252
                    int w = geometry.width();
×
253
                    int h = geometry.height();
×
254
                    if (m_overscan == true)
×
255
                    {
256
                        // if overscan is requested, introduce a 5% margin
257
                        w = (float)geometry.width() * 0.95;
×
258
                        h = (float)geometry.height() * 0.95;
×
259
                    }
260
                    setGeometry((geometry.width() - w) / 2, (geometry.height() - h) / 2, w, h);
×
261
                }
262
            }
263
            else
264
                resize(800, 600);
×
265
        }
266

267
        QVariant state = settings.value("/workspace/state", Qt::WindowNoState);
×
268
        if (state.isValid() == true)
×
269
            setWindowState(Qt::WindowState(state.toInt()));
×
270
    }
×
271

272
    QVariant dir = settings.value(SETTINGS_WORKINGPATH);
×
273
    if (dir.isValid() == true)
×
274
        m_workingDirectory = QDir(dir.toString());
×
275

276
    // The engine object
277
    initDoc();
×
278
    // Main view actions
279
    initActions();
×
280
    // Main tool bar
281
    initToolBar();
×
282

283
    m_dumpProperties = new DmxDumpFactoryProperties(KUniverseCount);
×
284

285
    // Create primary views.
286
    m_tab->setIconSize(QSize(24, 24));
×
287
    QWidget* w = new FixtureManager(m_tab, m_doc);
×
288
    m_tab->addTab(w, QIcon(":/fixture.png"), tr("Fixtures"));
×
289
    w = new FunctionManager(m_tab, m_doc);
×
290
    m_tab->addTab(w, QIcon(":/function.png"), tr("Functions"));
×
291
    w = new ShowManager(m_tab, m_doc);
×
292
    m_tab->addTab(w, QIcon(":/show.png"), tr("Shows"));
×
293
    w = new VirtualConsole(m_tab, m_doc);
×
294
    m_tab->addTab(w, QIcon(":/virtualconsole.png"), tr("Virtual Console"));
×
295
    w = new SimpleDesk(m_tab, m_doc);
×
296
    m_tab->addTab(w, QIcon(":/slidermatrix.png"), tr("Simple Desk"));
×
297
    w = new InputOutputManager(m_tab, m_doc);
×
298
    m_tab->addTab(w, QIcon(":/input_output.png"), tr("Inputs/Outputs"));
×
299

300
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
301
    /* Detach the tab's widget onto a new window on doubleClick */
302
    connect(m_tab, SIGNAL(tabBarDoubleClicked(int)), this, SLOT(slotDetachContext(int)));
×
303
#endif
304

305
    // Listen to blackout changes and toggle m_controlBlackoutAction
306
    connect(m_doc->inputOutputMap(), SIGNAL(blackoutChanged(bool)), this, SLOT(slotBlackoutChanged(bool)));
×
307

308
    // Listen to DMX value changes and update each Fixture values array
309
    connect(m_doc->inputOutputMap(), SIGNAL(universeWritten(quint32, const QByteArray&)),
×
310
            this, SLOT(slotUniverseWritten(quint32, const QByteArray&)));
311

312
    // Enable/Disable panic button
313
    connect(m_doc->masterTimer(), SIGNAL(functionListChanged()), this, SLOT(slotRunningFunctionsChanged()));
×
314
    slotRunningFunctionsChanged();
×
315

316
    // Start up in non-modified state
317
    m_doc->resetModified();
×
318

319
#if defined(WIN32) || defined(Q_OS_WIN)
320
    HotPlugMonitor::setWinId(winId());
321

322
    // When on Windows 11, disable system timer resolution throttling when
323
    // app is minimised, occluded, etc.
324
    disableTimerResolutionThrottling();
325
#endif
326

327
    this->setStyleSheet(AppUtil::getStyleSheet("MAIN"));
×
328

329
    m_videoProvider = new VideoProvider(m_doc, this);
×
330
}
×
331

332
void App::setActiveWindow(const QString& name)
×
333
{
334
    if (name.isEmpty() == true)
×
335
        return;
×
336

337
    for (int i = 0; i < m_tab->count(); i++)
×
338
    {
339
        QWidget* widget = m_tab->widget(i);
×
340
        if (widget != NULL && widget->metaObject()->className() == name)
×
341
        {
342
            m_tab->setCurrentIndex(i);
×
343
            break;
×
344
        }
345
    }
346
}
347

348
#if defined(WIN32) || defined(Q_OS_WIN)
349
bool App::nativeEvent(const QByteArray &eventType, void *message, long *result)
350
{
351
    Q_UNUSED(eventType)
352
    //qDebug() << Q_FUNC_INFO << eventType;
353
    return HotPlugMonitor::parseWinEvent(message, result);
354
}
355

356
void App::disableTimerResolutionThrottling()
357
{
358
    // On Windows 11, we want it to always honour system timer resolution requests,
359
    // because otherwise by default when an application is minimised, or otherwise
360
    // non-visible or non-audible to the end-user, Windows may ignore timer
361
    // resolution requests and not give a higher resolution than the default system
362
    // timer resolution (typically 15.625 ms).
363

364
    // Note: we must resolve the SetProcessInformation API function at run-time
365
    // because it does not exist prior to Windows 8. On supported Windows versions
366
    // earlier than 11, the call to SetProcessInformation will just fail, which we
367
    // can ignore.
368

369
    HMODULE hKernel32 = LoadLibrary(L"kernel32.dll");
370
    Q_ASSERT(hKernel32 != NULL); // Shouldn't ever fail because kernel32 already loaded into every process
371

372
    // Extra void* cast to avoid -Wcast-function-type warning.
373
    SetProcessInformationType pfnSetProcessInformation = (SetProcessInformationType)(void *)GetProcAddress(hKernel32, "SetProcessInformation");
374

375
    if (pfnSetProcessInformation != NULL)
376
    {
377
        PROCESS_POWER_THROTTLING_STATE pwrState = {
378
                .Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION,
379
                .ControlMask = PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION,
380
                .StateMask = 0 // Disables timer resolution throttling
381
        };
382

383
        if (!pfnSetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &pwrState, sizeof(pwrState)))
384
        {
385
            qWarning() << Q_FUNC_INFO << "SetProcessInformation() failed with error" << GetLastError() << "(ignore if Windows version < 11)";
386
        }
387
    }
388
    else
389
    {
390
        qDebug() << Q_FUNC_INFO << "SetProcessInformation() API does not exist on this version of Windows";
391
    }
392
}
393
#endif
394

395
void App::closeEvent(QCloseEvent* e)
×
396
{
397
    if (m_doc->mode() == Doc::Operate && m_doc->isKiosk() == false)
×
398
    {
399
        QMessageBox::warning(this,
×
400
                             tr("Cannot exit in Operate mode"),
×
401
                             tr("You must switch back to Design mode " \
×
402
                                "to close the application."));
403
        e->ignore();
×
404
        return;
×
405
    }
406

407
    if (m_doc->isKiosk() == false)
×
408
    {
409
        if (saveModifiedDoc(tr("Close"), tr("Do you wish to save the current workspace " \
×
410
                                            "before closing the application?")) == true)
×
411
        {
412
            e->accept();
×
413
        }
414
        else
415
        {
416
            e->ignore();
×
417
        }
418
    }
419
    else
420
    {
421
        if (m_doc->isKiosk() == true)
×
422
        {
423
            int result = QMessageBox::warning(this, tr("Close the application?"),
×
424
                                              tr("Do you wish to close the application?"),
×
425
                                              QMessageBox::Yes, QMessageBox::No);
426
            if (result == QMessageBox::No)
×
427
            {
428
                e->ignore();
×
429
                return;
×
430
            }
431
        }
432

433
        e->accept();
×
434
    }
435
}
436

437
/*****************************************************************************
438
 * Progress dialog
439
 *****************************************************************************/
440

441
void App::createProgressDialog()
×
442
{
443
    m_progressDialog = new QProgressDialog;
×
444
    m_progressDialog->setCancelButton(NULL);
×
445
    m_progressDialog->show();
×
446
    m_progressDialog->raise();
×
447
    m_progressDialog->setRange(0, 10);
×
448
    slotSetProgressText(QString());
×
449
    QApplication::processEvents();
×
450
}
×
451

452
void App::destroyProgressDialog()
×
453
{
454
    delete m_progressDialog;
×
455
    m_progressDialog = NULL;
×
456
}
×
457

458
void App::slotSetProgressText(const QString& text)
×
459
{
460
    if (m_progressDialog == NULL)
×
461
        return;
×
462

463
    static int progress = 0;
464
    m_progressDialog->setValue(progress++);
×
465
    m_progressDialog->setLabelText(QString("<B>%1</B><BR/>%2")
×
466
                                   .arg(tr("Starting Q Light Controller Plus"))
×
467
                                   .arg(text));
×
468
    QApplication::processEvents();
×
469
}
470

471
/*****************************************************************************
472
 * Doc
473
 *****************************************************************************/
474

475
void App::clearDocument()
×
476
{
477
    m_doc->masterTimer()->stop();
×
478
    VirtualConsole::instance()->resetContents();
×
479
    ShowManager::instance()->clearContents();
×
480
    m_doc->clearContents();
×
481
    if (Monitor::instance() != NULL)
×
482
        Monitor::instance()->updateView();
×
483
    SimpleDesk::instance()->clearContents();
×
484
    m_doc->inputOutputMap()->resetUniverses();
×
485
    setFileName(QString());
×
486
    m_doc->resetModified();
×
487
    m_doc->inputOutputMap()->startUniverses();
×
488
    m_doc->masterTimer()->start();
×
489
}
×
490

491
Doc *App::doc()
×
492
{
493
    return m_doc;
×
494
}
495

496
void App::initDoc()
×
497
{
498
    Q_ASSERT(m_doc == NULL);
×
499
    m_doc = new Doc(this);
×
500

501
    connect(m_doc, SIGNAL(modified(bool)), this, SLOT(slotDocModified(bool)));
×
502
    connect(m_doc, SIGNAL(needAutosave()), this, SLOT(slotDocAutosave()));
×
503
    connect(m_doc, SIGNAL(modeChanged(Doc::Mode)), this, SLOT(slotModeChanged(Doc::Mode)));
×
504
#ifdef DEBUG_SPEED
505
    speedTime.start();
506
#endif
507
    /* Load user fixtures first so that they override system fixtures */
508
    m_doc->fixtureDefCache()->load(QLCFixtureDefCache::userDefinitionDirectory());
×
509
    m_doc->fixtureDefCache()->loadMap(QLCFixtureDefCache::systemDefinitionDirectory());
×
510

511
    /* Load channel modifiers templates */
512
    m_doc->modifiersCache()->load(QLCModifiersCache::systemTemplateDirectory(), true);
×
513
    m_doc->modifiersCache()->load(QLCModifiersCache::userTemplateDirectory());
×
514

515
    /* Load RGB scripts */
516
    m_doc->rgbScriptsCache()->load(RGBScriptsCache::systemScriptsDirectory());
×
517
    m_doc->rgbScriptsCache()->load(RGBScriptsCache::userScriptsDirectory());
×
518

519
    /* Load plugins */
520
    connect(m_doc->ioPluginCache(), SIGNAL(pluginLoaded(const QString&)),
×
521
            this, SLOT(slotSetProgressText(const QString&)));
522
    m_doc->ioPluginCache()->load(IOPluginCache::systemPluginDirectory());
×
523

524
    /* Load audio decoder plugins
525
     * This doesn't use a AudioPluginCache::systemPluginDirectory() cause
526
     * otherwise the qlcconfig.h creation should have been moved into the
527
     * audio folder, which doesn't make much sense */
528
    m_doc->audioPluginCache()->load(QLCFile::systemDirectory(AUDIOPLUGINDIR, KExtPlugin));
×
529

530
    /* Load service plugins. They are optional and use the I/O plugins,
531
     * so they must be loaded after them */
532
    m_doc->serviceLoader()->load(ServiceLoader::systemServiceDirectory());
×
533

534
    /* Restore outputmap settings */
535
    Q_ASSERT(m_doc->inputOutputMap() != NULL);
×
536

537
    /* Load input plugins & profiles */
538
    m_doc->inputOutputMap()->loadProfiles(InputOutputMap::userProfileDirectory());
×
539
    m_doc->inputOutputMap()->loadProfiles(InputOutputMap::systemProfileDirectory());
×
540
    m_doc->inputOutputMap()->loadDefaults();
×
541

542
#ifdef DEBUG_SPEED
543
    qDebug() << "[App] Doc initialization took" << speedTime.elapsed() << "ms";
544
#endif
545

546
    m_doc->inputOutputMap()->startUniverses();
×
547
    m_doc->masterTimer()->start();
×
548
}
×
549

550
void App::slotDocModified(bool state)
×
551
{
552
    QString caption(APPNAME);
×
553

554
    if (fileName().isEmpty() == false)
×
555
        caption += QString(" - ") + QDir::toNativeSeparators(fileName());
×
556
    else
557
        caption += tr(" - New Workspace");
×
558

559
    if (state == true)
×
560
        setWindowTitle(caption + QString(" *"));
×
561
    else
562
        setWindowTitle(caption);
×
563
}
×
564

565
void App::slotDocAutosave()
×
566
{
567
    saveXML(autoSaveFileName(), true);
×
568
}
×
569

570
void App::slotUniverseWritten(quint32 idx, const QByteArray &ua)
×
571
{
572
    foreach (Fixture *fixture, m_doc->fixtures())
×
573
    {
574
        // a cross-universe fixture spans more universes
575
        if (idx < fixture->universe() || idx > fixture->lastUniverse())
×
576
            continue;
×
577

578
        fixture->setUniverseValues(idx, ua);
×
579
    }
×
580
}
×
581

582
/*****************************************************************************
583
 * Main application Mode
584
 *****************************************************************************/
585

586
void App::enableKioskMode()
×
587
{
588
    // Turn on operate mode
589
    m_doc->setKiosk(true);
×
590
    m_doc->setMode(Doc::Operate);
×
591

592
    // No need for these
593
    m_tab->removeTab(m_tab->indexOf(FixtureManager::instance()));
×
594
    m_tab->removeTab(m_tab->indexOf(FunctionManager::instance()));
×
595
    m_tab->removeTab(m_tab->indexOf(ShowManager::instance()));
×
596
    m_tab->removeTab(m_tab->indexOf(SimpleDesk::instance()));
×
597
    m_tab->removeTab(m_tab->indexOf(InputOutputManager::instance()));
×
598

599
    // Hide the tab bar to save some pixels
600
    m_tab->tabBar()->hide();
×
601

602
    // No need for the toolbar
603
    delete m_toolbar;
×
604
    m_toolbar = NULL;
×
605
}
×
606

607
void App::createKioskCloseButton(const QRect& rect)
×
608
{
609
    QPushButton* btn = new QPushButton(VirtualConsole::instance()->contents());
×
610
    btn->setIcon(QIcon(":/exit.png"));
×
611
    btn->setToolTip(tr("Exit"));
×
612
    btn->setGeometry(rect);
×
613
    connect(btn, SIGNAL(clicked()), this, SLOT(close()));
×
614
    btn->show();
×
615
}
×
616

617
void App::slotModeOperate()
×
618
{
619
    m_doc->setMode(Doc::Operate);
×
620
}
×
621

622
void App::slotModeDesign()
×
623
{
624
    if (m_doc->masterTimer()->runningFunctions() > 0)
×
625
    {
626
        int result = QMessageBox::warning(
×
627
                         this,
628
                         tr("Switch to Design Mode"),
×
629
                         tr("There are still running functions.\n"
×
630
                            "Really stop them and switch back to "
631
                            "Design mode?"),
632
                         QMessageBox::Yes,
633
                         QMessageBox::No);
634

635
        if (result == QMessageBox::No)
×
636
            return;
×
637
        else
638
            m_doc->masterTimer()->stopAllFunctions();
×
639
    }
640

641
    m_liveEditVirtualConsoleAction->setChecked(false);
×
642
    m_doc->setMode(Doc::Design);
×
643
}
644

645
void App::slotModeToggle()
×
646
{
647
    if (m_doc->mode() == Doc::Design)
×
648
        slotModeOperate();
×
649
    else
650
        slotModeDesign();
×
651
}
×
652

653
void App::slotModeChanged(Doc::Mode mode)
×
654
{
655
    if (mode == Doc::Operate)
×
656
    {
657
        /* Disable editing features */
658
        m_fileNewAction->setEnabled(false);
×
659
        m_fileOpenAction->setEnabled(false);
×
660
        m_liveEditAction->setEnabled(true);
×
661
        m_liveEditVirtualConsoleAction->setEnabled(true);
×
662

663
        m_modeToggleAction->setIcon(QIcon(":/design.png"));
×
664
        m_modeToggleAction->setText(tr("Design"));
×
665
        m_modeToggleAction->setToolTip(tr("Switch to design mode"));
×
666
    }
667
    else if (mode == Doc::Design)
×
668
    {
669
        /* Enable editing features */
670
        m_fileNewAction->setEnabled(true);
×
671
        m_fileOpenAction->setEnabled(true);
×
672
        m_liveEditAction->setEnabled(false);
×
673
        m_liveEditVirtualConsoleAction->setEnabled(false);
×
674

675
        m_modeToggleAction->setIcon(QIcon(":/operate.png"));
×
676
        m_modeToggleAction->setText(tr("Operate"));
×
677
        m_modeToggleAction->setToolTip(tr("Switch to operate mode"));
×
678
    }
679
}
×
680

681
/*****************************************************************************
682
 * Actions and toolbar
683
 *****************************************************************************/
684

685
void App::initActions()
×
686
{
687
    /* File actions */
688
    m_fileNewAction = new QAction(QIcon(":/filenew.png"), tr("&New"), this);
×
689
    m_fileNewAction->setShortcut(QKeySequence("CTRL+N"));
×
690
    connect(m_fileNewAction, SIGNAL(triggered(bool)), this, SLOT(slotFileNew()));
×
691

692
    m_fileOpenAction = new QAction(QIcon(":/fileopen.png"), tr("&Open"), this);
×
693
    m_fileOpenAction->setShortcut(QKeySequence("CTRL+O"));
×
694
    connect(m_fileOpenAction, SIGNAL(triggered(bool)), this, SLOT(slotFileOpen()));
×
695

696
    m_fileSaveAction = new QAction(QIcon(":/filesave.png"), tr("&Save"), this);
×
697
    m_fileSaveAction->setShortcut(QKeySequence("CTRL+S"));
×
698
    connect(m_fileSaveAction, SIGNAL(triggered(bool)), this, SLOT(slotFileSave()));
×
699

700
    m_fileSaveAsAction = new QAction(QIcon(":/filesaveas.png"), tr("Save &As..."), this);
×
701
    connect(m_fileSaveAsAction, SIGNAL(triggered(bool)), this, SLOT(slotFileSaveAs()));
×
702

703
    /* Control actions */
704
    m_modeToggleAction = new QAction(QIcon(":/operate.png"), tr("&Operate"), this);
×
705
    m_modeToggleAction->setToolTip(tr("Switch to operate mode"));
×
706
    m_modeToggleAction->setShortcut(QKeySequence("CTRL+F12"));
×
707
    connect(m_modeToggleAction, SIGNAL(triggered(bool)), this, SLOT(slotModeToggle()));
×
708

709
    m_controlMonitorAction = new QAction(QIcon(":/monitor.png"), tr("&Monitor"), this);
×
710
    m_controlMonitorAction->setShortcut(QKeySequence("CTRL+M"));
×
711
    connect(m_controlMonitorAction, SIGNAL(triggered(bool)), this, SLOT(slotControlMonitor()));
×
712

713
    m_addressToolAction = new QAction(QIcon(":/diptool.png"), tr("Address Tool"), this);
×
714
    connect(m_addressToolAction, SIGNAL(triggered()), this, SLOT(slotAddressTool()));
×
715

716
    m_controlBlackoutAction = new QAction(QIcon(":/blackout.png"), tr("Toggle &Blackout"), this);
×
717
    m_controlBlackoutAction->setCheckable(true);
×
718
    connect(m_controlBlackoutAction, SIGNAL(triggered(bool)), this, SLOT(slotControlBlackout()));
×
719
    m_controlBlackoutAction->setChecked(m_doc->inputOutputMap()->blackout());
×
720

721
    m_liveEditAction = new QAction(QIcon(":/liveedit.png"), tr("Live edit a function"), this);
×
722
    connect(m_liveEditAction, SIGNAL(triggered()), this, SLOT(slotFunctionLiveEdit()));
×
723
    m_liveEditAction->setEnabled(false);
×
724

725
    m_liveEditVirtualConsoleAction = new QAction(QIcon(":/liveedit_vc.png"), tr("Toggle Virtual Console Live edit"), this);
×
726
    connect(m_liveEditVirtualConsoleAction, SIGNAL(triggered()), this, SLOT(slotLiveEditVirtualConsole()));
×
727
    m_liveEditVirtualConsoleAction->setCheckable(true);
×
728
    m_liveEditVirtualConsoleAction->setEnabled(false);
×
729

730
    m_dumpDmxAction = new QAction(QIcon(":/add_dump.png"), tr("Dump DMX values to a function"), this);
×
731
    m_dumpDmxAction->setShortcut(QKeySequence("CTRL+D"));
×
732
    connect(m_dumpDmxAction, SIGNAL(triggered()), this, SLOT(slotDumpDmxIntoFunction()));
×
733

734
    m_controlPanicAction = new QAction(QIcon(":/panic.png"), tr("Stop ALL functions!"), this);
×
735
    m_controlPanicAction->setShortcut(QKeySequence("CTRL+SHIFT+ESC"));
×
736
    connect(m_controlPanicAction, SIGNAL(triggered(bool)), this, SLOT(slotControlPanic()));
×
737

738
    m_fadeAndStopMenu = new QMenu();
×
739
    QAction *fade1 = new QAction(tr("Fade 1 second and stop"), this);
×
740
    fade1->setData(QVariant(1000));
×
741
    connect(fade1, SIGNAL(triggered()), this, SLOT(slotFadeAndStopAll()));
×
742
    m_fadeAndStopMenu->addAction(fade1);
×
743

744
    QAction *fade5 = new QAction(tr("Fade 5 seconds and stop"), this);
×
745
    fade5->setData(QVariant(5000));
×
746
    connect(fade5, SIGNAL(triggered()), this, SLOT(slotFadeAndStopAll()));
×
747
    m_fadeAndStopMenu->addAction(fade5);
×
748

749
    QAction *fade10 = new QAction(tr("Fade 10 second and stop"), this);
×
750
    fade10->setData(QVariant(10000));
×
751
    connect(fade10, SIGNAL(triggered()), this, SLOT(slotFadeAndStopAll()));
×
752
    m_fadeAndStopMenu->addAction(fade10);
×
753

754
    QAction *fade30 = new QAction(tr("Fade 30 second and stop"), this);
×
755
    fade30->setData(QVariant(30000));
×
756
    connect(fade30, SIGNAL(triggered()), this, SLOT(slotFadeAndStopAll()));
×
757
    m_fadeAndStopMenu->addAction(fade30);
×
758

759
    m_controlPanicAction->setMenu(m_fadeAndStopMenu);
×
760

761
    m_controlFullScreenAction = new QAction(QIcon(":/fullscreen.png"), tr("Toggle Full Screen"), this);
×
762
    m_controlFullScreenAction->setCheckable(true);
×
763
    m_controlFullScreenAction->setShortcut(QKeySequence("CTRL+F11"));
×
764
    connect(m_controlFullScreenAction, SIGNAL(triggered(bool)), this, SLOT(slotControlFullScreen()));
×
765

766
    /* Help actions */
767
    m_helpIndexAction = new QAction(QIcon(":/help.png"), tr("&Index"), this);
×
768
    m_helpIndexAction->setShortcut(QKeySequence("SHIFT+F1"));
×
769
    connect(m_helpIndexAction, SIGNAL(triggered(bool)), this, SLOT(slotHelpIndex()));
×
770

771
    m_helpAboutAction = new QAction(QIcon(":/qlcplus.png"), tr("&About QLC+"), this);
×
772
    connect(m_helpAboutAction, SIGNAL(triggered(bool)), this, SLOT(slotHelpAbout()));
×
773

774
    if (QLCFile::hasWindowManager() == false)
×
775
    {
776
        m_quitAction = new QAction(QIcon(":/exit.png"), tr("Quit QLC+"), this);
×
777
        m_quitAction->setShortcut(QKeySequence("CTRL+ALT+Backspace"));
×
778
        connect(m_quitAction, SIGNAL(triggered(bool)), this, SLOT(close()));
×
779
    }
780
}
×
781

782
void App::initToolBar()
×
783
{
784
    m_toolbar = new QToolBar(tr("Workspace"), this);
×
785
    m_toolbar->setFloatable(false);
×
786
    m_toolbar->setMovable(false);
×
787
    m_toolbar->setAllowedAreas(Qt::TopToolBarArea);
×
788
    m_toolbar->setContextMenuPolicy(Qt::CustomContextMenu);
×
789
    addToolBar(m_toolbar);
×
790
    m_toolbar->addAction(m_fileNewAction);
×
791
    m_toolbar->addAction(m_fileOpenAction);
×
792
    m_toolbar->addAction(m_fileSaveAction);
×
793
    m_toolbar->addAction(m_fileSaveAsAction);
×
794
    m_toolbar->addSeparator();
×
795
    m_toolbar->addAction(m_controlMonitorAction);
×
796
    m_toolbar->addAction(m_addressToolAction);
×
797
    m_toolbar->addSeparator();
×
798
    m_toolbar->addAction(m_controlFullScreenAction);
×
799
    m_toolbar->addAction(m_helpIndexAction);
×
800
    m_toolbar->addAction(m_helpAboutAction);
×
801
    if (QLCFile::hasWindowManager() == false)
×
802
        m_toolbar->addAction(m_quitAction);
×
803

804
    /* Create an empty widget between help items to flush them to the right */
805
    QWidget* widget = new QWidget(this);
×
806
    widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
×
807
    m_toolbar->addWidget(widget);
×
808
    m_toolbar->addAction(m_dumpDmxAction);
×
809
    m_toolbar->addAction(m_liveEditAction);
×
810
    m_toolbar->addAction(m_liveEditVirtualConsoleAction);
×
811
    m_toolbar->addSeparator();
×
812
    m_toolbar->addAction(m_controlPanicAction);
×
813
    m_toolbar->addSeparator();
×
814
    m_toolbar->addAction(m_controlBlackoutAction);
×
815
    m_toolbar->addSeparator();
×
816
    m_toolbar->addAction(m_modeToggleAction);
×
817

818
    QToolButton* btn = qobject_cast<QToolButton*> (m_toolbar->widgetForAction(m_fileOpenAction));
×
819
    Q_ASSERT(btn != NULL);
×
820
    btn->setPopupMode(QToolButton::DelayedPopup);
×
821
    updateFileOpenMenu("");
×
822

823
    btn = qobject_cast<QToolButton*> (m_toolbar->widgetForAction(m_controlPanicAction));
×
824
    Q_ASSERT(btn != NULL);
×
825
    btn->setPopupMode(QToolButton::DelayedPopup);
×
826
}
×
827

828
/*****************************************************************************
829
 * File action slots
830
 *****************************************************************************/
831

832
bool App::handleFileError(QFile::FileError error)
×
833
{
834
    QString msg;
×
835

836
    switch (error)
×
837
    {
838
        case QFile::NoError:
×
839
            return true;
×
840
        break;
841
        case QFile::ReadError:
×
842
            msg = tr("Unable to read from file");
×
843
        break;
×
844
        case QFile::WriteError:
×
845
            msg = tr("Unable to write to file");
×
846
        break;
×
847
        case QFile::FatalError:
×
848
            msg = tr("A fatal error occurred");
×
849
        break;
×
850
        case QFile::ResourceError:
×
851
            msg = tr("Unable to access resource");
×
852
        break;
×
853
        case QFile::OpenError:
×
854
            msg = tr("Unable to open file for reading or writing");
×
855
        break;
×
856
        case QFile::AbortError:
×
857
            msg = tr("Operation was aborted");
×
858
        break;
×
859
        case QFile::TimeOutError:
×
860
            msg = tr("Operation timed out");
×
861
        break;
×
862
        default:
×
863
        case QFile::UnspecifiedError:
864
            msg = tr("An unspecified error has occurred. Nice.");
×
865
        break;
×
866
    }
867

868
    QMessageBox::warning(this, tr("File error"), msg);
×
869

870
    return false;
×
871
}
×
872

873
bool App::saveModifiedDoc(const QString & title, const QString & message)
×
874
{
875
    // if it's not modified, there's nothing to save
876
    if (m_doc->isModified() == false)
×
877
        return true;
×
878

879
    int result = QMessageBox::warning(this, title,
×
880
                                          message,
881
                                          QMessageBox::Yes |
×
882
                                          QMessageBox::No |
×
883
                                          QMessageBox::Cancel);
884
    if (result == QMessageBox::Yes)
×
885
    {
886
        slotFileSave();
×
887
        // we check whether m_doc is not modified anymore, rather than
888
        // result of slotFileSave() since the latter returns NoError
889
        // in cases like when the user pressed cancel in the save dialog
890
        if (m_doc->isModified() == false)
×
891
        {
892
            return true;
×
893
        }
894
        else
895
        {
896
            return false;
×
897
        }
898
    }
899
    else if (result == QMessageBox::No)
×
900
    {
901
        return true;
×
902
    }
903
    else
904
    {
905
        return false;
×
906
    }
907
}
908

909
void App::updateFileOpenMenu(QString addRecent)
×
910
{
911
    QSettings settings;
×
912
    QStringList menuRecentList;
×
913

914
    if (m_fileOpenMenu == NULL)
×
915
    {
916
        m_fileOpenMenu = new QMenu(this);
×
917
        QPalette p = palette();
×
918
        QString style = QString("QMenu { background: %1;"
×
919
                        "border: 1px solid black; font:bold; }"
920
                        "QMenu::item { background-color: transparent; padding: 5px 10px 5px 10px; border: 1px solid black; }"
921
                        "QMenu::item:selected { background-color: #2D8CFF; }").arg(p.color(QPalette::Window).name());
×
922
        m_fileOpenMenu->setStyleSheet(style);
×
923
        connect(m_fileOpenMenu, SIGNAL(triggered(QAction*)),
×
924
                this, SLOT(slotRecentFileClicked(QAction*)));
925
    }
×
926

927
    foreach (QAction* a, m_fileOpenMenu->actions())
×
928
    {
929
        menuRecentList.append(a->text());
×
930
        m_fileOpenMenu->removeAction(a);
×
931
    }
×
932

933
    if (addRecent.isEmpty() == false)
×
934
    {
935
        menuRecentList.removeAll(addRecent); // in case the string is already present, remove it...
×
936
        menuRecentList.prepend(addRecent); // and add it to the top
×
937
        for (int i = 0; i < menuRecentList.count(); i++)
×
938
        {
939
            settings.setValue(QString("%1%2").arg(SETTINGS_RECENTFILE).arg(i), menuRecentList.at(i));
×
940
            m_fileOpenMenu->addAction(menuRecentList.at(i));
×
941
        }
942
    }
943
    else
944
    {
945
        for (int i = 0; i < MAX_RECENT_FILES; i++)
×
946
        {
947
            QVariant recent = settings.value(QString("%1%2").arg(SETTINGS_RECENTFILE).arg(i));
×
948
            if (recent.isValid() == true)
×
949
            {
950
                menuRecentList.append(recent.toString());
×
951
                m_fileOpenMenu->addAction(menuRecentList.at(i));
×
952
            }
953
        }
×
954
    }
955

956
    // Set the recent files menu to the file open action
957
    if (menuRecentList.isEmpty() == false)
×
958
        m_fileOpenAction->setMenu(m_fileOpenMenu);
×
959
}
×
960

961
bool App::slotFileNew()
×
962
{
963
    QString msg(tr("Do you wish to save the current workspace?\n" \
964
                   "Changes will be lost if you don't save them."));
×
965
    if (saveModifiedDoc(tr("New Workspace"), msg) == false)
×
966
    {
967
        return false;
×
968
    }
969

970
    clearDocument();
×
971
    return true;
×
972
}
×
973

974
QFile::FileError App::slotFileOpen()
×
975
{
976
    QString fn;
×
977

978
    /* Check that the user is aware of losing previous changes */
979
    QString msg(tr("Do you wish to save the current workspace?\n" \
980
                   "Changes will be lost if you don't save them."));
×
981
    if (saveModifiedDoc(tr("Open Workspace"), msg) == false)
×
982
    {
983
        /* Second thoughts... Cancel loading. */
984
        return QFile::NoError;
×
985
    }
986

987
    /* Create a file open dialog */
988
    QFileDialog dialog(this);
×
989
    dialog.setWindowTitle(tr("Open Workspace"));
×
990
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
×
991
    dialog.selectFile(fileName());
×
992
    if (m_workingDirectory.exists() == true)
×
993
        dialog.setDirectory(m_workingDirectory);
×
994

995
    /* Append file filters to the dialog */
996
    QStringList filters;
×
997
    filters << tr("Workspaces (*%1)").arg(KExtWorkspace);
×
998
#if defined(WIN32) || defined(Q_OS_WIN)
999
    filters << tr("All Files (*.*)");
1000
#else
1001
    filters << tr("All Files (*)");
×
1002
#endif
1003
    dialog.setNameFilters(filters);
×
1004

1005
    /* Append useful URLs to the dialog */
1006
    QList <QUrl> sidebar;
×
1007
    sidebar.append(QUrl::fromLocalFile(QDir::homePath()));
×
1008
    sidebar.append(QUrl::fromLocalFile(QDir::rootPath()));
×
1009
    dialog.setSidebarUrls(sidebar);
×
1010

1011
    /* Get file name */
1012
    if (dialog.exec() != QDialog::Accepted)
×
1013
        return QFile::NoError;
×
1014
    QSettings settings;
×
1015
    m_workingDirectory = dialog.directory();
×
1016
    settings.setValue(SETTINGS_WORKINGPATH, m_workingDirectory.absolutePath());
×
1017

1018
    fn = dialog.selectedFiles().first();
×
1019
    if (fn.isEmpty() == true)
×
1020
        return QFile::NoError;
×
1021

1022
    /* Clear existing document data */
1023
    clearDocument();
×
1024

1025
#ifdef DEBUG_SPEED
1026
    speedTime.restart();
1027
#endif
1028

1029
    /* Load the file */
1030
    QFile::FileError error = loadXML(fn);
×
1031
    if (handleFileError(error) == true)
×
1032
        m_doc->resetModified();
×
1033

1034
#ifdef DEBUG_SPEED
1035
    qDebug() << "[App] Project loaded in" << speedTime.elapsed() << "ms.";
1036
#endif
1037

1038
    /* Update these in any case, since they are at least emptied now as
1039
       a result of calling clearDocument() a few lines ago. */
1040
    //if (FunctionManager::instance() != NULL)
1041
    //    FunctionManager::instance()->updateTree();
1042
    if (FixtureManager::instance() != NULL)
×
1043
        FixtureManager::instance()->updateView();
×
1044
    if (InputOutputManager::instance() != NULL)
×
1045
        InputOutputManager::instance()->updateList();
×
1046
    if (Monitor::instance() != NULL)
×
1047
        Monitor::instance()->updateView();
×
1048

1049
    updateFileOpenMenu(fn);
×
1050

1051
    return error;
×
1052
}
×
1053

1054
QFile::FileError App::slotFileSave()
×
1055
{
1056
    QFile::FileError error;
1057
    QString asfName = autoSaveFileName();
×
1058

1059
    /* Attempt to save with the existing name. Fall back to Save As. */
1060
    if (fileName().isEmpty() == true)
×
1061
        error = slotFileSaveAs();
×
1062
    else
1063
        error = saveXML(fileName());
×
1064

1065
    if (handleFileError(error))
×
1066
    {
1067
        QFile asFile(asfName);
×
1068
        if (asFile.exists())
×
1069
            asFile.remove();
×
1070
    }
×
1071
    return error;
×
1072
}
×
1073

1074
QFile::FileError App::slotFileSaveAs()
×
1075
{
1076
    QString fn;
×
1077
    QString asfName = autoSaveFileName();
×
1078

1079
    /* Create a file save dialog */
1080
    QFileDialog dialog(this);
×
1081
    dialog.setWindowTitle(tr("Save Workspace As"));
×
1082
    dialog.setAcceptMode(QFileDialog::AcceptSave);
×
1083
    dialog.selectFile(fileName());
×
1084

1085
    /* Append file filters to the dialog */
1086
    QStringList filters;
×
1087
    filters << tr("Workspaces (*%1)").arg(KExtWorkspace);
×
1088
#if defined(WIN32) || defined(Q_OS_WIN)
1089
    filters << tr("All Files (*.*)");
1090
#else
1091
    filters << tr("All Files (*)");
×
1092
#endif
1093
    dialog.setNameFilters(filters);
×
1094

1095
    /* Append useful URLs to the dialog */
1096
    QList <QUrl> sidebar;
×
1097
    sidebar.append(QUrl::fromLocalFile(QDir::homePath()));
×
1098
    sidebar.append(QUrl::fromLocalFile(QDir::rootPath()));
×
1099
    dialog.setSidebarUrls(sidebar);
×
1100

1101
    /* Get file name */
1102
    if (dialog.exec() != QDialog::Accepted)
×
1103
        return QFile::NoError;
×
1104

1105
    fn = dialog.selectedFiles().first();
×
1106
    if (fn.isEmpty() == true)
×
1107
        return QFile::NoError;
×
1108

1109
    /* Always use the workspace suffix */
1110
    if (fn.right(4) != KExtWorkspace)
×
1111
        fn += KExtWorkspace;
×
1112

1113
    /* Set the workspace path before saving the new XML. In this way local files
1114
       can be loaded even if the workspace file will be moved */
1115
    m_doc->setWorkspacePath(QFileInfo(fn).absolutePath());
×
1116

1117
    /* Save the document and set workspace name */
1118
    QFile::FileError error = saveXML(fn);
×
1119

1120
    if (handleFileError(error))
×
1121
    {
1122
        /* remove autosave file if present */
1123
        QFile asFile(asfName);
×
1124
        if (asFile.exists())
×
1125
            asFile.remove();
×
1126
    }
×
1127

1128
    updateFileOpenMenu(fn);
×
1129
    return error;
×
1130
}
×
1131

1132
/*****************************************************************************
1133
 * Control action slots
1134
 *****************************************************************************/
1135

1136
void App::slotControlMonitor()
×
1137
{
1138
    Monitor::createAndShow(this, m_doc);
×
1139
}
×
1140

1141
void App::slotAddressTool()
×
1142
{
1143
    AddressTool at(this);
×
1144
    at.exec();
×
1145
}
×
1146

1147
void App::slotControlBlackout()
×
1148
{
1149
    m_doc->inputOutputMap()->setBlackout(!m_doc->inputOutputMap()->blackout());
×
1150
}
×
1151

1152
void App::slotBlackoutChanged(bool state)
×
1153
{
1154
    m_controlBlackoutAction->setChecked(state);
×
1155
}
×
1156

1157
void App::slotControlPanic()
×
1158
{
1159
    m_doc->masterTimer()->stopAllFunctions();
×
1160
}
×
1161

1162
void App::slotFadeAndStopAll()
×
1163
{
1164
    QAction *action = (QAction *)sender();
×
1165
    int timeout = action->data().toInt();
×
1166

1167
    m_doc->masterTimer()->fadeAndStopAll(timeout);
×
1168
}
×
1169

1170
void App::slotRunningFunctionsChanged()
×
1171
{
1172
    if (m_doc->masterTimer()->runningFunctions() > 0)
×
1173
        m_controlPanicAction->setEnabled(true);
×
1174
    else
1175
        m_controlPanicAction->setEnabled(false);
×
1176
}
×
1177

1178
void App::slotDumpDmxIntoFunction()
×
1179
{
1180
    DmxDumpFactory ddf(m_doc, m_dumpProperties, this);
×
1181
    if (ddf.exec() != QDialog::Accepted)
×
1182
        return;
×
1183
}
×
1184

1185
void App::slotFunctionLiveEdit()
×
1186
{
1187
    FunctionSelection fs(this, m_doc);
×
1188
    fs.setMultiSelection(false);
×
1189
    fs.setFilter(Function::SceneType | Function::ChaserType | Function::SequenceType | Function::EFXType | Function::RGBMatrixType);
×
1190
    fs.disableFilters(Function::ShowType | Function::ScriptType | Function::CollectionType | Function::AudioType);
×
1191

1192
    if (fs.exec() == QDialog::Accepted)
×
1193
    {
1194
        if (fs.selection().count() > 0)
×
1195
        {
1196
            FunctionLiveEditDialog fle(m_doc, fs.selection().first(), this);
×
1197
            fle.exec();
×
1198
        }
×
1199
    }
1200
}
×
1201

1202
void App::slotLiveEditVirtualConsole()
×
1203
{
1204
    VirtualConsole::instance()->toggleLiveEdit();
×
1205
}
×
1206

1207
void App::slotDetachContext(int index)
×
1208
{
1209
    /* Get the widget that has been double-clicked */
1210
    QWidget *context = m_tab->widget(index);
×
1211
    context->setProperty("tabIndex", index);
×
1212
    context->setProperty("tabIcon", QVariant::fromValue(m_tab->tabIcon(index)));
×
1213
    context->setProperty("tabLabel", m_tab->tabText(index));
×
1214

1215
    qDebug() << "Detaching context" << context;
×
1216

1217
    DetachedContext *detachedWindow = new DetachedContext(this);
×
1218
    detachedWindow->setCentralWidget(context);
×
1219
    detachedWindow->resize(800, 600);
×
1220
    detachedWindow->show();
×
1221
    context->show();
×
1222

1223
    connect(detachedWindow, SIGNAL(closing()),
×
1224
            this, SLOT(slotReattachContext()));
1225
}
×
1226

1227
void App::slotReattachContext()
×
1228
{
1229
    DetachedContext *window = qobject_cast<DetachedContext *>(sender());
×
1230

1231
    QWidget *context = window->centralWidget();
×
1232
    int tabIndex = context->property("tabIndex").toInt();
×
1233
    QIcon tabIcon = context->property("tabIcon").value<QIcon>();
×
1234
    QString tabLabel = context->property("tabLabel").toString();
×
1235

1236
    qDebug() << "Reattaching context" << tabIndex << tabLabel << context;
×
1237

1238
    context->setParent(m_tab);
×
1239
    m_tab->insertTab(tabIndex, context, tabIcon, tabLabel);
×
1240
}
×
1241

1242
void App::slotControlFullScreen()
×
1243
{
1244
    static int wstate = windowState();
×
1245

1246
    if (windowState() & Qt::WindowFullScreen)
×
1247
    {
1248
        if (wstate & Qt::WindowMaximized)
×
1249
            showMaximized();
×
1250
        else
1251
            showNormal();
×
1252
        wstate = windowState();
×
1253
    }
1254
    else
1255
    {
1256
        wstate = windowState();
×
1257
        showFullScreen();
×
1258

1259
        // In case slotControlFullScreen() is called programmatically (from main.cpp)
1260
        if (m_controlFullScreenAction->isChecked() == false)
×
1261
            m_controlFullScreenAction->setChecked(true);
×
1262
    }
1263
}
×
1264

1265
void App::slotControlFullScreen(bool usingGeometry)
×
1266
{
1267
    if (usingGeometry == true)
×
1268
    {
1269
        QScreen *screen = QGuiApplication::screens().first();
×
1270
        setGeometry(screen->geometry());
×
1271
    }
1272
    else
1273
    {
1274
        slotControlFullScreen();
×
1275
    }
1276
}
×
1277

1278
/*****************************************************************************
1279
 * Help action slots
1280
 *****************************************************************************/
1281

1282
void App::slotHelpIndex()
×
1283
{
1284
    QDesktopServices::openUrl(QUrl("https://docs.qlcplus.org/"));
×
1285
}
×
1286

1287
void App::slotHelpAbout()
×
1288
{
1289
    AboutBox ab(this);
×
1290
    ab.exec();
×
1291
}
×
1292

1293
void App::slotRecentFileClicked(QAction *recent)
×
1294
{
1295
    if (recent == NULL)
×
1296
        return;
×
1297

1298
    QString recentAbsPath = recent->text();
×
1299
    QFile testFile(recentAbsPath);
×
1300
    if (testFile.exists() == false)
×
1301
    {
1302
        QMessageBox::critical(this, tr("Error"),
×
1303
                              tr("File not found!\nThe selected file has been moved or deleted."),
×
1304
                              QMessageBox::Close);
1305
        return;
×
1306
    }
1307

1308
    /* Check that the user is aware of losing previous changes */
1309
    QString msg(tr("Do you wish to save the current workspace?\n" \
1310
                   "Changes will be lost if you don't save them."));
×
1311
    if (saveModifiedDoc(tr("Open Workspace"), msg) == false)
×
1312
    {
1313
        /* Second thoughts... Cancel loading. */
1314
        return;
×
1315
    }
1316

1317
    m_workingDirectory = QFileInfo(recentAbsPath).absoluteDir();
×
1318
    QSettings settings;
×
1319
    settings.setValue(SETTINGS_WORKINGPATH, m_workingDirectory.absolutePath());
×
1320

1321
    /* Clear existing document data */
1322
    clearDocument();
×
1323

1324
#ifdef DEBUG_SPEED
1325
    speedTime.restart();
1326
#endif
1327

1328
    /* Load the file */
1329
    QFile::FileError error = loadXML(recentAbsPath);
×
1330
    if (handleFileError(error) == true)
×
1331
        m_doc->resetModified();
×
1332

1333
#ifdef DEBUG_SPEED
1334
    qDebug() << "[App] Project loaded in" << speedTime.elapsed() << "ms.";
1335
#endif
1336

1337
    /* Update these in any case, since they are at least emptied now as
1338
       a result of calling clearDocument() a few lines ago. */
1339
    //if (FunctionManager::instance() != NULL)
1340
    //    FunctionManager::instance()->updateTree();
1341
    if (FixtureManager::instance() != NULL)
×
1342
        FixtureManager::instance()->updateView();
×
1343
    if (InputOutputManager::instance() != NULL)
×
1344
        InputOutputManager::instance()->updateList();
×
1345
    if (Monitor::instance() != NULL)
×
1346
        Monitor::instance()->updateView();
×
1347
}
×
1348

1349
/*****************************************************************************
1350
 * Load & Save
1351
 *****************************************************************************/
1352

1353
void App::setFileName(const QString& fileName)
×
1354
{
1355
    m_fileName = fileName;
×
1356
}
×
1357

1358
QString App::fileName() const
×
1359
{
1360
    return m_fileName;
×
1361
}
1362

1363
QString App::autoSaveFileName() const
×
1364
{
1365
    QString fName = m_fileName;
×
1366

1367
    if (fName.isEmpty())
×
1368
        fName = "NewProject.autosave.qxw";
×
1369
    else
1370
    {
1371
        fName.remove(".qxw");
×
1372
        fName.append(".autosave.qxw");
×
1373
    }
1374

1375
    return fName;
×
1376
}
×
1377

1378
QFile::FileError App::loadXML(const QString& fileName)
×
1379
{
1380
    QFile::FileError retval = QFile::NoError;
×
1381

1382
    if (fileName.isEmpty() == true)
×
1383
        return QFile::OpenError;
×
1384

1385
    QXmlStreamReader *doc = QLCFile::getXMLReader(fileName);
×
1386
    if (doc == NULL || doc->device() == NULL || doc->hasError())
×
1387
    {
1388
        qWarning() << Q_FUNC_INFO << "Unable to read from" << fileName;
×
1389
        return QFile::ReadError;
×
1390
    }
1391

1392
    while (!doc->atEnd())
×
1393
    {
1394
        if (doc->readNext() == QXmlStreamReader::DTD)
×
1395
            break;
×
1396
    }
1397
    if (doc->hasError())
×
1398
    {
1399
        QLCFile::releaseXMLReader(doc);
×
1400
        return QFile::ResourceError;
×
1401
    }
1402

1403
    /* Set the workspace path before loading the new XML. In this way local files
1404
       can be loaded even if the workspace file has been moved */
1405
    m_doc->setWorkspacePath(QFileInfo(fileName).absolutePath());
×
1406

1407
    if (doc->dtdName() == KXMLQLCWorkspace)
×
1408
    {
1409
        if (loadXML(*doc) == false)
×
1410
        {
1411
            retval = QFile::ReadError;
×
1412
        }
1413
        else
1414
        {
1415
            setFileName(fileName);
×
1416
            m_doc->resetModified();
×
1417
            retval = QFile::NoError;
×
1418
        }
1419
    }
1420
    else
1421
    {
1422
        retval = QFile::ReadError;
×
1423
        qWarning() << Q_FUNC_INFO << fileName
×
1424
                   << "is not a workspace file";
×
1425
    }
1426

1427
    QLCFile::releaseXMLReader(doc);
×
1428

1429
    return retval;
×
1430
}
1431

1432
bool App::loadXML(QXmlStreamReader& doc, bool goToConsole, bool fromMemory)
×
1433
{
1434
    if (doc.readNextStartElement() == false)
×
1435
        return false;
×
1436

1437
    if (doc.name() != KXMLQLCWorkspace)
×
1438
    {
1439
        qWarning() << Q_FUNC_INFO << "Workspace node not found";
×
1440
        return false;
×
1441
    }
1442

1443
    QString activeWindowName = doc.attributes().value(KXMLQLCWorkspaceWindow).toString();
×
1444

1445
    while (doc.readNextStartElement())
×
1446
    {
1447
        if (doc.name() == KXMLQLCEngine)
×
1448
        {
1449
            m_doc->loadXML(doc);
×
1450
        }
1451
        else if (doc.name() == KXMLQLCVirtualConsole)
×
1452
        {
1453
            VirtualConsole::instance()->loadXML(doc);
×
1454
        }
1455
        else if (doc.name() == KXMLQLCSimpleDesk)
×
1456
        {
1457
            SimpleDesk::instance()->loadXML(doc);
×
1458
        }
1459
        else if (doc.name() == KXMLFixture)
×
1460
        {
1461
            /* Legacy support code, nowadays in Doc */
1462
            Fixture::loader(doc, m_doc);
×
1463
        }
1464
        else if (doc.name() == KXMLQLCFunction)
×
1465
        {
1466
            /* Legacy support code, nowadays in Doc */
1467
            Function::loader(doc, m_doc);
×
1468
        }
1469
        else if (doc.name() == KXMLQLCCreator)
×
1470
        {
1471
            /* Ignore creator information */
1472
            doc.skipCurrentElement();
×
1473
        }
1474
        else
1475
        {
1476
            qWarning() << Q_FUNC_INFO << "Unknown Workspace tag:" << doc.name();
×
1477
            doc.skipCurrentElement();
×
1478
        }
1479
    }
1480

1481
    if (goToConsole == true)
×
1482
        // Force the active window to be Virtual Console
1483
        setActiveWindow(VirtualConsole::staticMetaObject.className());
×
1484
    else
1485
        // Set the active window to what was saved in the workspace file
1486
        setActiveWindow(activeWindowName);
×
1487

1488
    // Perform post-load operations
1489
    VirtualConsole::instance()->postLoad();
×
1490

1491
    if (m_doc->errorLog().isEmpty() == false &&
×
1492
        fromMemory == false)
×
1493
    {
1494
        QMessageBox msg(QMessageBox::Warning, tr("Warning"),
×
1495
                        tr("Some errors occurred while loading the project:") + "<br><br>" + m_doc->errorLog(),
×
1496
                        QMessageBox::Ok);
×
1497
        msg.setTextFormat(Qt::RichText);
×
1498
        QSpacerItem* horizontalSpacer = new QSpacerItem(800, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
×
1499
        QGridLayout* layout = (QGridLayout*)msg.layout();
×
1500
        layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
×
1501
        msg.exec();
×
1502
    }
×
1503

1504
    m_doc->inputOutputMap()->startUniverses();
×
1505

1506
    return true;
×
1507
}
×
1508

1509
QFile::FileError App::saveXML(const QString& fileName, bool autosave)
×
1510
{
1511
    QString tempFileName(fileName);
×
1512
    tempFileName += ".temp";
×
1513
    QFile file(tempFileName);
×
1514
    if (file.open(QIODevice::WriteOnly) == false)
×
1515
        return file.error();
×
1516

1517
    QXmlStreamWriter doc(&file);
×
1518
    doc.setAutoFormatting(true);
×
1519
    doc.setAutoFormattingIndent(1);
×
1520
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
1521
    doc.setCodec("UTF-8");
1522
#endif
1523

1524
    doc.writeStartDocument();
×
1525
    doc.writeDTD(QString("<!DOCTYPE %1>").arg(KXMLQLCWorkspace));
×
1526

1527
    doc.writeStartElement(KXMLQLCWorkspace);
×
1528
    doc.writeAttribute("xmlns", QString("%1%2").arg(KXMLQLCplusNamespace).arg(KXMLQLCWorkspace));
×
1529
    /* Currently active window */
1530
    QWidget* widget = m_tab->currentWidget();
×
1531
    if (widget != NULL)
×
1532
        doc.writeAttribute(KXMLQLCWorkspaceWindow, QString(widget->metaObject()->className()));
×
1533

1534
    doc.writeStartElement(KXMLQLCCreator);
×
1535
    doc.writeTextElement(KXMLQLCCreatorName, APPNAME);
×
1536
    doc.writeTextElement(KXMLQLCCreatorVersion, APPVERSION);
×
1537
    doc.writeTextElement(KXMLQLCCreatorAuthor, QLCFile::currentUserName());
×
1538
    doc.writeEndElement(); // close KXMLQLCCreator
×
1539

1540
    /* Write engine components to the XML document */
1541
    m_doc->saveXML(&doc);
×
1542

1543
    /* Write virtual console to the XML document */
1544
    VirtualConsole::instance()->saveXML(&doc);
×
1545

1546
    /* Write Simple Desk to the XML document */
1547
    SimpleDesk::instance()->saveXML(&doc);
×
1548

1549
    doc.writeEndElement(); // close KXMLQLCWorkspace
×
1550

1551
    /* End the document and close all the open elements */
1552
    doc.writeEndDocument();
×
1553
    file.close();
×
1554
#ifdef Q_OS_UNIX
1555
    sync();
×
1556
#endif
1557

1558
    // Save to actual requested file name
1559
    QFile currFile(fileName);
×
1560
    if (currFile.exists() && !currFile.remove())
×
1561
    {
1562
        qWarning() << "Could not erase" << fileName;
×
1563
        return currFile.error();
×
1564
    }
1565
    if (!file.rename(fileName))
×
1566
    {
1567
        qWarning() << "Could not rename" << tempFileName << "to" << fileName;
×
1568
        return file.error();
×
1569
    }
1570

1571
    if (!autosave)
×
1572
    {
1573
        /* Set the file name for the current Doc instance and
1574
           set it also in an unmodified state. */
1575
        setFileName(fileName);
×
1576
        m_doc->resetModified();
×
1577
    }
1578

1579
    return QFile::NoError;
×
1580
}
×
1581

1582
void App::slotLoadDocFromMemory(QString xmlData)
×
1583
{
1584
    if (xmlData.isEmpty())
×
1585
        return;
×
1586

1587
    /* Clear existing document data */
1588
    clearDocument();
×
1589

1590
    QBuffer databuf;
×
1591
    databuf.setData(xmlData.simplified().toUtf8());
×
1592
    databuf.open(QIODevice::ReadOnly | QIODevice::Text);
×
1593

1594
    //qDebug() << "Buffer data:" << databuf.data();
1595
    QXmlStreamReader doc(&databuf);
×
1596

1597
    if (doc.hasError())
×
1598
    {
1599
        qWarning() << Q_FUNC_INFO << "Unable to read from XML in memory";
×
1600
        return;
×
1601
    }
1602

1603
    while (!doc.atEnd())
×
1604
    {
1605
        if (doc.readNext() == QXmlStreamReader::DTD)
×
1606
            break;
×
1607
    }
1608
    if (doc.hasError())
×
1609
    {
1610
        qDebug() << "XML has errors:" << doc.errorString();
×
1611
        return;
×
1612
    }
1613

1614
    if (doc.dtdName() == KXMLQLCWorkspace)
×
1615
        loadXML(doc, true, true);
×
1616
    else
1617
        qDebug() << "XML doesn't have a Workspace tag";
×
1618
}
×
1619

1620
void App::slotSaveAutostart(QString fileName)
×
1621
{
1622
    /* Set the workspace path before saving the new XML. In this way local files
1623
       can be loaded even if the workspace file will be moved */
1624
    m_doc->setWorkspacePath(QFileInfo(fileName).absolutePath());
×
1625

1626
    /* Save the document and set workspace name */
1627
    QFile::FileError error = saveXML(fileName);
×
1628
    handleFileError(error);
×
1629
}
×
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