File indexing completed on 2024-04-28 04:38:10

0001 /*
0002     SPDX-FileCopyrightText: 2006 Adam Treat <treat@kde.org>
0003     SPDX-FileCopyrightText: 2006-2007 Hamish Rodda <rodda@kde.org>
0004     SPDX-FileCopyrightText: 2009 Lior Mualem <lior.m.kde@gmail.com>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "classwidget.h"
0010 
0011 #include <QLabel>
0012 #include <QHBoxLayout>
0013 #include <QVBoxLayout>
0014 #include <QHeaderView>
0015 
0016 #include <KLocalizedString>
0017 #include <QLineEdit>
0018 #include <QTimer>
0019 
0020 #include "language/classmodel/classmodel.h"
0021 #include "classtree.h"
0022 #include "classbrowserplugin.h"
0023 
0024 using namespace KDevelop;
0025 
0026 ClassWidget::ClassWidget(QWidget* parent, ClassBrowserPlugin* plugin)
0027     : QWidget(parent)
0028     , m_plugin(plugin)
0029     , m_model(new ClassModel())
0030     , m_tree(new ClassTree(this, plugin))
0031     , m_searchLine(new QLineEdit(this))
0032     , m_filterTimer(new QTimer(this))
0033 {
0034     setObjectName(QStringLiteral("Class Browser Tree"));
0035     setWindowTitle(i18nc("@title:window", "Classes"));
0036     setWindowIcon(QIcon::fromTheme(QStringLiteral("code-class"), windowIcon()));
0037 
0038     // Set tree in the plugin
0039     m_plugin->setActiveClassTree(m_tree);
0040 
0041     // Set model in the tree view
0042     m_tree->setModel(m_model);
0043     m_tree->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
0044     m_tree->header()->setStretchLastSection(false);
0045 
0046     // We need notification in the model for the collapse/expansion of nodes.
0047     connect(m_tree, &ClassTree::collapsed,
0048             m_model, &ClassModel::collapsed);
0049     connect(m_tree, &ClassTree::expanded,
0050             m_model, &ClassModel::expanded);
0051 
0052     // Init filter timer
0053     m_filterTimer->setSingleShot(true);
0054     m_filterTimer->setInterval(500);
0055     connect(m_filterTimer, &QTimer::timeout, this, [this]() {
0056         m_tree->setCurrentIndex(QModelIndex());
0057         m_model->updateFilterString(m_filterText);
0058 
0059         if (m_filterText.isEmpty())
0060             m_tree->collapseAll();
0061         else
0062             m_tree->expandToDepth(0);
0063     });
0064 
0065     // Init search box
0066     m_searchLine->setClearButtonEnabled(true);
0067     connect(m_searchLine, &QLineEdit::textChanged, this, [this](const QString& newFilter) {
0068         m_filterText = newFilter;
0069         m_filterTimer->start();
0070     });
0071 
0072     auto* searchLabel = new QLabel(i18nc("@label:textbox", "S&earch:"), this);
0073     searchLabel->setBuddy(m_searchLine);
0074 
0075     auto* layout = new QHBoxLayout();
0076     layout->setSpacing(5);
0077     layout->setContentsMargins(0, 0, 0, 0);
0078     layout->addWidget(searchLabel);
0079     layout->addWidget(m_searchLine);
0080 
0081     setFocusProxy(m_searchLine);
0082 
0083     auto* vbox = new QVBoxLayout(this);
0084     vbox->setContentsMargins(0, 0, 0, 0);
0085     vbox->addLayout(layout);
0086     vbox->addWidget(m_tree);
0087     setLayout(vbox);
0088 
0089     setWhatsThis(i18nc("@info:whatsthis", "Class Browser"));
0090 }
0091 
0092 ClassWidget::~ClassWidget()
0093 {
0094     delete m_model;
0095 }
0096 
0097 #include "moc_classwidget.cpp"