outputwindow.cpp 20.3 KB
Newer Older
con's avatar
con committed
1
2
3
4
/***************************************************************************
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
7
8
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
9
10
11
**
** Non-Open Source Usage
**
con's avatar
con committed
12
13
14
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
15
16
17
18
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
con's avatar
con committed
19
20
21
22
23
24
25
26
27
28
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
29
** rights. These rights are described in the Nokia Qt GPL Exception
30
** version 1.3, included in the file GPL_EXCEPTION.txt in this package.
31
32
**
***************************************************************************/
hjk's avatar
hjk committed
33

con's avatar
con committed
34
35
36
37
#include "outputwindow.h"
#include "projectexplorerconstants.h"
#include "runconfiguration.h"

38
#include <coreplugin/actionmanager/actionmanager.h>
39
40
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
con's avatar
con committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <find/basetextfind.h>
#include <aggregation/aggregate.h>

#include <QtGui/QIcon>
#include <QtGui/QScrollBar>
#include <QtGui/QTextLayout>
#include <QtGui/QPainter>
#include <QtGui/QApplication>
#include <QtGui/QClipboard>
#include <QtGui/QMenu>
#include <QtGui/QMessageBox>
#include <QtGui/QVBoxLayout>
#include <QtGui/QTabWidget>

using namespace ProjectExplorer::Internal;
using namespace ProjectExplorer;

hjk's avatar
hjk committed
58
OutputPane::OutputPane()
59
    : m_mainWidget(new QWidget)
con's avatar
con committed
60
61
62
63
64
65
66
67
68
69
70
{
//     m_insertLineButton = new QToolButton;
//     m_insertLineButton->setIcon(QIcon(ProjectExplorer::Constants::ICON_INSERT_LINE));
//     m_insertLineButton->setText(tr("Insert line"));
//     m_insertLineButton->setToolTip(tr("Insert line"));
//     m_insertLineButton->setAutoRaise(true);
//     connect(m_insertLineButton, SIGNAL(clicked()), this, SLOT(insertLine()));

    QIcon runIcon(Constants::ICON_RUN);
    runIcon.addFile(Constants::ICON_RUN_SMALL);

71
    // Rerun
con's avatar
con committed
72
73
74
75
76
77
78
79
    m_reRunButton = new QToolButton;
    m_reRunButton->setIcon(runIcon);
    m_reRunButton->setToolTip(tr("Rerun this runconfiguration"));
    m_reRunButton->setAutoRaise(true);
    m_reRunButton->setEnabled(false);
    connect(m_reRunButton, SIGNAL(clicked()),
            this, SLOT(reRunRunControl()));

80
    // Stop
hjk's avatar
hjk committed
81
    Core::ActionManager *am = Core::ICore::instance()->actionManager();
82
83
84
85
86
87
88
    QList<int> globalcontext;
    globalcontext.append(Core::Constants::C_GLOBAL_ID);

    m_stopAction = new QAction(QIcon(Constants::ICON_STOP), tr("Stop"), this);
    m_stopAction->setToolTip(tr("Stop"));
    m_stopAction->setEnabled(false);

con's avatar
con committed
89
    Core::Command *cmd = am->registerAction(m_stopAction, Constants::STOP, globalcontext);
90
91
    cmd->setDefaultKeySequence(QKeySequence(tr("Ctrl+Shift+R")));

con's avatar
con committed
92
    m_stopButton = new QToolButton;
93
    m_stopButton->setDefaultAction(cmd->action());
con's avatar
con committed
94
    m_stopButton->setAutoRaise(true);
95
96

    connect(m_stopAction, SIGNAL(triggered()),
con's avatar
con committed
97
98
99
100
101
102
103
104
105
106
107
108
109
            this, SLOT(stopRunControl()));

    // Spacer (?)

    QVBoxLayout *layout = new QVBoxLayout;
    layout->setMargin(0);
    m_tabWidget = new QTabWidget;
    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
110
    connect(m_tabWidget, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
con's avatar
con committed
111
112

    m_mainWidget->setLayout(layout);
113
114
115

    connect(Core::ICore::instance(), SIGNAL(coreAboutToClose()),
            this, SLOT(coreAboutToClose()));
con's avatar
con committed
116
117
}

118
void OutputPane::coreAboutToClose()
con's avatar
con committed
119
120
121
122
123
124
125
{
    while (m_tabWidget->count()) {
        RunControl *rc = runControlForTab(0);
        if (rc->isRunning())
            rc->stop();
        closeTab(0);
    }
126
127
128
129
}

OutputPane::~OutputPane()
{
con's avatar
con committed
130
131
132
    delete m_mainWidget;
}

133
QWidget *OutputPane::outputWidget(QWidget *)
con's avatar
con committed
134
{
135
    return m_mainWidget;
con's avatar
con committed
136
137
}

hjk's avatar
hjk committed
138
QList<QWidget*> OutputPane::toolBarWidgets() const
con's avatar
con committed
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
    return QList<QWidget*>() << m_reRunButton << m_stopButton
            ; // << m_insertLineButton;
}

