Changeset View
Changeset View
Standalone View
Standalone View
languages/clang/clangdebugwidget.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This file is part of KDevelop | ||||
3 | | ||||
4 | Copyright 2016 Aleix Pol Gonzalez <aleixpol@kde.org> | ||||
5 | | ||||
6 | This library is free software; you can redistribute it and/or | ||||
7 | modify it under the terms of the GNU Library General Public | ||||
8 | License as published by the Free Software Foundation; either | ||||
9 | version 2 of the License, or (at your option) any later version. | ||||
10 | | ||||
11 | This library is distributed in the hope that it will be useful, | ||||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
14 | Library General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU Library General Public License | ||||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||||
18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
19 | Boston, MA 02110-1301, USA. | ||||
20 | */ | ||||
21 | | ||||
22 | #include "clangdebugwidget.h" | ||||
23 | #include <languages/plugins/custom-definesandincludes/idefinesandincludesmanager.h> | ||||
24 | #include <interfaces/icore.h> | ||||
25 | #include <util/path.h> | ||||
26 | #include <language/duchain/duchain.h> | ||||
27 | #include <language/duchain/duchainlock.h> | ||||
28 | | ||||
29 | #include "duchain/clangparsingenvironmentfile.h" | ||||
30 | | ||||
31 | #include <QExplicitlySharedDataPointer> | ||||
32 | #include <QComboBox> | ||||
33 | #include <QVBoxLayout> | ||||
34 | #include <QStackedLayout> | ||||
35 | #include <QTextEdit> | ||||
36 | | ||||
37 | using namespace KDevelop; | ||||
38 | | ||||
39 | class ClangDocumentDebugWidget : public QWidget | ||||
40 | { | ||||
41 | public: | ||||
42 | ClangDocumentDebugWidget(const Path &url, QWidget* w) | ||||
43 | : QWidget(w) | ||||
44 | , m_url(url) | ||||
45 | { | ||||
46 | setLayout(new QVBoxLayout); | ||||
47 | m_edit = new QTextEdit(this); | ||||
48 | layout()->addWidget(m_edit); | ||||
49 | m_randomClangInfo = new QTextEdit(this); | ||||
50 | layout()->addWidget(m_randomClangInfo); | ||||
51 | | ||||
52 | } | ||||
53 | | ||||
54 | void addText(const QString &text) | ||||
55 | { | ||||
56 | m_edit->setPlainText(m_edit->toPlainText()+QLatin1Char('\n')+text); | ||||
57 | } | ||||
58 | | ||||
59 | void showEvent(QShowEvent* ev) override | ||||
60 | { | ||||
61 | QString infoText; | ||||
62 | | ||||
63 | const Path::List incs = IDefinesAndIncludesManager::manager()->includes(m_url.toLocalFile()); | ||||
64 | infoText += QStringLiteral("Includes:\n"); | ||||
65 | foreach(const Path& inc, incs) { | ||||
66 | infoText.append(inc.toUrl().toDisplayString()); | ||||
67 | infoText.append(QLatin1Char('\n')); | ||||
68 | } | ||||
69 | | ||||
70 | const auto defs = IDefinesAndIncludesManager::manager()->defines(m_url.toLocalFile()); | ||||
71 | infoText += QStringLiteral("\nDefines:\n"); | ||||
72 | for(auto it=defs.constBegin(), itEnd=defs.constEnd(); it!=itEnd; ++it) { | ||||
73 | infoText.append(QStringLiteral("-D%1=%2").arg(it.key(), it.value())); | ||||
74 | infoText.append(QLatin1Char('\n')); | ||||
75 | } | ||||
76 | m_randomClangInfo->setText(infoText); | ||||
77 | } | ||||
78 | | ||||
79 | private: | ||||
80 | const Path m_url; | ||||
81 | QTextEdit* m_edit; | ||||
82 | QTextEdit* m_randomClangInfo; | ||||
83 | }; | ||||
84 | | ||||
85 | ClangDebugWidget::ClangDebugWidget(QWidget* parent) | ||||
86 | : QWidget(parent) | ||||
87 | { | ||||
88 | connect(ICore::self(), &ICore::aboutToShutdown, this, &QObject::deleteLater); | ||||
89 | | ||||
90 | auto mainLayout = new QVBoxLayout(this); | ||||
91 | m_combo = new QComboBox(this); | ||||
92 | m_layout = new QStackedLayout(this); | ||||
93 | m_layout->setStackingMode(QStackedLayout::StackOne); | ||||
94 | | ||||
95 | mainLayout->addWidget(m_combo); | ||||
96 | mainLayout->addLayout(m_layout); | ||||
97 | | ||||
98 | setLayout(mainLayout); | ||||
99 | | ||||
100 | connect(m_combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated), this, &ClangDebugWidget::switchDocument); | ||||
101 | } | ||||
102 | | ||||
103 | ClangDebugWidget* ClangDebugWidget::self() | ||||
104 | { | ||||
105 | static ClangDebugWidget* self = nullptr; | ||||
106 | if (!self) | ||||
107 | self = new ClangDebugWidget; | ||||
108 | return self; | ||||
109 | } | ||||
110 | | ||||
111 | void ClangDebugWidget::addText(const QUrl& url, const QString& t) | ||||
112 | { | ||||
113 | if (!isVisible()) | ||||
114 | return; | ||||
115 | | ||||
116 | auto view = m_views[url]; | ||||
117 | if (!view) { | ||||
118 | view = new ClangDocumentDebugWidget(Path(url), this); | ||||
119 | m_views.insert(url, view); | ||||
120 | m_layout->addWidget(view); | ||||
121 | | ||||
122 | m_combo->addItem(url.toDisplayString(), qVariantFromValue(view)); | ||||
123 | } | ||||
124 | | ||||
125 | view->addText(t); | ||||
126 | } | ||||
127 | | ||||
128 | void ClangDebugWidget::sendText(const QUrl& url, const QString& text) | ||||
129 | { | ||||
130 | auto s = self(); | ||||
131 | QMetaObject::invokeMethod(s, "addText", Qt::QueuedConnection, Q_ARG(QUrl, url), Q_ARG(QString, text)); | ||||
132 | } | ||||
133 | | ||||
134 | void ClangDebugWidget::switchDocument(int comboIndex) | ||||
135 | { | ||||
136 | m_layout->setCurrentIndex(comboIndex); | ||||
137 | } |