QTfrontend/ui/widget/SmartLineEdit.h
author sheepluva
Fri, 21 Oct 2011 01:36:13 +0200
changeset 6162 437116977d43
parent 6161 98486efeb0b7
child 6163 fcb709d55413
permissions -rw-r--r--
comment fixes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     1
/*
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     3
 * Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     4
 * Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     5
 *
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     8
 * the Free Software Foundation; version 2 of the License
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
     9
 *
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    13
 * GNU General Public License for more details.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    14
 *
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    16
 * along with this program; if not, write to the Free Software
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    18
 */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    19
6153
3881126e06e8 allow changing number of hogs or weapons with mousewheel
sheepluva
parents: 6151
diff changeset
    20
#ifndef HEDGEWARS_SMARTLINEEDIT_H
3881126e06e8 allow changing number of hogs or weapons with mousewheel
sheepluva
parents: 6151
diff changeset
    21
#define HEDGEWARS_SMARTLINEEDIT_H
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    22
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    23
#include <QStringList>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    24
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    25
#include <QEvent>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    26
#include <QRegExp>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    27
6151
9fd5b70acb1a give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents: 6150
diff changeset
    28
#include "HistoryLineEdit.h"
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    29
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    30
/**
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    31
 * @brief A {@link HistoryLineEdit} that features auto-completion with TAB key
6162
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
    32
 *         and clear with ESC key.
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    33
 *
6160
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    34
 * Notes:
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    35
 * <ul>
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    36
 *   <li>A Keyword can either be a command (if first word) or
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    37
 *       a nickname (completed regardless of position in text).</li>
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    38
 *   <li>Public methods for accessing keywords are <b>thread-safe.</b></li>
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    39
 * </ul>
863d3edf5690 cleaning up some more, also adding a WIP file. changes: hats in the hat selection are now sorted like this: NoHat, Reserved hats (alphabetically), All other hats (alphabeticall)
sheepluva
parents: 6153
diff changeset
    40
 * 
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    41
 * @author sheepluva
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    42
 * @since 0.9.17
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    43
 */
6151
9fd5b70acb1a give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents: 6150
diff changeset
    44
class SmartLineEdit : public HistoryLineEdit
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    45
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    46
 Q_OBJECT
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    47
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    48
public:
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    49
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    50
    * @brief Class constructor.
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    51
    * @param parent parent QWidget.
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    52
    * @param maxHistorySize maximum amount of history entries kept.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    53
    */
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    54
    SmartLineEdit(QWidget * parent = 0, int maxHistorySize = 64);
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    55
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    56
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    57
     * @brief Adds commands to the auto-completion feature.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    58
     * @param commands list of commands to be added.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    59
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    60
    void addCommands(const QStringList & commands);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    61
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    62
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    63
     * @brief Adds a single nickname to the auto-completion feature.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    64
     * @param nickname name to be added.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    65
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    66
    void addNickname(const QString & nickname);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    67
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    68
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    69
     * @brief Removes commands from the auto-completion feature.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    70
     * @param commands list of commands to be removed.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    71
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    72
    void removeCommands(const QStringList & commands);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    73
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    74
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    75
     * @brief Removes a single nickname from the auto-completion feature.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    76
     * @param nickname name to be removed.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    77
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    78
    void removeNickname(const QString & nickname);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    79
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    80
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    81
     * @brief Forget all keywords and input history.
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    82
     */
6151
9fd5b70acb1a give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents: 6150
diff changeset
    83
    void reset();
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    84
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    85
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    86
protected:
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    87
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    88
     * @brief Overrides method of parent class.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    89
     * Forward pressed TAB to parent class' method (for focus handling etc)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    90
     * only if line is empty.
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    91
     * 
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    92
     * @param event the key event.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    93
     * @return returns true if the event was recognized.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    94
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    95
    virtual bool event(QEvent * event);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    96
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    97
    /**
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
    98
     * @brief Overrides method of parent class.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    99
     * Autocompletes if TAB is reported as pressed key in the key event,
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   100
     * ESC leads to the contents being cleared.
6161
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
   101
     * 
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
   102
     * Other keys are forwarded to parent method.
98486efeb0b7 comments
sheepluva
parents: 6160
diff changeset
   103
     *
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   104
     * @param event the key event.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   105
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   106
    virtual void keyPressEvent(QKeyEvent * event);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   107
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   108
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   109
private:
6162
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   110
    QRegExp m_whitespace; ///< regexp that matches a whitespace
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   111
6162
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   112
    QStringList * m_cmds;  ///< list of recognized commands
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   113
    QStringList * m_nicks; ///< list of recognized nicknames
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   114
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   115
    // these variables contain information about the last replacement
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   116
    // they get reset whenever cursor is moved or text is changed
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   117
6162
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   118
    QString m_beforeMatch; ///< the string that was just matched
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   119
    bool m_hasJustMatched; ///< whether this widget just did an auto-completion
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   120
    QString m_prefix; ///< prefix of the text replacement this widget just did
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   121
    QString m_postfix; ///< postfix of the text replacement this widget just did
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   122
6162
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   123
    QMutex m_keywordMutex; ///< make keyword QStringList action thread-safe
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   124
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   125
    /**
6162
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   126
     * @brief Autocompletes the contents based on the known commands and/or names.
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   127
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   128
    void autoComplete();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   129
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   130
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   131
private slots:
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   132
    /**
6162
437116977d43 comment fixes
sheepluva
parents: 6161
diff changeset
   133
     * @brief Resets the information about the last match and text replacement.
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   134
     */
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   135
    void resetAutoCompletionStatus();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   136
};
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   137
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   138
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   139
6153
3881126e06e8 allow changing number of hogs or weapons with mousewheel
sheepluva
parents: 6151
diff changeset
   140
#endif // HEDGEWARS_SMARTLINEEDIT_H