mainwindow.cpp 22 KB
Newer Older
jhammen's avatar
jhammen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 * This file is part of Bipscript-IDE.
 *
 * Bipscript-IDE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Bipscript-IDE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Bipscript-IDE.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "mainwindow.h"

#include "ui_mainwindow.h"
#include "editorwidget.h"
#include "scriptrun.h"
#include "projectsdialog.h"
#include "version.h"

#include <QDir>
#include <QDebug>
#include <QFileDialog>
#include <QInputDialog>
#include <QTime>
#include <QLabel>
#include <QDesktopServices>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    audioEngine(AudioEngine::instance()),
    transportPosition(0),
    ui(new Ui::MainWindow),
    timeWidget(this),
    bbtWidget(this)
{
    ui->setupUi(this);

    // time in MM:SS + BBT + engine config
    QString labelStyle("QLabel {font-family: monospace; color: green }");
    timeWidget.setFrameStyle(QFrame::Panel | QFrame::Sunken);
    timeWidget.setStyleSheet(labelStyle);
    timeWidget.setText("00:00.000");
    ui->transportToolBar->addWidget(&timeWidget);
    bbtWidget.setFrameStyle(QFrame::Panel | QFrame::Sunken);
    bbtWidget.setStyleSheet(labelStyle);
    bbtWidget.setText("--|--|----");
    ui->transportToolBar->addWidget(&bbtWidget);
    engineWidget.setFrameStyle(QFrame::Panel | QFrame::Sunken);
    engineWidget.setStyleSheet("QLabel {font-family: monospace; color: blue }");
    engineWidget.setText("----- Hz");
    ui->transportToolBar->addWidget(&engineWidget);

    ui->folderTree->setColumnCount(1);

    ProjectList &projectList = ProjectList::getProjectList();
    projectList.load();
    if(projectList.currentExists()) {
        loadProject();
    } else {
        on_actionOpenProject_triggered();
    }

    connect(ui->folderTree, SIGNAL(itemClicked(QTreeWidgetItem *, int)), this,
            SLOT(folderTreeClicked(QTreeWidgetItem *, int)));

    connect(ui->tabWidget, SIGNAL(currentChanged(int)), this,
            SLOT(tabChanged(int)));

    connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this,
            SLOT(closeEditorTab(int)));
    connect(ui->outputTabWidget, SIGNAL(tabCloseRequested(int)), this,
            SLOT(closeOutputTab(int)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

// ------------ superclass methods

void MainWindow::closeEvent(QCloseEvent *event)
{
    // save settings
    ProjectList::getProjectList().save();
    // close tabs
    if(!saveAllTabs()) {
        event->ignore();
    }
    // kill running scripts
    if(!killAllScripts()) {
        event->ignore();
    }
}

// ------------ slots

void MainWindow::changeProject()
{
    ui->folderTree->clear();
    loadProject();
    // close all edit tabs not still applicable
    for(int i = ui->tabWidget->count() - 1; i >= 0; i--) {
        QWidget *widget = ui->tabWidget->widget(i);
        EditorWidget *editor = static_cast<EditorWidget*>(widget);
        if(findTreeItem(editor->path()) == nullptr) {
            ui->tabWidget->removeTab(i);
        }
    }
    selectedFolder = nullptr;
}

void MainWindow::closeEditorTab(int index)
{
    EditorWidget *editor = qobject_cast<EditorWidget*>(ui->tabWidget->widget(index));
    if(saveOrAbandon(editor)) {
        delete editor;
    }
}

void MainWindow::closeOutputTab(int index)
{
    OutputWidget *output = qobject_cast<OutputWidget*>(ui->outputTabWidget->widget(index));
    QString name = ui->outputTabWidget->tabText(index);
    if(killOrDetach(output, name)) {
        delete output;
    }
}

void MainWindow::codeError(const QString &path, int line)
{
    EditorWidget *editor = findEditor(path);
    if(editor) {
        editor->addError(line);
    }
}
void MainWindow::engineDisconnect()
{
    ui->actionStartStopTransport->setChecked(false);
    enableTransportControls(false);
    audioEngine.disconnect();
    timeWidget.setText("00:00.000");
    bbtWidget.setText("--|--|----");
    ui->actionJackConnect->setChecked(false);
    ui->actionJackConnect->setEnabled(true);
}

void MainWindow::createOrOpenEditor(NamedFile &file)
{
    EditorWidget *editor = findEditor(file.path);
    if (editor) {
        ui->tabWidget->setCurrentWidget(editor);
    } else {
        // new editor for this file
        editor = new EditorWidget(file);
        ui->tabWidget->addTab(editor, file.name);
        ui->tabWidget->setCurrentWidget(editor);
        editor->load();
        connect(editor->document(), SIGNAL(modificationChanged(bool)), this, SLOT(markDirty(bool)));
        connect(editor, SIGNAL(undoAvailable(bool)), ui->actionUndo, SLOT(setEnabled(bool)));
        connect(editor, SIGNAL(redoAvailable(bool)), ui->actionRedo, SLOT(setEnabled(bool)));
        connect(editor, SIGNAL(copyAvailable(bool)), ui->actionCut, SLOT(setEnabled(bool)));
        connect(editor, SIGNAL(copyAvailable(bool)), ui->actionCopy, SLOT(setEnabled(bool)));
    }
    selectedFolder = nullptr;
    enableFileActions(editor);
}

void MainWindow::createOrOpenEditor(QString &path)
{
    QFileInfo fileInfo(path);
    NamedFile file(fileInfo.fileName(), fileInfo.filePath());
    createOrOpenEditor(file);
}

EditorWidget *MainWindow::currentEditor()
{
    return static_cast<EditorWidget*>(ui->tabWidget->currentWidget());
}

void MainWindow::folderTreeClicked(QTreeWidgetItem *item, int)
{
    NamedFile &file = static_cast<FolderTreeItem*>(item)->file();
    QFileInfo fileInfo(file.path);
    if(fileInfo.isDir()) {
        selectedFolder = &(file.path);
        enableFileActions(nullptr);
    } else if(fileInfo.isFile()) {
        createOrOpenEditor(file);
    }
    else {
       QMessageBox::information(this, "Unknown Entry", file.path);
    }
}

void MainWindow::handleLink(const QUrl &url)
{
    QString path(url.path());
    createOrOpenEditor(path);
    if(url.hasQuery()) {
        int column = QString(url.query()).toInt();
        currentEditor()->focusLine(url.port(), column);
    } else {
        currentEditor()->focusLine(url.port());
    }
}

void MainWindow::markDirty(bool value)
{
    QString tabText(currentEditor()->name());
    if(value) { tabText.append("*"); }
    int index = ui->tabWidget->currentIndex();
    ui->tabWidget->setTabText(index, tabText);
    ui->actionSaveFile->setEnabled(value);
    setWindowModified(value);
}

void MainWindow::showTime(Position pos)
{
    QTime qtime(0, 0, 0);
    qtime = qtime.addMSecs(pos.msec);
    timeWidget.setText(qtime.toString("mm:ss.zzz"));
    bbtWidget.setText(QString("%1|%2|%3")
                      .arg(pos.bar, 2).arg(pos.beat, 2).arg(pos.tick, 4));
    transportPosition = pos.frame;
}

void MainWindow::tabChanged(int i)
{
    EditorWidget *editor = qobject_cast<EditorWidget*>(ui->tabWidget->widget(i));
    if(editor == nullptr) { // all tabs closed
        ui->folderTree->clearSelection();
        setWindowTitle(QCoreApplication::applicationName());
        enableFileActions(nullptr);
    } else {
        QTreeWidgetItem *item = findTreeItem(editor->path());
        if(item) {
            ui->folderTree->setCurrentItem(item);
        } else {
            ui->folderTree->clearSelection();
        }
        setWindowTitle(QString("%1[*]").arg(editor->name()));
        setWindowModified(editor->isDirty());
        enableFileActions(editor);
    }
}

// ------------ auto slots

void MainWindow::on_actionAbout_triggered()
{
    QString lang = QLocale::languageToString(QLocale().language());
    QString message = tr("Bipscript IDE %1\n%2\nQT %3")
            .arg(BIPSCRIPT_IDE_VERSION).arg(lang).arg(QT_VERSION_STR);
    QMessageBox::about(this, "Bipscript-IDE", message);
}

void MainWindow::on_actionAPIDocumentation_triggered()
{
    QStringList supported = {"en", "es"};
    QString lang = QLocale().name().left(2);
    if(supported.contains(lang)) { lang = supported[0]; }
    QString link = QString("http://www.bipscript.org/%1/api/").arg(lang);
    QDesktopServices::openUrl(QUrl(link));
}

void MainWindow::on_actionCloseFile_triggered()
{
    closeEditorTab(ui->tabWidget->currentIndex());
}

void MainWindow::on_actionComment_triggered()
{
    currentEditor()->commentSelection();
}

void MainWindow::on_actionCopy_triggered()
{
    currentEditor()->copy();
}

void MainWindow::on_actionCut_triggered()
{
    currentEditor()->cut();
}

void MainWindow::on_actionNewFile_triggered()
{
    QString message("New file in ");
    message.append(*selectedFolder);
    bool ok;
    QString name = QInputDialog::getText(this, "New File", message, QLineEdit::Normal, "", &ok);
    if(name.length() > 0) {
        // create file
        QFileInfo fileInfo(*selectedFolder + "/" + name);
        QString path(fileInfo.filePath());
        QFile file(path);
        if(file.exists()) {
            QMessageBox::warning(this, "File Exists", path + " already exists");
        } else {
            file.open(QIODevice::WriteOnly);
            file.close();
            // open in tree
            ui->folderTree->clear();
            loadProject();
        }
        QTreeWidgetItem *item = findTreeItem(path);
        ui->folderTree->setCurrentItem(item);
        createOrOpenEditor(path);
    }
}

void MainWindow::on_actionDeleteFile_triggered()
{
    QMessageBox msgBox;
    QString path(currentEditor()->path());
    msgBox.setText("Delete " + path);
    msgBox.setInformativeText("Do you want to delete " + currentEditor()->name() + "?");
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Cancel);
    int choice = msgBox.exec();
    if(choice == QMessageBox::Ok) {
        // remove from tree
        FolderTreeItem *item = static_cast<FolderTreeItem*>(findTreeItem(path));
        item->parent()->removeChild(item);
        // remove editor if any
        delete findEditor(path);
        // remove file from filesystem
        QFile::remove(path);
    }
}

void MainWindow::on_actionExamples_triggered()
{
    QRegExp versionExp = QRegExp("v([0-9\\.]+)");
    QString versionString = QString(BIPSCRIPT_IDE_VERSION);
    if(versionExp.indexIn(versionString) == 0) {
        QString version = versionExp.cap(1);
        QString link("https://github.com/bipscript/examples/releases/tag/v");
        link.append(version);
        QDesktopServices::openUrl(QUrl(link));
    } else {
        QString message = tr("No examples for version %1").arg(versionString);
        QMessageBox::warning(this, tr("Unknown Version"), message, QMessageBox::Ok);
    }
}

void MainWindow::on_actionExit_triggered()
{
    QApplication::closeAllWindows();
}

void MainWindow::on_actionFastForward_triggered()
{
    transportPosition = transportPosition + audioConfig.framesPerSecond;
    audioEngine.reposition(transportPosition);
}

/**
 * @brief MainWindow::on_actionRun_triggered
 *
 * action to run script
 */
void MainWindow::on_actionRun_triggered()
{
    // check if possible
    if(!ScriptRun::possible()) {
        QMessageBox::warning(this, "Executable", "Cannot launch bipscript executable");
        return;
    }
    ScriptRun *run = new ScriptRun(currentEditor()->path());
    // add output widget for this run
    OutputWidget *out = new OutputWidget();
    ui->outputTabWidget->addTab(out, currentEditor()->name());
    ui->outputTabWidget->setCurrentWidget(out);
    connect(run, SIGNAL(started(int)), out, SLOT(running(int)));
    connect(run, SIGNAL(readStdout(QString)), out, SLOT(write(QString)));
    connect(run, SIGNAL(readStderr(QString)), out, SLOT(writeError(QString)));
    connect(run, SIGNAL(error(QString)), out, SLOT(writeError(QString)));
    connect(run, SIGNAL(exited(int)),  out, SLOT(completed(int)));
    connect(out, SIGNAL(codeError(const QString &, int)), this, SLOT(codeError(const QString &, int)));
    connect(out, SIGNAL(linkClicked(const QUrl&)), this, SLOT(handleLink(const QUrl&)));
    // show output area if hidden
    ui->outputDockWidget->show();
    run->start();
}

void MainWindow::enableTransportControls(bool enabled)
{
    ui->actionStartStopTransport->setEnabled(enabled);
    ui->actionRewind->setEnabled(enabled);
    ui->actionRestart->setEnabled(enabled);
    ui->actionFastForward->setEnabled(enabled);
}

void MainWindow::on_actionJackConnect_toggled(bool connected)
{
    if(connected) {
        audioConfig = audioEngine.connect();
        atimer = audioEngine.timerService();
        connect(atimer, SIGNAL(transport(Position)), this, SLOT(showTime(Position)));
        connect(atimer, SIGNAL(rolling(bool)), ui->actionStartStopTransport, SLOT(setChecked(bool)));
        connect(atimer, SIGNAL(finished()), this, SLOT(engineDisconnect()));
        engineWidget.setText(QString("%1 Hz").arg(audioConfig.framesPerSecond));
    } else {
        ui->actionJackConnect->setEnabled(false);
        atimer->stop();
        engineWidget.setText("----- Hz");
    }
    enableTransportControls(connected && !ui->actionTransportLock->isChecked());
}

void MainWindow::on_actionOpenFile_triggered()
{
    ProjectList &projectList = ProjectList::getProjectList();
    QFileDialog fileDialog(this);
    fileDialog.setFileMode(QFileDialog::AnyFile);
    fileDialog.setDirectory(projectList.commonRoot());
    if(fileDialog.exec()) {
        QString path = fileDialog.selectedFiles()[0];
        createOrOpenEditor(path);
    }
}

void MainWindow::on_actionOpenProject_triggered()
{
    if(saveAllTabs()) {
        ProjectsDialog projectsDialog(this);
        connect(&projectsDialog, SIGNAL(projectChanged()), this, SLOT(changeProject()));
        projectsDialog.exec();
    }
}

void MainWindow::on_actionPaste_triggered()
{
    currentEditor()->paste();
}

void MainWindow::on_actionProjectOptions_triggered()
{
    ProjectList &projects = ProjectList::getProjectList();
    Project &project = projects.currentProject();
    QString value = project.executable();
    QString message("Executable Path");
    bool ok;
    QString name = QInputDialog::getText(this, "Options", message, QLineEdit::Normal, value, &ok);
    if(name.length() > 0) {
        // TODO: test existance etc.
        project.executable(name);
        ScriptRun::setExecutable(project.executable());
    }
}

void MainWindow::on_actionRedo_triggered()
{
    currentEditor()->redo();
}

void MainWindow::on_actionRenameFile_triggered()
{
    if(saveOrAbandon(currentEditor())) {
        QString message("New name");
        QString oldPath = currentEditor()->path();
        bool ok;
        QString path = QInputDialog::getText(this, "Rename", message, QLineEdit::Normal, oldPath, &ok);
        if(path.length() > 0) {
            QFileInfo fileInfo(path);
            if(fileInfo.exists()) {
                QString message = QString("Cannot move file, target exists:\n%1").arg(path);
                QMessageBox::warning(this, "File exists", message);
                return;
            }
            QFile oldFile(oldPath);
            if(!oldFile.rename(fileInfo.filePath())) {
                QString message = QString("Error on move\n %1 ->\n %2").arg(oldPath).arg(path);
                QMessageBox::warning(this, "File Move Error", message);
                return;
            }
            // success
            NamedFile file;
            file.name = fileInfo.fileName();
            file.path = fileInfo.filePath();
            // remove old from tree
            QTreeWidgetItem *item = findTreeItem(oldPath);
            if(item) {
                item->parent()->removeChild(item);
            }
            // add new to tree if possible
            QTreeWidgetItem *parent = findTreeItem(fileInfo.path());
            ui->folderTree->clearSelection();
            if(parent) {
                FolderTreeItem *newItem = new FolderTreeItem(parent, fileInfo.fileName(), fileInfo.filePath());
                ui->folderTree->setCurrentItem(newItem);
            }
            // update editor + tab
            currentEditor()->moveFile(file);
            int index = ui->tabWidget->currentIndex();
            QString tabText(currentEditor()->name());
            if(currentEditor()->isDirty()) { tabText.append("*"); }
            ui->tabWidget->setTabText(index, tabText);
            // update main window title
            setWindowTitle(QString("%1[*]").arg(file.name));
        }
    }
}

void MainWindow::on_actionRestart_triggered()
{
    audioEngine.reposition(0);
}

void MainWindow::on_actionRewind_triggered()
{
    int newpos = static_cast<int>(transportPosition - audioConfig.framesPerSecond);
    transportPosition = static_cast<uint>(qMax(newpos, 0));
    audioEngine.reposition(transportPosition);
}

void MainWindow::on_actionSaveFile_triggered()
{
    currentEditor()->save();
}

void MainWindow::on_actionStartStopTransport_toggled(bool start)
{
    if(start) {
        audioEngine.transportStart();
    } else {
        audioEngine.transportStop();
    }
    ui->actionJackConnect->setEnabled(!start);
}

void MainWindow::on_actionTransportLock_toggled(bool locked)
{
    bool enabled = !locked && ui->actionJackConnect->isChecked();
    ui->actionStartStopTransport->setEnabled(enabled);
    ui->actionRewind->setEnabled(enabled);
    ui->actionRestart->setEnabled(enabled);
    ui->actionFastForward->setEnabled(enabled);
}

void MainWindow::on_actionUndo_triggered()
{
    currentEditor()->undo();
}

// ------------ private methods

void MainWindow::enableFileActions(EditorWidget *editor)
{
    // requires file selected
    ui->actionDeleteFile->setEnabled(editor);
    ui->actionRun->setEnabled(editor);
    ui->actionPaste->setEnabled(editor);
    ui->actionCloseFile->setEnabled(editor);
    ui->actionComment->setEnabled(editor);
    ui->actionRenameFile->setEnabled(editor);
    // requires directory selected
    ui->actionNewFile->setEnabled(selectedFolder);
    // requires state from the editor
    ui->actionSaveFile->setEnabled(editor && editor->isDirty());
    ui->actionUndo->setEnabled(editor && editor->isUndoAvailable());
    ui->actionRedo->setEnabled(editor && editor->isRedoAvailable());
    ui->actionCut->setEnabled(editor && editor->hasSelection());
    ui->actionCopy->setEnabled(editor && editor->hasSelection());
}

EditorWidget *MainWindow::findEditor(const QString &path)
{
    for(int i = 0; i < ui->tabWidget->count(); i++) {
        EditorWidget *editor = qobject_cast<EditorWidget*>(ui->tabWidget->widget(i));
        if (editor->path() == path) { return editor; }
    }
    return nullptr;
}

QTreeWidgetItem *MainWindow::findTreeItem(const QString &path)
{
    QTreeWidgetItemIterator it(ui->folderTree);
    while (*it) {
        FolderTreeItem *item = static_cast<FolderTreeItem*>(*it);
        if (item->file().path == path) { return item; }
        ++it;
    }
    return nullptr;
}

void MainWindow::loadProject()
{
    ProjectList &projects = ProjectList::getProjectList();
    Project &project = projects.currentProject();
    ScriptRun::setExecutable(project.executable());
    for (int i = 0; i < project.folderCount(); ++i) {
        NamedFile &folder = project.folder(i);
        ui->folderTree->addTopLevelItem(toTreeFolder(folder));
    }
}

bool MainWindow::saveAllTabs()
{
    for(int i = 0; i < ui->tabWidget->count(); i++) {
        EditorWidget *editor = static_cast<EditorWidget*>(ui->tabWidget->widget(i));
        if(editor->isDirty()) {
            QMessageBox::StandardButton choice = unsavedChangesDialog(editor->path());
            if(choice == QMessageBox::Cancel) {
                return false;
            } else if(choice == QMessageBox::Save) {
                editor->save();
            }
        }
    }
    return true;
}

bool MainWindow::saveOrAbandon(EditorWidget *editor)
{
    if(editor->isDirty()) {
       QMessageBox::StandardButton choice = unsavedChangesDialog(editor->path());
        if(choice == QMessageBox::Cancel) {
            return false;
        } else if(choice == QMessageBox::Save) {
            if(!editor->save()) {
                return false;
            }
        }
    }
    return true;
}

bool MainWindow::killAllScripts()
{
    for(int i = 0; i < ui->outputTabWidget->count(); i++) {
        OutputWidget *output = qobject_cast<OutputWidget*>(ui->outputTabWidget->widget(i));
        QString name = ui->outputTabWidget->tabText(i);
        if(!killOrDetach(output, name)) {
            return false;
        }
    }
    return true;
}

bool MainWindow::killOrDetach(OutputWidget *output, const QString &name)
{
    if(output->pid() != 0) {
        QString id = QString("%1 pid %2").arg(name).arg(output->pid());
        QString mesg = "%1 is still running.\nDo you wish to kill the process?";
        QMessageBox::StandardButton choice = QMessageBox::warning(this, tr("Running Process"), mesg.arg(id),
                                                                  QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
        if(choice == QMessageBox::Cancel) {
            return false;
        } else if(choice == QMessageBox::Yes) {
            output->killScript(true);
        }
     }
     return true;
}

FolderTreeItem *MainWindow::toTreeFolder(NamedFile &folder)
{
    FolderTreeItem *folderItem = new FolderTreeItem(nullptr, folder.name, folder.path);
    QDir dir(folder.path);
    dir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
    foreach (QFileInfo fileInfo, dir.entryInfoList()) {
        if(fileInfo.isFile()) {
            folderItem->addChild(new FolderTreeItem(folderItem, fileInfo.fileName(), fileInfo.filePath()));
        } else if(fileInfo.isDir()) {
            NamedFile childFolder(fileInfo.baseName(), fileInfo.filePath());
            folderItem->addChild(toTreeFolder(childFolder));
        }
    }
    return folderItem;
}

QMessageBox::StandardButton MainWindow::unsavedChangesDialog(const QString &path)
{
    return QMessageBox::warning(this, tr("Unsaved Changes"), tr("%1 has unsaved changes.\nDo you wish to save?").arg(path),
                                QMessageBox::Save | QMessageBox::No | QMessageBox::Cancel);
}

void MainWindow::on_actionWebsite_triggered()
{
    QString link = "http://www.bipscript.org";
    QDesktopServices::openUrl(QUrl(link));
}