outputwindow.cpp 21.8 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
#include "outputwindow.h"
#include "projectexplorerconstants.h"
32
33
#include "projectexplorer.h"
#include "projectexplorersettings.h"
con's avatar
con committed
34
#include "runconfiguration.h"
dt's avatar
dt committed
35
#include "session.h"
36
#include "outputformatter.h"
con's avatar
con committed
37

38
#include <coreplugin/actionmanager/actionmanager.h>
39
40
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/command.h>
41
42
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
43
#include <coreplugin/uniqueidmanager.h>
44
#include <coreplugin/icontext.h>
con's avatar
con committed
45
46
#include <find/basetextfind.h>
#include <aggregation/aggregate.h>
dt's avatar
dt committed
47
48
#include <texteditor/basetexteditor.h>
#include <projectexplorer/project.h>
49
#include <qt4projectmanager/qt4projectmanagerconstants.h>
50
#include <utils/qtcassert.h>
con's avatar
con committed
51
52
53
54

#include <QtGui/QIcon>
#include <QtGui/QScrollBar>
#include <QtGui/QTextLayout>
55
#include <QtGui/QTextBlock>
con's avatar
con committed
56
57
58
59
60
61
62
#include <QtGui/QPainter>
#include <QtGui/QApplication>
#include <QtGui/QClipboard>
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>
#include <QtGui/QVBoxLayout>
#include <QtGui/QTabWidget>
63
#include <QtGui/QToolButton>
64
#include <QtGui/QShowEvent>
con's avatar
con committed
65

66
#include <QtCore/QDebug>
con's avatar
con committed
67

68
69
static const int MaxBlockCount = 100000;

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
enum { debug = 0 };

