File indexing completed on 2024-05-12 05:43:34

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include "typeview.h"
0019 #include "ui_typeview.h"
0020 
0021 #include <typemodel/typemodel.h>
0022 #include <navigator/codenavigator.h>
0023 
0024 #include <QItemSelectionModel>
0025 #include <QMessageBox>
0026 #include <QSortFilterProxyModel>
0027 
0028 TypeView::TypeView(QWidget* parent):
0029     QWidget(parent),
0030     ui(new Ui::TypeView),
0031     m_model(new TypeModel(this))
0032 {
0033     ui->setupUi(this);
0034 
0035     auto proxy = new QSortFilterProxyModel(this);
0036     proxy->setRecursiveFilteringEnabled(true);
0037     proxy->setSourceModel(m_model);
0038     ui->typeTreeView->setModel(proxy);
0039     ui->typeTreeView->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
0040 
0041     connect(ui->typeTreeView->selectionModel(), &QItemSelectionModel::selectionChanged,
0042             this, &TypeView::selectionChanged);
0043     connect(ui->searchLineEdit, &QLineEdit::textChanged, this, [proxy](const QString &text) {
0044         proxy->setFilterFixedString(text);
0045     });
0046     connect(ui->detailsView, &QTextBrowser::anchorClicked, this, [](const QUrl &url) {
0047         if (url.scheme() == QLatin1String("code"))
0048             CodeNavigator::goTo(url);
0049     });
0050 
0051 }
0052 
0053 TypeView::~TypeView() = default;
0054 
0055 void TypeView::setFileSet(ElfFileSet* fileSet)
0056 {
0057     m_fileSet = fileSet;
0058     if (isVisible()) {
0059         m_model->setFileSet(m_fileSet);
0060         if (m_model->hasInvalidDies())
0061             QMessageBox::warning(this, tr("Invalid DWARF entries"),
0062                                  tr("An error occurred while reading DWARF data of some ELF objects, the tree will be incomplete."));
0063     }
0064 }
0065 
0066 void TypeView::showEvent(QShowEvent* event)
0067 {
0068     if (isVisible() && m_model->rowCount() == 0 && m_fileSet) {
0069         const auto res = QMessageBox::question(this, tr("Compute Type Tree"), tr("Computing the type tree from DWARF data can take up to several minutes in which the application will not respond, and use up to 1.5GB of memory. Proceed anyway?"), QMessageBox::Yes, QMessageBox::Cancel);
0070         if (res == QMessageBox::Yes) {
0071             m_model->setFileSet(m_fileSet);
0072             if (m_model->hasInvalidDies())
0073                 QMessageBox::warning(this, tr("Invalid DWARF entries"),
0074                                      tr("An error occurred while reading DWARF data of some ELF objects, the tree will be incomplete."));
0075         }
0076     }
0077     QWidget::showEvent(event);
0078 }
0079 
0080 void TypeView::selectionChanged(const QItemSelection& selection)
0081 {
0082     if (selection.isEmpty())
0083         ui->detailsView->clear();
0084 
0085     const QModelIndex index = selection.first().topLeft();
0086     ui->detailsView->setHtml(index.data(TypeModel::DetailRole).toString());
0087 }