Skip to content

Commit

Permalink
add multiline input mode in the chat dialog (disabled when client sta…
Browse files Browse the repository at this point in the history
…rts)

apply monospaced format on multiline chat messages in server
  • Loading branch information
passing committed Jun 11, 2021
1 parent 7dcd207 commit 5cbeebc
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 8 deletions.
92 changes: 87 additions & 5 deletions src/chatdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,56 @@ CChatDlg::CChatDlg ( QWidget* parent ) : CBaseDlg ( parent, Qt::Window ) // use

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

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

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

// clear chat window and edit line
// multiline input message text
edtLocalInputTextMultiline->setWhatsThis ( "<b>" + tr ( "Multiline Input Message Text" ) + ":</b> " +
tr ( "Enter the chat message text in the edit box and press ctrl+enter to send the "
"message to the server which distributes the message to all connected "
"clients. Your message will then show up in the chat window." ) );

edtLocalInputTextMultiline->setAccessibleName ( tr ( "New multiline chat text edit box" ) );

// clear chat window and edit line / multiline edit line
txvChatWindow->clear();
edtLocalInputText->clear();
edtLocalInputTextMultiline->clear();

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

// set a placeholder text to make sure where to type the message in (#384)
edtLocalInputText->setPlaceholderText ( tr ( "Type a message here" ) );
edtLocalInputTextMultiline->setPlaceholderText ( tr ( "Type a message here" ) );

// hide the multiline input
edtLocalInputTextMultiline->hide();

// Menu -------------------------------------------------------------------
QMenuBar* pMenu = new QMenuBar ( this );
QMenu* pViewMenu = new QMenu ( tr ( "&View" ), this );
QMenu* pEditMenu = new QMenu ( tr ( "&Edit" ), this );

QAction* InputModeAction =
pViewMenu->addAction ( tr ( "&Multiline Input Mode" ), this, SLOT ( OnInputModeAction() ), QKeySequence ( Qt::CTRL + Qt::Key_M ) );
InputModeAction->setCheckable ( true );

pEditMenu->addAction ( tr ( "Cl&ear Chat History" ), this, SLOT ( OnClearChatHistory() ), QKeySequence ( Qt::CTRL + Qt::Key_E ) );

// create action so Ctrl+Return sends a message
QAction* SendAction = new QAction ( this );
SendAction->setAutoRepeat ( false );
SendAction->setShortcut ( tr ( "Ctrl+Return" ) );
connect ( SendAction, SIGNAL ( triggered() ), this, SLOT ( OnSendText() ) );
this->addAction ( SendAction );

pMenu->addMenu ( pViewMenu );
pMenu->addMenu ( pEditMenu );
#if defined( Q_OS_IOS )
QAction* action = pMenu->addAction ( tr ( "&Close" ) );
Expand All @@ -71,6 +97,8 @@ CChatDlg::CChatDlg ( QWidget* parent ) : CBaseDlg ( parent, Qt::Window ) // use
// Connections -------------------------------------------------------------
QObject::connect ( edtLocalInputText, &QLineEdit::textChanged, this, &CChatDlg::OnLocalInputTextTextChanged );

QObject::connect ( edtLocalInputTextMultiline, &QPlainTextEdit::textChanged, this, &CChatDlg::OnLocalInputTextMultilineTextChanged );

QObject::connect ( butSend, &QPushButton::clicked, this, &CChatDlg::OnSendText );

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

void CChatDlg::OnLocalInputTextMultilineTextChanged()
{
// check and correct length
if ( edtLocalInputTextMultiline->toPlainText().length() > MAX_LEN_CHAT_TEXT )
{
// text is too long, update control with shortened text
edtLocalInputTextMultiline->setPlainText ( edtLocalInputTextMultiline->toPlainText().left ( MAX_LEN_CHAT_TEXT ) );

// move cursor to the end
QTextCursor cursor ( edtLocalInputTextMultiline->textCursor() );
cursor.movePosition ( QTextCursor::End, QTextCursor::MoveAnchor );
edtLocalInputTextMultiline->setTextCursor ( cursor );
}
}

void CChatDlg::OnSendText()
{
// send new text and clear line afterwards, do not send an empty message
if ( !edtLocalInputText->text().isEmpty() )
// send new text from whichever input is visible
if ( edtLocalInputText->isVisible() )
{
emit NewLocalInputText ( edtLocalInputText->text() );
// do not send an empty message
if ( !edtLocalInputText->text().isEmpty() )
{
// send text and clear line afterwards
emit NewLocalInputText ( edtLocalInputText->text() );
edtLocalInputText->clear();
edtLocalInputText->setFocus();
}
}
else
{
// do not send an empty message
if ( !edtLocalInputTextMultiline->toPlainText().isEmpty() )
{
// send text and clear multiline input afterwards
emit NewLocalInputText ( edtLocalInputTextMultiline->toPlainText() );
edtLocalInputTextMultiline->clear();
edtLocalInputTextMultiline->setFocus();
}
}
}

void CChatDlg::OnInputModeAction()
{
// switch between single-line and multiline input
if ( edtLocalInputText->isVisible() )
{
// show multiline input only
edtLocalInputText->hide();
edtLocalInputTextMultiline->setPlainText ( edtLocalInputText->text() );
edtLocalInputTextMultiline->show();
edtLocalInputTextMultiline->setFocus();
edtLocalInputText->clear();
}
else
{
// show single-line input only
edtLocalInputTextMultiline->hide();
edtLocalInputText->show();
edtLocalInputText->setFocus();
edtLocalInputTextMultiline->clear();
}
}

void CChatDlg::OnClearChatHistory()
Expand Down
2 changes: 2 additions & 0 deletions src/chatdlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CChatDlg : public CBaseDlg, private Ui_CChatDlgBase
public slots:
void OnSendText();
void OnLocalInputTextTextChanged ( const QString& strNewText );
void OnLocalInputTextMultilineTextChanged();
void OnInputModeAction();
void OnClearChatHistory();
void OnAnchorClicked ( const QUrl& Url );

Expand Down
27 changes: 26 additions & 1 deletion src/chatdlgbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,35 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="edtLocalInputText"/>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLineEdit" name="edtLocalInputText"/>
</item>
<item>
<widget class="QPlainTextEdit" name="edtLocalInputTextMultiline">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Monospace</family>
</font>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="butSend">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&amp;Send</string>
</property>
Expand Down
14 changes: 12 additions & 2 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1411,8 +1411,18 @@ void CServer::CreateAndSendChatTextForAllConChannels ( const int iCurChanID, con
// use different colors
QString sCurColor = vstrChatColors[iCurChanID % vstrChatColors.Size()];

const QString strActualMessageText = "<font color=\"" + sCurColor + "\">(" + QTime::currentTime().toString ( "hh:mm:ss AP" ) + ") <b>" +
ChanName.toHtmlEscaped() + "</b></font> " + strChatText.toHtmlEscaped();
QString strActualMessageText =
"<font color=\"" + sCurColor + "\">(" + QTime::currentTime().toString ( "hh:mm:ss AP" ) + ") <b>" + ChanName.toHtmlEscaped() + "</b></font> ";

if ( strChatText.contains ( "\n" ) )
{
// add 'preformatted' tags around multiline strings
strActualMessageText += "<pre>" + strChatText.toHtmlEscaped() + "</pre>";
}
else
{
strActualMessageText += strChatText.toHtmlEscaped();
}

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

0 comments on commit 5cbeebc

Please sign in to comment.