summaryrefslogtreecommitdiff
path: root/src/plugins/qmlprojectmanager/qmloutputformatter.cpp
blob: 821a492b6116ed2885754d050c9c86d67e5cf1de (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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** 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.
**
** GNU Lesser General Public License Usage
**
** 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.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

#include "qmloutputformatter.h"

#include <texteditor/basetexteditor.h>

#include <QtGui/QPlainTextEdit>

using namespace ProjectExplorer;
using namespace QmlProjectManager::Internal;

QmlOutputFormatter::QmlOutputFormatter(QObject *parent)
    : OutputFormatter(parent)
    , m_qmlError(QLatin1String("(file:///[^:]+:\\d+:\\d+):"))
    , m_linksActive(true)
    , m_mousePressed(false)
{
}

void QmlOutputFormatter::appendApplicationOutput(const QString &text, bool /*onStdErr*/)
{
    QTextCharFormat normalFormat, linkFormat;
    normalFormat.setForeground(plainTextEdit()->palette().text().color());
    linkFormat.setForeground(plainTextEdit()->palette().link().color());
    linkFormat.setUnderlineStyle(QTextCharFormat::SingleUnderline);
    linkFormat.setAnchor(true);

    plainTextEdit()->setCurrentCharFormat(normalFormat);

    // Create links from QML errors (anything of the form "file:///...:[line]:[column]:")
    int index = 0;
    while (m_qmlError.indexIn(text, index) != -1) {
        const int matchPos = m_qmlError.pos(1);
        const QString leader = text.mid(index, matchPos - index);
        plainTextEdit()->insertPlainText(leader);

        const QString matched = m_qmlError.cap(1);
        linkFormat.setAnchorHref(matched);
        plainTextEdit()->setCurrentCharFormat(linkFormat);
        plainTextEdit()->insertPlainText(matched);
        plainTextEdit()->setCurrentCharFormat(normalFormat);

        index = matchPos + m_qmlError.matchedLength() - 1;
    }
    plainTextEdit()->insertPlainText(text.mid(index));
}

void QmlOutputFormatter::appendMessage(const QString &text, bool isError)
{
    appendApplicationOutput(text, isError);
}

void QmlOutputFormatter::mousePressEvent(QMouseEvent * /*e*/)
{
    m_mousePressed = true;
}

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

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

    const QString href = plainTextEdit()->anchorAt(e->pos());
    if (!href.isEmpty()) {
        QRegExp qmlErrorLink(QLatin1String("^file://(/[^:]+):(\\d+):(\\d+)"));

        if (qmlErrorLink.indexIn(href) != -1) {
            const QString fileName = qmlErrorLink.cap(1);
            const int line = qmlErrorLink.cap(2).toInt();
            const int column = qmlErrorLink.cap(3).toInt();
            TextEditor::BaseTextEditor::openEditorAt(fileName, line, column - 1);
        }
    }
}

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

    if (!m_linksActive || plainTextEdit()->anchorAt(e->pos()).isEmpty())
        plainTextEdit()->viewport()->setCursor(Qt::IBeamCursor);
    else
        plainTextEdit()->viewport()->setCursor(Qt::PointingHandCursor);
}