QString OutputPane::name() const
{
    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();
}

void OutputPane::appendOutput(const QString &/*out*/)
{
    // This function is in the interface, since we can't do anything sensible here, we don't do anything here.
con's avatar
con committed
184
185
186
187
188
189
190
}

void OutputPane::createNewOutputWindow(RunControl *rc)
{
    connect(rc, SIGNAL(started()),
            this, SLOT(runControlStarted()));
    connect(rc, SIGNAL(finished()),
191
192
            this, SLOT(runControlFinished()));

con's avatar
con committed
193
194
    // First look if we can reuse a tab
    bool found = false;
195
    for (int i = 0; i < m_tabWidget->count(); ++i) {
con's avatar
con committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        RunControl *old = runControlForTab(i);
        if (old->runConfiguration() == rc->runConfiguration() && !old->isRunning()) {
            // Reuse this tab
            delete old;
            m_outputWindows.remove(old);
            OutputWindow *ow = static_cast<OutputWindow *>(m_tabWidget->widget(i));
            ow->appendOutput("");//New line
            m_outputWindows.insert(rc, ow);
            found = true;
            break;
        }
    }
    if (!found) {
        OutputWindow *ow = new OutputWindow(m_tabWidget);
        Aggregation::Aggregate *agg = new Aggregation::Aggregate;
        agg->add(ow);
        agg->add(new Find::BaseTextFind(ow));
        m_outputWindows.insert(rc, ow);
        m_tabWidget->addTab(ow, rc->runConfiguration()->name());
    }
}

218
void OutputPane::appendOutput(RunControl *rc, const QString &out)
con's avatar
con committed
219
{
220
221
    OutputWindow *ow = m_outputWindows.value(rc);
    ow->appendOutput(out);
con's avatar
con committed
222
223
}

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
224
225
226
227
228
229
void OutputPane::appendOutputInline(RunControl *rc, const QString &out)
{
    OutputWindow *ow = m_outputWindows.value(rc);
    ow->appendOutputInline(out);
}

230
void OutputPane::showTabFor(RunControl *rc)
con's avatar
con committed
231
{
232
233
    OutputWindow *ow = m_outputWindows.value(rc);
    m_tabWidget->setCurrentWidget(ow);
con's avatar
con committed
234
235
}

236
void OutputPane::insertLine()
con's avatar
con committed
237
{
238
239
240
    OutputWindow *currentWindow = qobject_cast<OutputWindow *>(m_tabWidget->currentWidget());
    if (currentWindow)
        currentWindow->clear();
con's avatar
con committed
241
242
}

243
void OutputPane::reRunRunControl()
con's avatar
con committed
244
{
245
246
247
    RunControl *rc = runControlForTab(m_tabWidget->currentIndex());
    if (rc->runConfiguration()->project() != 0)
        rc->start();
con's avatar
con committed
248
249
}

250
void OutputPane::stopRunControl()
con's avatar
con committed
251
{
252
253
    RunControl *rc = runControlForTab(m_tabWidget->currentIndex());
    rc->stop();
con's avatar
con committed
254
255
}

256
void OutputPane::closeTab(int index)
con's avatar
con committed
257
{
258
259
260
261
262
263
264
265
266
267
268
269
    OutputWindow *ow = static_cast<OutputWindow *>(m_tabWidget->widget(index));
    RunControl *rc = m_outputWindows.key(ow);

    if (rc->isRunning()) {
        QString msg = tr("The application is still running. Close it first.");
        QMessageBox::critical(0, tr("Unable to close"), msg);
        return;
    }

    m_tabWidget->removeTab(index);
    delete ow;
    delete rc;
con's avatar
con committed
270
271
}

272
void OutputPane::projectRemoved()
con's avatar
con committed
273
{
274
275
    tabChanged(m_tabWidget->currentIndex());
}
con's avatar
con committed
276

