QTfrontend/ui/widget/SmartLineEdit.cpp
author sheepluva
Mon, 24 Oct 2011 20:01:25 +0200
changeset 6200 6a4ace88d85a
parent 6197 4ce7c29799b9
child 6205 7764cbe4ddd7
permissions -rw-r--r--
tweak and document abstract page
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
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6151
diff changeset
    20
/**
6170
2b1748161278 fix multiplayer page layout, doc/comment fixes
sheepluva
parents: 6168
diff changeset
    21
 * @file
6168
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6151
diff changeset
    22
 * @brief SmartLineEdit class implementation
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6151
diff changeset
    23
 */
6f301dac12ff fix an include; fix/add comments
sheepluva
parents: 6151
diff changeset
    24
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    25
#include "SmartLineEdit.h"
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    26
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    27
SmartLineEdit::SmartLineEdit(QWidget * parent, int maxHistorySize)
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
: HistoryLineEdit(parent, maxHistorySize)
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
    m_whitespace = QRegExp("\\s");
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
    m_cmds  = new QStringList();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    33
    m_nicks = new QStringList();
6187
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    34
    m_sorted_nicks = new QMap<QString, QString>();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    35
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    36
    resetAutoCompletionStatus();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    37
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    38
    // reset autocompletion status when cursor is moved or content is changed
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    39
    connect(this, SIGNAL(cursorPositionChanged(int, int)),
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    40
            this, SLOT(resetAutoCompletionStatus()));
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    41
    connect(this, SIGNAL(textChanged(const QString&)),
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
    42
            this, SLOT(resetAutoCompletionStatus()));
6147
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    45
6187
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    46
SmartLineEdit::~SmartLineEdit()
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    47
{
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    48
    delete m_cmds;
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    49
    delete m_nicks;
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    50
    delete m_sorted_nicks;
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    51
}
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    52
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    53
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    54
void SmartLineEdit::addCommands(const QStringList & commands)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    55
{
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    56
    m_keywordMutex.lock();
6147
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
    m_cmds->append(commands);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    59
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    60
    m_keywordMutex.unlock();
6147
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    64
void SmartLineEdit::removeCommands(const QStringList & commands)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    65
{
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    66
    m_keywordMutex.lock();
6147
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
    foreach (const QString & cmd, commands)
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
        m_cmds->removeAll(cmd);
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
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    73
    m_keywordMutex.unlock();
6147
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
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
void SmartLineEdit::addNickname(const QString & name)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    78
{
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    79
    m_keywordMutex.lock();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    80
6187
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    81
    m_sorted_nicks->insert(name.toLower(), name);
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    82
    m_nicks->append(name);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    83
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    84
    m_keywordMutex.unlock();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    85
}
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    88
void SmartLineEdit::removeNickname(const QString & name)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    89
{
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    90
    m_keywordMutex.lock();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    91
6187
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
    92
    m_sorted_nicks->remove(name.toLower());
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    93
    m_nicks->removeAll(name);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
    94
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    95
    m_keywordMutex.unlock();
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    96
}
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    97
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
    98
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
    99
void SmartLineEdit::reset()
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   100
{
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   101
    // forget keywords
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   102
    m_keywordMutex.lock();
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   103
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   104
    m_cmds->clear();
6187
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
   105
    m_sorted_nicks->clear();
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   106
    m_nicks->clear();
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
   107
    resetAutoCompletionStatus();
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   108
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   109
    m_keywordMutex.unlock();
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   110
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   111
    // forget history
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
   112
    HistoryLineEdit::reset();
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   113
}
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   114
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   115
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   116
bool SmartLineEdit::event(QEvent * event)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   117
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   118
    // we only want special treatment for key press events
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   119
    if (event->type() == QEvent::KeyPress)
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
        QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event);
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
        // 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
   124
        if ((keyEvent->key() == Qt::Key_Tab) && (!text().trimmed().isEmpty()))
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
            keyPressEvent(keyEvent);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   127
            if (event->isAccepted())
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   128
                return true;
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
    }
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
   131
    return HistoryLineEdit::event(event);
