Skip to content

Commit 5cbeebc

Browse files
committed
add multiline input mode in the chat dialog (disabled when client starts)
apply monospaced format on multiline chat messages in server
1 parent 7dcd207 commit 5cbeebc

File tree

4 files changed

+127
-8
lines changed

4 files changed

+127
-8
lines changed

src/chatdlg.cpp

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,56 @@ CChatDlg::CChatDlg ( QWidget* parent ) : CBaseDlg ( parent, Qt::Window ) // use
3535

3636
txvChatWindow->setAccessibleName ( tr ( "Chat history" ) );
3737

38-
// input message text
38+
// single-line input message text
3939
edtLocalInputText->setWhatsThis ( "<b>" + tr ( "Input Message Text" ) + ":</b> " +
4040
tr ( "Enter the chat message text in the edit box and press enter to send the "
4141
"message to the server which distributes the message to all connected "
4242
"clients. Your message will then show up in the chat window." ) );
4343

4444
edtLocalInputText->setAccessibleName ( tr ( "New chat text edit box" ) );
4545

46-
// clear chat window and edit line
46+
// multiline input message text
47+
edtLocalInputTextMultiline->setWhatsThis ( "<b>" + tr ( "Multiline Input Message Text" ) + ":</b> " +
48+
tr ( "Enter the chat message text in the edit box and press ctrl+enter to send the "
49+
"message to the server which distributes the message to all connected "
50+
"clients. Your message will then show up in the chat window." ) );
51+
52+
edtLocalInputTextMultiline->setAccessibleName ( tr ( "New multiline chat text edit box" ) );
53+
54+
// clear chat window and edit line / multiline edit line
4755
txvChatWindow->clear();
4856
edtLocalInputText->clear();
57+
edtLocalInputTextMultiline->clear();
4958

5059
// we do not want to show a cursor in the chat history
5160
txvChatWindow->setCursorWidth ( 0 );
5261

5362
// set a placeholder text to make sure where to type the message in (#384)
5463
edtLocalInputText->setPlaceholderText ( tr ( "Type a message here" ) );
64+
edtLocalInputTextMultiline->setPlaceholderText ( tr ( "Type a message here" ) );
65+
66+
// hide the multiline input
67+
edtLocalInputTextMultiline->hide();
5568

5669
// Menu -------------------------------------------------------------------
5770
QMenuBar* pMenu = new QMenuBar ( this );
71+
QMenu* pViewMenu = new QMenu ( tr ( "&View" ), this );
5872
QMenu* pEditMenu = new QMenu ( tr ( "&Edit" ), this );
5973

74+
QAction* InputModeAction =
75+
pViewMenu->addAction ( tr ( "&Multiline Input Mode" ), this, SLOT ( OnInputModeAction() ), QKeySequence ( Qt::CTRL + Qt::Key_M ) );
76+
InputModeAction->setCheckable ( true );
77+
6078
pEditMenu->addAction ( tr ( "Cl&ear Chat History" ), this, SLOT ( OnClearChatHistory() ), QKeySequence ( Qt::CTRL + Qt::Key_E ) );
6179

80+
// create action so Ctrl+Return sends a message
81+
QAction* SendAction = new QAction ( this );
82+
SendAction->setAutoRepeat ( false );
83+
SendAction->setShortcut ( tr ( "Ctrl+Return" ) );
84+
connect ( SendAction, SIGNAL ( triggered() ), this, SLOT ( OnSendText() ) );
85+
this->addAction ( SendAction );
86+
87+
pMenu->addMenu ( pViewMenu );
6288
pMenu->addMenu ( pEditMenu );
6389
#if defined( Q_OS_IOS )
6490
QAction* action = pMenu->addAction ( tr ( "&Close" ) );
@@ -71,6 +97,8 @@ CChatDlg::CChatDlg ( QWidget* parent ) : CBaseDlg ( parent, Qt::Window ) // use
7197
// Connections -------------------------------------------------------------
7298
QObject::connect ( edtLocalInputText, &QLineEdit::textChanged, this, &CChatDlg::OnLocalInputTextTextChanged );
7399

100+
QObject::connect ( edtLocalInputTextMultiline, &QPlainTextEdit::textChanged, this, &CChatDlg::OnLocalInputTextMultilineTextChanged );
101+
74102
QObject::connect ( butSend, &QPushButton::clicked, this, &CChatDlg::OnSendText );
75103

76104
QObject::connect ( txvChatWindow, &QTextBrowser::anchorClicked, this, &CChatDlg::OnAnchorClicked );
@@ -86,14 +114,68 @@ void CChatDlg::OnLocalInputTextTextChanged ( const QString& strNewText )
86114
}
87115
}
88116

