1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "basetextmark.h"
35 #include "itexteditor.h"
36 #include "basetextdocument.h"
38 #include <coreplugin/editormanager/editormanager.h>
39 #include <extensionsystem/pluginmanager.h>
41 #include <QtCore/QTimer>
42 #include <QtGui/QIcon>
44 namespace TextEditor {
47 class InternalMark : public TextEditor::ITextMark
50 explicit InternalMark(BaseTextMark *parent) : m_parent(parent) {}
52 virtual QIcon icon() const
54 return m_parent->icon();
57 virtual void updateLineNumber(int lineNumber)
59 return m_parent->updateLineNumber(lineNumber);
62 virtual void updateBlock(const QTextBlock &block)
64 return m_parent->updateBlock(block);
67 virtual void removedFromEditor()
69 m_parent->childRemovedFromEditor(this);
72 virtual void documentClosing()
74 m_parent->documentClosingFor(this);
77 BaseTextMark *m_parent;
80 } // namespace Internal
82 BaseTextMark::BaseTextMark(const QString &filename, int line)
83 : m_markableInterface(0)
85 , m_fileName(filename)
90 QTimer::singleShot(0, this, SLOT(init()));
93 BaseTextMark::~BaseTextMark()
96 if (m_markableInterface)
97 m_markableInterface->removeMark(m_internalMark);
101 void BaseTextMark::init()
104 Core::EditorManager *em = Core::EditorManager::instance();
105 connect(em, SIGNAL(editorOpened(Core::IEditor *)), this, SLOT(editorOpened(Core::IEditor *)));
107 foreach (Core::IEditor *editor, em->openedEditors())
108 editorOpened(editor);
111 void BaseTextMark::editorOpened(Core::IEditor *editor)
114 if (m_fileName.compare(editor->file()->fileName(), Qt::CaseInsensitive))
117 if (editor->file()->fileName() != m_fileName)
120 if (ITextEditor *textEditor = qobject_cast<ITextEditor *>(editor)) {
121 if (m_markableInterface == 0) { // We aren't added to something
122 m_markableInterface = textEditor->markableInterface();
123 m_internalMark = new Internal::InternalMark(this);
125 if (m_markableInterface->addMark(m_internalMark, m_line)) {
126 // Handle reload of text documents, readding the mark as necessary
127 connect(textEditor->file(), SIGNAL(reloaded()),
128 this, SLOT(documentReloaded()), Qt::UniqueConnection);
130 removeInternalMark();
136 void BaseTextMark::documentReloaded()
138 if (m_markableInterface)
141 BaseTextDocument *doc = qobject_cast<BaseTextDocument*>(sender());
145 m_markableInterface = doc->documentMarker();
146 m_internalMark = new Internal::InternalMark(this);
148 if (!m_markableInterface->addMark(m_internalMark, m_line))
149 removeInternalMark();
152 void BaseTextMark::childRemovedFromEditor(Internal::InternalMark *mark)
155 // m_internalMark was removed from the editor
156 removeInternalMark();
160 void BaseTextMark::documentClosingFor(Internal::InternalMark *mark)
163 removeInternalMark();
166 void BaseTextMark::removeInternalMark()
168 delete m_internalMark;
170 m_markableInterface = 0;
175 void BaseTextMark::updateMarker()
177 //qDebug()<<"BaseTextMark::updateMarker()"<<m_markableInterface<<m_internalMark;
178 if (m_markableInterface)
179 m_markableInterface->updateMark(m_internalMark);
182 void BaseTextMark::moveMark(const QString & /* filename */, int /* line */)
184 Core::EditorManager *em = Core::EditorManager::instance();
186 connect(em, SIGNAL(editorOpened(Core::IEditor *)), this, SLOT(editorOpened(Core::IEditor *)));
190 if (m_markableInterface)
191 m_markableInterface->removeMark(m_internalMark);
192 // This is only necessary since m_internalMark is created in editorOpened
193 removeInternalMark();
195 foreach (Core::IEditor *editor, em->openedEditors())
196 editorOpened(editor);
198 } // namespace TextEditor