File indexing completed on 2024-05-05 04:40:54

0001 /*
0002     SPDX-FileCopyrightText: 2007 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KDEVPLATFORM_PLUGIN_QUICKOPENMODEL_H
0008 #define KDEVPLATFORM_PLUGIN_QUICKOPENMODEL_H
0009 
0010 #include <QMultiMap>
0011 #include <QString>
0012 #include <QSet>
0013 
0014 #include <serialization/indexedstring.h>
0015 
0016 #include <language/interfaces/quickopendataprovider.h>
0017 #include "expandingtree/expandingwidgetmodel.h"
0018 class QTimer;
0019 class QuickOpenModel
0020     : public ExpandingWidgetModel
0021 {
0022     Q_OBJECT
0023 public:
0024     explicit QuickOpenModel(QWidget* parent);
0025 
0026     void registerProvider(const QStringList& scopes, const QStringList& type, KDevelop::QuickOpenDataProviderBase* provider);
0027 
0028     /**
0029      * Remove provider.
0030      * @param provider The provider to remove
0031      * @return Whether a provider was removed. If false, the provider was not attached.
0032      * */
0033     bool removeProvider(KDevelop::QuickOpenDataProviderBase* provider);
0034 
0035     ///Returns a list of all scopes that a registered through some providers
0036     QStringList allScopes() const;
0037     ///Returns a list of all types that a registered through some providers
0038     QStringList allTypes() const;
0039 
0040     /**
0041      * @param items The list of items that should be used.
0042      * @param scopes The list of scopes that should be used.
0043 
0044      * When this is called, the state is restart()ed.
0045      * */
0046     void enableProviders(const QStringList& items, const QStringList& scopes);
0047 
0048     ///Reset all providers, unexpand everything, empty caches.
0049     void restart(bool keepFilterText = false);
0050 
0051     QModelIndex index(int, int, const QModelIndex& parent) const override;
0052     QModelIndex parent(const QModelIndex&) const override;
0053     int rowCount(const QModelIndex&) const override;
0054     int unfilteredRowCount() const;
0055     int columnCount() const;
0056     int columnCount(const QModelIndex&) const override;
0057     QVariant data(const QModelIndex&, int) const override;
0058 
0059     /**
0060      * Tries to execute the item currently selected.
0061      * Returns true if the quickopen-dialog should be closed.
0062      * @param filterText Should be the current content of the filter line-edit.
0063      *
0064      * If this returns false, and filterText was changed, the change must be put
0065      * into the line-edit. That way items may execute by changing the content
0066      * of the line-edit.
0067      * */
0068     bool execute(const QModelIndex& index, QString& filterText);
0069 
0070     //The expandingwidgetmodel needs access to the tree-view
0071     void setTreeView(QTreeView* view);
0072 
0073     QTreeView* treeView() const override;
0074 
0075     virtual QSet<KDevelop::IndexedString> fileSet() const;
0076 
0077     ///This value will be added to the height of all created expanding-widgets
0078     void setExpandingWidgetHeightIncrease(int pixels);
0079 public Q_SLOTS:
0080     void textChanged(const QString& str);
0081 private Q_SLOTS:
0082     void destroyed(QObject* obj);
0083     void resetTimer();
0084     void restart_internal(bool keepFilterText);
0085 private:
0086     bool indexIsItem(const QModelIndex& index) const override;
0087 
0088     int contextMatchQuality(const QModelIndex& index) const override;
0089 
0090     KDevelop::QuickOpenDataPointer getItem(int row, bool noReset = false) const;
0091 
0092     using DataList = QHash<int, KDevelop::QuickOpenDataPointer>;
0093     mutable DataList m_cachedData;
0094 
0095     QTreeView* m_treeView;
0096     QTimer* m_resetTimer;
0097 
0098     struct ProviderEntry
0099     {
0100         ProviderEntry()
0101         {
0102         }
0103         bool enabled = false;
0104         QSet<QString> scopes;
0105         QSet<QString> types;
0106         KDevelop::QuickOpenDataProviderBase* provider;
0107     };
0108 
0109     //using ProviderMap = QMultiMap<QString, ProviderEntry>;
0110     using ProviderList = QVector<ProviderEntry>;
0111     ProviderList m_providers;
0112     QString m_filterText;
0113     int m_expandingWidgetHeightIncrease;
0114     mutable int m_resetBehindRow;
0115 
0116     QSet<QString> m_enabledItems;
0117     QSet<QString> m_enabledScopes;
0118 };
0119 
0120 #endif
0121