1 /* 2 * Hedgewars, a free turn based strategy game 3 * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com> 4 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; version 2 of the License 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 /** 21 * @file 22 * @brief SmartLineEdit class definition 23 */ 24 25 #ifndef HEDGEWARS_SMARTLINEEDIT_H 26 #define HEDGEWARS_SMARTLINEEDIT_H 27 28 #include <QMap> 29 #include <QString> 30 #include <QStringList> 31 32 #include <QEvent> 33 #include <QKeyEvent> 34 35 #include <QRegExp> 36 37 #include "HistoryLineEdit.h" 38 39 /** 40 * @brief {@link HistoryLineEdit} that features auto-completion with TAB key 41 * and clear with ESC key. 42 * 43 * Notes: 44 * <ul> 45 * <li>A Keyword can either be a command (if first word) or 46 * a nickname (completed regardless of position in text).</li> 47 * </ul> 48 * 49 * @author sheepluva 50 * @since 0.9.17 51 */ 52 class SmartLineEdit : public HistoryLineEdit 53 { 54 Q_OBJECT 55 56 public: 57 /** 58 * @brief Class constructor. 59 * @param parent parent QWidget. 60 * @param maxHistorySize maximum amount of history entries kept. 61 */ 62 SmartLineEdit(QWidget * parent = 0, int maxHistorySize = 64); 63 64 /** 65 * @brief Class destructor. 66 */ 67 ~SmartLineEdit(); 68 69 /** 70 * @brief Adds commands to the auto-completion feature. 71 * @param commands list of commands to be added. 72 */ 73 void addCommands(const QStringList & commands); 74 75 /** 76 * @brief Adds a single nickname to the auto-completion feature. 77 * @param nickname name to be added. 78 */ 79 void addNickname(const QString & nickname); 80 81 /** 82 * @brief Removes commands from the auto-completion feature. 83 * @param commands list of commands to be removed. 84 */ 85 void removeCommands(const QStringList & commands); 86 87 /** 88 * @brief Removes a single nickname from the auto-completion feature. 89 * @param nickname name to be removed. 90 */ 91 void removeNickname(const QString & nickname); 92 93 /** 94 * @brief Forget all keywords and input history. 95 */ 96 void reset(); 97 98 99 protected: 100 /** 101 * @brief Overrides method of parent class. 102 * Forward pressed TAB to parent class' method (for focus handling etc) 103 * only if line is empty. 104 * 105 * @param event the event. 106 * @return returns true if the event was recognized. 107 */ 108 virtual bool event(QEvent * event); 109 110 /** 111 * @brief Overrides method of parent class. 112 * Autocompletes if TAB is reported as pressed key in the key event, 113 * ESC leads to the contents being cleared. 114 * 115 * Other keys are forwarded to parent method. 116 * 117 * @param event the key event. 118 */ 119 virtual void keyPressEvent(QKeyEvent * event); 120 121 122 private: 123 QRegExp m_whitespace; ///< regexp that matches a whitespace 124 125 QStringList * m_cmds; ///< list of recognized commands 126 QStringList * m_nicks; ///< list of recognized nicknames 127 128 /// recognized nicknames, sorted case-insensitive 129 QMap<QString, QString> * m_sorted_nicks; 130 131 // these variables contain information about the last replacement 132 // they get reset whenever cursor is moved or text is changed 133 134 QString m_beforeMatch; ///< the string that was just matched 135 bool m_hasJustMatched; ///< whether this widget just did an auto-completion 136 QString m_prefix; ///< prefix of the text replacement this widget just did 137 QString m_postfix; ///< postfix of the text replacement this widget just did 138 139 /** 140 * @brief Autocompletes the contents based on the known commands and/or names. 141 */ 142 void autoComplete(); 143 144 145 private slots: 146 /** 147 * @brief Resets the information about the last match and text replacement. 148 */ 149 void resetAutoCompletionStatus(); 150 }; 151 152 153 154 #endif // HEDGEWARS_SMARTLINEEDIT_H 155