1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/chatform.cpp
Tux3 / Mlkj / !Lev.uXFMLA 18cee41cfb Initial commit
2014-06-24 22:11:11 +02:00

154 lines
4.6 KiB
C++

#include "chatform.h"
#include "friend.h"
#include "friendwidget.h"
#include "widget.h"
#include <QFont>
#include <QTime>
#include <QScrollBar>
ChatForm::ChatForm(Friend* chatFriend)
: f(chatFriend), curRow{0}, lockSliderToBottom{true}
{
main = new QWidget(), head = new QWidget(), chatAreaWidget = new QWidget();
name = new QLabel(), avatar = new QLabel(), statusMessage = new QLabel();
headLayout = new QHBoxLayout(), mainFootLayout = new QHBoxLayout();
headTextLayout = new QVBoxLayout(), mainLayout = new QVBoxLayout();
mainChatLayout = new QGridLayout();
msgEdit = new ChatTextEdit();
sendButton = new QPushButton();
chatArea = new QScrollArea();
QFont bold;
bold.setBold(true);
name->setText(chatFriend->widget->name.text());
name->setFont(bold);
statusMessage->setText(chatFriend->widget->statusMessage.text());
avatar->setPixmap(*chatFriend->widget->avatar.pixmap());
chatAreaWidget->setLayout(mainChatLayout);
chatArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
chatArea->setWidgetResizable(true);
mainChatLayout->setColumnStretch(1,1);
mainChatLayout->setSpacing(10);
sendButton->setIcon(QIcon("img/button icons/sendmessage_2x.png"));
sendButton->setFlat(true);
QPalette pal;
pal.setColor(QPalette::Button, QColor(107,194,96)); // Tox Green
sendButton->setPalette(pal);
sendButton->setAutoFillBackground(true);
msgEdit->setFixedHeight(50);
sendButton->setFixedSize(50, 50);
main->setLayout(mainLayout);
mainLayout->addWidget(chatArea);
mainLayout->addLayout(mainFootLayout);
mainLayout->setMargin(0);
mainFootLayout->addWidget(msgEdit);
mainFootLayout->addWidget(sendButton);
head->setLayout(headLayout);
headLayout->addWidget(avatar);
headLayout->addLayout(headTextLayout);
headLayout->addStretch();
headTextLayout->addStretch();
headTextLayout->addWidget(name);
headTextLayout->addWidget(statusMessage);
headTextLayout->addStretch();
chatArea->setWidget(chatAreaWidget);
connect(sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered()));
connect(chatArea->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged()));
}
ChatForm::~ChatForm()
{
delete main;
delete head;
}
void ChatForm::show(Ui::Widget &ui)
{
ui.mainContent->layout()->addWidget(main);
ui.mainHead->layout()->addWidget(head);
main->show();
head->show();
}
void ChatForm::setName(QString newName)
{
name->setText(newName);
}
void ChatForm::setStatusMessage(QString newMessage)
{
statusMessage->setText(newMessage);
}
void ChatForm::onSendTriggered()
{
QString msg = msgEdit->toPlainText();
if (msg.isEmpty())
return;
QString name = Widget::getInstance()->getUsername();
msgEdit->clear();
addMessage(name, msg);
emit sendMessage(f->friendId, msg);
}
void ChatForm::addFriendMessage(QString message)
{
QLabel *msgAuthor = new QLabel(name->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);
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);
curRow++;
}
mainChatLayout->addWidget(author, curRow, 0);
}
previousName = author->text();
mainChatLayout->addWidget(message, curRow, 1);
mainChatLayout->addWidget(date, curRow, 3);
mainChatLayout->setRowStretch(curRow+1, 1);
mainChatLayout->setRowStretch(curRow, 0);
curRow++;
}
void ChatForm::onSliderRangeChanged()
{
QScrollBar* scroll = chatArea->verticalScrollBar();
if (lockSliderToBottom)
scroll->setValue(scroll->maximum());
}