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

708 lines
23 KiB
C++
Raw Normal View History

2014-07-07 00:19:45 +08:00
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
2014-06-25 04:11:11 +08:00
#include "chatform.h"
#include "friend.h"
2014-07-25 20:52:14 +08:00
#include "smileypack.h"
#include "widget/friendwidget.h"
#include "widget/widget.h"
#include "widget/filetransfertwidget.h"
2014-06-25 04:11:11 +08:00
#include <QFont>
#include <QTime>
#include <QScrollBar>
2014-06-26 04:43:28 +08:00
#include <QFileDialog>
#include <QMenu>
2014-07-31 04:07:26 +08:00
#include <QWidgetAction>
#include <QGridLayout>
2014-06-25 04:11:11 +08:00
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();
2014-06-27 07:49:48 +08:00
headTextLayout = new QVBoxLayout(), mainLayout = new QVBoxLayout(), footButtonsSmall = new QVBoxLayout();
2014-06-25 04:11:11 +08:00
mainChatLayout = new QGridLayout();
msgEdit = new ChatTextEdit();
2014-06-28 14:05:31 +08:00
sendButton = new QPushButton(), fileButton = new QPushButton(), emoteButton = new QPushButton(), callButton = new QPushButton(), videoButton = new QPushButton();
2014-06-25 04:11:11 +08:00
chatArea = new QScrollArea();
netcam = new NetCamView();
2014-06-25 04:11:11 +08:00
QFont bold;
bold.setBold(true);
name->setText(chatFriend->widget->name.text());
name->setFont(bold);
statusMessage->setText(chatFriend->widget->statusMessage.text());
2014-06-29 23:04:49 +08:00
// No real avatar support in toxcore, better draw a pretty picture
//avatar->setPixmap(*chatFriend->widget->avatar.pixmap());
avatar->setPixmap(QPixmap(":/img/contact_dark.png"));
2014-06-25 04:11:11 +08:00
chatAreaWidget->setLayout(mainChatLayout);
QString chatAreaStylesheet = "";
try
{
QFile f(":/ui/chatArea/chatArea.css");
f.open(QFile::ReadOnly | QFile::Text);
QTextStream chatAreaStylesheetStream(&f);
chatAreaStylesheet = chatAreaStylesheetStream.readAll();
}
catch (int e) {}
chatArea->setStyleSheet(chatAreaStylesheet);
2014-06-25 04:11:11 +08:00
chatArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
chatArea->setWidgetResizable(true);
chatArea->setContextMenuPolicy(Qt::CustomContextMenu);
chatArea->setFrameStyle(QFrame::NoFrame);
2014-06-25 04:11:11 +08:00
mainChatLayout->setColumnStretch(1,1);
2014-06-29 23:04:49 +08:00
mainChatLayout->setSpacing(5);
2014-06-25 04:11:11 +08:00
2014-06-27 07:49:48 +08:00
footButtonsSmall->setSpacing(2);
QString msgEditStylesheet = "";
try
{
QFile f(":/ui/msgEdit/msgEdit.css");
f.open(QFile::ReadOnly | QFile::Text);
QTextStream msgEditStylesheetStream(&f);
msgEditStylesheet = msgEditStylesheetStream.readAll();
}
catch (int e) {}
msgEdit->setStyleSheet(msgEditStylesheet);
2014-06-26 04:43:28 +08:00
msgEdit->setFixedHeight(50);
msgEdit->setFrameStyle(QFrame::NoFrame);
2014-06-28 13:14:38 +08:00
QString sendButtonStylesheet = "";
try
{
QFile f(":/ui/sendButton/sendButton.css");
2014-06-28 13:14:38 +08:00
f.open(QFile::ReadOnly | QFile::Text);
QTextStream sendButtonStylesheetStream(&f);
sendButtonStylesheet = sendButtonStylesheetStream.readAll();
}
catch (int e) {}
sendButton->setStyleSheet(sendButtonStylesheet);
QString fileButtonStylesheet = "";
try
{
QFile f(":/ui/fileButton/fileButton.css");
2014-06-28 13:14:38 +08:00
f.open(QFile::ReadOnly | QFile::Text);
QTextStream fileButtonStylesheetStream(&f);
fileButtonStylesheet = fileButtonStylesheetStream.readAll();
}
catch (int e) {}
fileButton->setStyleSheet(fileButtonStylesheet);
QString emoteButtonStylesheet = "";
try
{
QFile f(":/ui/emoteButton/emoteButton.css");
2014-06-28 13:14:38 +08:00
f.open(QFile::ReadOnly | QFile::Text);
QTextStream emoteButtonStylesheetStream(&f);
emoteButtonStylesheet = emoteButtonStylesheetStream.readAll();
}
catch (int e) {}
emoteButton->setStyleSheet(emoteButtonStylesheet);
QString callButtonStylesheet = "";
try
{
QFile f(":/ui/callButton/callButton.css");
2014-06-28 13:14:38 +08:00
f.open(QFile::ReadOnly | QFile::Text);
QTextStream callButtonStylesheetStream(&f);
callButtonStylesheet = callButtonStylesheetStream.readAll();
}
catch (int e) {}
callButton->setObjectName("green");
callButton->setStyleSheet(callButtonStylesheet);
2014-06-28 14:05:31 +08:00
QString videoButtonStylesheet = "";
try
{
QFile f(":/ui/videoButton/videoButton.css");
2014-06-28 14:05:31 +08:00
f.open(QFile::ReadOnly | QFile::Text);
QTextStream videoButtonStylesheetStream(&f);
videoButtonStylesheet = videoButtonStylesheetStream.readAll();
}
catch (int e) {}
videoButton->setObjectName("green");
videoButton->setStyleSheet(videoButtonStylesheet);
2014-06-25 04:11:11 +08:00
main->setLayout(mainLayout);
mainLayout->addWidget(chatArea);
mainLayout->addLayout(mainFootLayout);
mainLayout->setMargin(0);
2014-06-27 07:49:48 +08:00
footButtonsSmall->addWidget(emoteButton);
footButtonsSmall->addWidget(fileButton);
2014-06-25 04:11:11 +08:00
mainFootLayout->addWidget(msgEdit);
2014-06-27 07:49:48 +08:00
mainFootLayout->addLayout(footButtonsSmall);
2014-06-27 08:26:30 +08:00
mainFootLayout->addSpacing(5);
2014-06-25 04:11:11 +08:00
mainFootLayout->addWidget(sendButton);
2014-06-27 08:26:30 +08:00
mainFootLayout->setSpacing(0);
2014-06-25 04:11:11 +08:00
head->setLayout(headLayout);
headLayout->addWidget(avatar);
headLayout->addLayout(headTextLayout);
headLayout->addStretch();
headLayout->addWidget(callButton);
2014-06-28 14:05:31 +08:00
headLayout->addWidget(videoButton);
2014-06-25 04:11:11 +08:00
headTextLayout->addStretch();
headTextLayout->addWidget(name);
headTextLayout->addWidget(statusMessage);
headTextLayout->addStretch();
chatArea->setWidget(chatAreaWidget);
2014-07-06 07:39:35 +08:00
//Fix for incorrect layouts on OS X as per
//https://bugreports.qt-project.org/browse/QTBUG-14591
sendButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
fileButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
emoteButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
2014-07-07 05:43:02 +08:00
// callButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
// videoButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
// msgEdit->setAttribute(Qt::WA_LayoutUsesWidgetRect);
// chatArea->setAttribute(Qt::WA_LayoutUsesWidgetRect);
2014-07-06 07:39:35 +08:00
2014-06-26 06:30:24 +08:00
connect(Widget::getInstance()->getCore(), &Core::fileSendStarted, this, &ChatForm::startFileSend);
connect(Widget::getInstance()->getCore(), &Core::videoFrameReceived, netcam, &NetCamView::updateDisplay);
2014-06-25 04:11:11 +08:00
connect(sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
2014-06-26 04:43:28 +08:00
connect(fileButton, SIGNAL(clicked()), this, SLOT(onAttachClicked()));
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
2014-06-25 04:11:11 +08:00
connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered()));
connect(chatArea->verticalScrollBar(), SIGNAL(rangeChanged(int,int)), this, SLOT(onSliderRangeChanged()));
connect(chatArea, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onChatContextMenuRequested(QPoint)));
2014-07-31 04:07:26 +08:00
connect(emoteButton, SIGNAL(clicked()), this, SLOT(onEmoteButtonClicked()));
2014-06-25 04:11:11 +08:00
}
ChatForm::~ChatForm()
{
delete main;
delete head;
delete netcam;
2014-06-25 04:11:11 +08:00
}
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);
name->setToolTip(newName); // for overlength names
2014-06-25 04:11:11 +08:00
}
void ChatForm::setStatusMessage(QString newMessage)
{
statusMessage->setText(newMessage);
statusMessage->setToolTip(newMessage); // for overlength messsages
2014-06-25 04:11:11 +08:00
}
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)
{
2014-07-31 23:36:55 +08:00
message = SmileyPack::getInstance().smileyfied(message);
2014-06-25 04:11:11 +08:00
addMessage(new QLabel(author), new QLabel(message), new QLabel(date));
}
void ChatForm::addMessage(QLabel* author, QLabel* message, QLabel* date)
{
2014-07-05 03:30:59 +08:00
QPalette greentext;
greentext.setColor(QPalette::WindowText, QColor(61,204,61));
2014-06-25 04:11:11 +08:00
QScrollBar* scroll = chatArea->verticalScrollBar();
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
author->setAlignment(Qt::AlignTop | Qt::AlignRight);
date->setAlignment(Qt::AlignTop);
2014-06-28 08:10:47 +08:00
message->setWordWrap(true);
2014-06-28 06:45:17 +08:00
message->setTextInteractionFlags(Qt::TextBrowserInteraction);
author->setTextInteractionFlags(Qt::TextBrowserInteraction);
date->setTextInteractionFlags(Qt::TextBrowserInteraction);
2014-06-25 04:11:11 +08:00
if (author->text() == Widget::getInstance()->getUsername())
{
QPalette pal;
pal.setColor(QPalette::WindowText, QColor(100,100,100));
2014-06-25 04:11:11 +08:00
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++;
2014-06-25 04:11:11 +08:00
}
else if (curRow)// onSaveLogClicked expects 0 or 3 QLabel per line
author->setText("");
2014-07-05 03:30:59 +08:00
if (message->text()[0] == '>')
message->setPalette(greentext);
mainChatLayout->addWidget(author, curRow, 0);
2014-06-25 04:11:11 +08:00
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)));
2014-06-25 04:11:11 +08:00
}
2014-06-26 04:43:28 +08:00
void ChatForm::onAttachClicked()
{
2014-07-04 03:42:23 +08:00
QString path = QFileDialog::getOpenFileName(0,tr("Send a file"));
2014-06-26 04:43:28 +08:00
if (path.isEmpty())
return;
QFile file(path);
if (!file.exists() || !file.open(QIODevice::ReadOnly))
return;
2014-07-01 19:52:53 +08:00
long long filesize = file.size();
2014-06-26 04:43:28 +08:00
file.close();
QFileInfo fi(path);
2014-07-01 19:52:53 +08:00
emit sendFile(f->friendId, fi.fileName(), path, filesize);
2014-06-26 04:43:28 +08:00
}
2014-06-25 04:11:11 +08:00
void ChatForm::onSliderRangeChanged()
{
QScrollBar* scroll = chatArea->verticalScrollBar();
if (lockSliderToBottom)
scroll->setValue(scroll->maximum());
}
2014-06-26 06:30:24 +08:00
void ChatForm::startFileSend(ToxFile file)
2014-06-26 06:30:24 +08:00
{
if (file.friendId != f->friendId)
return;
QLabel *author = new QLabel(Widget::getInstance()->getUsername());
2014-06-27 01:53:21 +08:00
QLabel *date = new QLabel(QTime::currentTime().toString("hh:mm"));
2014-06-26 06:30:24 +08:00
QScrollBar* scroll = chatArea->verticalScrollBar();
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
author->setAlignment(Qt::AlignTop | Qt::AlignRight);
date->setAlignment(Qt::AlignTop);
QPalette pal;
pal.setColor(QPalette::WindowText, Qt::gray);
author->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);
}
FileTransfertWidget* fileTrans = new FileTransfertWidget(file);
previousName = author->text();
mainChatLayout->addWidget(fileTrans, curRow, 1);
mainChatLayout->addWidget(date, curRow, 3);
mainChatLayout->setRowStretch(curRow+1, 1);
mainChatLayout->setRowStretch(curRow, 0);
curRow++;
connect(Widget::getInstance()->getCore(), &Core::fileTransferInfo, fileTrans, &FileTransfertWidget::onFileTransferInfo);
connect(Widget::getInstance()->getCore(), &Core::fileTransferCancelled, fileTrans, &FileTransfertWidget::onFileTransferCancelled);
connect(Widget::getInstance()->getCore(), &Core::fileTransferFinished, fileTrans, &FileTransfertWidget::onFileTransferFinished);
}
void ChatForm::onFileRecvRequest(ToxFile file)
{
if (file.friendId != f->friendId)
return;
QLabel *author = new QLabel(f->getName());
2014-06-27 01:53:21 +08:00
QLabel *date = new QLabel(QTime::currentTime().toString("hh:mm"));
QScrollBar* scroll = chatArea->verticalScrollBar();
lockSliderToBottom = scroll && scroll->value() == scroll->maximum();
author->setAlignment(Qt::AlignTop | Qt::AlignRight);
date->setAlignment(Qt::AlignTop);
if (previousName.isEmpty() || previousName != author->text())
2014-06-26 06:30:24 +08:00
{
if (curRow)
{
mainChatLayout->setRowStretch(curRow, 0);
mainChatLayout->addItem(new QSpacerItem(0,AUTHOR_CHANGE_SPACING),curRow,0,1,3);
curRow++;
}
mainChatLayout->addWidget(author, curRow, 0);
}
FileTransfertWidget* fileTrans = new FileTransfertWidget(file);
previousName = author->text();
mainChatLayout->addWidget(fileTrans, curRow, 1);
mainChatLayout->addWidget(date, curRow, 3);
mainChatLayout->setRowStretch(curRow+1, 1);
mainChatLayout->setRowStretch(curRow, 0);
curRow++;
connect(Widget::getInstance()->getCore(), &Core::fileTransferInfo, fileTrans, &FileTransfertWidget::onFileTransferInfo);
connect(Widget::getInstance()->getCore(), &Core::fileTransferCancelled, fileTrans, &FileTransfertWidget::onFileTransferCancelled);
connect(Widget::getInstance()->getCore(), &Core::fileTransferFinished, fileTrans, &FileTransfertWidget::onFileTransferFinished);
2014-06-26 06:30:24 +08:00
}
void ChatForm::onAvInvite(int FriendId, int CallId, bool video)
{
if (FriendId != f->friendId)
return;
callId = CallId;
callButton->disconnect();
videoButton->disconnect();
if (video)
{
callButton->setObjectName("grey");
callButton->style()->polish(callButton);
videoButton->setObjectName("yellow");
videoButton->style()->polish(videoButton);
connect(videoButton, SIGNAL(clicked()), this, SLOT(onAnswerCallTriggered()));
}
else
{
callButton->setObjectName("yellow");
callButton->style()->polish(callButton);
videoButton->setObjectName("grey");
videoButton->style()->polish(videoButton);
connect(callButton, SIGNAL(clicked()), this, SLOT(onAnswerCallTriggered()));
}
2014-07-01 03:14:51 +08:00
Widget* w = Widget::getInstance();
if (!w->isFriendWidgetCurActiveWidget(f))
{
w->newMessageAlert();
f->hasNewMessages=true;
w->updateFriendStatusLights(f->friendId);
}
}
void ChatForm::onAvStart(int FriendId, int CallId, bool video)
{
if (FriendId != f->friendId)
return;
callId = CallId;
callButton->disconnect();
videoButton->disconnect();
if (video)
{
callButton->setObjectName("grey");
callButton->style()->polish(callButton);
videoButton->setObjectName("red");
videoButton->style()->polish(videoButton);
connect(videoButton, SIGNAL(clicked()), this, SLOT(onHangupCallTriggered()));
netcam->show();
}
else
{
callButton->setObjectName("red");
callButton->style()->polish(callButton);
videoButton->setObjectName("grey");
videoButton->style()->polish(videoButton);
connect(callButton, SIGNAL(clicked()), this, SLOT(onHangupCallTriggered()));
}
}
void ChatForm::onAvCancel(int FriendId, int)
{
if (FriendId != f->friendId)
return;
callButton->disconnect();
videoButton->disconnect();
callButton->setObjectName("green");
callButton->style()->polish(callButton);
videoButton->setObjectName("green");
videoButton->style()->polish(videoButton);
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
netcam->hide();
}
void ChatForm::onAvEnd(int FriendId, int)
{
if (FriendId != f->friendId)
return;
callButton->disconnect();
videoButton->disconnect();
2014-06-28 13:14:38 +08:00
callButton->setObjectName("green");
callButton->style()->polish(callButton);
videoButton->setObjectName("green");
videoButton->style()->polish(videoButton);
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
netcam->hide();
}
void ChatForm::onAvRinging(int FriendId, int CallId, bool video)
2014-06-27 09:06:56 +08:00
{
if (FriendId != f->friendId)
return;
callId = CallId;
callButton->disconnect();
videoButton->disconnect();
if (video)
{
callButton->setObjectName("grey");
callButton->style()->polish(callButton);
videoButton->setObjectName("yellow");
videoButton->style()->polish(videoButton);
connect(videoButton, SIGNAL(clicked()), this, SLOT(onCancelCallTriggered()));
}
else
{
callButton->setObjectName("yellow");
callButton->style()->polish(callButton);
videoButton->setObjectName("grey");
videoButton->style()->polish(videoButton);
connect(callButton, SIGNAL(clicked()), this, SLOT(onCancelCallTriggered()));
}
2014-06-27 09:06:56 +08:00
}
void ChatForm::onAvStarting(int FriendId, int, bool video)
2014-06-27 09:06:56 +08:00
{
if (FriendId != f->friendId)
return;
callButton->disconnect();
videoButton->disconnect();
if (video)
{
callButton->setObjectName("grey");
callButton->style()->polish(callButton);
videoButton->setObjectName("red");
videoButton->style()->polish(videoButton);
connect(videoButton, SIGNAL(clicked()), this, SLOT(onHangupCallTriggered()));
netcam->show();
}
else
{
callButton->setObjectName("red");
callButton->style()->polish(callButton);
videoButton->setObjectName("grey");
videoButton->style()->polish(videoButton);
connect(callButton, SIGNAL(clicked()), this, SLOT(onHangupCallTriggered()));
}
2014-06-27 09:06:56 +08:00
}
void ChatForm::onAvEnding(int FriendId, int)
{
if (FriendId != f->friendId)
return;
callButton->disconnect();
videoButton->disconnect();
2014-06-28 13:14:38 +08:00
callButton->setObjectName("green");
callButton->style()->polish(callButton);
callButton->disconnect();
videoButton->setObjectName("green");
videoButton->style()->polish(videoButton);
videoButton->disconnect();
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
netcam->hide();
}
2014-06-27 09:06:56 +08:00
void ChatForm::onAvRequestTimeout(int FriendId, int)
{
if (FriendId != f->friendId)
return;
callButton->disconnect();
videoButton->disconnect();
2014-06-28 13:14:38 +08:00
callButton->setObjectName("green");
callButton->style()->polish(callButton);
2014-06-27 09:06:56 +08:00
callButton->disconnect();
videoButton->setObjectName("green");
videoButton->style()->polish(videoButton);
videoButton->disconnect();
2014-06-27 09:06:56 +08:00
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
netcam->hide();
2014-06-27 09:06:56 +08:00
}
2014-06-28 03:59:25 +08:00
void ChatForm::onAvPeerTimeout(int FriendId, int)
{
if (FriendId != f->friendId)
return;
callButton->disconnect();
videoButton->disconnect();
2014-06-28 13:14:38 +08:00
callButton->setObjectName("green");
callButton->style()->polish(callButton);
2014-06-28 03:59:25 +08:00
callButton->disconnect();
videoButton->setObjectName("green");
videoButton->style()->polish(videoButton);
videoButton->disconnect();
2014-06-28 03:59:25 +08:00
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
netcam->hide();
2014-06-28 03:59:25 +08:00
}
void ChatForm::onAnswerCallTriggered()
{
emit answerCall(callId);
}
void ChatForm::onHangupCallTriggered()
{
emit hangupCall(callId);
}
void ChatForm::onCallTriggered()
{
2014-06-27 09:06:56 +08:00
callButton->disconnect();
videoButton->disconnect();
2014-06-27 09:06:56 +08:00
emit startCall(f->friendId);
}
void ChatForm::onVideoCallTriggered()
{
callButton->disconnect();
videoButton->disconnect();
emit startVideoCall(f->friendId, true);
}
2014-06-27 09:06:56 +08:00
void ChatForm::onCancelCallTriggered()
{
callButton->disconnect();
videoButton->disconnect();
2014-06-28 13:14:38 +08:00
callButton->setObjectName("green");
callButton->style()->polish(callButton);
2014-06-27 09:06:56 +08:00
callButton->disconnect();
videoButton->setObjectName("green");
videoButton->style()->polish(videoButton);
videoButton->disconnect();
2014-06-27 09:06:56 +08:00
connect(callButton, SIGNAL(clicked()), this, SLOT(onCallTriggered()));
connect(videoButton, SIGNAL(clicked()), this, SLOT(onVideoCallTriggered()));
netcam->hide();
2014-06-27 09:06:56 +08:00
emit cancelCall(callId, f->friendId);
}
void ChatForm::onChatContextMenuRequested(QPoint pos)
{
QWidget* sender = (QWidget*)QObject::sender();
pos = sender->mapToGlobal(pos);
QMenu menu;
2014-07-04 03:42:23 +08:00
menu.addAction(tr("Save chat log"), this, SLOT(onSaveLogClicked()));
menu.exec(pos);
}
void ChatForm::onSaveLogClicked()
{
2014-07-04 03:42:23 +08:00
QString path = QFileDialog::getSaveFileName(0,tr("Save chat log"));
if (path.isEmpty())
return;
QFile file(path);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QString log;
QList<QLabel*> labels = chatAreaWidget->findChildren<QLabel*>();
int i=0;
for (QLabel* label : labels)
{
log += label->text();
if (i==2)
{
i=0;
log += '\n';
}
else
{
log += '\t';
i++;
}
}
file.write(log.toUtf8());
file.close();
}
2014-07-31 04:07:26 +08:00
void ChatForm::onEmoteButtonClicked()
{
QList<QStringList> emoticons = SmileyPack::getInstance().getEmoticons();
QMenu menu;
QGridLayout* gridLayout = new QGridLayout;
menu.setLayout(gridLayout);
int colCount = sqrt(emoticons.size()) + 1;
int row = 0;
int col = 0;
2014-07-31 22:08:44 +08:00
for (const QStringList& set : emoticons)
2014-07-31 04:07:26 +08:00
{
QPushButton* button = new QPushButton;
2014-07-31 23:36:55 +08:00
button->setIcon(SmileyPack::getInstance().getAsIcon(set[0]));
2014-07-31 04:07:26 +08:00
button->setToolTip(set.join(" "));
button->setProperty("sequence", set[0]);
connect(button, &QPushButton::clicked, this, &ChatForm::onAddEmote);
gridLayout->addWidget(button, row, ++col);
if (col >= colCount)
{
col = 0;
row++;
}
}
QWidget* sender = qobject_cast<QWidget*>(QObject::sender());
if (sender)
{
QPoint pos(gridLayout->totalSizeHint().width() / 2, gridLayout->totalSizeHint().height());
menu.exec(sender->mapToGlobal(-pos));
}
}
void ChatForm::onAddEmote()
{
// hide the QMenu
QMenu* menu = qobject_cast<QMenu*>(QObject::sender()->parent());
if (menu)
menu->hide();
// insert the emoticon
QWidget* sender = qobject_cast<QWidget*>(QObject::sender());
if (sender)
msgEdit->insertPlainText(' ' + sender->property("sequence").toString() + ' ');
msgEdit->setFocus(); // refocus so that you can continue typing
}