forked from CAST-projects/Clang-ast-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
169 lines (154 loc) · 5.57 KB
/
MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "MainWindow.h"
#include <qmessagebox.h>
#include <qwindow.h>
#include <qfilesystemmodel.h>
#include <qstringlist.h>
#include "AstModel.h"
class UpdateLock
{
public:
UpdateLock(bool &lock) : myLock(lock)
{
myLock = true;
}
~UpdateLock()
{
myLock = false;
}
bool & myLock;
};
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
isUpdateInProgress(false)
{
myUi.setupUi(this);
connect(myUi.actionRefresh, &QAction::triggered, this, &MainWindow::RefreshAst);
myHighlighter = new Highlighter(myUi.codeViewer->document());
myUi.nodeProperties->setHeaderLabels({ "Property", "Value" });
connect(myUi.codeViewer, &QTextEdit::cursorPositionChanged, this, &MainWindow::HighlightNodeMatchingCode);
connect(myUi.codeViewer, &QTextEdit::textChanged, this, &MainWindow::OnCodeChange);
connect(myUi.showDetails, &QPushButton::clicked, this, &MainWindow::ShowNodeDetails);
}
void MainWindow::RefreshAst()
{
auto ast = myReader.readAst(myUi.codeViewer->document()->toPlainText().toStdString(),
myUi.commandLineArgs->document()->toPlainText().toStdString());
auto model = new AstModel(std::move(ast));
myUi.astTreeView->setModel(model);
myUi.astTreeView->setRootIndex(model->rootIndex());
connect(myUi.astTreeView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &MainWindow::HighlightCodeMatchingNode);
connect(myUi.astTreeView->selectionModel(), &QItemSelectionModel::currentChanged,
this, &MainWindow::DisplayNodeProperties);
myUi.astTreeView->setEnabled(myReader.ready());
}
void MainWindow::HighlightCodeMatchingNode(const QModelIndex &newNode, const QModelIndex &previousNode)
{
if (isUpdateInProgress)
{
return;
}
auto lock = UpdateLock{ isUpdateInProgress };
auto node = myUi.astTreeView->model()->data(newNode, Qt::NodeRole).value<GenericAstNode*>();
auto &manager = myReader.getManager();
std::pair<int, int> location;
if (!node->getRangeInMainFile(location, manager, myReader.getContext()))
{
return;
}
auto cursor = myUi.codeViewer->textCursor();
cursor.setPosition(location.first);
cursor.setPosition(location.second, QTextCursor::KeepAnchor);
myUi.codeViewer->setTextCursor(cursor);
}
void MainWindow::DisplayNodeProperties(const QModelIndex &newNode, const QModelIndex &previousNode)
{
myUi.nodeProperties->clear();
auto node = myUi.astTreeView->model()->data(newNode, Qt::NodeRole).value<GenericAstNode*>();
for (auto &prop : node->getProperties())
{
new QTreeWidgetItem(myUi.nodeProperties, QStringList{ QString::fromStdString(prop.first), QString::fromStdString(prop.second) });
}
myUi.showDetails->setVisible(node->hasDetails);
}
void MainWindow::HighlightNodeMatchingCode()
{
if (isUpdateInProgress || !myReader.ready())
{
return;
}
auto lock = UpdateLock{ isUpdateInProgress };
auto cursorPosition = myUi.codeViewer->textCursor().position();
auto nodePath = myReader.getBestNodeMatchingPosition(cursorPosition);
auto model = myUi.astTreeView->model();
if (!nodePath.empty())
{
auto currentIndex = model->index(0, 0); // Returns the root
currentIndex = model->index(0, 0, currentIndex); // Returns the AST node
auto currentNode = nodePath.front();
bool first = true;
for (auto node : nodePath)
{
if (first)
{
first = false;
}
else
{
auto index = currentNode->findChildIndex(node);
if (index == -1)
{
// Something wrong, just silently return
return;
}
currentIndex = model->index(index, 0, currentIndex);
currentNode = node;
}
}
myUi.astTreeView->scrollTo(currentIndex, QAbstractItemView::EnsureVisible);
auto selectionModel = myUi.astTreeView->selectionModel();
// selectionModel->select(currentIndex, QItemSelectionModel::ClearAndSelect);
selectionModel->setCurrentIndex(currentIndex, QItemSelectionModel::ClearAndSelect);
DisplayNodeProperties(currentIndex, currentIndex); // Since we won't use the previous node, it's not an issue if it is wrong...
}
}
void MainWindow::ShowNodeDetails()
{
auto selectionModel = myUi.astTreeView->selectionModel();
auto model = myUi.astTreeView->model();
auto node = myUi.astTreeView->model()->data(selectionModel->currentIndex(), Qt::NodeRole).value<GenericAstNode*>();
if (! node || !node->hasDetails)
{
QMessageBox::warning(this, windowTitle() + " - Error in details",
"The currently selected node does not have details", QMessageBox::Ok);
return;
}
if (node->details.empty())
{
node->details = node->detailsComputer();
}
auto win = new QDialog(this);
win->setLayout(new QGridLayout());
win->resize(size());
win->move(pos());
win->setWindowTitle(windowTitle() + " - " + QString::fromStdString(node->name) + " - " + QString::fromStdString(node->detailsTitle));
auto edit = new QTextEdit(win);
win->layout()->addWidget(edit);
edit->setText(QString::fromStdString(node->details));
edit->setReadOnly(true);
myDetailWindows.push_back(win);
win->show();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
for (auto win : myDetailWindows)
{
win->close();
}
event->accept();
}
void MainWindow::OnCodeChange()
{
myUi.astTreeView->setEnabled(false);
myReader.dirty();
}