File indexing completed on 2024-04-21 04:03:16

0001 /*
0002     SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "chatwidget.h"
0008 
0009 #include <KLineEdit>
0010 #include <KTextEdit>
0011 
0012 #include <QVBoxLayout>
0013 #include <QKeyEvent>
0014 
0015 #include "entity.h"
0016 
0017 ChatWidget::ChatWidget(QWidget* parent)
0018 : QWidget(parent)
0019 {
0020     QVBoxLayout* layout = new QVBoxLayout;
0021     layout->setContentsMargins(0, 0, 0, 0);
0022     
0023     m_chat = new KTextEdit(this);
0024     m_chat->setReadOnly(true);
0025     layout->addWidget(m_chat);
0026     
0027     m_input = new KLineEdit(this);
0028     m_input->installEventFilter(this);
0029     layout->addWidget(m_input);
0030     
0031     setLayout(layout);
0032     setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Maximum);
0033     
0034     m_chat->setFocusProxy(m_input);
0035     
0036     m_history.push_back(QLatin1String(""));
0037     m_current = 0;
0038     
0039     connect(m_input, &QLineEdit::returnPressed, this, &ChatWidget::sendLine);
0040 }
0041 
0042 void ChatWidget::setNick(const QString& nick)
0043 {
0044     m_nick = nick;
0045 }
0046 
0047 void ChatWidget::setHistoryText(int index)
0048 {
0049     m_history[m_current] = m_input->text();
0050     m_current = index;
0051     m_input->setText(m_history[m_current]);
0052 }
0053 
0054 bool ChatWidget::eventFilter(QObject* obj, QEvent* event)
0055 {
0056     if (obj == m_input && event->type() == QEvent::KeyPress) {
0057         QKeyEvent* e = (QKeyEvent*)event;
0058         if (e->key() == Qt::Key_Up) {
0059             if (m_current > 0) {
0060                 setHistoryText(m_current - 1);
0061             }
0062         }
0063         else if (e->key() == Qt::Key_Down) {
0064             if (m_current < m_history.size() - 1) {
0065                 setHistoryText(m_current + 1);
0066             }
0067         }
0068     }
0069     
0070     return false;
0071 }
0072 
0073 void ChatWidget::sendLine()
0074 {
0075     QString text = m_input->text();
0076     m_history.push_back(QLatin1String(""));
0077     setHistoryText(m_history.size() - 1);
0078     display(m_nick, text);
0079     Q_EMIT message(text);
0080 }
0081 
0082 void ChatWidget::display(const QString& nick, const QString& text)
0083 {
0084     display(QLatin1Char('<') + nick + QLatin1String("> ") + text);
0085 }
0086 
0087 void ChatWidget::display(const QString& text)
0088 {
0089     if (isVisible()) {
0090         m_chat->append(text);
0091     }
0092 }
0093 
0094 void ChatWidget::bindTo(Entity* entity)
0095 {
0096     connect(this, &ChatWidget::message, entity, &Entity::chat);
0097     
0098     m_chat->clear();
0099     QWidget::show();
0100 }
0101 
0102 void ChatWidget::resizeEvent(QResizeEvent*)
0103 {
0104 }
0105 
0106 QSize ChatWidget::sizeHint() const
0107 {
0108     return QSize(100, 100);
0109 }
0110 
0111 #include "moc_chatwidget.cpp"