-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdlineedit.cpp
172 lines (151 loc) · 4.64 KB
/
cmdlineedit.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* cmdlineedit.cpp - Sof Game Psi plugin
* Copyright (C) 2011 Aleksey Andreev
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* You can also redistribute and/or modify this program under the
* terms of the Psi License, specified in the accompanied COPYING
* file, as published by the Psi Project; either dated January 1st,
* 2005, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <QTimer>
#include <QAbstractTextDocumentLayout>
#include "cmdlineedit.h"
#define MAX_MESSAGE_HISTORY 50
CmdLineEdit::CmdLineEdit(QWidget *parent) :
QTextEdit(parent),
typedMsgsIndex(0)
{
setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); // no need for horizontal scrollbar with this
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setMinimumHeight(0);
connect(this, SIGNAL(textChanged()), SLOT(recalculateSize()));
initAction();
}
void CmdLineEdit::initAction()
{
actShowMessagePrev = new QAction(this);
actShowMessagePrev->setShortcut(QKeySequence("Ctrl+Up"));
connect(actShowMessagePrev, SIGNAL(triggered()), this, SLOT(showHistoryMessagePrev()));
addAction(actShowMessagePrev);
actShowMessageNext = new QAction(this);
actShowMessageNext->setShortcut(QKeySequence("Ctrl+Down"));
connect(actShowMessageNext, SIGNAL(triggered()), this, SLOT(showHistoryMessageNext()));
addAction(actShowMessageNext);
}
void CmdLineEdit::showHistoryMessagePrev()
{
if (!typedMsgsHistory.isEmpty() && typedMsgsIndex > 0) {
if (typedMsgsIndex == typedMsgsHistory.size())
currentText = toPlainText();
--typedMsgsIndex;
showMessageHistory();
}
}
void CmdLineEdit::showHistoryMessageNext()
{
int sz = typedMsgsHistory.size();
if (sz != 0) {
if (typedMsgsIndex + 1 < sz) {
++typedMsgsIndex;
showMessageHistory();
} else {
if (typedMsgsIndex != sz) {
typedMsgsIndex = sz;
setEditText(currentText);
}
}
}
}
void CmdLineEdit::showMessageHistory()
{
setEditText(typedMsgsHistory.at(typedMsgsIndex));
}
void CmdLineEdit::setEditText(const QString &text)
{
setPlainText(text);
moveCursor(QTextCursor::End);
}
void CmdLineEdit::appendMessageHistory(const QString &text)
{
if (text.simplified().length() > 2) {
if (currentText == text)
// Remove current typed text only if we want to add it to history
currentText.clear();
int index = typedMsgsHistory.indexOf(text);
if (index >= 0) {
typedMsgsHistory.removeAt(index);
} else {
if (typedMsgsHistory.size() >= MAX_MESSAGE_HISTORY)
typedMsgsHistory.removeFirst();
}
typedMsgsHistory.append(text);
typedMsgsIndex = typedMsgsHistory.size();
}
}
QSize CmdLineEdit::minimumSizeHint() const
{
QSize sh = QTextEdit::minimumSizeHint();
sh.setHeight(fontMetrics().height() + 1);
sh += QSize(0, QFrame::lineWidth() * 2);
return sh;
}
QSize CmdLineEdit::sizeHint() const
{
QSize sh = QTextEdit::sizeHint();
sh.setHeight(int(document()->documentLayout()->documentSize().height()));
sh += QSize(0, QFrame::lineWidth() * 2);
((QTextEdit*)this)->setMaximumHeight(sh.height());
return sh;
}
bool CmdLineEdit::focusNextPrevChild(bool next)
{
return QWidget::focusNextPrevChild(next);
}
void CmdLineEdit::resizeEvent(QResizeEvent* e)
{
QTextEdit::resizeEvent(e);
QTimer::singleShot(0, this, SLOT(updateScrollBar()));
}
void CmdLineEdit::keyPressEvent(QKeyEvent *e)
{
if ((e->modifiers() == Qt::NoModifier && e->key() == Qt::Key_Return) ||
(e->modifiers() == Qt::KeypadModifier && e->key() == Qt::Key_Enter)) {
emit returnPressed();
return;
}
QTextEdit::keyPressEvent(e);
}
// Qt text controls are quite greedy to grab key events.
// disable that.
bool CmdLineEdit::event(QEvent * event) {
if (event->type() == QEvent::ShortcutOverride) {
return false;
}
return QTextEdit::event(event);
}
void CmdLineEdit::recalculateSize()
{
updateGeometry();
QTimer::singleShot(0, this, SLOT(updateScrollBar()));
}
void CmdLineEdit::updateScrollBar()
{
setVerticalScrollBarPolicy(sizeHint().height() > height() ? Qt::ScrollBarAlwaysOn : Qt::ScrollBarAlwaysOff);
ensureCursorVisible();
}