1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "valgrindbar.h" |
6 | #include "valgrindrunner.h" |
7 | #include "xmlstreamreader.h" |
8 | |
9 | #include <QTableWidget> |
10 | #include <QTreeWidget> |
11 | #include <QHeaderView> |
12 | #include <QHBoxLayout> |
13 | |
14 | static QStringList memcheckItemNames {QObject::tr("Issue" ), QObject::tr("Location" )}; |
15 | static QStringList helgrindItemNames {QObject::tr("Issue" ), QObject::tr("Location" )}; |
16 | |
17 | class ValgrindBarPrivate |
18 | { |
19 | friend class ValgrindBar; |
20 | QTreeWidget *memcheckWidget {nullptr}; |
21 | QTreeWidget *helgrindWidget {nullptr}; |
22 | QTabWidget *tabWidget {nullptr}; |
23 | }; |
24 | |
25 | ValgrindBar::ValgrindBar(QWidget *parent) |
26 | : QWidget(parent) |
27 | , d(new ValgrindBarPrivate()) |
28 | { |
29 | d->tabWidget = new QTabWidget(this); |
30 | d->memcheckWidget = new QTreeWidget(d->tabWidget); |
31 | d->helgrindWidget = new QTreeWidget(d->tabWidget); |
32 | |
33 | initValgrindbar(); |
34 | |
35 | QObject::connect(ValgrindRunner::instance(), &ValgrindRunner::valgrindFinished, this, &ValgrindBar::showResult); |
36 | QObject::connect(ValgrindRunner::instance(), &ValgrindRunner::clearValgrindBar, this, &ValgrindBar::clearDisplay); |
37 | } |
38 | |
39 | void ValgrindBar::refreshDisplay(QTreeWidget *treeWidget) |
40 | { |
41 | treeWidget->clear(); |
42 | } |
43 | |
44 | void ValgrindBar::clearDisplay(const QString &type) |
45 | { |
46 | if ("memcheck" == type) { |
47 | d->memcheckWidget->clear(); |
48 | } else if ("helgrind" == type) { |
49 | d->helgrindWidget->clear(); |
50 | } |
51 | } |
52 | |
53 | void ValgrindBar::initValgrindbar() |
54 | { |
55 | setWidgetStyle(d->memcheckWidget, memcheckItemNames); |
56 | setWidgetStyle(d->helgrindWidget, helgrindItemNames); |
57 | |
58 | d->tabWidget->addTab(d->memcheckWidget, tr("memcheck" )); |
59 | d->tabWidget->addTab(d->helgrindWidget, tr("helgrind" )); |
60 | d->tabWidget->setTabPosition(QTabWidget::South); |
61 | |
62 | QHBoxLayout *hLayout = new QHBoxLayout(this); |
63 | this->setLayout(hLayout); |
64 | hLayout->addWidget(d->tabWidget); |
65 | } |
66 | |
67 | void ValgrindBar::setWidgetStyle(QTreeWidget *treeWidget, const QStringList &itemNames) |
68 | { |
69 | treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeMode::ResizeToContents); |
70 | treeWidget->setHeaderLabels(itemNames); |
71 | treeWidget->setColumnCount(itemNames.size()); |
72 | } |
73 | |
74 | void ValgrindBar::showResult(const QString &xmlFileName, const QString &type) |
75 | { |
76 | QTreeWidget *treeWidget = nullptr; |
77 | |
78 | if ("memcheck" == type) { |
79 | treeWidget = d->memcheckWidget; |
80 | } else if ("helgrind" == type){ |
81 | treeWidget = d->helgrindWidget; |
82 | } |
83 | |
84 | d->tabWidget->setCurrentWidget(treeWidget); |
85 | refreshDisplay(treeWidget); |
86 | |
87 | XmlStreamReader reader(treeWidget); |
88 | reader.readFile(xmlFileName); |
89 | editor.switchContext(tr("&Valgrind" )); |
90 | } |
91 | |
92 | |