cpptoolseditorsupport.cpp 3.91 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
**
16
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
con's avatar
con committed
29

30
#include "cpptoolseditorsupport.h"
31

con's avatar
con committed
32 33
#include "cppmodelmanager.h"

34
#include <QTimer>
con's avatar
con committed
35

36
using namespace CppTools;
con's avatar
con committed
37
using namespace CppTools::Internal;
Roberto Raggi's avatar
Roberto Raggi committed
38 39
using namespace CPlusPlus;

con's avatar
con committed
40 41 42
CppEditorSupport::CppEditorSupport(CppModelManager *modelManager)
    : QObject(modelManager),
      _modelManager(modelManager),
43
      _updateDocumentInterval(UPDATE_DOCUMENT_DEFAULT_INTERVAL)
con's avatar
con committed
44
{
45 46
    _revision = 0;

con's avatar
con committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    _updateDocumentTimer = new QTimer(this);
    _updateDocumentTimer->setSingleShot(true);
    _updateDocumentTimer->setInterval(_updateDocumentInterval);
    connect(_updateDocumentTimer, SIGNAL(timeout()), this, SLOT(updateDocumentNow()));
}

CppEditorSupport::~CppEditorSupport()
{ }

TextEditor::ITextEditor *CppEditorSupport::textEditor() const
{ return _textEditor; }

void CppEditorSupport::setTextEditor(TextEditor::ITextEditor *textEditor)
{
    _textEditor = textEditor;

63
    if (_textEditor) {
Erik Verbruggen's avatar
Erik Verbruggen committed
64 65
        connect(_textEditor, SIGNAL(contentsChanged()), this, SIGNAL(contentsChanged()));
        connect(this, SIGNAL(contentsChanged()), this, SLOT(updateDocument()));
Roberto Raggi's avatar
Roberto Raggi committed
66

Erik Verbruggen's avatar
Erik Verbruggen committed
67 68
        updateDocument();
    }
con's avatar
con committed
69 70
}

71
QString CppEditorSupport::contents()
con's avatar
con committed
72 73
{
    if (! _textEditor)
74
        return QString();
Roberto Raggi's avatar
Roberto Raggi committed
75
    else if (! _cachedContents.isEmpty())
76
        _cachedContents = _textEditor->textDocument()->contents();
con's avatar
con committed
77

Roberto Raggi's avatar
Roberto Raggi committed
78
    return _cachedContents;
con's avatar
con committed
79 80
}

81 82 83
unsigned CppEditorSupport::editorRevision() const
{
    if (_textEditor) {
84
        if (TextEditor::BaseTextEditorWidget *ed = qobject_cast<TextEditor::BaseTextEditorWidget *>(_textEditor->widget()))
85 86 87 88 89 90
            return ed->document()->revision();
    }

    return 0;
}

con's avatar
con committed
91 92 93 94 95 96 97
int CppEditorSupport::updateDocumentInterval() const
{ return _updateDocumentInterval; }

void CppEditorSupport::setUpdateDocumentInterval(int updateDocumentInterval)
{ _updateDocumentInterval = updateDocumentInterval; }

void CppEditorSupport::updateDocument()
Roberto Raggi's avatar
Roberto Raggi committed
98
{
99 100
    _revision = editorRevision();

101
    if (qobject_cast<TextEditor::BaseTextEditorWidget*>(_textEditor->widget()) != 0)
Roberto Raggi's avatar
Roberto Raggi committed
102 103 104 105
        _modelManager->stopEditorSelectionsUpdate();

    _updateDocumentTimer->start(_updateDocumentInterval);
}
con's avatar
con committed
106 107 108

void CppEditorSupport::updateDocumentNow()
{
109
    if (_documentParser.isRunning() || _revision != editorRevision()) {
con's avatar
con committed
110 111 112
        _updateDocumentTimer->start(_updateDocumentInterval);
    } else {
        _updateDocumentTimer->stop();
Roberto Raggi's avatar
Roberto Raggi committed
113

114
        QStringList sourceFiles(_textEditor->document()->fileName());
115
        _cachedContents = _textEditor->textDocument()->contents();
116
        _documentParser = _modelManager->updateSourceFiles(sourceFiles);
con's avatar
con committed
117 118 119
    }
}