basetextmark.cpp 4.62 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) 2012 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
**
10
** GNU Lesser General Public License Usage
11
**
hjk's avatar
hjk committed
12 13 14 15 16 17
** 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.
18
**
con's avatar
con committed
19
** In addition, as a special exception, Nokia gives you certain additional
hjk's avatar
hjk committed
20
** rights. These rights are described in the Nokia Qt LGPL Exception
con's avatar
con committed
21 22
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
23 24 25 26 27
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
con's avatar
con committed
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
con's avatar
con committed
30
**
31
**************************************************************************/
hjk's avatar
hjk committed
32

con's avatar
con committed
33
#include "basetextmark.h"
34
#include "itexteditor.h"
35
#include "basetextdocument.h"
Daniel Teske's avatar
Daniel Teske committed
36
#include "texteditorplugin.h"
37

con's avatar
con committed
38 39
#include <coreplugin/editormanager/editormanager.h>
#include <extensionsystem/pluginmanager.h>
hjk's avatar
hjk committed
40

Daniel Teske's avatar
Daniel Teske committed
41 42
using namespace TextEditor;
using namespace TextEditor::Internal;
43

Daniel Teske's avatar
Daniel Teske committed
44 45
BaseTextMarkRegistry::BaseTextMarkRegistry(QObject *parent)
    : QObject(parent)
con's avatar
con committed
46
{
47
    Core::EditorManager *em = Core::EditorManager::instance();
48 49
    connect(em, SIGNAL(editorOpened(Core::IEditor *)),
        SLOT(editorOpened(Core::IEditor *)));
con's avatar
con committed
50 51
}

Daniel Teske's avatar
Daniel Teske committed
52
void BaseTextMarkRegistry::add(BaseTextMark *mark)
con's avatar
con committed
53
{
Daniel Teske's avatar
Daniel Teske committed
54 55 56 57 58 59 60 61 62 63 64 65 66
    m_marks[Utils::FileName::fromString(mark->fileName())].append(mark);
    Core::EditorManager *em = Core::EditorManager::instance();
    foreach (Core::IEditor *editor, em->editorsForFileName(mark->fileName())) {
        if (ITextEditor *textEditor = qobject_cast<ITextEditor *>(editor)) {
            if (mark->m_markableInterface == 0) { // We aren't added to something
                ITextMarkable *markableInterface = textEditor->markableInterface();
                if (markableInterface->addMark(mark, mark->m_line)) {
                    mark->m_markableInterface = markableInterface;
                    // Handle reload of text documents, readding the mark as necessary
                    connect(textEditor->file(), SIGNAL(reloaded()),
                            this, SLOT(documentReloaded()), Qt::UniqueConnection);
                    break;
                }
67
            }
con's avatar
con committed
68 69 70 71
        }
    }
}

Daniel Teske's avatar
Daniel Teske committed
72
void BaseTextMarkRegistry::editorOpened(Core::IEditor *editor)
73
{
Daniel Teske's avatar
Daniel Teske committed
74 75
    ITextEditor *textEditor = qobject_cast<ITextEditor *>(editor);
    if (!textEditor)
76
        return;
Daniel Teske's avatar
Daniel Teske committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    if (!m_marks.contains(Utils::FileName::fromString(editor->file()->fileName())))
        return;

    // Handle reload of text documents, readding the mark as necessary
    connect(textEditor->file(), SIGNAL(reloaded()),
            this, SLOT(documentReloaded()), Qt::UniqueConnection);

    foreach (BaseTextMark *mark, m_marks.value(Utils::FileName::fromString(editor->file()->fileName()))) {
        if (mark->m_markableInterface == 0) { // We aren't added to something
            ITextMarkable *markableInterface = textEditor->markableInterface();
            if (markableInterface->addMark(mark, mark->m_line))
                mark->m_markableInterface = markableInterface;
        }
    }
}
92

Daniel Teske's avatar
Daniel Teske committed
93 94
void BaseTextMarkRegistry::documentReloaded()
{
95 96 97 98
    BaseTextDocument *doc = qobject_cast<BaseTextDocument*>(sender());
    if (!doc)
        return;

Daniel Teske's avatar
Daniel Teske committed
99 100 101 102 103 104 105 106 107 108 109
    if (!m_marks.contains(Utils::FileName::fromString(doc->fileName())))
        return;

    foreach (BaseTextMark *mark, m_marks.value(Utils::FileName::fromString(doc->fileName()))) {
        if (mark->m_markableInterface)
            return;
        ITextMarkable *markableInterface = doc->documentMarker();
        if (markableInterface->addMark(mark, mark->m_line))
            mark->m_markableInterface = markableInterface;
    }
}
110

Daniel Teske's avatar
Daniel Teske committed
111 112 113 114
BaseTextMark::BaseTextMark(const QString &fileName, int lineNumber)
    : m_fileName(fileName), m_line(lineNumber)
{
    Internal::TextEditorPlugin::instance()->baseTextMarkRegistry()->add(this);
115 116
}

Daniel Teske's avatar
Daniel Teske committed
117
BaseTextMark::~BaseTextMark()
con's avatar
con committed
118
{
Daniel Teske's avatar
Daniel Teske committed
119 120 121 122
    // oha we are deleted
    if (m_markableInterface)
        m_markableInterface.data()->removeMark(this);
    m_markableInterface.clear();
con's avatar
con committed
123 124 125 126 127
}

void BaseTextMark::updateMarker()
{
    if (m_markableInterface)
Daniel Teske's avatar
Daniel Teske committed
128
        m_markableInterface.data()->updateMark(this);
con's avatar
con committed
129
}