File indexing completed on 2024-04-28 05:48:19

0001 /**
0002  * \file
0003  *
0004  * \brief Declare Kate's Close Except/Like plugin classes
0005  *
0006  * SPDX-FileCopyrightText: 2012 Alex Turbov <i.zaufi@gmail.com>
0007  *
0008  * \date Thu Mar  8 08:13:43 MSK 2012 -- Initial design
0009  */
0010 /*
0011  * KateCloseExceptPlugin is free software: you can redistribute it and/or modify it
0012  * under the terms of the GNU General Public License as published by the
0013  * Free Software Foundation, either version 3 of the License, or
0014  * (at your option) any later version.
0015  *
0016  * KateCloseExceptPlugin is distributed in the hope that it will be useful, but
0017  * WITHOUT ANY WARRANTY; without even the implied warranty of
0018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0019  * See the GNU General Public License for more details.
0020  *
0021  * You should have received a copy of the GNU General Public License along
0022  * with this program.  If not, see <http://www.gnu.org/licenses/>.
0023  */
0024 
0025 #pragma once
0026 
0027 // Project specific includes
0028 
0029 // Standard includes
0030 #include <KActionMenu>
0031 #include <KConfigGroup>
0032 #include <KTextEditor/Document>
0033 #include <KTextEditor/Editor>
0034 #include <KTextEditor/Message>
0035 #include <KTextEditor/Plugin>
0036 #include <KTextEditor/View>
0037 #include <KToggleAction>
0038 #include <QPointer>
0039 #include <cassert>
0040 #include <ktexteditor/sessionconfiginterface.h>
0041 #include <set>
0042 
0043 namespace kate
0044 {
0045 class CloseExceptPlugin; // forward declaration
0046 
0047 /**
0048  * \brief Plugin to close docs grouped by extension or location
0049  */
0050 class CloseExceptPluginView : public QObject, public KXMLGUIClient
0051 {
0052     Q_OBJECT
0053     typedef std::map<QString, QPointer<QAction>> actions_map_type;
0054 
0055 public:
0056     /// Default constructor
0057     CloseExceptPluginView(KTextEditor::MainWindow *, CloseExceptPlugin *);
0058     /// Destructor
0059     ~CloseExceptPluginView() override;
0060 
0061 private Q_SLOTS:
0062     void viewCreated(KTextEditor::View *);
0063     void documentCreated(KTextEditor::Editor *, KTextEditor::Document *);
0064     void updateMenuSlotStub(KTextEditor::Document *);
0065     void close(const QString &, const bool);
0066     void closeExcept(const QString &item)
0067     {
0068         close(item, false);
0069     }
0070     void closeLike(const QString &item)
0071     {
0072         close(item, true);
0073     }
0074 
0075 private:
0076     void displayMessage(const QString &, const QString &, KTextEditor::Message::MessageType);
0077     void connectToDocument(KTextEditor::Document *);
0078     void updateMenu();
0079     using CloseFunction = void (CloseExceptPluginView::*)(const QString &);
0080     void updateMenu(const std::set<QUrl> &, const std::set<QString> &, actions_map_type &, KActionMenu *, CloseFunction);
0081     void appendActionsFrom(const std::set<QUrl> &, actions_map_type &, KActionMenu *, CloseFunction);
0082     void appendActionsFrom(const std::set<QString> &masks, actions_map_type &actions, KActionMenu *menu, CloseFunction);
0083 
0084     CloseExceptPlugin *m_plugin;
0085     QPointer<KToggleAction> m_show_confirmation_action;
0086     QPointer<KActionMenu> m_except_menu;
0087     QPointer<KActionMenu> m_like_menu;
0088     actions_map_type m_except_actions;
0089     actions_map_type m_like_actions;
0090     KTextEditor::MainWindow *m_mainWindow;
0091     QPointer<KTextEditor::Message> m_infoMessage;
0092 };
0093 
0094 /**
0095  * \brief Plugin view class
0096  */
0097 class CloseExceptPlugin : public KTextEditor::Plugin, public KTextEditor::SessionConfigInterface
0098 {
0099     Q_OBJECT
0100     Q_INTERFACES(KTextEditor::SessionConfigInterface)
0101 public:
0102     /// Default constructor
0103     explicit CloseExceptPlugin(QObject * = nullptr, const QVariantList & = QVariantList());
0104     /// Destructor
0105     ~CloseExceptPlugin() override
0106     {
0107     }
0108     /// Create a new view of this plugin for the given main window
0109     QObject *createView(KTextEditor::MainWindow *) override;
0110     /// \name Plugin interface implementation
0111     //@{
0112     void readSessionConfig(const KConfigGroup &) override;
0113     void writeSessionConfig(KConfigGroup &) override;
0114     //@}
0115     bool showConfirmationNeeded() const
0116     {
0117         return m_show_confirmation_needed;
0118     }
0119 
0120 public Q_SLOTS:
0121     void toggleShowConfirmation(bool flag)
0122     {
0123         m_show_confirmation_needed = flag;
0124     }
0125 
0126 private:
0127     bool m_show_confirmation_needed = false;
0128 };
0129 
0130 } // namespace kate