OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / basetextmark.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "basetextmark.h"
35 #include "itexteditor.h"
36 #include "basetextdocument.h"
37
38 #include <coreplugin/editormanager/editormanager.h>
39 #include <extensionsystem/pluginmanager.h>
40
41 #include <QtCore/QTimer>
42 #include <QtGui/QIcon>
43
44 namespace TextEditor {
45 namespace Internal {
46
47 class InternalMark : public TextEditor::ITextMark
48 {
49 public:
50     explicit InternalMark(BaseTextMark *parent) : m_parent(parent) {}
51
52     virtual QIcon icon() const
53     {
54         return m_parent->icon();
55     }
56
57     virtual void updateLineNumber(int lineNumber)
58     {
59         return m_parent->updateLineNumber(lineNumber);
60     }
61
62     virtual void updateBlock(const QTextBlock &block)
63     {
64         return m_parent->updateBlock(block);
65     }
66
67     virtual void removedFromEditor()
68     {
69         m_parent->childRemovedFromEditor(this);
70     }
71
72     virtual void documentClosing()
73     {
74         m_parent->documentClosingFor(this);
75     }
76 private:
77     BaseTextMark *m_parent;
78 };
79
80 } // namespace Internal
81
82 BaseTextMark::BaseTextMark(const QString &filename, int line)
83     : m_markableInterface(0)
84     , m_internalMark(0)
85     , m_fileName(filename)
86     , m_line(line)
87     , m_init(false)
88 {
89     // Why is this?
90     QTimer::singleShot(0, this, SLOT(init()));
91 }
92
93 BaseTextMark::~BaseTextMark()
94 {
95     // oha we are deleted
96     if (m_markableInterface)
97         m_markableInterface->removeMark(m_internalMark);
98     removeInternalMark();
99 }
100
101 void BaseTextMark::init()
102 {
103     m_init = true;
104     Core::EditorManager *em = Core::EditorManager::instance();
105     connect(em, SIGNAL(editorOpened(Core::IEditor *)), this, SLOT(editorOpened(Core::IEditor *)));
106
107     foreach (Core::IEditor *editor, em->openedEditors())
108         editorOpened(editor);
109 }
110
111 void BaseTextMark::editorOpened(Core::IEditor *editor)
112 {
113 #ifdef Q_OS_WIN
114     if (m_fileName.compare(editor->file()->fileName(), Qt::CaseInsensitive))
115         return;
116 #else
117     if (editor->file()->fileName() != m_fileName)
118         return;
119 #endif
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);
124
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);
129             } else {
130                 removeInternalMark();
131             }
132         }
133     }
134 }
135
136 void BaseTextMark::documentReloaded()
137 {
138     if (m_markableInterface)
139         return;
140
141     BaseTextDocument *doc = qobject_cast<BaseTextDocument*>(sender());
142     if (!doc)
143         return;
144
145     m_markableInterface = doc->documentMarker();
146     m_internalMark = new Internal::InternalMark(this);
147
148     if (!m_markableInterface->addMark(m_internalMark, m_line))
149         removeInternalMark();
150 }
151
152 void BaseTextMark::childRemovedFromEditor(Internal::InternalMark *mark)
153 {
154     Q_UNUSED(mark)
155     // m_internalMark was removed from the editor
156     removeInternalMark();
157     removedFromEditor();
158 }
159
160 void BaseTextMark::documentClosingFor(Internal::InternalMark *mark)
161 {
162     Q_UNUSED(mark)
163     removeInternalMark();
164 }
165
166 void BaseTextMark::removeInternalMark()
167 {
168     delete m_internalMark;
169     m_internalMark = 0;
170     m_markableInterface = 0;
171 }
172
173 //#include <QDebug>
174
175 void BaseTextMark::updateMarker()
176 {
177     //qDebug()<<"BaseTextMark::updateMarker()"<<m_markableInterface<<m_internalMark;
178     if (m_markableInterface)
179         m_markableInterface->updateMark(m_internalMark);
180 }
181
182 void BaseTextMark::moveMark(const QString & /* filename */, int /* line */)
183 {
184     Core::EditorManager *em = Core::EditorManager::instance();
185     if (!m_init) {
186         connect(em, SIGNAL(editorOpened(Core::IEditor *)), this, SLOT(editorOpened(Core::IEditor *)));
187         m_init = true;
188     }
189
190     if (m_markableInterface)
191         m_markableInterface->removeMark(m_internalMark);
192     // This is only necessary since m_internalMark is created in editorOpened
193     removeInternalMark();
194
195     foreach (Core::IEditor *editor, em->openedEditors())
196         editorOpened(editor);
197 }
198 } // namespace TextEditor