1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "sourceagent.h"
35 #include "breakhandler.h"
36 #include "debuggerengine.h"
37 #include "debuggercore.h"
38 #include "debuggerstringutils.h"
39 #include "stackframe.h"
40 #include "stackhandler.h"
42 #include <coreplugin/coreconstants.h>
43 #include <coreplugin/editormanager/editormanager.h>
44 #include <coreplugin/editormanager/ieditor.h>
45 #include <coreplugin/icore.h>
47 #include <texteditor/basetextdocument.h>
48 #include <texteditor/basetexteditor.h>
49 #include <texteditor/basetextmark.h>
50 #include <texteditor/plaintexteditor.h>
51 #include <texteditor/texteditorconstants.h>
53 #include <cppeditor/cppeditorconstants.h>
55 #include <utils/qtcassert.h>
57 #include <QtCore/QDebug>
58 #include <QtCore/QMetaObject>
59 #include <QtCore/QTimer>
60 #include <QtCore/QPointer>
61 #include <QtCore/QFileInfo>
63 #include <QtGui/QMessageBox>
64 #include <QtGui/QPlainTextEdit>
65 #include <QtGui/QTextBlock>
66 #include <QtGui/QTextCursor>
67 #include <QtGui/QIcon>
76 class LocationMarkFoo : public TextEditor::ITextMark
81 QIcon icon() const { return debuggerCore()->locationMarkIcon(); }
82 void updateLineNumber(int /*lineNumber*/) {}
83 void updateBlock(const QTextBlock & /*block*/) {}
84 void removedFromEditor() {}
85 void documentClosing() {}
86 TextEditor::ITextMark::Priority priority() const
87 { return TextEditor::ITextMark::HighPriority; }
90 class SourceAgentPrivate
94 ~SourceAgentPrivate();
97 QPointer<TextEditor::ITextEditor> editor;
98 QPointer<DebuggerEngine> engine;
99 TextEditor::ITextMark *locationMark;
104 SourceAgentPrivate::SourceAgentPrivate()
106 , locationMark(new LocationMarkFoo)
111 SourceAgentPrivate::~SourceAgentPrivate()
114 EditorManager *editorManager = EditorManager::instance();
115 editorManager->closeEditors(QList<IEditor *>() << editor);
121 SourceAgent::SourceAgent(DebuggerEngine *engine)
122 : QObject(0), d(new SourceAgentPrivate)
127 SourceAgent::~SourceAgent()
133 void SourceAgent::setSourceProducerName(const QString &name)
138 void SourceAgent::setContent(const QString &filePath, const QString &content)
140 QTC_ASSERT(d, return);
141 using namespace Core;
142 using namespace TextEditor;
146 EditorManager *editorManager = EditorManager::instance();
148 QString titlePattern = d->producer + QLatin1String(": ")
149 + QFileInfo(filePath).fileName();
150 d->editor = qobject_cast<ITextEditor *>(
151 editorManager->openEditorWithContents(
152 CppEditor::Constants::CPPEDITOR_ID,
153 &titlePattern, content));
154 QTC_ASSERT(d->editor, return);
155 d->editor->setProperty(Debugger::Constants::OPENED_BY_DEBUGGER, true);
157 BaseTextEditorWidget *baseTextEdit =
158 qobject_cast<BaseTextEditorWidget *>(d->editor->widget());
160 baseTextEdit->setRequestMarkEnabled(true);
163 editorManager->activateEditor(d->editor);
165 QPlainTextEdit *plainTextEdit =
166 qobject_cast<QPlainTextEdit *>(d->editor->widget());
167 QTC_ASSERT(plainTextEdit, return);
168 plainTextEdit->setReadOnly(true);
170 updateLocationMarker();
173 void SourceAgent::updateLocationMarker()
175 QTC_ASSERT(d->editor, return);
177 d->editor->markableInterface()->removeMark(d->locationMark);
178 if (d->engine->stackHandler()->currentFrame().file == d->path) {
179 int lineNumber = d->engine->stackHandler()->currentFrame().line;
180 d->editor->markableInterface()->addMark(d->locationMark, lineNumber);
181 QPlainTextEdit *plainTextEdit = qobject_cast<QPlainTextEdit *>(d->editor->widget());
182 QTC_ASSERT(plainTextEdit, return);
183 QTextCursor tc = plainTextEdit->textCursor();
184 QTextBlock block = tc.document()->findBlockByNumber(lineNumber - 1);
185 tc.setPosition(block.position());
186 plainTextEdit->setTextCursor(tc);
187 EditorManager *editorManager = EditorManager::instance();
188 editorManager->activateEditor(d->editor);
192 } // namespace Internal
193 } // namespace Debugger