OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / sourceagent.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "sourceagent.h"
34
35 #include "breakhandler.h"
36 #include "debuggerengine.h"
37 #include "debuggercore.h"
38 #include "debuggerstringutils.h"
39 #include "stackframe.h"
40 #include "stackhandler.h"
41
42 #include <coreplugin/coreconstants.h>
43 #include <coreplugin/editormanager/editormanager.h>
44 #include <coreplugin/editormanager/ieditor.h>
45 #include <coreplugin/icore.h>
46
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>
52
53 #include <cppeditor/cppeditorconstants.h>
54
55 #include <utils/qtcassert.h>
56
57 #include <QtCore/QDebug>
58 #include <QtCore/QMetaObject>
59 #include <QtCore/QTimer>
60 #include <QtCore/QPointer>
61 #include <QtCore/QFileInfo>
62
63 #include <QtGui/QMessageBox>
64 #include <QtGui/QPlainTextEdit>
65 #include <QtGui/QTextBlock>
66 #include <QtGui/QTextCursor>
67 #include <QtGui/QIcon>
68
69 #include <limits.h>
70
71 using namespace Core;
72
73 namespace Debugger {
74 namespace Internal {
75
76 class LocationMarkFoo : public TextEditor::ITextMark
77 {
78 public:
79     LocationMarkFoo() {}
80
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; }
88 };
89
90 class SourceAgentPrivate
91 {
92 public:
93     SourceAgentPrivate();
94     ~SourceAgentPrivate();
95
96 public:
97     QPointer<TextEditor::ITextEditor> editor;
98     QPointer<DebuggerEngine> engine;
99     TextEditor::ITextMark *locationMark;
100     QString path;
101     QString producer;
102 };
103
104 SourceAgentPrivate::SourceAgentPrivate()
105   : editor(0)
106   , locationMark(new LocationMarkFoo)
107   , producer("remote")
108 {
109 }
110
111 SourceAgentPrivate::~SourceAgentPrivate()
112 {
113     if (editor) {
114         EditorManager *editorManager = EditorManager::instance();
115         editorManager->closeEditors(QList<IEditor *>() << editor);
116     }
117     editor = 0;
118     delete locationMark;
119 }
120
121 SourceAgent::SourceAgent(DebuggerEngine *engine)
122     : QObject(0), d(new SourceAgentPrivate)
123 {
124     d->engine = engine;
125 }
126
127 SourceAgent::~SourceAgent()
128 {
129     delete d;
130     d = 0;
131 }
132
133 void SourceAgent::setSourceProducerName(const QString &name)
134 {
135     d->producer = name;
136 }
137
138 void SourceAgent::setContent(const QString &filePath, const QString &content)
139 {
140     QTC_ASSERT(d, return);
141     using namespace Core;
142     using namespace TextEditor;
143
144     d->path = filePath;
145
146     EditorManager *editorManager = EditorManager::instance();
147     if (!d->editor) {
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);
156
157         BaseTextEditorWidget *baseTextEdit =
158                 qobject_cast<BaseTextEditorWidget *>(d->editor->widget());
159         if (baseTextEdit)
160             baseTextEdit->setRequestMarkEnabled(true);
161     }
162
163     editorManager->activateEditor(d->editor);
164
165     QPlainTextEdit *plainTextEdit =
166         qobject_cast<QPlainTextEdit *>(d->editor->widget());
167     QTC_ASSERT(plainTextEdit, return);
168     plainTextEdit->setReadOnly(true);
169
170     updateLocationMarker();
171 }
172
173 void SourceAgent::updateLocationMarker()
174 {
175     QTC_ASSERT(d->editor, return);
176
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);
189     }
190 }
191
192 } // namespace Internal
193 } // namespace Debugger