Skip to content

Commit

Permalink
Update chatbubble layout and combine messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fvantienen committed May 16, 2024
1 parent 59a37ed commit 4c3759c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 21 deletions.
32 changes: 26 additions & 6 deletions src/widgets/basics/chatbubble.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include "chatbubble.h"
#include "ui_chatbubble.h"
#include <QTime>
#include "AircraftManager.h"
#include <QDebug>

ChatBubble::ChatBubble(QString source, QString dest, QString msg, bool sent, QWidget *parent) :
QWidget(parent),
ui(new Ui::ChatBubble)
ui(new Ui::ChatBubble),
source(source),
dest(dest)
{
ui->setupUi(this);

Expand All @@ -22,10 +23,18 @@ ChatBubble::ChatBubble(QString source, QString dest, QString msg, bool sent, QWi
source_name += QString::fromUtf8(" \u2794 ") + dest;
}

auto date = QTime::currentTime().toString();
ui->name_label->setText("<b>"+source_name+"</b> "+date+" :");
// Add option for bold text
msg = msg.toHtmlEscaped();
QRegularExpression re(QStringLiteral("\\*([^*]+)\\*"));
msg.replace(re, "<b>\\1</b>");

chat_time = QTime::currentTime();
auto date = chat_time.toString();
ui->time_label->setText(date+" ");
ui->name_label->setText("<b>"+source_name+"</b>");
ui->name_label->setStyleSheet("color:" + color.name());
ui->message_label->setText(msg);
ui->message_label->setTextFormat(Qt::RichText);

if(sent) {
ui->name_label->setAlignment(Qt::AlignLeft);
Expand All @@ -37,7 +46,7 @@ ChatBubble::ChatBubble(QString source, QString dest, QString msg, bool sent, QWi

// delay set max width to wait for the widget to be shown.
QTimer::singleShot(20, this, [=](){
auto width = size().width() * 0.7;
auto width = size().width() * 0.95;
ui->frame->setMaximumWidth(width);
});
}
Expand All @@ -47,11 +56,22 @@ ChatBubble::~ChatBubble()
delete ui;
}

void ChatBubble::addText(QString text) {
// Add option for bold text
text = text.toHtmlEscaped();
QRegularExpression re(QStringLiteral("\\*([^*]+)\\*"));
text.replace(re, "<b>\\1</b>");

chat_time = QTime::currentTime();
ui->message_label->setText(ui->message_label->text() + "<br>" + text);
ui->message_label->setTextFormat(Qt::RichText);
}

bool ChatBubble::eventFilter(QObject *obj, QEvent *ev) {
(void)obj;
(void)ev;
if(ev->type() == QEvent::Resize) {
auto width = size().width() * 0.7;
auto width = size().width() * 0.95;
ui->frame->setMaximumWidth(width);
}

Expand Down
12 changes: 11 additions & 1 deletion src/widgets/basics/chatbubble.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CHATBUBBLE_H

#include <QWidget>
#include <QTime>

namespace Ui {
class ChatBubble;
Expand All @@ -12,14 +13,23 @@ class ChatBubble : public QWidget
Q_OBJECT

public:
explicit ChatBubble(QString sender, QString dest, QString msg, bool sent=false, QWidget *parent = nullptr);
explicit ChatBubble(QString source, QString dest, QString msg, bool sent=false, QWidget *parent = nullptr);
~ChatBubble();

QString getSource() {return source;}
QString getDest() {return dest;}
QTime getTime() {return chat_time;}
void addText(QString text);

protected:
bool eventFilter(QObject *obj, QEvent *ev) override;

private:
Ui::ChatBubble *ui;

QString source;
QString dest;
QTime chat_time;
};

#endif // CHATBUBBLE_H
54 changes: 40 additions & 14 deletions src/widgets/basics/chatbubble.ui
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">.QFrame{background-color: rgba(200,200,200,50); border-radius: 20px;}</string>
<string notr="true">.QFrame{background-color: rgba(200,200,200,50); border-radius: 15px;}</string>
</property>
<property name="frameShape">
<enum>QFrame::Panel</enum>
Expand All @@ -42,20 +42,46 @@
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<widget class="QLabel" name="name_label">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<layout class="QHBoxLayout" name="title_bar">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<property name="text">
<string>name</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
<item>
<widget class="QLabel" name="time_label">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="styleSheet">
<string notr="true">color: grey;</string>
</property>
<property name="text">
<string>time</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="name_label">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>name</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="message_label">
Expand Down
10 changes: 10 additions & 0 deletions src/widgets/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ void Chat::onSend() {

void Chat::addMessage(QString txt, QString source, QString dst, bool sent) {
(void)dst;
// Check if it can be combined with the previous message
if(ui->scroll_widget->layout()->count() > 0) {
auto last = ui->scroll_widget->layout()->itemAt(ui->scroll_widget->layout()->count()-1);
auto cb = dynamic_cast<ChatBubble*>(last->widget());
if(cb != nullptr && cb->getSource() == source && cb->getDest() == dst && cb->getTime().secsTo(QTime::currentTime()) < 5){
cb->addText(txt);
return;
}
}

auto cb = new ChatBubble(source, dst, txt, sent, this);
ui->scroll_widget->layout()->addWidget(cb);
ui->scroll_widget->installEventFilter(cb);
Expand Down

0 comments on commit 4c3759c

Please sign in to comment.