File indexing completed on 2024-04-28 05:48:42
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org> 0003 SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org> 0004 SPDX-FileCopyrightText: 2002 Anders Lund <anders.lund@lund.tdcadsl.dk> 0005 SPDX-FileCopyrightText: 2017 Ederag <edera@gmx.fr> 0006 0007 SPDX-License-Identifier: LGPL-2.0-only 0008 */ 0009 0010 #pragma once 0011 0012 #include <KTextEditor/Plugin> 0013 #include <ktexteditor/configpage.h> 0014 #include <ktexteditor/mainwindow.h> 0015 0016 #include <QKeyEvent> 0017 #include <QList> 0018 0019 #include <KXMLGUIClient> 0020 0021 class QShowEvent; 0022 0023 namespace KParts 0024 { 0025 class ReadOnlyPart; 0026 } 0027 0028 class KateConsole; 0029 class KateKonsolePluginView; 0030 class KPluginFactory; 0031 0032 class KateKonsolePlugin : public KTextEditor::Plugin 0033 { 0034 Q_OBJECT 0035 0036 friend class KateKonsolePluginView; 0037 0038 public: 0039 explicit KateKonsolePlugin(QObject *parent = nullptr, const QVariantList & = QVariantList()); 0040 ~KateKonsolePlugin() override; 0041 0042 QObject *createView(KTextEditor::MainWindow *mainWindow) override; 0043 0044 int configPages() const override 0045 { 0046 return 1; 0047 } 0048 KTextEditor::ConfigPage *configPage(int number = 0, QWidget *parent = nullptr) override; 0049 0050 void readConfig(); 0051 0052 QByteArray previousEditorEnv() 0053 { 0054 return m_previousEditorEnv; 0055 } 0056 0057 private: 0058 QList<KateKonsolePluginView *> mViews; 0059 QByteArray m_previousEditorEnv; 0060 }; 0061 0062 class KateKonsolePluginView : public QObject 0063 { 0064 Q_OBJECT 0065 0066 public: 0067 /** 0068 * Constructor. 0069 */ 0070 KateKonsolePluginView(KateKonsolePlugin *plugin, KTextEditor::MainWindow *mainWindow); 0071 0072 /** 0073 * Virtual destructor. 0074 */ 0075 ~KateKonsolePluginView() override; 0076 0077 void readConfig(); 0078 0079 private: 0080 KateKonsolePlugin *m_plugin; 0081 KateConsole *m_console; 0082 }; 0083 0084 /** 0085 * KateConsole 0086 * This class is used for the internal terminal emulator 0087 * It uses internally the konsole part, thx to konsole devs :) 0088 */ 0089 class KateConsole : public QWidget, public KXMLGUIClient 0090 { 0091 Q_OBJECT 0092 0093 public: 0094 /** 0095 * construct us 0096 * @param mw main window 0097 * @param parent toolview 0098 */ 0099 KateConsole(KateKonsolePlugin *plugin, KTextEditor::MainWindow *mw, QWidget *parent); 0100 0101 /** 0102 * destruct us 0103 */ 0104 ~KateConsole() override; 0105 0106 void readConfig(); 0107 0108 /** 0109 * cd to dir 0110 * @param path given local directory 0111 */ 0112 void cd(const QString &path); 0113 0114 /** 0115 * send given text to console 0116 * @param text commands for console 0117 */ 0118 void sendInput(const QString &text); 0119 0120 KTextEditor::MainWindow *mainWindow() 0121 { 0122 return m_mw; 0123 } 0124 0125 bool eventFilter(QObject *w, QEvent *e) override; 0126 0127 static KPluginFactory *pluginFactory(); 0128 0129 bool hasKonsole() const 0130 { 0131 return pluginFactory() != nullptr; 0132 } 0133 0134 public Q_SLOTS: 0135 /** 0136 * pipe current document to console 0137 */ 0138 void slotPipeToConsole(); 0139 0140 /** 0141 * synchronize the konsole with the current document (cd to the directory) 0142 */ 0143 void slotSync(); 0144 0145 /** 0146 * synchronize the konsole when the current document's url changes (e.g. save as) 0147 */ 0148 void slotViewOrUrlChanged(KTextEditor::View *view = nullptr); 0149 0150 /** 0151 * When syncing is done by the user, also show the terminal if it is hidden 0152 */ 0153 void slotManualSync(); 0154 0155 /** 0156 * run the current document in the konsole 0157 */ 0158 void slotRun(); 0159 0160 private Q_SLOTS: 0161 /** 0162 * the konsole exited ;) 0163 * handle that, hide the dock 0164 */ 0165 void slotDestroyed(); 0166 0167 /** 0168 * construct console if needed 0169 */ 0170 void loadConsoleIfNeeded(); 0171 0172 /** 0173 * Show or hide the konsole view as appropriate. 0174 */ 0175 void slotToggleVisibility(); 0176 0177 /** 0178 * set or clear focus as appropriate. 0179 */ 0180 void slotToggleFocus(); 0181 0182 /** 0183 * changes the menu actions text based on focus 0184 */ 0185 void focusChanged(QWidget *, QWidget *now); 0186 0187 /** 0188 * Handle that shortcuts are not eaten by console 0189 */ 0190 static void overrideShortcut(QKeyEvent *event, bool &override); 0191 0192 /** 0193 * hide terminal on Esc key press 0194 */ 0195 void handleEsc(QEvent *e); 0196 0197 protected: 0198 /** 0199 * the konsole get shown 0200 * @param ev show event 0201 */ 0202 void showEvent(QShowEvent *ev) override; 0203 0204 void paintEvent(QPaintEvent *e) override; 0205 0206 private: 0207 /** 0208 * plugin factory for the terminal 0209 */ 0210 static inline KPluginFactory *s_pluginFactory = nullptr; 0211 0212 /** 0213 * console part 0214 */ 0215 KParts::ReadOnlyPart *m_part; 0216 0217 /** 0218 * main window of this console 0219 */ 0220 KTextEditor::MainWindow *m_mw; 0221 0222 /** 0223 * toolview for this console 0224 */ 0225 QWidget *m_toolView; 0226 0227 KateKonsolePlugin *m_plugin; 0228 QString m_currentPath; 0229 QMetaObject::Connection m_urlChangedConnection; 0230 }; 0231 0232 class KateKonsoleConfigPage : public KTextEditor::ConfigPage 0233 { 0234 Q_OBJECT 0235 public: 0236 explicit KateKonsoleConfigPage(QWidget *parent = nullptr, KateKonsolePlugin *plugin = nullptr); 0237 ~KateKonsoleConfigPage() override 0238 { 0239 } 0240 0241 QString name() const override; 0242 QString fullName() const override; 0243 QIcon icon() const override; 0244 0245 void apply() override; 0246 void reset() override; 0247 void defaults() override 0248 { 0249 } 0250 0251 private: 0252 class QCheckBox *cbAutoSyncronize; 0253 class QCheckBox *cbRemoveExtension; 0254 class QLineEdit *lePrefix; 0255 class QCheckBox *cbSetEditor; 0256 class QCheckBox *cbSetEscHideKonsole; 0257 class QLineEdit *leEscExceptions; 0258 KateKonsolePlugin *mPlugin; 0259 0260 private Q_SLOTS: 0261 /** 0262 * Enable the warning dialog for the next "Run in terminal" 0263 */ 0264 static void slotEnableRunWarning(); 0265 };