277
278
279
void OutputPane::tabChanged(int i)
{
    if (i == -1) {
280
        m_stopAction->setEnabled(false);
281
282
283
        m_reRunButton->setEnabled(false);
    } else {
        RunControl *rc = runControlForTab(i);
284
        m_stopAction->setEnabled(rc->isRunning());
285
286
        m_reRunButton->setEnabled(!rc->isRunning() && rc->runConfiguration()->project());
    }
con's avatar
con committed
287
288
}

289
void OutputPane::runControlStarted()
con's avatar
con committed
290
{
291
292
293
    RunControl *rc = runControlForTab(m_tabWidget->currentIndex());
    if (rc == qobject_cast<RunControl *>(sender())) {
        m_reRunButton->setEnabled(false);
294
        m_stopAction->setEnabled(true);
295
    }
con's avatar
con committed
296
297
}

298
void OutputPane::runControlFinished()
con's avatar
con committed
299
{
300
301
302
    RunControl *rc = runControlForTab(m_tabWidget->currentIndex());
    if (rc == qobject_cast<RunControl *>(sender())) {
        m_reRunButton->setEnabled(rc->runConfiguration()->project());
303
        m_stopAction->setEnabled(false);
304
    }
con's avatar
con committed
305
306
}

307
RunControl* OutputPane::runControlForTab(int index) const
con's avatar
con committed
308
{
309
    return m_outputWindows.key(qobject_cast<OutputWindow *>(m_tabWidget->widget(index)));
con's avatar
con committed
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
}


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

OutputWindow::OutputWindow(QWidget *parent)
    : QPlainTextEdit(parent)
{
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    //setCenterOnScroll(false);
    //setMaximumBlockCount(10000);
    setWindowTitle(tr("Application Output Window"));
    setWindowIcon(QIcon(":/qt4projectmanager/images/window.png"));
    setFrameShape(QFrame::NoFrame);
}


OutputWindow::~OutputWindow()
{
}

void OutputWindow::appendOutput(const QString &out)
{
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
333
334
335
336
337
338
339
340
341
342
    if (out.endsWith('\n'))
        appendPlainText(out);
    else
        appendPlainText(out + '\n');
}

void OutputWindow::appendOutputInline(const QString &out)
{
    moveCursor(QTextCursor::End);
    insertPlainText(out);
con's avatar
con committed
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
}

void OutputWindow::insertLine()
{
    appendPlainText(QString());
}

#if 0
OutputWindow::OutputWindow(QWidget *parent)
        : QAbstractScrollArea(parent)
{
    max_lines = 1000;
    width_used = 0;
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    same_height = true;
    block_scroll = false;
    setWindowTitle(tr("Application Output Window"));
    setWindowIcon(QIcon(":/qt4projectmanager/images/window.png"));
}

void OutputWindow::changed() {
    int remove = lines.size() - max_lines;
    if (remove > 0) {
        selection_start.line -= remove;
        selection_end.line -= remove;
        selection_start = qMax(selection_start, Selection());
        selection_end = qMax(selection_end, Selection());
        if (remove > verticalScrollBar()->value()) {
            if (same_height)
                viewport()->scroll(0, -remove * fontMetrics().lineSpacing());
            else
                viewport()->update();
        } else {
            block_scroll = true;
            verticalScrollBar()->setValue(verticalScrollBar()->value() - remove);
            block_scroll = false;
        }
        while (remove--)
            lines.removeFirst();
    }

    verticalScrollBar()->setRange(0, lines.size() - 1);

}


bool OutputWindow::getCursorPos(int *lineNumber, int *position, const QPoint &pos) {
    if (lines.isEmpty())
        return false;
    *lineNumber = verticalScrollBar()->value();

    int x = 4 - horizontalScrollBar()->value();

    int spacing = fontMetrics().lineSpacing();
    int leading = fontMetrics().leading();
    int height = 0;

    QTextLayout textLayout;
    textLayout.setFont(font());

    if (same_height && pos.y() > 0) {
        int skipLines = pos.y() / spacing;
        height += skipLines * spacing;
        *lineNumber = qMin(*lineNumber + skipLines, lines.size() - 1);
    }

    same_height = true;

    while ( *lineNumber < lines.size()) {
        textLayout.setText(lines.at(*lineNumber));

        textLayout.beginLayout();
        while (1) {
            QTextLine line = textLayout.createLine();
            if (!line.isValid())
                break;
            line.setLineWidth(INT_MAX/256);
            height += leading;
            line.setPosition(QPoint(x, height));
            height += static_cast<int>(line.height());
        }
        textLayout.endLayout();
        if (height > pos.y()) {
            *position = textLayout.lineAt(0).xToCursor(pos.x());
            break;
        }
        ++*lineNumber;
    }
    return true;
}

