QTfrontend/ui/widget/SmartLineEdit.h
author sheepluva
Tue, 18 Oct 2011 14:28:50 +0200
changeset 6147 b4d7d8d62feb
child 6149 0b92341adb6a
permissions -rw-r--r--
feature-pimpin'-up the chat input line in frontend: ESC key will clear input TAB key will expand names and commands
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    20
#ifndef HEDGEWARS_SMARTLINEDIT
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    21
#define HEDGEWARS_SMARTLINEDIT
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 <QLineEdit>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    24
#include <QStringList>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    25
#include <QString>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    26
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    27
#include <QEvent>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    28
#include <QKeyEvent>
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
#include <QMutex>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    31
#include <QRegExp>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    32
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    33
class QLineEdit;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    34
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    35
/**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    36
 * A modification of QLineEdit that will attempt to auto-complete the current
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    37
 * word with cursor when the TAB key is pressed.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    38
 * Additionally it will delete its contents when ESC is pressed.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    39
 * A Keyword can either be a command (if first word) or
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    40
 * a nickname (completed if any word)
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
 */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    44
class SmartLineEdit : public QLineEdit
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
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    50
    * Class constructor.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    51
    */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    52
    SmartLineEdit(QWidget * parent = 0);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    53
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    54
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    55
     * Adds commands to the auto-completion feature.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    56
     * @param commands list of commands to be added.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    57
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    58
    void addCommands(const QStringList & commands);
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
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    61
     * Adds a single nickname to the auto-completion feature.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    62
     * @param nickname name to be added.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    63
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    64
    void addNickname(const QString & nickname);
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
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    67
     * Removes commands from the auto-completion feature.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    68
     * @param commands list of commands to be removed.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    69
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    70
    void removeCommands(const QStringList & commands);
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
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    73
     * Removes a single nickname from the auto-completion feature.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    74
     * @param nickname name to be removed.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    75
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    76
    void removeNickname(const QString & nickname);
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    79
protected:
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    80
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    81
     * Overrides method of parent class.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    82
     * 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
    83
     * only if line is empty.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    84
     * @param event the key event.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    85
     * @return returns true if the event was recognized.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    86
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    87
    virtual bool event(QEvent * event);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    88
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    89
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    90
     * Overrides method of parent class.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    91
     * 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
    92
     * otherwise keys except for ESC (with no modifiers)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    93
     * are forwarded to parent method.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    94
     * ESC leads to the contents being cleared.
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    95
     * @param event the key 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
    virtual void keyPressEvent(QKeyEvent * event);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    98
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    99
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   100
private:
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   101
    QRegExp m_whitespace; // regexp that matches a whitespace
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   102
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   103
    QStringList * m_cmds;  // list of recognized commands
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   104
    QStringList * m_nicks; // list of recognized nicknames
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
    // these variables contain information about the last replacement
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   107
    // 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
   108
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   109
    QString m_beforeMatch; // the string that was just matched
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   110
    bool m_hasJustMatched; // whether this widget just did an auto-completion
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   111
    QString m_prefix; // prefix of the text replacement this widget just did
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   112
    QString m_postfix; // postfix of the text replacement this widget just did
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   113
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   114
    QMutex m_mutex; // make all the QStringList action thread-safe
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   115
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   116
    /**
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   117
     * Autocompletes the contents based on the known commands and/or names
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   118
     */
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   119
    void autoComplete();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   120
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   121
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   122
private slots:
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   123
    // resets the information about the last match and text replacement
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   124
    void reset();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   125
};
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   126
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   129
#endif // HEDGEWARS_SMARTLINEDIT