aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/serialterminal/serialoutputpane.cpp
blob: 34657313f18892c7be9149836a1a2c6d7b67fbfb (plain)
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/****************************************************************************
**
** Copyright (C) 2018 Benjamin Balga
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/

#include "serialoutputpane.h"

#include "consolelineedit.h"
#include "serialcontrol.h"
#include "serialterminalconstants.h"

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <coreplugin/outputwindow.h>
#include <texteditor/fontsettings.h>
#include <texteditor/texteditorsettings.h>

#include <utils/algorithm.h>
#include <utils/icon.h>
#include <utils/outputformatter.h>
#include <utils/qtcassert.h>
#include <utils/theme/theme.h>
#include <utils/utilsicons.h>

#include <QAction>
#include <QComboBox>
#include <QLoggingCategory>
#include <QMenu>
#include <QTabBar>
#include <QToolButton>
#include <QVBoxLayout>

namespace SerialTerminal {
namespace Internal {

static Q_LOGGING_CATEGORY(log, Constants::LOGGING_CATEGORY, QtWarningMsg)

// Tab Widget helper for middle click tab close
class TabWidget : public QTabWidget
{
    Q_OBJECT
public:
    explicit TabWidget(QWidget *parent = nullptr);
signals:
    void contextMenuRequested(const QPoint &pos, int index);
protected:
    bool eventFilter(QObject *object, QEvent *event) final;
private:
    int m_tabIndexForMiddleClick = -1;
};

TabWidget::TabWidget(QWidget *parent) :
    QTabWidget(parent)
{
    tabBar()->installEventFilter(this);
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &QWidget::customContextMenuRequested,
            [this](const QPoint &pos) {
        emit contextMenuRequested(pos, tabBar()->tabAt(pos));
    });
}

bool TabWidget::eventFilter(QObject *object, QEvent *event)
{
    if (object == tabBar()) {
        if (event->type() == QEvent::MouseButtonPress) {
            const auto *me = static_cast<QMouseEvent *>(event);
            if (me->button() == Qt::MiddleButton) {
                m_tabIndexForMiddleClick = tabBar()->tabAt(me->pos());
                event->accept();
                return true;
            }
        } else if (event->type() == QEvent::MouseButtonRelease) {
            const auto *me = static_cast<QMouseEvent *>(event);
            if (me->button() == Qt::MiddleButton) {
                int tabIndex = tabBar()->tabAt(me->pos());
                if (tabIndex != -1 && tabIndex == m_tabIndexForMiddleClick)
                    emit tabCloseRequested(tabIndex);
                m_tabIndexForMiddleClick = -1;
                event->accept();
                return true;
            }
        }
    }
    return QTabWidget::eventFilter(object, event);
}

// QComboBox with a signal emitted before showPopup() to update on opening
class ComboBox : public QComboBox
{
    Q_OBJECT
public:
    void showPopup() final;
signals:
    void opened();
};

void ComboBox::showPopup()
{
    emit opened();
    QComboBox::showPopup();
}

SerialOutputPane::SerialControlTab::SerialControlTab(SerialControl *serialControl, Core::OutputWindow *w) :
    serialControl(serialControl), window(w)
{}

SerialOutputPane::SerialOutputPane(Settings &settings) :
    m_mainWidget(new QWidget),
    m_inputLine(new ConsoleLineEdit),
    m_tabWidget(new TabWidget),
    m_settings(settings),
    m_devicesModel(new SerialDeviceModel),
    m_closeCurrentTabAction(new QAction(tr("Close Tab"), this)),
    m_closeAllTabsAction(new QAction(tr("Close All Tabs"), this)),
    m_closeOtherTabsAction(new QAction(tr("Close Other Tabs"), this))
{
    createToolButtons();

    auto layout = new QVBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);

    m_tabWidget->setDocumentMode(true);
    m_tabWidget->setTabsClosable(true);
    m_tabWidget->setMovable(true);
    connect(m_tabWidget, &QTabWidget::tabCloseRequested,
            this, [this](int index) { closeTab(index); });
    layout->addWidget(m_tabWidget);

    connect(m_tabWidget, &QTabWidget::currentChanged, this, &SerialOutputPane::tabChanged);
    connect(m_tabWidget, &TabWidget::contextMenuRequested,
            this, &SerialOutputPane::contextMenuRequested);

    auto inputLayout = new QHBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(2);

    m_inputLine->setPlaceholderText(tr("Type text and hit Enter to send."));
    inputLayout->addWidget(m_inputLine);

    connect(m_inputLine, &QLineEdit::returnPressed, this, &SerialOutputPane::sendInput);

    m_lineEndingsSelection = new QComboBox;
    updateLineEndingsComboBox();
    inputLayout->addWidget(m_lineEndingsSelection);

    connect(m_lineEndingsSelection, QOverload<int>::of(&QComboBox::currentIndexChanged),
            this, &SerialOutputPane::defaultLineEndingChanged);

    layout->addLayout(inputLayout);

    m_mainWidget->setLayout(layout);

    enableDefaultButtons();
}

QWidget *SerialOutputPane::outputWidget(QWidget *parent)
{
    Q_UNUSED(parent)
    return m_mainWidget.get();
}

QList<QWidget *> SerialOutputPane::toolBarWidgets() const
{
    return { m_newButton,
                m_portsSelection, m_baudRateSelection,
                m_connectButton, m_disconnectButton,
                m_resetButton };
}

QString SerialOutputPane::displayName() const
{
    return tr(Constants::OUTPUT_PANE_TITLE);
}

int SerialOutputPane::priorityInStatusBar() const
{
    return 30;
}

void SerialOutputPane::clearContents()
{
    auto currentWindow = qobject_cast<Core::OutputWindow *>(m_tabWidget->currentWidget());
    if (currentWindow)
        currentWindow->clear();
}

void SerialOutputPane::visibilityChanged(bool)
{
    // Unused but pure virtual
}

bool SerialOutputPane::canFocus() const
{
    return m_tabWidget->currentWidget();
}

bool SerialOutputPane::hasFocus() const
{
    const QWidget *widget = m_tabWidget->currentWidget();
    return widget ? widget->window()->focusWidget() == widget : false;
}

void SerialOutputPane::setFocus()
{
    if (m_tabWidget->currentWidget())
        m_tabWidget->currentWidget()->setFocus();
}

bool SerialOutputPane::canNext() const
{
    return false;
}

bool SerialOutputPane::canPrevious() const
{
    return false;
}

void SerialOutputPane::goToNext()
{
    // Unused but pure virtual
}

void SerialOutputPane::goToPrev()
{
    // Unused but pure virtual
}

bool SerialOutputPane::canNavigate() const
{
    return false;
}

void SerialOutputPane::appendMessage(SerialControl *rc, const QString &out, Utils::OutputFormat format)
{
    const int index = indexOf(rc);
    if (index != -1) {
        Core::OutputWindow *window = m_serialControlTabs.at(index).window;
        window->appendMessage(out, format);
        if (format != Utils::NormalMessageFormat) {
            if (m_serialControlTabs.at(index).behaviorOnOutput == Flash)
                flash();
            else
                popup(NoModeSwitch);
        }
    }
}

void SerialOutputPane::setSettings(const Settings &settings)
{
    m_settings = settings;
}

void SerialOutputPane::createNewOutputWindow(SerialControl *rc)
{
    if (!rc)
        return;

    // Signals to update buttons
    connect(rc, &SerialControl::started,
            [this, rc]() {
        if (isCurrent(rc))
            enableButtons(rc, true);
    });

    connect(rc, &SerialControl::finished,
            [this, rc]() {
        rc->outputFormatter()->flush();
        if (isCurrent(rc))
            enableButtons(rc, false);
    });

    connect(rc, &SerialControl::appendMessageRequested,
            this, &SerialOutputPane::appendMessage);

    Utils::OutputFormatter *formatter = rc->outputFormatter();

    // Create new
    static uint counter = 0;
    Core::Id contextId = Core::Id(Constants::C_SERIAL_OUTPUT).withSuffix(counter++);
    Core::Context context(contextId);
    Core::OutputWindow *ow = new Core::OutputWindow(context, QString(), m_tabWidget);
    using TextEditor::TextEditorSettings;
    auto fontSettingsChanged = [ow] {
        ow->setBaseFont(TextEditorSettings::fontSettings().font());
    };

    connect(TextEditorSettings::instance(), &TextEditorSettings::fontSettingsChanged,
            this, fontSettingsChanged);
    fontSettingsChanged();
    ow->setWindowTitle(tr("Serial Terminal Window"));
    ow->setFormatter(formatter);
    // TODO: wordwrap, maxLineCount, zoom/wheelZoom (add to settings)

    auto controlTab = SerialControlTab(rc, ow);
    controlTab.lineEndingIndex = m_settings.defaultLineEndingIndex;
    controlTab.lineEnd = m_settings.defaultLineEnding();

    m_serialControlTabs.push_back(controlTab);
    m_tabWidget->addTab(ow, rc->displayName());
    m_tabWidget->setCurrentIndex(m_tabWidget->count()-1); // Focus new tab

    qCDebug(log) << "Adding tab for " << rc;

    updateCloseActions();
}

bool SerialOutputPane::closeTabs(CloseTabMode mode)
{
    bool allClosed = true;
    for (int t = m_tabWidget->count() - 1; t >= 0; t--) {
        if (!closeTab(t, mode))
            allClosed = false;
    }

    qCDebug(log) << "all tabs closed: " << allClosed;

    return allClosed;
}

void SerialOutputPane::createToolButtons()
{
    // TODO: add actions for keyboard shortcuts?

    // Connect button
    m_connectButton = new QToolButton;
    m_connectButton->setIcon(Utils::Icons::RUN_SMALL_TOOLBAR.icon());
    m_connectButton->setToolTip(tr("Connect"));
    m_connectButton->setAutoRaise(true);
    m_connectButton->setEnabled(false);
    connect(m_connectButton, &QToolButton::clicked,
            this, &SerialOutputPane::connectControl);

    // Disconnect button
    m_disconnectButton = new QToolButton;
    m_disconnectButton->setIcon(Utils::Icons::STOP_SMALL_TOOLBAR.icon());
    m_disconnectButton->setToolTip(tr("Disconnect"));
    m_disconnectButton->setAutoRaise(true);
    m_disconnectButton->setEnabled(false);

    connect(m_disconnectButton, &QToolButton::clicked,
            this, &SerialOutputPane::disconnectControl);

    // Reset button
    m_resetButton = new QToolButton;
    m_resetButton->setIcon(Utils::Icons::RELOAD.icon());
    m_resetButton->setToolTip(tr("Reset Board"));
    m_resetButton->setAutoRaise(true);
    m_resetButton->setEnabled(false);

    connect(m_resetButton, &QToolButton::clicked,
            this, &SerialOutputPane::resetControl);

    // New terminal button
    m_newButton = new QToolButton;
    m_newButton->setIcon(Utils::Icons::PLUS_TOOLBAR.icon());
    m_newButton->setToolTip(tr("Add New Terminal"));
    m_newButton->setAutoRaise(true);
    m_newButton->setEnabled(true);

    connect(m_newButton, &QToolButton::clicked,
            this, &SerialOutputPane::openNewTerminalControl);

    // Available devices box
    m_portsSelection = new ComboBox;
    m_portsSelection->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    m_portsSelection->setModel(m_devicesModel);
    updatePortsList();
    connect(m_portsSelection, &ComboBox::opened, this, &SerialOutputPane::updatePortsList);
    connect(m_portsSelection, QOverload<int>::of(&ComboBox::currentIndexChanged),
            this, &SerialOutputPane::activePortNameChanged);
    // TODO: the ports are not updated with the box opened (if the user wait for it) -> add a timer?

    // Baud rates box
    // TODO: add only most common bauds and custom field
    m_baudRateSelection = new ComboBox;
    m_baudRateSelection->setSizeAdjustPolicy(QComboBox::AdjustToContents);
    m_baudRateSelection->addItems(m_devicesModel->baudRates());
    connect(m_baudRateSelection, QOverload<int>::of(&ComboBox::currentIndexChanged),
            this, &SerialOutputPane::activeBaudRateChanged);

    if (m_settings.baudRate > 0)
        m_baudRateSelection->setCurrentIndex(m_devicesModel->indexForBaudRate(m_settings.baudRate));
    else // Set a default baudrate
        m_baudRateSelection->setCurrentIndex(m_devicesModel->indexForBaudRate(115200));
}

void SerialOutputPane::updateLineEndingsComboBox()
{
    m_lineEndingsSelection->clear();
    for (auto &value : m_settings.lineEndings)
        m_lineEndingsSelection->addItem(value.first, value.second);

    m_lineEndingsSelection->setCurrentIndex(m_settings.defaultLineEndingIndex);
}

void SerialOutputPane::updatePortsList()
{
    m_devicesModel->update();
    m_portsSelection->setCurrentIndex(m_devicesModel->indexForPort(m_settings.portName));
}

int SerialOutputPane::indexOf(const SerialControl *rc) const
{
    return Utils::indexOf(m_serialControlTabs, [rc](const SerialControlTab &tab) {
        return tab.serialControl == rc;
    });
}

int SerialOutputPane::indexOf(const QWidget *outputWindow) const
{
    return Utils::indexOf(m_serialControlTabs, [outputWindow](const SerialControlTab &tab) {
        return tab.window == outputWindow;
    });
}

int SerialOutputPane::currentIndex() const
{
    if (const QWidget *w = m_tabWidget->currentWidget())
        return indexOf(w);
    return -1;
}

SerialControl *SerialOutputPane::currentSerialControl() const
{
    const int index = currentIndex();
    if (index != -1)
        return m_serialControlTabs.at(index).serialControl;
    return nullptr;
}

bool SerialOutputPane::isCurrent(const SerialControl *rc) const
{
    const int index = currentIndex();
    return index >= 0 ? m_serialControlTabs.at(index).serialControl == rc : false;
}

int SerialOutputPane::findTabWithPort(const QString &portName) const
{
    return Utils::indexOf(m_serialControlTabs, [&portName](const SerialControlTab &tab) {
        return tab.serialControl->portName() == portName;
    });
}

int SerialOutputPane::findRunningTabWithPort(const QString &portName) const
{
    return Utils::indexOf(m_serialControlTabs, [&portName](const SerialControlTab &tab) {
        return tab.serialControl->isRunning() && tab.serialControl->portName() == portName;
    });
}

void SerialOutputPane::handleOldOutput(Core::OutputWindow *window) const
{
    // TODO: cleanOldAppOutput setting? (window->clear();)
    window->grayOutOldContent();
}

void SerialOutputPane::updateCloseActions()
{
    const int tabCount = m_tabWidget->count();
    m_closeCurrentTabAction->setEnabled(tabCount > 0);
    m_closeAllTabsAction->setEnabled(tabCount > 0);
    m_closeOtherTabsAction->setEnabled(tabCount > 1);
}

bool SerialOutputPane::closeTab(int tabIndex, CloseTabMode closeTabMode)
{
    Q_UNUSED(closeTabMode)

    int index = indexOf(m_tabWidget->widget(tabIndex));
    QTC_ASSERT(index != -1, return true);

    qCDebug(log) << "close tab " << tabIndex << m_serialControlTabs[index].serialControl
                 << m_serialControlTabs[index].window;

    // TODO: Prompt user to stop
    if (m_serialControlTabs[index].serialControl->isRunning()) {
        m_serialControlTabs[index].serialControl->stop(true); // Force stop
    }

    m_tabWidget->removeTab(tabIndex);
    delete m_serialControlTabs[index].serialControl;
    delete m_serialControlTabs[index].window;
    m_serialControlTabs.removeAt(index);
    updateCloseActions();

    if (m_serialControlTabs.isEmpty())
        hide();

    return true;
}

void SerialOutputPane::contextMenuRequested(const QPoint &pos, int index)
{
    QList<QAction *> actions { m_closeCurrentTabAction, m_closeAllTabsAction, m_closeOtherTabsAction };

    QAction *action = QMenu::exec(actions, m_tabWidget->mapToGlobal(pos), nullptr, m_tabWidget);
    const int currentIdx = index != -1 ? index : currentIndex();

    if (action == m_closeCurrentTabAction) {
        if (currentIdx >= 0)
            closeTab(currentIdx);
    } else if (action == m_closeAllTabsAction) {
        closeTabs(SerialOutputPane::CloseTabWithPrompt);
    } else if (action == m_closeOtherTabsAction) {
        for (int t = m_tabWidget->count() - 1; t >= 0; t--)
            if (t != currentIdx)
                closeTab(t);
    }
}

void SerialOutputPane::enableDefaultButtons()
{
    const SerialControl *rc = currentSerialControl();
    const bool isRunning = rc && rc->isRunning();
    enableButtons(rc, isRunning);
}

void SerialOutputPane::enableButtons(const SerialControl *rc, bool isRunning)
{
    // TODO: zoom buttons?
    if (rc) {
        m_connectButton->setEnabled(!isRunning);
        m_disconnectButton->setEnabled(isRunning);
        m_resetButton->setEnabled(isRunning);

        m_portsSelection->setEnabled(!isRunning);
        m_baudRateSelection->setEnabled(!isRunning);
    } else {
        m_connectButton->setEnabled(true);
        m_disconnectButton->setEnabled(false);

        m_portsSelection->setEnabled(true);
        m_baudRateSelection->setEnabled(true);
    }
}

void SerialOutputPane::tabChanged(int i)
{
    if (m_prevTabIndex >= 0 && m_prevTabIndex < m_serialControlTabs.size()) {
        m_serialControlTabs[m_prevTabIndex].inputText = m_inputLine->text();
        m_serialControlTabs[m_prevTabIndex].inputCursorPosition = m_inputLine->cursorPosition();
    }

    const int index = indexOf(m_tabWidget->widget(i));
    if (i != -1 && index != -1) {
        SerialControlTab &tab = m_serialControlTabs[index];
        const SerialControl *rc = tab.serialControl;

        // Update combobox index
        m_portsSelection->blockSignals(true);
        m_baudRateSelection->blockSignals(true);
        m_lineEndingsSelection->blockSignals(true);

        m_portsSelection->setCurrentText(rc->portName());
        m_baudRateSelection->setCurrentText(rc->baudRateText());
        m_lineEndingsSelection->setCurrentIndex(tab.lineEndingIndex);
        tab.lineEnd = m_lineEndingsSelection->currentData().toByteArray();

        m_portsSelection->blockSignals(false);
        m_baudRateSelection->blockSignals(false);
        m_lineEndingsSelection->blockSignals(false);

        m_inputLine->setText(tab.inputText);
        m_inputLine->setCursorPosition(tab.inputCursorPosition);

        qCDebug(log) << "Changed tab, is running:" << rc->isRunning();

        // Update buttons
        enableButtons(rc, rc->isRunning());
    } else {
        enableDefaultButtons();
    }

    m_prevTabIndex = index;
}

bool SerialOutputPane::isRunning() const
{
    return Utils::anyOf(m_serialControlTabs, [](const SerialControlTab &rt) {
        return rt.serialControl->isRunning();
    });
}

void SerialOutputPane::activePortNameChanged(int index)
{
    // When the model is updated, current index is reset -> set it back
    if (index < 0) {
        m_portsSelection->setCurrentText(m_currentPortName);
        return;
    }

    const QString pn = m_devicesModel->portName(index);

    if (SerialControl *current = currentSerialControl()) {
        if (current->portName() == pn || pn.isEmpty())
            return;

        m_currentPortName = current->portName();

        qCDebug(log) << "Set port to" << pn << "(" << index << ")";
        current->setPortName(pn);

        // Update tab text
        const int tabIndex = indexOf(current);
        if (tabIndex >= 0)
            m_tabWidget->setTabText(tabIndex, pn);
    }

    // Update current port name
    m_currentPortName = pn;
    m_settings.setPortName(pn);
    emit settingsChanged(m_settings);
}

void SerialOutputPane::activeBaudRateChanged(int index)
{
    if (index < 0)
        return;

    SerialControl *current = currentSerialControl();
    const qint32 br = m_devicesModel->baudRate(index);

    qCDebug(log) << "Set baudrate to" << br << "(" << index << ")";

    if (current)
        current->setBaudRate(br);

    m_settings.setBaudRate(br);
    emit settingsChanged(m_settings);
}

void SerialOutputPane::defaultLineEndingChanged(int index)
{
    if (index < 0)
        return;

    m_settings.setDefaultLineEndingIndex(index);
    const int currentControlIndex = currentIndex();
    if (currentControlIndex >= 0) {
        m_serialControlTabs[currentControlIndex].lineEnd =
                m_lineEndingsSelection->currentData().toByteArray();
    }

    qCDebug(log) << "Set default line ending to "
                 << m_settings.defaultLineEndingText()
                 << "(" << index << ")";

    emit settingsChanged(m_settings);
}

void SerialOutputPane::connectControl()
{
    const QString currentPortName = m_devicesModel->portName(m_portsSelection->currentIndex());
    if (currentPortName.isEmpty())
        return;

    SerialControl *current = currentSerialControl();
    const int index = currentIndex();
    // MAYBE: use current->canReUseOutputPane(...)?

    // Show tab if already opened and running
    const int i = findRunningTabWithPort(currentPortName);
    if (i >= 0) {
        m_tabWidget->setCurrentIndex(i);
        qCDebug(log) << "Port running in tab #" << i;
        return;
    }

    if (current) {
        current->setPortName(currentPortName);
        current->setBaudRate(m_devicesModel->baudRate(m_baudRateSelection->currentIndex()));
        // Gray out old and connect
        if (index != -1) {
            auto& tab = m_serialControlTabs[index];
            handleOldOutput(tab.window);
            tab.window->scrollToBottom();
        }
        qCDebug(log) << "Connect to" << current->portName();
    } else {
        // Create a new window
        auto rc = new SerialControl(m_settings);
        rc->setPortName(currentPortName);
        createNewOutputWindow(rc);

        qCDebug(log) << "Create and connect to" << rc->portName();

        current = rc;
    }

    // Update tab text
    if (index != -1)
        m_tabWidget->setTabText(index, current->portName());

    current->start();
}

void SerialOutputPane::disconnectControl()
{
    SerialControl *current = currentSerialControl();
    if (current) {
        current->stop(true);
        qCDebug(log) << "Disconnected.";
    }
}

void SerialOutputPane::resetControl()
{
    SerialControl *current = currentSerialControl();
    if (current)
        current->pulseDataTerminalReady();
}

void SerialOutputPane::openNewTerminalControl()
{
    const QString currentPortName = m_devicesModel->portName(m_portsSelection->currentIndex());
    if (currentPortName.isEmpty())
        return;

    auto rc = new SerialControl(m_settings);
    rc->setPortName(currentPortName);
    createNewOutputWindow(rc);

    qCDebug(log) << "Created new terminal on" << rc->portName();
}

// Send lineedit input to serial port
void SerialOutputPane::sendInput()
{
    SerialControl *current = currentSerialControl();
    const int index = currentIndex();
    if (current && current->isRunning() && index >= 0) {
        qCDebug(log) << "Sending:" << m_inputLine->text().toUtf8();

        current->writeData(m_inputLine->text().toUtf8() + m_serialControlTabs.at(index).lineEnd);
    }
    m_inputLine->selectAll();
    // TODO: add a blink or something to visually see if the data was sent or not
}

} // namespace Internal
} // namespace SerialTerminal

#include "serialoutputpane.moc"