namespace ProjectExplorer {
namespace Internal {

OutputPane::RunControlTab::RunControlTab(RunControl *rc, OutputWindow *w) :
    runControl(rc), window(w), asyncClosing(false)
{
}

OutputPane::OutputPane() :
    m_mainWidget(new QWidget),
    m_tabWidget(new QTabWidget),
    m_stopAction(new QAction(QIcon(QLatin1String(Constants::ICON_STOP)), tr("Stop"), this)),
    m_reRunButton(new QToolButton),
    m_stopButton(new QToolButton)
con's avatar
con committed
86
{
dt's avatar
dt committed
87
88
89
90
91
    m_runIcon.addFile(Constants::ICON_RUN);
    m_runIcon.addFile(Constants::ICON_RUN_SMALL);

    m_debugIcon.addFile(Constants::ICON_DEBUG);
    m_debugIcon.addFile(Constants::ICON_DEBUG_SMALL);
con's avatar
con committed
92

93
    // Rerun
dt's avatar
dt committed
94
    m_reRunButton->setIcon(m_runIcon);
Friedemann Kleint's avatar
Friedemann Kleint committed
95
    m_reRunButton->setToolTip(tr("Re-run this run-configuration"));
con's avatar
con committed
96
97
98
99
100
    m_reRunButton->setAutoRaise(true);
    m_reRunButton->setEnabled(false);
    connect(m_reRunButton, SIGNAL(clicked()),
            this, SLOT(reRunRunControl()));

101
    // Stop
hjk's avatar
hjk committed
102
    Core::ActionManager *am = Core::ICore::instance()->actionManager();
103
    Core::Context globalcontext(Core::Constants::C_GLOBAL);
104
105
106
107

    m_stopAction->setToolTip(tr("Stop"));
    m_stopAction->setEnabled(false);

con's avatar
con committed
108
    Core::Command *cmd = am->registerAction(m_stopAction, Constants::STOP, globalcontext);
109
110

    m_stopButton->setDefaultAction(cmd->action());
con's avatar
con committed
111
    m_stopButton->setAutoRaise(true);
112
113

    connect(m_stopAction, SIGNAL(triggered()),
con's avatar
con committed
114
115
116
117
118
119
120
121
122
123
124
125
            this, SLOT(stopRunControl()));

    // Spacer (?)

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    m_tabWidget->setDocumentMode(true);
    m_tabWidget->setTabsClosable(true);
    m_tabWidget->setMovable(true);
    connect(m_tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
    layout->addWidget(m_tabWidget);

hjk's avatar
hjk committed
126
    connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
con's avatar
con committed
127
128

    m_mainWidget->setLayout(layout);
129

dt's avatar
dt committed
130
131
    connect(ProjectExplorer::ProjectExplorerPlugin::instance()->session(), SIGNAL(aboutToUnloadSession()),
            this, SLOT(aboutToUnloadSession()));
con's avatar
con committed
132
133
}

134
OutputPane::~OutputPane()
con's avatar
con committed
135
{
136
137
138
139
140
141
    if (debug)
        qDebug() << "OutputPane::~OutputPane: Entries left" << m_runControlTabs.size();

    foreach(const RunControlTab &rt, m_runControlTabs)
        delete rt.runControl;
    delete m_mainWidget;
142
143
}

144
int OutputPane::currentIndex() const
dt's avatar
dt committed
145
{
146
147
148
    if (const QWidget *w = m_tabWidget->currentWidget())
        return indexOf(w);
    return -1;
dt's avatar
dt committed
149
150
}

151
RunControl *OutputPane::currentRunControl() const
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
    const int index = currentIndex();
    if (index != -1)
        return m_runControlTabs.at(index).runControl;
    return 0;
}

int OutputPane::indexOf(const RunControl *rc) const
{
    for (int i = m_runControlTabs.size() - 1; i >= 0; i--)
        if (m_runControlTabs.at(i).runControl == rc)
            return i;
    return -1;
}

int OutputPane::indexOf(const QWidget *outputWindow) const
{
    for (int i = m_runControlTabs.size() - 1; i >= 0; i--)
        if (m_runControlTabs.at(i).window == outputWindow)
            return i;
    return -1;
}

int OutputPane::tabWidgetIndexOf(int runControlIndex) const
{
    if (runControlIndex >= 0 && runControlIndex < m_runControlTabs.size())
        return m_tabWidget->indexOf(m_runControlTabs.at(runControlIndex).window);
    return -1;
}

bool OutputPane::aboutToClose() const
{
    foreach(const RunControlTab &rt, m_runControlTabs)
        if (rt.runControl->isRunning() && !rt.runControl->aboutToStop())
            return false;
    return true;
}

void OutputPane::aboutToUnloadSession()
{
    closeTabs(true);
con's avatar
con committed
193
194
}

195
QWidget *OutputPane::outputWidget(QWidget *)
con's avatar
con committed
196
{
197
    return m_mainWidget;
con's avatar
con committed
198
199
}

hjk's avatar
hjk committed
200
QList<QWidget*> OutputPane::toolBarWidgets() const
con's avatar
con committed
201
{
Thorbjørn Lindeijer's avatar
Thorbjørn Lindeijer committed
202
    return QList<QWidget*>() << m_reRunButton << m_stopButton;
203
204
}

205
QString OutputPane::displayName() const
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
{
    return tr("Application Output");
}

int OutputPane::priorityInStatusBar() const
{
    return 60;
}

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

void OutputPane::visibilityChanged(bool /* b */)
{
}

bool OutputPane::hasFocus()
{
    return m_tabWidget->currentWidget() && m_tabWidget->currentWidget()->hasFocus();
}

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

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

con's avatar
con committed
242
243
244
245
246
void OutputPane::createNewOutputWindow(RunControl *rc)
{
    connect(rc, SIGNAL(started()),
            this, SLOT(runControlStarted()));
    connect(rc, SIGNAL(finished()),
247
248
            this, SLOT(runControlFinished()));

con's avatar
con committed
249
    // First look if we can reuse a tab
250
251
252
253
    const int size = m_runControlTabs.size();
    for (int i = 0; i < size; i++) {
        RunControlTab &tab =m_runControlTabs[i];
        if (tab.runControl->sameRunConfiguration(rc) && !tab.runControl->isRunning()) {
con's avatar
con committed
254
            // Reuse this tab
255
256
            delete tab.runControl;
            tab.runControl = rc;
257
            tab.window->handleOldOutput();
258
259
260
261
262
            tab.window->scrollToBottom();
            tab.window->setFormatter(rc->outputFormatter());
            if (debug)
                qDebug() << "OutputPane::createNewOutputWindow: Reusing tab" << i << " for " << rc;
            return;
con's avatar
con committed
263
264
        }
    }
265
266
267
268
269
270
271
272
273
274
275
276
    // Create new
    OutputWindow *ow = new OutputWindow(m_tabWidget);
    ow->setWindowTitle(tr("Application Output Window"));
    ow->setWindowIcon(QIcon(QLatin1String(Qt4ProjectManager::Constants::ICON_WINDOW)));
    ow->setFormatter(rc->outputFormatter());
    Aggregation::Aggregate *agg = new Aggregation::Aggregate;
    agg->add(ow);
    agg->add(new Find::BaseTextFind(ow));
    m_runControlTabs.push_back(RunControlTab(rc, ow));
    m_tabWidget->addTab(ow, rc->displayName());
    if (debug)
        qDebug() << "OutputPane::createNewOutputWindow: Adding tab for " << rc;
con's avatar
con committed
277
278
}

279
280
void OutputPane::appendApplicationOutput(RunControl *rc, const QString &out,
                                         bool onStdErr)
con's avatar
con committed
281
{
282
283
284
    const int index = indexOf(rc);
    if (index != -1)
        m_runControlTabs.at(index).window->appendApplicationOutput(out, onStdErr);
con's avatar
con committed
285
286
}

287
288
289
void OutputPane::appendApplicationOutputInline(RunControl *rc,
                                               const QString &out,
                                               bool onStdErr)
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
290
{
291
292
293
    const int index = indexOf(rc);
    if (index != -1)
        m_runControlTabs.at(index).window->appendApplicationOutputInline(out, onStdErr);
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
294
295
}

296
void OutputPane::appendMessage(RunControl *rc, const QString &out, bool isError)
297
{
298
299
300
    const int index = indexOf(rc);
    if (index != -1)
        m_runControlTabs.at(index).window->appendMessage(out, isError);
301
302
}

303
void OutputPane::showTabFor(RunControl *rc)
con's avatar
con committed
304
{
305
    m_tabWidget->setCurrentIndex(tabWidgetIndexOf(indexOf(rc)));
con's avatar
con committed
306
307
}

308
void OutputPane::reRunRunControl()
con's avatar
con committed
309
{
310
311
312
313
    const int index = currentIndex();
    QTC_ASSERT(index != -1 && !m_runControlTabs.at(index).runControl->isRunning(), return;)

    RunControlTab &tab = m_runControlTabs[index];
314
315

    tab.window->handleOldOutput();
316
317
    tab.window->scrollToBottom();
    tab.runControl->start();
con's avatar
con committed
318
319
}

320
void OutputPane::stopRunControl()
con's avatar
con committed
321
{
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
    const int index = currentIndex();
    QTC_ASSERT(index != -1 && m_runControlTabs.at(index).runControl->isRunning(), return;)

    RunControl *rc = m_runControlTabs.at(index).runControl;
    if (rc->isRunning() && rc->aboutToStop())
        rc->stop();

    if (debug)
        qDebug() << "OutputPane::stopRunControl " << rc;
}

bool OutputPane::closeTabs(bool prompt)
{
    bool allClosed = true;
    for (int t = m_tabWidget->count() - 1; t >= 0; t--)
        if (!closeTab(t, prompt))
            allClosed = false;
    if (debug)
        qDebug() << "OutputPane::closeTabs() returns " << allClosed;
    return allClosed;
con's avatar
con committed
342
343
}

dt's avatar
dt committed
344
bool OutputPane::closeTab(int index)
con's avatar
con committed
345
{
346
347
    return closeTab(index, true);
}
348

349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
bool OutputPane::closeTab(int tabIndex, bool prompt)
{
    const int index = indexOf(m_tabWidget->widget(tabIndex));
    QTC_ASSERT(index != -1, return true;)

    RunControlTab &tab = m_runControlTabs[index];

    if (debug)
            qDebug() << "OutputPane::closeTab tab " << tabIndex << tab.runControl
                        << tab.window << tab.asyncClosing;
    // Prompt user to stop
    if (tab.runControl->isRunning()) {
        if (prompt && !tab.runControl->aboutToStop())
            return false;
        if (tab.runControl->stop() == RunControl::AsynchronousStop) {
            tab.asyncClosing = true;
            return false;
        }
367
368
    }

369
370
371
372
373
374
375
376
    m_tabWidget->removeTab(tabIndex);
    if (tab.asyncClosing) { // We were invoked from its finished() signal.
        tab.runControl->deleteLater();
    } else {
        delete tab.runControl;
    }
    delete tab.window;
    m_runControlTabs.removeAt(index);
dt's avatar
dt committed
377
    return true;
con's avatar
con committed
378
379
}

380
void OutputPane::projectRemoved()
con's avatar
con committed
381
{
382
383
    tabChanged(m_tabWidget->currentIndex());
}
con's avatar
con committed
384

385
386
387
void OutputPane::tabChanged(int i)
{
    if (i == -1) {
388
        m_stopAction->setEnabled(false);
389
390
        m_reRunButton->setEnabled(false);
    } else {
391
392
393
394
        const int index = indexOf(m_tabWidget->widget(i));
        QTC_ASSERT(index != -1, return; )

        RunControl *rc = m_runControlTabs.at(index).runControl;
395
        m_stopAction->setEnabled(rc->isRunning());
396
        m_reRunButton->setEnabled(!rc->isRunning());
hjk's avatar
hjk committed
397
        m_reRunButton->setIcon(rc->runMode() == Constants::DEBUGMODE ? m_debugIcon : m_runIcon);
398
    }
con's avatar
con committed
399
400
}

401
void OutputPane::runControlStarted()
con's avatar
con committed
402
{
403
404
    RunControl *current = currentRunControl();
    if (current && current == sender()) {
405
        m_reRunButton->setEnabled(false);
406
        m_stopAction->setEnabled(true);
407
        m_reRunButton->setIcon(current->runMode() == Constants::DEBUGMODE ? m_debugIcon : m_runIcon);
408
    }
con's avatar
con committed
409
410
}

411
void OutputPane::runControlFinished()
con's avatar
con committed
412
{
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    RunControl *senderRunControl = qobject_cast<RunControl *>(sender());
    const int senderIndex = indexOf(senderRunControl);

    QTC_ASSERT(senderIndex != -1, return; )

    // Enable buttons for current
    RunControl *current = currentRunControl();

    if (debug)
        qDebug() << "OutputPane::runControlFinished"  << senderRunControl << senderIndex
                    << " current " << current << m_runControlTabs.size();

    if (current && current == sender()) {
        m_reRunButton->setEnabled(true);
427
        m_stopAction->setEnabled(false);
428
        m_reRunButton->setIcon(current->runMode() == Constants::DEBUGMODE ? m_debugIcon : m_runIcon);
429
    }
430
431
432
433
434
435
    // Check for asynchronous close. Close the tab.
    if (m_runControlTabs.at(senderIndex).asyncClosing)
        closeTab(tabWidgetIndexOf(senderIndex), false);

    if (!isRunning())
        emit allRunControlsFinished();
con's avatar
con committed
436
437
}

438
bool OutputPane::isRunning() const
con's avatar
con committed
439
{
440
441
442
443
    foreach(const RunControlTab &rt, m_runControlTabs)
        if (rt.runControl->isRunning())
            return true;
    return false;
con's avatar
con committed
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
bool OutputPane::canNext()
{
    return false;
}

bool OutputPane::canPrevious()
{
    return false;
}

void OutputPane::goToNext()
{

}

void OutputPane::goToPrev()
{

}

bool OutputPane::canNavigate()
{
    return false;
}
con's avatar
con committed
470
471
472
473
474

/*******************/

OutputWindow::OutputWindow(QWidget *parent)
    : QPlainTextEdit(parent)
475
    , m_formatter(0)
476
477
    , m_enforceNewline(false)
    , m_scrollToBottom(false)
dt's avatar
dt committed
478
479
    , m_linksActive(true)
    , m_mousePressed(false)
con's avatar
con committed
480
481
482
483
{
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    //setCenterOnScroll(false);
    setFrameShape(QFrame::NoFrame);
484
    setMouseTracking(true);
485
486
    if (!ProjectExplorerPlugin::instance()->projectExplorerSettings().wrapAppOutput)
        setWordWrapMode(QTextOption::NoWrap);
con's avatar
con committed
487

488
489
    static uint usedIds = 0;
    Core::ICore *core = Core::ICore::instance();
490
    Core::Context context(Constants::C_APP_OUTPUT, usedIds++);
491
492
493
494
495
496
497
498
499
500
    m_outputWindowContext = new Core::BaseContext(this, context);
    core->addContextObject(m_outputWindowContext);

    QAction *undoAction = new QAction(this);
    QAction *redoAction = new QAction(this);
    QAction *cutAction = new QAction(this);
    QAction *copyAction = new QAction(this);
    QAction *pasteAction = new QAction(this);
    QAction *selectAllAction = new QAction(this);

501
502
503
504
505
506
507
    Core::ActionManager *am = core->actionManager();
    am->registerAction(undoAction, Core::Constants::UNDO, context);
    am->registerAction(redoAction, Core::Constants::REDO, context);
    am->registerAction(cutAction, Core::Constants::CUT, context);
    am->registerAction(copyAction, Core::Constants::COPY, context);
    am->registerAction(pasteAction, Core::Constants::PASTE, context);
    am->registerAction(selectAllAction, Core::Constants::SELECTALL, context);
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524

    connect(undoAction, SIGNAL(triggered()), this, SLOT(undo()));
    connect(redoAction, SIGNAL(triggered()), this, SLOT(redo()));
    connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
    connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
    connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
    connect(selectAllAction, SIGNAL(triggered()), this, SLOT(selectAll()));

    connect(this, SIGNAL(undoAvailable(bool)), undoAction, SLOT(setEnabled(bool)));
    connect(this, SIGNAL(redoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
    connect(this, SIGNAL(copyAvailable(bool)), cutAction, SLOT(setEnabled(bool)));  // OutputWindow never read-only
    connect(this, SIGNAL(copyAvailable(bool)), copyAction, SLOT(setEnabled(bool)));

    undoAction->setEnabled(false);
    redoAction->setEnabled(false);
    cutAction->setEnabled(false);
    copyAction->setEnabled(false);
525
526
527

    connect(ProjectExplorerPlugin::instance(), SIGNAL(settingsChanged()),
            this, SLOT(updateWordWrapMode()));
528
}
con's avatar
con committed
529
530
531

OutputWindow::~OutputWindow()
{
532
    Core::ICore::instance()->removeContextObject(m_outputWindowContext);
533
    delete m_outputWindowContext;
con's avatar
con committed
534
535
}

dt's avatar
dt committed
536
void OutputWindow::mousePressEvent(QMouseEvent * e)
dt's avatar
dt committed
537
538
{
    m_mousePressed = true;
dt's avatar
dt committed
539
    QPlainTextEdit::mousePressEvent(e);
dt's avatar
dt committed
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
}

void OutputWindow::mouseReleaseEvent(QMouseEvent *e)
{
    m_mousePressed = false;

    if (!m_linksActive) {
        // Mouse was released, activate links again
        m_linksActive = true;
        return;
    }

    const QString href = anchorAt(e->pos());
    if (m_formatter)
        m_formatter->handleLink(href);
dt's avatar
dt committed
555
    QPlainTextEdit::mousePressEvent(e);
dt's avatar
dt committed
556
557
558
559
560
561
562
563
564
565
566
567
}

void OutputWindow::mouseMoveEvent(QMouseEvent *e)
{
    // Cursor was dragged to make a selection, deactivate links
    if (m_mousePressed && textCursor().hasSelection())
        m_linksActive = false;

    if (!m_linksActive || anchorAt(e->pos()).isEmpty())
        viewport()->setCursor(Qt::IBeamCursor);
    else
        viewport()->setCursor(Qt::PointingHandCursor);
dt's avatar
dt committed
568
    QPlainTextEdit::mouseMoveEvent(e);
dt's avatar
dt committed
569
570
}

571
572
573
574
575
576
577
578
579
580
581
OutputFormatter *OutputWindow::formatter() const
{
    return m_formatter;
}

void OutputWindow::setFormatter(OutputFormatter *formatter)
{
    m_formatter = formatter;
    m_formatter->setPlainTextEdit(this);
}

582
583
584
585
586
587
588
589
590
void OutputWindow::showEvent(QShowEvent *e)
{
    QPlainTextEdit::showEvent(e);
    if (m_scrollToBottom) {
        verticalScrollBar()->setValue(verticalScrollBar()->maximum());
    }
    m_scrollToBottom = false;
}

591
QString OutputWindow::doNewlineEnfocement(const QString &out)
con's avatar
con committed
592
{
593
    m_scrollToBottom = true;
594
    QString s = out;
595
596
597
    if (m_enforceNewline)
        s.prepend(QLatin1Char('\n'));

598
    m_enforceNewline = true; // make appendOutputInline put in a newline next time
599
600

    if (s.endsWith(QLatin1Char('\n')))
601
        s.chop(1);
602

603
604
605
    return s;
}

dt's avatar
dt committed
606
void OutputWindow::appendApplicationOutput(const QString &output, bool onStdErr)
607
{
dt's avatar
dt committed
608
609
    QString out = output;
    out.remove(QLatin1Char('\r'));
610
611
    setMaximumBlockCount(MaxBlockCount);
    const bool atBottom = isScrollbarAtBottom();
612
    m_formatter->appendApplicationOutput(doNewlineEnfocement(out), onStdErr);
613
614
    if (atBottom)
        scrollToBottom();
615
    enableUndoRedo();
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
616
617
}

dt's avatar
dt committed
618
void OutputWindow::appendApplicationOutputInline(const QString &output, bool onStdErr)
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
619
{
dt's avatar
dt committed
620
621
    QString out = output;
    out.remove(QLatin1Char('\r'));
622
    m_scrollToBottom = true;
623
624
    setMaximumBlockCount(MaxBlockCount);

625
626
627
    int newline = -1;
    bool enforceNewline = m_enforceNewline;
    m_enforceNewline = false;
628
    const bool atBottom = isScrollbarAtBottom();
629
630
631

    if (!enforceNewline) {
        newline = out.indexOf(QLatin1Char('\n'));
632
        moveCursor(QTextCursor::End);
633
634
        if (newline != -1)
            m_formatter->appendApplicationOutput(out.left(newline), onStdErr); // doesn't enforce new paragraph like appendPlainText
635
636
637
638
639
640
641
642
643
    }

    QString s = out.mid(newline+1);
    if (s.isEmpty()) {
        m_enforceNewline = true;
    } else {
        if (s.endsWith(QLatin1Char('\n'))) {
            m_enforceNewline = true;
            s.chop(1);
644
        }
645
        m_formatter->appendApplicationOutput(QLatin1Char('\n') + s, onStdErr);
646
    }
647

648
649
    if (atBottom)
        scrollToBottom();
650
    enableUndoRedo();
con's avatar
con committed
651
652
}

dt's avatar
dt committed
653
void OutputWindow::appendMessage(const QString &output, bool isError)
654
{
dt's avatar
dt committed
655
656
    QString out = output;
    out.remove(QLatin1Char('\r'));
657
    setMaximumBlockCount(MaxBlockCount);
658
    const bool atBottom = isScrollbarAtBottom();
659
    m_formatter->appendMessage(doNewlineEnfocement(out), isError);
660
661
    if (atBottom)
        scrollToBottom();
662
663
664
    enableUndoRedo();
}

665
// TODO rename
dt's avatar
dt committed
666
void OutputWindow::appendText(const QString &textIn, const QTextCharFormat &format, int maxLineCount)
667
{
dt's avatar
dt committed
668
669
    QString text = textIn;
    text.remove(QLatin1Char('\r'));
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
    if (document()->blockCount() > maxLineCount)
        return;
    const bool atBottom = isScrollbarAtBottom();
    QTextCursor cursor = QTextCursor(document());
    cursor.movePosition(QTextCursor::End);
    cursor.beginEditBlock();
    cursor.insertText(doNewlineEnfocement(text), format);

    if (document()->blockCount() > maxLineCount) {
        QTextCharFormat tmp;
        tmp.setFontWeight(QFont::Bold);
        cursor.insertText(tr("Additional output omitted\n"), tmp);
    }

    cursor.endEditBlock();
    if (atBottom)
        scrollToBottom();
}

689
690
bool OutputWindow::isScrollbarAtBottom() const
{
mae's avatar
mae committed
691
692
693
694
695
    return isVisible()
            && (blockBoundingRect(document()->lastBlock()).bottom()
                + contentOffset().y() <= viewport()->rect().bottom());

    // return verticalScrollBar()->value() == verticalScrollBar()->maximum();
696
697
}

698
699
700
701
702
703
704
705
706
707
708
709
710
711
void OutputWindow::clear()
{
    m_enforceNewline = false;
    QPlainTextEdit::clear();
}

void OutputWindow::handleOldOutput()
{
    if (ProjectExplorerPlugin::instance()->projectExplorerSettings().cleanOldAppOutput)
        clear();
    else
        grayOutOldContent();
}

712
713
714
715
716
void OutputWindow::scrollToBottom()
{
    verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}

717
718
719
void OutputWindow::grayOutOldContent()
{
    QTextCursor cursor = textCursor();
720
721
722
    cursor.movePosition(QTextCursor::End);
    QTextCharFormat endFormat = cursor.charFormat();

723
    cursor.select(QTextCursor::Document);
724
725

    QTextCharFormat format;
726
727
    const QColor bkgColor = palette().base().color();
    const QColor fgdColor = palette().text().color();
728
    double bkgFactor = 0.50;
729
    double fgdFactor = 1.-bkgFactor;
730
    format.setForeground(QColor((bkgFactor * bkgColor.red() + fgdFactor * fgdColor.red()),
731
732
                             (bkgFactor * bkgColor.green() + fgdFactor * fgdColor.green()),
                             (bkgFactor * bkgColor.blue() + fgdFactor * fgdColor.blue()) ));
733
    cursor.mergeCharFormat(format);
734
735

    cursor.movePosition(QTextCursor::End);
736
    cursor.setCharFormat(endFormat);
737
738
739
    cursor.insertBlock(QTextBlockFormat());
}

740
741
742
743
744
void OutputWindow::enableUndoRedo()
{
    setMaximumBlockCount(0);
    setUndoRedoEnabled(true);
}
745

746
747
748
749
750
751
752
753
void OutputWindow::updateWordWrapMode()
{
    if (ProjectExplorerPlugin::instance()->projectExplorerSettings().wrapAppOutput)
        setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
    else
        setWordWrapMode(QTextOption::NoWrap);
}

754
755
} // namespace Internal
} // namespace ProjectExplorer