File indexing completed on 2024-04-21 15:55:34
0001 /************************************************************************** 0002 * Copyright (C) 2006-2019 by Michel Ludwig (michel.ludwig@kdemail.net) * 0003 ***************************************************************************/ 0004 0005 /************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published by * 0009 * the Free Software Foundation; either version 2 of the License, or * 0010 * (at your option) any later version. * 0011 * * 0012 ***************************************************************************/ 0013 0014 #ifndef EDITORKEYSEQUENCEMANAGER_H 0015 #define EDITORKEYSEQUENCEMANAGER_H 0016 0017 #include <QEvent> 0018 #include <QMap> 0019 #include <QObject> 0020 #include <QString> 0021 #include <QStringList> 0022 0023 #include <KTextEditor/View> 0024 0025 namespace KileScript { 0026 class Script; 0027 class Manager; 0028 } 0029 0030 class KileInfo; 0031 0032 namespace KileEditorKeySequence { 0033 /** 0034 * This class represents an action that can be assigned to an editor key sequence. 0035 **/ 0036 class Action { 0037 public: 0038 Action(); 0039 virtual ~Action(); 0040 0041 /** 0042 * The main method, which implements the "action" itself. 0043 **/ 0044 virtual void execute() = 0; 0045 0046 /** 0047 * Returns a textual representation of the action. 0048 **/ 0049 virtual QString getDescription() const; 0050 }; 0051 0052 /** 0053 * This class represents the execution of a script in Kile. 0054 **/ 0055 class ExecuteScriptAction : public Action { 0056 public: 0057 ExecuteScriptAction(KileScript::Script *script, KileScript::Manager *scriptManager); 0058 virtual ~ExecuteScriptAction(); 0059 0060 virtual void execute() override; 0061 virtual QString getDescription() const override; 0062 0063 protected: 0064 KileScript::Script *m_script; 0065 KileScript::Manager *m_scriptManager; 0066 }; 0067 0068 // forward declaration 0069 class Recorder; 0070 0071 /** 0072 * This manager class is responsible for handling the key sequences that get assigned 0073 * to actions. Currently, every key sequence can only trigger one single action. 0074 * 0075 * Whenever a watched key sequence is typed, the manager triggers the corresponding 0076 * action. The only characters that are allowed in key sequences are those that make 0077 * the cursor advance by one position, i.e. for example tabs are not allowed in key 0078 * sequences. 0079 **/ 0080 class Manager : public QObject { 0081 Q_OBJECT 0082 0083 friend class Recorder; 0084 0085 public: 0086 /** 0087 * Constructs a new manager object. 0088 **/ 0089 explicit Manager(KileInfo* kileInfo, QObject *parent = 0, const char *name = 0); 0090 virtual ~Manager(); 0091 0092 /** 0093 * Adds a new consequence and the corresponding action. 0094 * @param seq the key sequence 0095 * @param action the action for the sequence 0096 **/ 0097 void addAction(const QString& seq, Action *action); 0098 0099 /** 0100 * Convenience method. Adds a key sequence-to-action map to this 0101 * manager, removing any existing mappings. 0102 * @warning This method overrides any exising mappings ! 0103 **/ 0104 void addActionMap(const QMap<QString, Action*>& map); 0105 0106 /** 0107 * Removes all the mappings. 0108 **/ 0109 void clear(); 0110 0111 /** 0112 * Returns a list of all the key sequences that are currently being 0113 * watched. 0114 **/ 0115 const QStringList& getWatchedKeySequences(); 0116 0117 /** 0118 * Returns the key sequence that corresponds to an action. 0119 * @param a the action that is considered 0120 **/ 0121 QString getKeySequence(const Action* a); 0122 0123 /** 0124 * Returns the action that corresponds to a key sequence. 0125 **/ 0126 Action* getAction(const QString& seq); 0127 0128 /** 0129 * Remove a key sequence, i.e. the key sequence is no longer watched. 0130 * @param seq the key sequence that should be removed 0131 **/ 0132 void removeKeySequence(const QString& seq); 0133 0134 /** 0135 * Convenience method. Removes every key sequence contained in the list. 0136 * @see removeKeySequence(const QString& seq) 0137 **/ 0138 void removeKeySequence(const QStringList& l); 0139 0140 /** 0141 * @warning not implemented yet ! 0142 **/ 0143 void setEditorKeySequence(const QString& seq, Action *action); 0144 0145 /** 0146 * Checks whether the sequence "seq" is already assigned to an action. 0147 * This method also checks whether a longer sequence that starts with 0148 * "seq" is assigned to an action. 0149 * @param seq the sequence that should be checked 0150 * @return "true" if and only the sequence "seq" or another sequence 0151 * that starts with "seq" is assigned to an action 0152 **/ 0153 bool isSequenceAssigned(const QString& seq) const; 0154 0155 /** 0156 * Performs a few checks on a key sequence. 0157 * @returns in the first component: 0 if the sequence is free; 1 0158 * if the sequence is assigned; 2 if there is a longer, 0159 * currently stored sequence that starts with "seq"; 3 0160 * if "seq" starts with a shorter sequence that is currently 0161 * stored 0162 * 0163 * in the second component: a string that corresponds to one 0164 * of the previous cases (in the case 0: QString()) 0165 **/ 0166 QPair<int, QString> checkSequence(const QString& seq, const QString& skip = QString()); 0167 0168 Q_SIGNALS: 0169 /** 0170 * Emitted whenever the set of watched key sequences changes. 0171 **/ 0172 void watchedKeySequencesChanged(); 0173 0174 protected Q_SLOTS: 0175 /** 0176 * Signalises to the manager that a (watched) sequence has been typed. 0177 * @param seq the sequence that has been typed 0178 **/ 0179 void keySequenceTyped(const QString& seq); 0180 0181 protected: 0182 KileInfo *m_kileInfo; 0183 QMap<QString, Action*> m_actionMap; 0184 QStringList m_watchedKeySequencesList; 0185 }; 0186 0187 /** 0188 * This class keeps track of the characters that are typed. It is used in 0189 * conjunction with a KTextEditor view and a KileEditorKeySequence::Manager. 0190 **/ 0191 class Recorder : public QObject { 0192 Q_OBJECT 0193 public: 0194 Recorder(KTextEditor::View *view, Manager *manager); 0195 virtual ~Recorder(); 0196 0197 Q_SIGNALS: 0198 /** 0199 * Emitted whenever a key sequence that is currently watched has 0200 * been typed. 0201 **/ 0202 void detectedTypedKeySequence(const QString& seq); 0203 0204 0205 public Q_SLOTS: 0206 /** 0207 * Reloads the key sequences that this recorders watches. 0208 **/ 0209 void reloadWatchedKeySequences(); 0210 0211 protected: 0212 Manager *m_manager; 0213 QString m_typedSequence; 0214 int m_maxSequenceLength; 0215 int m_oldCol, m_oldLine; 0216 KTextEditor::View* m_view; 0217 QStringList m_watchedKeySequencesList; 0218 0219 virtual bool eventFilter(QObject *o, QEvent *e) override; 0220 0221 /** 0222 * Checks whether a key sequence is currently watched. 0223 * @param s the key sequence that should be checked 0224 **/ 0225 bool seekForKeySequence(const QString& s); 0226 }; 0227 } 0228 0229 #endif