1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

addMessage moved to parent class

This commit is contained in:
apprb 2014-09-05 22:24:29 +07:00
parent 522d570e62
commit 2826e83cef
7 changed files with 70 additions and 147 deletions

View File

@ -77,81 +77,6 @@ void ChatForm::onSendTriggered()
emit sendMessage(f->friendId, msg);
}
void ChatForm::addFriendMessage(QString message)
{
QLabel *msgAuthor = new QLabel(nameLabel->text());
QLabel *msgText = new QLabel(message);
QLabel *msgDate = new QLabel(QTime::currentTime().toString("hh:mm"));
addMessage(msgAuthor, msgText, msgDate);
}
void ChatForm::addMessage(QString author, QString message, QString date)
{
addMessage(new QLabel(author), new QLabel(message), new QLabel(date));
}
void ChatForm::addMessage(QLabel* author, QLabel* message, QLabel* date)
{
QScrollBar* scroll = chatArea->verticalScrollBar();
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
author->setAlignment(Qt::AlignTop | Qt::AlignRight);
date->setAlignment(Qt::AlignTop);
message->setWordWrap(true);
message->setTextInteractionFlags(Qt::TextBrowserInteraction);
author->setTextInteractionFlags(Qt::TextBrowserInteraction);
date->setTextInteractionFlags(Qt::TextBrowserInteraction);
if (author->text() == Widget::getInstance()->getUsername())
{
QPalette pal;
pal.setColor(QPalette::WindowText, QColor(100,100,100));
author->setPalette(pal);
message->setPalette(pal);
}
if (previousName.isEmpty() || previousName != author->text())
{
if (curRow)
{
mainChatLayout->setRowStretch(curRow, 0);
mainChatLayout->addItem(new QSpacerItem(0,AUTHOR_CHANGE_SPACING),curRow,0,1,3);
}
previousName = author->text();
curRow++;
}
else if (curRow)// onSaveLogClicked expects 0 or 3 QLabel per line
author->setText("");
QColor greentext(61,204,61);
QString fontTemplate = "<font color='%1'>%2</font>";
QString finalMessage;
QStringList messageLines = message->text().split("\n");
for (QString& s : messageLines)
{
if (QRegExp("^[ ]*>.*").exactMatch(s))
finalMessage += fontTemplate.arg(greentext.name(), s.replace(" ", "&nbsp;"));
else
finalMessage += s.replace(" ", "&nbsp;");
finalMessage += "<br>";
}
message->setText(finalMessage.left(finalMessage.length()-4));
message->setText(SmileyPack::getInstance().smileyfied(message->text()));
message->setTextFormat(Qt::RichText);
mainChatLayout->addWidget(author, curRow, 0);
mainChatLayout->addWidget(message, curRow, 1);
mainChatLayout->addWidget(date, curRow, 3);
mainChatLayout->setRowStretch(curRow+1, 1);
mainChatLayout->setRowStretch(curRow, 0);
curRow++;
author->setContextMenuPolicy(Qt::CustomContextMenu);
message->setContextMenuPolicy(Qt::CustomContextMenu);
date->setContextMenuPolicy(Qt::CustomContextMenu);
connect(author, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(message, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(date, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
}
void ChatForm::onAttachClicked()
{
QString path = QFileDialog::getOpenFileName(0,tr("Send a file"));

View File

@ -33,9 +33,6 @@
#include "core.h"
#include "widget/netcamview.h"
// Spacing in px inserted when the author of the last message changes
#define AUTHOR_CHANGE_SPACING 5
struct Friend;
class ChatForm : public GenericChatForm
@ -45,9 +42,6 @@ public:
ChatForm(Friend* chatFriend);
~ChatForm();
void setStatusMessage(QString newMessage);
void addFriendMessage(QString message);
void addMessage(QString author, QString message, QString date=QTime::currentTime().toString("hh:mm"));
void addMessage(QLabel* author, QLabel* message, QLabel* date);
signals:
void sendFile(int32_t friendId, QString, QString, long long);

View File

@ -22,6 +22,7 @@
#include "smileypack.h"
#include "widget/emoticonswidget.h"
#include "style.h"
#include "widget/widget.h"
GenericChatForm::GenericChatForm(QObject *parent) :
QObject(parent)
@ -212,7 +213,67 @@ void GenericChatForm::onSaveLogClicked()
void GenericChatForm::addMessage(QString author, QString message, QString date)
{
//
QLabel *authorLabel = new QLabel(author);
QLabel *messageLabel = new QLabel(message);
QLabel *dateLabel = new QLabel(date);
QScrollBar* scroll = chatArea->verticalScrollBar();
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
authorLabel->setAlignment(Qt::AlignTop | Qt::AlignRight);
dateLabel->setAlignment(Qt::AlignTop);
messageLabel->setWordWrap(true);
messageLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
authorLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
dateLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
if (author == Widget::getInstance()->getUsername())
{
QPalette pal;
pal.setColor(QPalette::WindowText, QColor(100,100,100));
authorLabel->setPalette(pal);
messageLabel->setPalette(pal);
}
if (previousName.isEmpty() || previousName != author)
{
if (curRow)
{
mainChatLayout->setRowStretch(curRow, 0);
mainChatLayout->addItem(new QSpacerItem(0,AUTHOR_CHANGE_SPACING),curRow,0,1,3);
}
previousName = author;
curRow++;
}
else if (curRow)// onSaveLogClicked expects 0 or 3 QLabel per line
authorLabel->setText("");
QColor greentext(61,204,61);
QString fontTemplate = "<font color='%1'>%2</font>";
QString finalMessage;
QStringList messageLines = message.split("\n");
for (QString& s : messageLines)
{
if (QRegExp("^[ ]*>.*").exactMatch(s))
finalMessage += fontTemplate.arg(greentext.name(), s.replace(" ", "&nbsp;"));
else
finalMessage += s.replace(" ", "&nbsp;");
finalMessage += "<br>";
}
messageLabel->setText(finalMessage.left(finalMessage.length()-4));
messageLabel->setText(SmileyPack::getInstance().smileyfied(messageLabel->text()));
messageLabel->setTextFormat(Qt::RichText);
mainChatLayout->addWidget(authorLabel, curRow, 0);
mainChatLayout->addWidget(messageLabel, curRow, 1);
mainChatLayout->addWidget(dateLabel, curRow, 3);
mainChatLayout->setRowStretch(curRow+1, 1);
mainChatLayout->setRowStretch(curRow, 0);
curRow++;
authorLabel->setContextMenuPolicy(Qt::CustomContextMenu);
messageLabel->setContextMenuPolicy(Qt::CustomContextMenu);
dateLabel->setContextMenuPolicy(Qt::CustomContextMenu);
connect(authorLabel, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(messageLabel, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(dateLabel, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
}
GenericChatForm::~GenericChatForm()

View File

@ -30,6 +30,9 @@
#include "widget/tool/chattextedit.h"
// Spacing in px inserted when the author of the last message changes
#define AUTHOR_CHANGE_SPACING 5
namespace Ui {
class MainWindow;
}

View File

@ -88,68 +88,13 @@ void GroupChatForm::onSendTriggered()
void GroupChatForm::addGroupMessage(QString message, int peerId)
{
QLabel *msgAuthor;
QString msgAuthor;
if (group->peers.contains(peerId))
msgAuthor = new QLabel(group->peers[peerId]);
msgAuthor = group->peers[peerId];
else
msgAuthor = new QLabel(tr("<Unknown>"));
msgAuthor = tr("<Unknown>");
QLabel *msgText = new QLabel(message);
QLabel *msgDate = new QLabel(QTime::currentTime().toString("hh:mm"));
addMessage(msgAuthor, msgText, msgDate);
}
void GroupChatForm::addMessage(QString author, QString message, QString date)
{
addMessage(new QLabel(author), new QLabel(message), new QLabel(date));
}
void GroupChatForm::addMessage(QLabel* author, QLabel* message, QLabel* date)
{
QPalette greentext;
greentext.setColor(QPalette::WindowText, QColor(61,204,61));
QScrollBar* scroll = chatArea->verticalScrollBar();
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
author->setAlignment(Qt::AlignTop | Qt::AlignLeft);
date->setAlignment(Qt::AlignTop);
message->setWordWrap(true);
message->setTextInteractionFlags(Qt::TextBrowserInteraction);
author->setTextInteractionFlags(Qt::TextBrowserInteraction);
date->setTextInteractionFlags(Qt::TextBrowserInteraction);
if (author->text() == Widget::getInstance()->getUsername())
{
QPalette pal;
pal.setColor(QPalette::WindowText, Qt::gray);
author->setPalette(pal);
message->setPalette(pal);
}
if (previousName.isEmpty() || previousName != author->text())
{
if (curRow)
{
mainChatLayout->setRowStretch(curRow, 0);
mainChatLayout->addItem(new QSpacerItem(0,AUTHOR_CHANGE_SPACING),curRow,0,1,3);
}
previousName = author->text();
curRow++;
}
else if (curRow)// onSaveLogClicked expects 0 or 3 QLabel per line
author->setText("");
if (message->text()[0] == '>')
message->setPalette(greentext);
mainChatLayout->addWidget(author, curRow, 0);
mainChatLayout->addWidget(message, curRow, 1);
mainChatLayout->addWidget(date, curRow, 3);
mainChatLayout->setRowStretch(curRow+1, 1);
mainChatLayout->setRowStretch(curRow, 0);
curRow++;
author->setContextMenuPolicy(Qt::CustomContextMenu);
message->setContextMenuPolicy(Qt::CustomContextMenu);
date->setContextMenuPolicy(Qt::CustomContextMenu);
connect(author, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(message, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
connect(date, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
addMessage(msgAuthor, message);
}
void GroupChatForm::onUserListChanged()

View File

@ -30,9 +30,6 @@
#include "widget/tool/chattextedit.h"
#include "ui_mainwindow.h"
// Spacing in px inserted when the author of the last message changes
#define AUTHOR_CHANGE_SPACING 5
class Group;
class GroupChatForm : public GenericChatForm
@ -42,8 +39,6 @@ public:
GroupChatForm(Group* chatGroup);
~GroupChatForm();
void addGroupMessage(QString message, int peerId);
void addMessage(QString author, QString message, QString date=QTime::currentTime().toString("hh:mm"));
void addMessage(QLabel* author, QLabel* message, QLabel* date);
void onUserListChanged();
private slots:

View File

@ -509,7 +509,7 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message)
if (!f)
return;
f->chatForm->addFriendMessage(message);
f->chatForm->addMessage(f->getName(), message);
if (activeChatroomWidget != nullptr)
{