File indexing completed on 2024-05-12 04:04:14

0001 /*
0002     This file is part of Knights, a chess board for KDE SC 4.
0003     SPDX-FileCopyrightText: 2009, 2010, 2011 Miha Čančula <miha@noughmad.eu>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "proto/chatwidget.h"
0009 #include "ui_chatwidget.h"
0010 
0011 #include <QScrollBar>
0012 
0013 using namespace Knights;
0014 
0015 class ScrollBarPin {
0016 public:
0017     ScrollBarPin(QScrollBar *scrollBar) : m_bar(scrollBar) {
0018         if (m_bar)
0019             m_bar = m_bar->value() == m_bar->maximum()? m_bar : nullptr;
0020     }
0021     ~ScrollBarPin() {
0022         if (m_bar)
0023             m_bar->setValue(m_bar->maximum());
0024     }
0025 private:
0026     QPointer<QScrollBar> m_bar;
0027 };
0028 
0029 void Terminal::resizeEvent ( QResizeEvent * event ) {
0030     ScrollBarPin b(verticalScrollBar());
0031     QTextBrowser::resizeEvent(event);
0032 }
0033 
0034 ChatWidget::ChatWidget ( QWidget* parent, Qt::WindowFlags f ) : QWidget ( parent, f ) {
0035     ui = new Ui::ChatWidget;
0036     ui->setupUi(this);
0037 
0038     connect ( ui->sendButton, &QPushButton::clicked, this, &ChatWidget::sendButtonClicked );
0039     ui->sendButton->setIcon( QIcon::fromTheme(QStringLiteral("mail-send")) );
0040 
0041     m_terminal = new Terminal;
0042     ui->consoleLayout->addWidget(m_terminal);
0043 
0044     setConsoleMode ( false );
0045 }
0046 
0047 ChatWidget::~ChatWidget() {
0048     delete ui;
0049 }
0050 
0051 void ChatWidget::addText ( const QString& text, ChatWidget::MessageType type ) {
0052     ScrollBarPin b(m_terminal->verticalScrollBar());
0053     QTextCursor cursor = m_terminal->textCursor();
0054     QTextCharFormat format = cursor.charFormat();
0055     cursor.movePosition(QTextCursor::End);
0056     if ( type == ChatMessage && text.contains(QLatin1String(" says: ")) ) {
0057         format.setForeground( QBrush( messageColor ( StatusMessage ) ) );
0058         cursor.setCharFormat( format );
0059         cursor.insertText ( text.left ( text.indexOf(QLatin1String(" says: ")) ) );
0060         format.setForeground( QBrush( messageColor ( GeneralMessage ) ) );
0061         cursor.setCharFormat( format );
0062         cursor.insertText ( i18n(" says: ") );
0063         format.setForeground( QBrush( messageColor ( ChatMessage ) ) );
0064         cursor.setCharFormat( format );
0065         cursor.insertText ( text.mid ( text.indexOf(QLatin1String(" says: ")) + 7 ) );
0066     } else if (type == GreetMessage) {
0067         format.setFontItalic(true);
0068         format.setForeground( QBrush( messageColor ( type ) ) );
0069         cursor.setCharFormat( format );
0070         cursor.insertText( text );
0071     } else {
0072         format.setForeground( QBrush( messageColor ( type ) ) );
0073         cursor.setCharFormat( format );
0074         cursor.insertText( text );
0075     }
0076     cursor.insertText(QStringLiteral("\n"));
0077 }
0078 
0079 void ChatWidget::addText ( const QByteArray& text, ChatWidget::MessageType type ) {
0080     addText( QLatin1String(text), type );
0081 }
0082 
0083 void ChatWidget::addText(const Message& message) {
0084     addText ( message.first, message.second );
0085 }
0086 
0087 void ChatWidget::setPasswordMode ( bool pwMode ) {
0088     ui->line->setEchoMode(pwMode ? QLineEdit::Password : QLineEdit::Normal);
0089 }
0090 
0091 void ChatWidget::sendButtonClicked() {
0092     if ( m_consoleMode )
0093         addText ( ui->line->text(), GeneralMessage );
0094     else
0095         addText ( i18n("You: ") + ui->line->text(), ChatMessage );
0096     Q_EMIT sendText ( ui->line->text() );
0097     ui->line->clear();
0098 }
0099 
0100 void ChatWidget::addExtraButton ( const QString& text, const QString& title, const QString& icon ) {
0101     QPushButton* button = new QPushButton ( this );
0102     if ( !title.isEmpty() )
0103         button->setText ( title );
0104     else
0105         button->setText ( text );
0106     if ( !icon.isEmpty() )
0107         button->setIcon ( QIcon::fromTheme(icon) );
0108     ui->extraButtonsLayout->addWidget ( button );
0109     m_extraButtons.insert ( button, text );
0110     connect ( button, &QPushButton::clicked, this, &ChatWidget::buttonClicked );
0111 }
0112 
0113 void ChatWidget::buttonClicked() {
0114     QObject* s = sender();
0115     if ( m_extraButtons.contains ( s ) )
0116         Q_EMIT sendText ( m_extraButtons[s] );
0117 }
0118 
0119 QColor ChatWidget::messageColor ( ChatWidget::MessageType type ) const {
0120     if ( m_colors.contains ( type ) )
0121         return m_colors[type];
0122     return m_consoleMode ? Qt::white : Qt::black;
0123 }
0124 
0125 void ChatWidget::setMessageColor ( ChatWidget::MessageType type, const QColor& color ) {
0126     m_colors[type] = color;
0127 }
0128 
0129 void ChatWidget::setConsoleMode ( bool console ) {
0130     m_consoleMode = console;
0131     m_colors.clear();
0132     m_colors[AccountMessage] = Qt::magenta;
0133     m_colors[ErrorMessage] = Qt::red;
0134     m_colors[GreetMessage] = Qt::gray;
0135     m_colors[ChatMessage] = Qt::blue;
0136     m_colors[ChallengeMessage] = Qt::cyan;
0137 
0138     if ( console ) {
0139         QPalette p = m_terminal->palette();
0140         p.setColor ( QPalette::Base, Qt::black );
0141         m_terminal->setPalette ( p );
0142 
0143         m_colors[StatusMessage] = Qt::green;
0144         m_colors[SeekMessage] = Qt::yellow;
0145         m_colors[GeneralMessage] = Qt::white;
0146     } else {
0147         m_terminal->setPalette( palette() );
0148 
0149         m_colors[StatusMessage] = Qt::darkGreen;
0150         m_colors[GeneralMessage] = Qt::black;
0151         m_colors[SeekMessage] = Qt::darkYellow;
0152     }
0153 }
0154 
0155 bool ChatWidget::consoleMode() const {
0156     return m_consoleMode;
0157 }
0158 
0159 #include "moc_chatwidget.cpp"