QTfrontend/ui/widget/SmartLineEdit.h
changeset 6151 9fd5b70acb1a
parent 6150 1d98752c1fba
child 6153 3881126e06e8
equal deleted inserted replaced
6150:1d98752c1fba 6151:9fd5b70acb1a
    15  * You should have received a copy of the GNU General Public License
    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
    16  * along with this program; if not, write to the Free Software
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    18  */
    18  */
    19 
    19 
    20 #ifndef HEDGEWARS_SMARTLINEDIT
    20 #ifndef HEDGEWARS_SMARTLINEEDIT
    21 #define HEDGEWARS_SMARTLINEDIT
    21 #define HEDGEWARS_SMARTLINEEDIT
    22 
    22 
    23 #include <QLineEdit>
       
    24 #include <QStringList>
    23 #include <QStringList>
    25 #include <QString>
       
    26 
    24 
    27 #include <QEvent>
    25 #include <QEvent>
    28 #include <QKeyEvent>
       
    29 
       
    30 #include <QMutex>
       
    31 #include <QRegExp>
    26 #include <QRegExp>
    32 
    27 
    33 class QLineEdit;
    28 #include "HistoryLineEdit.h"
    34 
    29 
    35 /**
    30 /**
    36  * A modification of QLineEdit that features:
    31  * A {@link HistoryLineEdit} that additionally features:
    37  * + Auto-completion for word under cursor when the TAB key is pressed.
    32  * + Auto-completion for word under cursor when the TAB key is pressed.
    38  * + ESC key clears text.
    33  * + ESC key clears text.
    39  * + History of previous contents, re-selectable using the arrow keys.
       
    40  *
    34  *
    41  * Note:
    35  * Note:
    42  *   * A Keyword can either be a command (if first word) or
    36  *   * A Keyword can either be a command (if first word) or
    43  *     a nickname (completed regardless of position in text).
    37  *     a nickname (completed regardless of position in text).
    44  *   * Public methods for accessing keywords and history are thread-safe.
    38  *   * Public methods for accessing keywords are thread-safe.
    45  * @author sheepluva
    39  * @author sheepluva
    46  * @since 0.9.17
    40  * @since 0.9.17
    47  */
    41  */
    48 class SmartLineEdit : public QLineEdit
    42 class SmartLineEdit : public HistoryLineEdit
    49 {
    43 {
    50  Q_OBJECT
    44  Q_OBJECT
    51 
    45 
    52 public:
    46 public:
    53     /**
    47     /**
    68      * @param nickname name to be added.
    62      * @param nickname name to be added.
    69      */
    63      */
    70     void addNickname(const QString & nickname);
    64     void addNickname(const QString & nickname);
    71 
    65 
    72     /**
    66     /**
    73      * Appends current text to history.
       
    74      */
       
    75     void rememberCurrentText();
       
    76 
       
    77     /**
       
    78      * Removes commands from the auto-completion feature.
    67      * Removes commands from the auto-completion feature.
    79      * @param commands list of commands to be removed.
    68      * @param commands list of commands to be removed.
    80      */
    69      */
    81     void removeCommands(const QStringList & commands);
    70     void removeCommands(const QStringList & commands);
    82 
    71 
    87     void removeNickname(const QString & nickname);
    76     void removeNickname(const QString & nickname);
    88 
    77 
    89     /**
    78     /**
    90      * Forget all keywords and input history.
    79      * Forget all keywords and input history.
    91      */
    80      */
    92     void forgetEverything();
    81     void reset();
    93 
       
    94 
       
    95 public slots:
       
    96     /**
       
    97      * Clears the contents.
       
    98      */
       
    99     void clear();
       
   100 
    82 
   101 
    83 
   102 protected:
    84 protected:
   103     /**
    85     /**
   104      * Overrides method of parent class.
    86      * Overrides method of parent class.
   110     virtual bool event(QEvent * event);
    92     virtual bool event(QEvent * event);
   111 
    93 
   112     /**
    94     /**
   113      * Overrides method of parent class.
    95      * Overrides method of parent class.
   114      * Autocompletes if TAB is reported as pressed key in the key event,
    96      * Autocompletes if TAB is reported as pressed key in the key event,
   115      * otherwise keys except for ESC and Up/Down (with no modifiers)
       
   116      * are forwarded to parent method.
       
   117      * ESC leads to the contents being cleared.
    97      * ESC leads to the contents being cleared.
   118      * Arrow keys are used for navigating the history.
    98      * otherwise keys are forwarded to parent method.
   119      * @param event the key event.
    99      * @param event the key event.
   120      */
   100      */
   121     virtual void keyPressEvent(QKeyEvent * event);
   101     virtual void keyPressEvent(QKeyEvent * event);
   122 
   102 
   123 
   103 
   124 private:
   104 private:
   125     QRegExp m_whitespace; // regexp that matches a whitespace
   105     QRegExp m_whitespace; // regexp that matches a whitespace
   126 
   106 
   127     int m_maxHistorySize; // the maximum allowed size for the history
       
   128     int m_curHistEntryIdx; // the index of the currently used entry or -1
       
   129 
       
   130     QStringList * m_cmds;  // list of recognized commands
   107     QStringList * m_cmds;  // list of recognized commands
   131     QStringList * m_nicks; // list of recognized nicknames
   108     QStringList * m_nicks; // list of recognized nicknames
   132 
       
   133     QStringList * m_history; // history of previous inputs
       
   134 
   109 
   135     // these variables contain information about the last replacement
   110     // these variables contain information about the last replacement
   136     // they get reset whenever cursor is moved or text is changed
   111     // they get reset whenever cursor is moved or text is changed
   137 
   112 
   138     QString m_beforeMatch; // the string that was just matched
   113     QString m_beforeMatch; // the string that was just matched
   139     bool m_hasJustMatched; // whether this widget just did an auto-completion
   114     bool m_hasJustMatched; // whether this widget just did an auto-completion
   140     QString m_prefix; // prefix of the text replacement this widget just did
   115     QString m_prefix; // prefix of the text replacement this widget just did
   141     QString m_postfix; // postfix of the text replacement this widget just did
   116     QString m_postfix; // postfix of the text replacement this widget just did
   142 
   117 
   143     QMutex m_keywordMutex; // make keyword QStringList action thread-safe
   118     QMutex m_keywordMutex; // make keyword QStringList action thread-safe
   144     QMutex m_historyMutex; // make history QStringList action thread-safe
       
   145 
   119 
   146     /**
   120     /**
   147      * Autocompletes the contents based on the known commands and/or names.
   121      * Autocompletes the contents based on the known commands and/or names.
   148      */
   122      */
   149     void autoComplete();
   123     void autoComplete();
   150 
       
   151     /**
       
   152      * Navigates content history in the desired direction.
       
   153      * Note: no wrap-around on purpose (so that holding down/up will get the
       
   154      * the user to the respective end rather than into an endless cycle :P)
       
   155      * @param isGoingUp true: next older entry, false: next more recent entry.
       
   156      */
       
   157     void navigateHistory(bool isGoingUp);
       
   158 
       
   159     /**
       
   160      * Appends current text to history, without Mutex.
       
   161      */
       
   162     void rememberCurrentTextUnsynced();
       
   163 
   124 
   164 
   125 
   165 private slots:
   126 private slots:
   166     /**
   127     /**
   167      * Resets the information about the last match and text replacement.
   128      * Resets the information about the last match and text replacement.
   169     void resetAutoCompletionStatus();
   130     void resetAutoCompletionStatus();
   170 };
   131 };
   171 
   132 
   172 
   133 
   173 
   134 
   174 #endif // HEDGEWARS_SMARTLINEDIT
   135 #endif // HEDGEWARS_SMARTLINEEDIT