117+
void CChatDlg::OnLocalInputTextMultilineTextChanged()
118+
{
119+
// check and correct length
120+
if ( edtLocalInputTextMultiline->toPlainText().length() > MAX_LEN_CHAT_TEXT )
121+
{
122+
// text is too long, update control with shortened text
123+
edtLocalInputTextMultiline->setPlainText ( edtLocalInputTextMultiline->toPlainText().left ( MAX_LEN_CHAT_TEXT ) );
124+
125+
// move cursor to the end
126+
QTextCursor cursor ( edtLocalInputTextMultiline->textCursor() );
127+
cursor.movePosition ( QTextCursor::End, QTextCursor::MoveAnchor );
128+
edtLocalInputTextMultiline->setTextCursor ( cursor );
129+
}
130+
}
131+
89132
void CChatDlg::OnSendText()
90133
{
91-
// send new text and clear line afterwards, do not send an empty message
92-
if ( !edtLocalInputText->text().isEmpty() )
134+
// send new text from whichever input is visible
135+
if ( edtLocalInputText->isVisible() )
93136
{
94-
emit NewLocalInputText ( edtLocalInputText->text() );
137+
// do not send an empty message
138+
if ( !edtLocalInputText->text().isEmpty() )
139+
{
140+
// send text and clear line afterwards
141+
emit NewLocalInputText ( edtLocalInputText->text() );
142+
edtLocalInputText->clear();
143+
edtLocalInputText->setFocus();
144+
}
145+
}
146+
else
147+
{
148+
// do not send an empty message
149+
if ( !edtLocalInputTextMultiline->toPlainText().isEmpty() )
150+
{
151+
// send text and clear multiline input afterwards
152+
emit NewLocalInputText ( edtLocalInputTextMultiline->toPlainText() );
153+
edtLocalInputTextMultiline->clear();
154+
edtLocalInputTextMultiline->setFocus();
155+
}
156+
}
157+
}
158+
159+
void CChatDlg::OnInputModeAction()
160+
{
161+
// switch between single-line and multiline input
162+
if ( edtLocalInputText->isVisible() )
163+
{
164+
// show multiline input only
165+
edtLocalInputText->hide();
166+
edtLocalInputTextMultiline->setPlainText ( edtLocalInputText->text() );
167+
edtLocalInputTextMultiline->show();
168+
edtLocalInputTextMultiline->setFocus();
95169
edtLocalInputText->clear();
96170
}
171+
else
172+
{
173+
// show single-line input only
174+
edtLocalInputTextMultiline->hide();
175+
edtLocalInputText->show();
176+
edtLocalInputText->setFocus();
177+
edtLocalInputTextMultiline->clear();
178+
}
97179
}
98180

99181
void CChatDlg::OnClearChatHistory()

src/chatdlg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class CChatDlg : public CBaseDlg, private Ui_CChatDlgBase
5151
public slots:
5252
void OnSendText();
5353
void OnLocalInputTextTextChanged ( const QString& strNewText );
54+
void OnLocalInputTextMultilineTextChanged();
55+
void OnInputModeAction();
5456
void OnClearChatHistory();
5557
void OnAnchorClicked ( const QUrl& Url );
5658

src/chatdlgbase.ui

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,35 @@
4646
<item>
4747
<layout class="QHBoxLayout" name="horizontalLayout">
4848
<item>
49-
<widget class="QLineEdit" name="edtLocalInputText"/>
49+
<layout class="QVBoxLayout" name="verticalLayout">
50+
<item>
51+
<widget class="QLineEdit" name="edtLocalInputText"/>
52+
</item>
53+
<item>
54+
<widget class="QPlainTextEdit" name="edtLocalInputTextMultiline">
55+
<property name="sizePolicy">
56+
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
57+
<horstretch>0</horstretch>
58+
<verstretch>0</verstretch>
59+
</sizepolicy>
60+
</property>
61+
<property name="font">
62+
<font>
63+
<family>Monospace</family>
64+
</font>
65+
</property>
66+
</widget>
67+
</item>
68+
</layout>
5069
</item>
5170
<item>
5271
<widget class="QPushButton" name="butSend">
72+
<property name="sizePolicy">
73+
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
74+
<horstretch>0</horstretch>
75+
<verstretch>0</verstretch>
76+
</sizepolicy>
77+
</property>
5378
<property name="text">
5479
<string>&amp;Send</string>
5580
</property>

src/server.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,8 +1411,18 @@ void CServer::CreateAndSendChatTextForAllConChannels ( const int iCurChanID, con
14111411
// use different colors
14121412
QString sCurColor = vstrChatColors[iCurChanID % vstrChatColors.Size()];
14131413

1414-
const QString strActualMessageText = "<font color=\"" + sCurColor + "\">(" + QTime::currentTime().toString ( "hh:mm:ss AP" ) + ") <b>" +
1415-
ChanName.toHtmlEscaped() + "</b></font> " + strChatText.toHtmlEscaped();
1414+
QString strActualMessageText =
1415+
"<font color=\"" + sCurColor + "\">(" + QTime::currentTime().toString ( "hh:mm:ss AP" ) + ") <b>" + ChanName.toHtmlEscaped() + "</b></font> ";
1416+
1417+
if ( strChatText.contains ( "\n" ) )
1418+
{
1419+
// add 'preformatted' tags around multiline strings
1420+
strActualMessageText += "<pre>" + strChatText.toHtmlEscaped() + "</pre>";
1421+
}
1422+
else
1423+
{
1424+
strActualMessageText += strChatText.toHtmlEscaped();
1425+
}
14161426

14171427
// Send chat text to all connected clients ---------------------------------
14181428
for ( int i = 0; i < iMaxNumChannels; i++ )

0 commit comments

Comments
 (0)