6147
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   134
void SmartLineEdit::keyPressEvent(QKeyEvent * event)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   135
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   136
    int key = event->key(); // retrieve pressed key
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
    // auto-complete on pressed TAB (except for whitespace-only contents)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   139
    if ((key == Qt::Key_Tab) && (!text().trimmed().isEmpty()))
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
        autoComplete();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   142
        event->accept();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   143
    }
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
   144
    // clear contents on pressed ESC
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
   145
    else if ((event->modifiers() == Qt::NoModifier) && (key == Qt::Key_Escape))
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
   146
    {
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
   147
        clear();
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
   148
        event->accept();
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
   149
    }
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   150
    // otherwise forward keys to parent
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   151
    else
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
   152
        HistoryLineEdit::keyPressEvent(event);
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   153
}
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   154
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   155
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   156
void SmartLineEdit::autoComplete()
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   157
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   158
    QString match = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   159
    bool isNick = false;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   160
    QString matchMe = text();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   161
    QString prefix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   162
    QString postfix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   163
    bool isFirstWord;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   164
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   165
    // 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
   166
    if (m_hasJustMatched)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   167
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   168
        // restore values from earlier auto-completion
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   169
        matchMe = m_beforeMatch;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   170
        prefix = m_prefix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   171
        postfix = m_postfix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   172
        isFirstWord = prefix.isEmpty();
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
    else
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   175
    {
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   176
        m_keywordMutex.lock();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   177
        m_cmds->sort();
6187
59ff93c0ae2d * adding destructors to my line edit subclasses
sheepluva
parents: 6170
diff changeset
   178
        m_nicks = new QStringList(m_sorted_nicks->values());
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   179
        m_keywordMutex.unlock();
6147
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
        int cp = cursorPosition();
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
        // cursor is not in or at end/beginning of word
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   184
        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
   185
            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
   186
                return;
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
        // crop matchMe at cursor position
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   189
        prefix  = matchMe.left (cp);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   190
        postfix = matchMe.right(matchMe.length()-cp);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   191
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   192
        matchMe = "";
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   195
        // use the whole word the curser is on for matching
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   196
        int prefixLen = prefix.lastIndexOf(m_whitespace) + 1;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   197
        int preWordLen = prefix.length() - prefixLen;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   198
        int postWordLen = postfix.indexOf(m_whitespace);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   199
        int postfixLen = 0;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   200
        if (postWordLen < 0)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   201
            postWordLen = postfix.length();
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   202
        else
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   203
            postfixLen = postfix.length() - (postWordLen + 1);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   204
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   205
        matchMe = prefix.right(preWordLen) + postfix.left(postWordLen);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   206
        prefix  = prefix.left(prefixLen);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   207
        postfix = postfix.right(postfixLen);
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
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   210
        isFirstWord = prefix.isEmpty(); // true if first word
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   211
    }
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
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   214
    m_keywordMutex.lock();
6147
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
    if (isFirstWord)
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
        // find matching commands
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   219
        foreach (const QString & cmd, *m_cmds)
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   220
        {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   221
            if (cmd.startsWith(matchMe, Qt::CaseInsensitive))
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
                match = cmd;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   224
6197
sheepluva
parents: 6187
diff changeset
   225
                // move match to end so next time new matches will be preferred
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   226
                if (m_cmds->removeAll(cmd) > 0);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   227
                    m_cmds->append(cmd);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   228
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   229
                break;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   230
            }
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
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   233
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   234
    if (match.isEmpty())
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   235
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   236
        // find matching nicks
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   237
        foreach (const QString & nick, *m_nicks)
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
            if (nick.startsWith(matchMe, Qt::CaseInsensitive))
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   240
            {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   241
                match = nick;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   242
                isNick = true;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   243
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   244
                // 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
   245
                if (m_nicks->removeAll(nick) > 0);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   246
                    m_nicks->append(nick);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   247
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   248
                break;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   249
            }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   250
        }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   251
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   252
6150
1d98752c1fba frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents: 6149
diff changeset
   253
    m_keywordMutex.unlock();
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   254
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   255
    // we found a single match?
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   256
    if (!match.isEmpty())
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   257
    {
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   258
        // replace last word with match
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   259
        // 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
   260
        // 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
   261
        QString addAfter = ((isNick && isFirstWord)?": ":" ");
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   262
        match = prefix + match + addAfter;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   263
        setText(match + postfix);
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   264
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   265
        setCursorPosition(match.length());
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   266
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   267
        // save values for for the case a rematch is requested
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   268
        m_beforeMatch = matchMe;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   269
        m_hasJustMatched = true;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   270
        m_prefix = prefix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   271
        m_postfix = postfix;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   272
    }
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   273
}
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   274
6149
0b92341adb6a + don't suggest ignored nicknames
sheepluva
parents: 6147
diff changeset
   275
void SmartLineEdit::resetAutoCompletionStatus()
6147
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   276
{
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   277
    m_beforeMatch = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   278
    m_hasJustMatched = false;
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   279
    m_prefix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   280
    m_postfix = "";
b4d7d8d62feb feature-pimpin'-up the chat input line in frontend:
sheepluva
parents:
diff changeset
   281
}
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
   282