QTfrontend/ui/widget/SmartLineEdit.cpp
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
#include <QStringList>
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    21
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    22
#include "SmartLineEdit.h"
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    23
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    24
SmartLineEdit::SmartLineEdit(QWidget * parent)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    25
: QLineEdit(parent)
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
    m_whitespace = QRegExp("\\s");
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    28
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    29
    m_cmds  = new QStringList();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    30
    m_nicks = new QStringList();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    31
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    32
    reset();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    33
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    34
    // reset when cursor is moved or content is changed
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    35
    connect(this, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(reset()));
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    36
    connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(reset()));
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    37
}
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    38
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    39
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    40
void SmartLineEdit::addCommands(const QStringList & commands)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    41
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    42
    m_mutex.lock();
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
    m_cmds->append(commands);
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
    m_mutex.unlock();
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
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
void SmartLineEdit::removeCommands(const QStringList & commands)
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
    m_mutex.lock();
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
    foreach (const QString & cmd, commands)
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
        m_cmds->removeAll(cmd);
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    59
    m_mutex.unlock();
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    62
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    63
void SmartLineEdit::addNickname(const QString & name)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    64
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    65
    m_mutex.lock();
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
    m_nicks->append(name);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    68
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    69
    m_mutex.unlock();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    70
}
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
void SmartLineEdit::removeNickname(const QString & name)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    74
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    75
    m_mutex.lock();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    76
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    77
    m_nicks->removeAll(name);
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
    m_mutex.unlock();
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    82
bool SmartLineEdit::event(QEvent * event)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    83
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    84
    // we only want special treatment for key press events
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    85
    if (event->type() == QEvent::KeyPress)
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
        QKeyEvent * keyEvent = static_cast<QKeyEvent*>(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
        // TAB key pressed and any useful chars in the matchMe -> let's process those
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    90
        if ((keyEvent->key() == Qt::Key_Tab) && (!text().trimmed().isEmpty()))
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    91
        {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    92
            keyPressEvent(keyEvent);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    93
            if (event->isAccepted())
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    94
                return true;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    95
        }
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
    return QLineEdit::event(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
void SmartLineEdit::keyPressEvent(QKeyEvent * event)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   101
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   102
    int key = event->key(); // retrieve pressed key
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   103
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   104
    // auto-complete on pressed TAB (except for whitespace-only contents)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   105
    if ((key == Qt::Key_Tab) && (!text().trimmed().isEmpty()))
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   106
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   107
        autoComplete();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   108
        event->accept();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   109
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   110
    // clear contents on pressed ESC
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   111
    else if ((event->key() == Qt::Key_Escape) &&
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   112
             (event->modifiers() == Qt::NoModifier)
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
        clear();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   115
    // otherwise forward keys to parent
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   116
    else
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   117
        QLineEdit::keyPressEvent(event);
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
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
void SmartLineEdit::autoComplete()
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   122
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   123
    QString match = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   124
    bool isNick = false;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   125
    QString matchMe = text();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   126
    QString prefix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   127
    QString postfix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   128
    bool isFirstWord;
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
    // we are trying to rematch, so use the data from earlier
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   131
    if (m_hasJustMatched)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   132
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   133
        // restore values from earlier auto-completion
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   134
        matchMe = m_beforeMatch;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   135
        prefix = m_prefix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   136
        postfix = m_postfix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   137
        isFirstWord = prefix.isEmpty();
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
    else
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   140
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   141
        m_mutex.lock();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   142
        m_cmds->sort();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   143
        m_nicks->sort();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   144
        m_mutex.unlock();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   145
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   146
        int cp = cursorPosition();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   148
        // cursor is not in or at end/beginning of word
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   149
        if ((cp = matchMe.length()) || (QString(matchMe.at(cp)).contains(m_whitespace)))
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   150
            if ((cp < 1) || (QString(matchMe.at(cp-1)).contains(m_whitespace)))
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   151
                return;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   152
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   153
        // crop matchMe at cursor position
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   154
        prefix  = matchMe.left (cp);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   155
        postfix = matchMe.right(matchMe.length()-cp);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   156
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   157
        matchMe = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   158
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   159
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   160
        // use the whole word the curser is on for matching
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   161
        int prefixLen = prefix.lastIndexOf(m_whitespace) + 1;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   162
        int preWordLen = prefix.length() - prefixLen;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   163
        int postWordLen = postfix.indexOf(m_whitespace);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   164
        int postfixLen = 0;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   165
        if (postWordLen < 0)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   166
            postWordLen = postfix.length();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   167
        else
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   168
            postfixLen = postfix.length() - (postWordLen + 1);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   169
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   170
        matchMe = prefix.right(preWordLen) + postfix.left(postWordLen);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   171
        prefix  = prefix.left(prefixLen);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   172
        postfix = postfix.right(postfixLen);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   173
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   174
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   175
        isFirstWord = prefix.isEmpty(); // true if first word
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   176
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   177
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   178
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   179
    m_mutex.lock();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   180
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   181
    if (isFirstWord)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   182
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   183
        // find matching commands
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   184
        foreach (const QString & cmd, *m_cmds)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   185
        {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   186
            if (cmd.startsWith(matchMe, Qt::CaseInsensitive))
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   187
            {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   188
                match = cmd;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   189
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   190
                // move match to end so next time new matches will be prefered
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   191
                if (m_cmds->removeAll(cmd) > 0);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   192
                    m_cmds->append(cmd);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   193
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   194
                break;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   195
            }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   196
        }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   197
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   198
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   199
    if (match.isEmpty())
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   200
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   201
        // find matching nicks
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   202
        foreach (const QString & nick, *m_nicks)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   203
        {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   204
            if (nick.startsWith(matchMe, Qt::CaseInsensitive))
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   205
            {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   206
                match = nick;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   207
                isNick = true;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   208
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   209
                // move match to end so next time new matches will be prefered
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   210
                if (m_nicks->removeAll(nick) > 0);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   211
                    m_nicks->append(nick);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   212
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   213
                break;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   214
            }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   215
        }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   216
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   217
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   218
    m_mutex.unlock();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   219
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   220
    // we found a single match?
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   221
    if (!match.isEmpty())
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   222
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   223
        // replace last word with match
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   224
        // and append ':' if a name at the beginning of the matchMe got completed
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   225
        // also add a space at the very end to ease further typing
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   226
        QString addAfter = ((isNick && isFirstWord)?": ":" ");
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   227
        match = prefix + match + addAfter;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   228
        setText(match + postfix);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   229
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   230
        setCursorPosition(match.length());
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   231
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   232
        // save values for for the case a rematch is requested
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   233
        m_beforeMatch = matchMe;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   234
        m_hasJustMatched = true;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   235
        m_prefix = prefix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   236
        m_postfix = postfix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   237
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   238
}
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   239
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   240
void SmartLineEdit::reset()
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   241
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   242
    m_beforeMatch = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   243
    m_hasJustMatched = false;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   244
    m_prefix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   245
    m_postfix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   246
}