This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextbox.cpp
153 lines (127 loc) · 3.98 KB
/
textbox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "textbox.h"
#include <QKeyEvent>
#include <QDebug>
#include <QPlainTextEdit>
#include "messagehistory.h"
TextBox::TextBox(QWidget *parent) : QPlainTextEdit(parent)
{
messageHistory = new MessageHistory(this);
searchingUsernames = QStringList();
userSearchIndex = 0;
channel = NULL;
searchString = "";
}
void TextBox::setChannel(Channel &chan)
{
channel = &chan;
userSearchIndex = 0;
searchingUsernames = QStringList();
}
TextBox::~TextBox()
{
}
void TextBox::keyPressEvent(QKeyEvent *event)
{
if ((event->key() == Qt::Key_Up || event->key() == Qt::Key_Down) &&
!(event->modifiers() & Qt::ShiftModifier))
{
bool willCycleUp = event->key() == Qt::Key_Up;
QString lastSent = messageHistory->getLastSentMessage(willCycleUp);
this->setPlainText(lastSent);
moveCursorToEnd();
}
else {
if (event->key() == Qt::Key_Enter || event->key() == Qt::Key_Return
&& !(event->modifiers() & Qt::ShiftModifier))
{
messageHistory->insertNewMessage(this->toPlainText());
emit(returnPressed());
return;
}
// With a TextEdit box, this would normally insert a tab in the box
// instead of doing the channel switch command we've implemented
if (event->modifiers() & Qt::ControlModifier &&
(event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab))
{
bool cycleDown = event->key() == Qt::Key_Tab;
emit(channelCycle(cycleDown));
return;
}
QPlainTextEdit::keyPressEvent(event);
}
}
bool TextBox::event(QEvent *e)
{
if(e->type() == QEvent::KeyPress) {
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
// We listen to the tab press here because
// it's not sent all the way to keyPressEvent
// Also don't want to fight with the ctrl + tab event in the main window
if(ke->key() == Qt::Key_Tab && !(ke->modifiers() & Qt::ControlModifier)) {
getLastArgument();
return true;
} else {
lastWord = "";
}
}
return QPlainTextEdit::event(e);
}
void TextBox::getLastArgument()
{
QStringList messageList = this->toPlainText().split(QRegExp("\\s+"));
if(!messageList.isEmpty() && lastWord == "") {
lastWord = messageList.last();
}
if(lastWord == NULL || lastWord.isEmpty()) {
return;
}
if(messageList.last().isEmpty()) {
messageList.removeLast();
}
if(searchString != lastWord) {
searchString = lastWord;
searchingUsernames = QStringList();
}
if(searchingUsernames.isEmpty()) {
userSearchIndex = 0;
QStringList usernames = channel->findUsersByPrefix(lastWord);
for(QStringList::Iterator iter = usernames.begin(); iter != usernames.end(); iter++) {
searchingUsernames.append(*iter);
}
} else {
userSearchIndex++;
}
bool useColon = messageList.size() == 1;
QString foundName = "";
if(!searchingUsernames.isEmpty()) {
foundName = searchingUsernames.at(userSearchIndex % searchingUsernames.length());
} else {
return;
}
QString fullMessage = "";
for(QStringList::Iterator iter = messageList.begin(); iter != messageList.end()-1; iter++) {
if(iter != messageList.begin()) {
fullMessage += " ";
}
fullMessage += *iter;
}
if(!foundName.isEmpty()) {
if(!fullMessage.isEmpty()) {
fullMessage += " ";
}
if(useColon) {
fullMessage += foundName + ": ";
} else {
fullMessage += foundName + " ";
}
}
this->setPlainText(fullMessage);
// have to move the cursor to the end, else it's at the beginning
moveCursorToEnd();
}
void TextBox::moveCursorToEnd()
{
QTextCursor cursor = this->textCursor();
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
this->setTextCursor(cursor);
}