void OutputWindow::setNumberOfLines(int max)
{
    max_lines = qMax(1, max);
    while (lines.size() > max_lines)
        lines.removeLast();
    changed();
}

int OutputWindow::numberOfLines() const
{
    return max_lines;
}

bool OutputWindow::hasSelectedText() const
{
    return selection_start != selection_end;
}

void OutputWindow::clearSelection()
{
    bool hadSelectedText = hasSelectedText();
    selection_start = selection_end = Selection();
    if (hadSelectedText)
        viewport()->update();
}

QString OutputWindow::selectedText() const
{
    Selection sel_start = qMin(selection_start, selection_end);
    Selection sel_end = qMax(selection_start, selection_end);
    QString text;

    if (sel_start.line == sel_end.line) {
        text += lines.at(sel_start.line).mid(sel_start.pos, sel_end.pos - sel_start.pos);
    } else {
        int line = sel_start.line;
        text += lines.at(line++).mid(sel_start.pos);
        text += QLatin1Char('\n');
        while (line < sel_end.line) {
            text += lines.at(line++);
            text += QLatin1Char('\n');
        }
        text += lines.at(sel_end.line).left(sel_end.pos);
    }
    return text;
}

void OutputWindow::appendOutput(const QString &text)
{
    lines.append(text);
    if (same_height)
        viewport()->update(
            QRect(0, (lines.size() - verticalScrollBar()->value() - 1) * fontMetrics().lineSpacing(),
                  viewport()->width(), viewport()->height()));
    else
        viewport()->update();

    changed();
    int top = lines.size() - (viewport()->height() / fontMetrics().lineSpacing());
    if (verticalScrollBar()->value() == top - 1)
        verticalScrollBar()->setValue(top);
}

void OutputWindow::clear()
{
    clearSelection();
    lines.clear();
    viewport()->update();
}

void OutputWindow::copy()
{
    if (hasSelectedText())
        QApplication::clipboard()->setText(selectedText());
}

void OutputWindow::selectAll()
{
    selection_start = Selection();
    selection_end.line = lines.size() - 1;
    selection_end.pos = lines.last().length() - 1;
    viewport()->update();
}

void OutputWindow::scrollContentsBy(int dx, int dy)
{
    if (block_scroll)
        return;
    if (dx && dy) {
        viewport()->update();
    } else if (dx && !dy) {
        viewport()->scroll(dx, 0);
    } else {
        if (same_height) {
            viewport()->scroll(0, fontMetrics().lineSpacing() * dy);
        } else {
            viewport()->update();
        }
    }
}

void OutputWindow::keyPressEvent(QKeyEvent *e)
{
    bool accept = true;
    if (e == QKeySequence::Copy) {
        copy();
    } else if (e == QKeySequence::SelectAll) {
        selectAll();
    } else if (e->key() == Qt::Key_Enter
               || e->key() == Qt::Key_Return) {
        insertLine();
    } else {
        accept = false;
    }

    if (accept)
        e->accept();
    else
        QAbstractScrollArea::keyPressEvent(e);
}

