File indexing completed on 2024-05-05 16:46:04

0001 /*
0002     SPDX-FileCopyrightText: 2010, 2015 Alex Richardson <alex.richardson@gmx.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "outlinewidget.h"
0008 
0009 #include <QAction>
0010 #include <QVBoxLayout>
0011 #include <QTreeView>
0012 #include <QLineEdit>
0013 #include <QIcon>
0014 #include <QWidgetAction>
0015 #include <QSortFilterProxyModel>
0016 
0017 #include <KLocalizedString>
0018 
0019 #include "outlineviewplugin.h"
0020 #include "outlinemodel.h"
0021 
0022 using namespace KDevelop;
0023 
0024 OutlineWidget::OutlineWidget(QWidget* parent, OutlineViewPlugin* plugin)
0025     : QWidget(parent)
0026     , m_plugin(plugin)
0027     , m_model(new OutlineModel(this))
0028     , m_tree(new QTreeView(this))
0029     , m_proxy(new QSortFilterProxyModel(this))
0030     , m_filter(new QLineEdit(this))
0031 {
0032     setObjectName(QStringLiteral("Outline View"));
0033     setWindowTitle(i18nc("@title:window", "Outline"));
0034     setWindowIcon(QIcon::fromTheme(QStringLiteral("code-class"), windowIcon())); //TODO: better icon?
0035 
0036     m_proxy->setRecursiveFilteringEnabled(true);
0037     m_proxy->setSourceModel(m_model);
0038     m_proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
0039     m_proxy->setSortCaseSensitivity(Qt::CaseInsensitive);
0040     m_proxy->setDynamicSortFilter(false);
0041 
0042     m_tree->setModel(m_proxy);
0043     m_tree->setHeaderHidden(true);
0044 
0045     // sort action
0046     m_sortAlphabeticallyAction = new QAction(QIcon::fromTheme(QStringLiteral("view-sort-ascending")),
0047                                              i18nc("@action", "Sort Alphabetically"), this);
0048     m_sortAlphabeticallyAction->setToolTip(i18nc("@info:tooltip", "Sort items alphabetically"));
0049     m_sortAlphabeticallyAction->setCheckable(true);
0050     connect(m_sortAlphabeticallyAction, &QAction::triggered, this, [this](bool sort) {
0051         // calling sort with -1 will restore the original order
0052         m_proxy->sort(sort ? 0 : -1, Qt::AscendingOrder);
0053         m_sortAlphabeticallyAction->setChecked(sort);
0054     });
0055     addAction(m_sortAlphabeticallyAction);
0056 
0057     // filter
0058     connect(m_filter, &QLineEdit::textChanged, m_proxy, &QSortFilterProxyModel::setFilterFixedString);
0059     connect(m_tree, &QTreeView::activated, this, &OutlineWidget::activated);
0060     m_filter->setPlaceholderText(i18nc("@info:placeholder", "Filter..."));
0061     auto filterAction = new QWidgetAction(this);
0062     filterAction->setDefaultWidget(m_filter);
0063     addAction(filterAction);
0064 
0065     setFocusProxy(m_filter);
0066 
0067     auto* vbox = new QVBoxLayout(this);
0068     vbox->setContentsMargins(0, 0, 0, 0);
0069     vbox->addWidget(m_tree);
0070     setLayout(vbox);
0071     expandFirstLevel();
0072     connect(m_model, &QAbstractItemModel::modelReset, this, &OutlineWidget::expandFirstLevel);
0073 }
0074 
0075 void OutlineWidget::activated(const QModelIndex& index)
0076 {
0077     QModelIndex realIndex = m_proxy->mapToSource(index);
0078     m_model->activate(realIndex);
0079 }
0080 
0081 OutlineWidget::~OutlineWidget()
0082 {
0083 }
0084 
0085 void OutlineWidget::expandFirstLevel()
0086 {
0087     for (int i = 0; i < m_proxy->rowCount(); i++) {
0088         m_tree->expand(m_proxy->index(i, 0));
0089     }
0090 }
0091 
0092 #include "moc_outlinewidget.cpp"