File indexing completed on 2024-04-28 17:02:18

0001 /*
0002    This file is part of Massif Visualizer
0003 
0004    Copyright 2014 Milian Wolff <mail@milianw.de>
0005 
0006    This program is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU General Public License as
0008    published by the Free Software Foundation; either version 2 of
0009    the License or (at your option) version 3 or any later version
0010    accepted by the membership of KDE e.V. (or its successor approved
0011    by the membership of KDE e.V.), which shall act as a proxy
0012    defined in Section 14 of version 3 of the license.
0013 
0014    This program is distributed in the hope that it will be useful,
0015    but WITHOUT ANY WARRANTY; without even the implied warranty of
0016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017    GNU General Public License for more details.
0018 
0019    You should have received a copy of the GNU General Public License
0020    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "allocatorstab.h"
0024 
0025 #include "visualizer/allocatorsmodel.h"
0026 
0027 #include <QVBoxLayout>
0028 #include <QTreeView>
0029 #include <QSortFilterProxyModel>
0030 #include <QMenu>
0031 
0032 using namespace Massif;
0033 
0034 AllocatorsTab::AllocatorsTab(const FileData* data,
0035                              KXMLGUIClient* guiParent, QWidget* parent)
0036     : DocumentTabInterface(data, guiParent, parent)
0037     , m_model(new AllocatorsModel(data, this))
0038     , m_proxy(new QSortFilterProxyModel(this))
0039     , m_view(new QTreeView(this))
0040 {
0041     m_proxy->setSourceModel(m_model);
0042     m_proxy->setSortRole(AllocatorsModel::SortRole);
0043 
0044     m_view->setRootIsDecorated(false);
0045     m_view->setModel(m_proxy);
0046     m_view->setSortingEnabled(true);
0047     m_view->sortByColumn(AllocatorsModel::Peak);
0048     m_view->resizeColumnToContents(AllocatorsModel::Function);
0049     m_view->resizeColumnToContents(AllocatorsModel::Peak);
0050     m_view->setContextMenuPolicy(Qt::CustomContextMenu);
0051     connect(m_view->selectionModel(), &QItemSelectionModel::currentChanged,
0052             this, &AllocatorsTab::selectionChanged);
0053     connect(m_view, &QTreeView::customContextMenuRequested,
0054             this, &AllocatorsTab::customContextMenuRequested);
0055 
0056     setLayout(new QVBoxLayout(this));
0057     layout()->addWidget(m_view);
0058 }
0059 
0060 AllocatorsTab::~AllocatorsTab()
0061 {
0062 
0063 }
0064 
0065 void AllocatorsTab::selectModelItem(const ModelItem& item)
0066 {
0067     const QModelIndex idx = m_model->indexForItem(item);
0068     if (!idx.isValid()) {
0069         return;
0070     }
0071 
0072     m_view->selectionModel()->clearSelection();
0073     m_view->selectionModel()->setCurrentIndex(m_proxy->mapFromSource(idx),
0074                                               QItemSelectionModel::Select | QItemSelectionModel::Rows);
0075     m_view->scrollTo(m_view->selectionModel()->currentIndex());
0076 }
0077 
0078 void AllocatorsTab::settingsChanged()
0079 {
0080     m_model->settingsChanged();
0081 }
0082 
0083 void AllocatorsTab::selectionChanged(const QModelIndex& current, const QModelIndex& /*previous*/)
0084 {
0085     const ModelItem item = current.data(AllocatorsModel::ItemRole).value<ModelItem>();
0086     if (item.first) {
0087         emit modelItemSelected(item);
0088     }
0089 }
0090 
0091 void AllocatorsTab::customContextMenuRequested(const QPoint& pos)
0092 {
0093     const QModelIndex idx = m_view->indexAt(pos);
0094     const ModelItem item = idx.data(AllocatorsModel::ItemRole).value<ModelItem>();
0095 
0096     QMenu menu;
0097     emit contextMenuRequested(item, &menu);
0098     if (!menu.actions().isEmpty()) {
0099         menu.exec(m_view->mapToGlobal(pos));
0100     }
0101 }