File indexing completed on 2024-04-28 05:49:18

0001 /* This file is part of the KDE project
0002 
0003    SPDX-FileCopyrightText: 2014 Dominik Haumann <dhaumann@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #pragma once
0009 
0010 #include <KTextEditor/MainWindow>
0011 #include <KTextEditor/Plugin>
0012 
0013 #include <doc_or_widget.h>
0014 
0015 #include <QList>
0016 #include <QVariant>
0017 
0018 #include <KXMLGUIClient>
0019 #include <unordered_set>
0020 
0021 class TabSwitcherPluginView;
0022 class TabSwitcherTreeView;
0023 class QStandardItemModel;
0024 class QModelIndex;
0025 namespace detail
0026 {
0027 class TabswitcherFilesModel;
0028 }
0029 
0030 class TabSwitcherPlugin : public KTextEditor::Plugin
0031 {
0032     Q_OBJECT
0033 
0034     friend TabSwitcherPluginView;
0035 
0036 public:
0037     /**
0038      * Plugin constructor.
0039      */
0040     explicit TabSwitcherPlugin(QObject *parent = nullptr, const QVariantList & = QVariantList());
0041 
0042     /**
0043      * Create a new tab switcher for @p mainWindow.
0044      */
0045     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
0046 
0047 private:
0048     QList<TabSwitcherPluginView *> m_views;
0049 };
0050 
0051 class TabSwitcherPluginView : public QObject, public KXMLGUIClient
0052 {
0053     Q_OBJECT
0054 
0055 public:
0056     /**
0057      * View constructor.
0058      */
0059     TabSwitcherPluginView(TabSwitcherPlugin *plugin, KTextEditor::MainWindow *mainWindow);
0060 
0061     /**
0062      * View destructor.
0063      */
0064     ~TabSwitcherPluginView() override;
0065 
0066     /**
0067      * Setup the shortcut actions.
0068      */
0069     void setupActions();
0070 
0071     /**
0072      * Initial fill of model with documents from the application.
0073      */
0074     void setupModel();
0075 
0076 public Q_SLOTS:
0077 
0078     /**
0079      * Adds @p widget to the model.
0080      */
0081     void onWidgetCreated(QWidget *widget);
0082 
0083     /**
0084      * Removes @p widget from the model.
0085      */
0086     void onWidgetRemoved(QWidget *widget);
0087 
0088     /**
0089      * Adds @p document to the model.
0090      */
0091     void registerDocument(KTextEditor::Document *document);
0092 
0093     /**
0094      * Removes @p document from the model.
0095      */
0096     void unregisterDocument(KTextEditor::Document *document);
0097 
0098     /**
0099      * Update the name in the model for @p document.
0100      */
0101     void updateDocumentName(KTextEditor::Document *document);
0102 
0103     /**
0104      * Raise @p view in a lru fashion.
0105      */
0106     void raiseView(KTextEditor::View *view);
0107 
0108     /**
0109      * Focus next item in the treeview.
0110      */
0111     void walkForward();
0112 
0113     /**
0114      * Focus previous item in the treeview.
0115      */
0116     void walkBackward();
0117 
0118     /**
0119      * Activate the document for @p index.
0120      */
0121     void switchToClicked(const QModelIndex &index);
0122 
0123     /**
0124      * Show the document for @p index.
0125      */
0126     void activateView(const QModelIndex &index);
0127 
0128     /**
0129      * Closes the current view
0130      */
0131     void closeView();
0132 
0133 protected:
0134     /**
0135      * Move through the list.
0136      */
0137     void walk(const int from, const int to);
0138 
0139     /**
0140      * Make sure the popup view has a sane size.
0141      */
0142     void updateViewGeometry();
0143 
0144 private:
0145     void registerItem(DocOrWidget docOrWidget);
0146     void unregisterItem(DocOrWidget docOrWidget);
0147 
0148     TabSwitcherPlugin *m_plugin;
0149     KTextEditor::MainWindow *m_mainWindow;
0150     detail::TabswitcherFilesModel *m_model;
0151     std::unordered_set<DocOrWidget> m_documents;
0152     TabSwitcherTreeView *m_treeView;
0153 };