void OutputWindow::paintEvent(QPaintEvent *e)
{
    int lineNumber = verticalScrollBar()->value();

    int x = 4 - horizontalScrollBar()->value();
    QPainter p(viewport());

    int spacing = fontMetrics().lineSpacing();
    int leading = fontMetrics().leading();
    int height = 0;

    QTextLayout textLayout;
    textLayout.setFont(font());

    QTextCharFormat selectionFormat;
    selectionFormat.setBackground(palette().highlight());
    selectionFormat.setForeground(palette().highlightedText());

    if (e->rect().top() <= 0 && e->rect().bottom() >= viewport()->rect().bottom())
        width_used = 0; // recalculate

    if (same_height) {
        int skipLines = e->rect().top() / spacing;
        height += skipLines * spacing;
        lineNumber += skipLines;
    }

    same_height = true;

    Selection sel_start = qMin(selection_start, selection_end);
    Selection sel_end = qMax(selection_start, selection_end);

    while ( lineNumber < lines.size() && height <= e->rect().bottom()) {

        QString line = lines.at(lineNumber);

        if (line.size() == 1 && line.at(0) == QChar::ParagraphSeparator) {
            int y = height + spacing/2;
            p.drawLine(e->rect().left(), y, e->rect().right(), y);
            height += spacing;

        } else {
            textLayout.setText(line);
            textLayout.beginLayout();
            while (1) {
                QTextLine line = textLayout.createLine();
                if (!line.isValid())
                    break;
                line.setLineWidth(INT_MAX/256);
                height += leading;
                line.setPosition(QPoint(x, height));
                height += static_cast<int>(line.height());

                same_height = same_height && (line.height() + leading) == spacing;
                width_used = qMax(width_used, 8 + static_cast<int>(line.naturalTextWidth()));
            }
            textLayout.endLayout();

            if (lineNumber >= sel_start.line && lineNumber <= sel_end.line) {
                QVector<QTextLayout::FormatRange> selection(1);
                selection[0].start = (lineNumber == sel_start.line)? sel_start.pos : 0;
                selection[0].length = ((lineNumber == sel_end.line) ? sel_end.pos : lines.at(lineNumber).size()) - selection[0].start;
                selection[0].format = selectionFormat;

                textLayout.draw(&p, QPoint(0, 0), selection);
            } else {
                textLayout.draw(&p, QPoint(0, 0));
            }
        }


        ++lineNumber;
    }

    horizontalScrollBar()->setRange(0, qMax(0, width_used - viewport()->width()));
    if (horizontalScrollBar()->pageStep() != viewport()->width())
        horizontalScrollBar()->setPageStep(viewport()->width());
    if (height > viewport()->height())
        verticalScrollBar()->setPageStep(lineNumber - verticalScrollBar()->value());
    else if (verticalScrollBar()->pageStep() != viewport()->height() / fontMetrics().lineSpacing())
        verticalScrollBar()->setPageStep(viewport()->height() / fontMetrics().lineSpacing());
}

void OutputWindow::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        clearSelection();
        if (getCursorPos(&selection_start.line, &selection_start.pos, e->pos())) {
            selection_end = selection_start;
            autoscroll = 0;
        }
    }
}

void OutputWindow::timerEvent(QTimerEvent *e)
{
    if (e->timerId() == autoscroll_timer.timerId()) {
        int autoscroll = 0;
        if (lastMouseMove.y() < 0)
            autoscroll = -1;
        else if (lastMouseMove.y() > viewport()->height())
            autoscroll = 1;
        if (autoscroll) {
            verticalScrollBar()->setValue(verticalScrollBar()->value() + autoscroll);
            OutputWindow::mouseMoveEvent(0);
        }
    }
    QAbstractScrollArea::timerEvent(e);
}

void OutputWindow::mouseReleaseEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
        autoscroll_timer.stop();
        if (hasSelectedText() &&  QApplication::clipboard()->supportsSelection())
            QApplication::clipboard()->setText(selectedText(), QClipboard::Selection);
    }
}

void OutputWindow::mouseMoveEvent(QMouseEvent *e)
{
    if (e) {
        lastMouseMove = e->pos();
        if (viewport()->rect().contains(e->pos()))
            autoscroll_timer.stop();
        else
            autoscroll_timer.start(20, this);
    }


    Selection old = selection_end;
    if (!getCursorPos(&selection_end.line, &selection_end.pos, lastMouseMove))
        return;
    if (same_height) {
        Selection from = qMin(old, selection_end);
        Selection to = qMax(old, selection_end);
        viewport()->update(QRect(0, -1 + (from.line - verticalScrollBar()->value()) * fontMetrics().lineSpacing(),
                                 viewport()->width(), 2 + (to.line - from.line + 1) * fontMetrics().lineSpacing()));
    } else {
        viewport()->update();
    }
}

void OutputWindow::contextMenuEvent(QContextMenuEvent * e)
{
    QMenu menu(this);
    QAction *clearAction = menu.addAction("Clear", this, SLOT(clear()));
    QAction *copyAction = menu.addAction("Copy", this, SLOT(copy()), QKeySequence::Copy);
    QAction *selectAllAction = menu.addAction("Select All", this, SLOT(selectAll()), QKeySequence::SelectAll);
    if (lines.empty()) {
        clearAction->setDisabled(true);
        selectAllAction->setDisabled(true);
    }
    if (!hasSelectedText())
        copyAction->setDisabled(true);

    menu.exec(e->globalPos());
}

#endif // 0