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

245 lines
6.8 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 "groupchatform.h"
#include "tabcompleter.h"
2014-10-08 12:26:25 +08:00
#include "src/group.h"
#include "src/widget/groupwidget.h"
#include "src/widget/tool/chattextedit.h"
#include "src/widget/croppinglabel.h"
#include "src/widget/maskablepixmapwidget.h"
#include "src/core.h"
#include "src/misc/style.h"
2014-09-11 21:44:34 +08:00
#include <QPushButton>
2014-09-26 00:03:25 +08:00
#include <QMimeData>
#include <QDragEnterEvent>
#include "src/historykeeper.h"
#include "src/misc/flowlayout.h"
#include <QDebug>
2014-06-25 04:11:11 +08:00
GroupChatForm::GroupChatForm(Group* chatGroup)
2014-11-13 20:11:23 +08:00
: group(chatGroup), inCall{false}
2014-06-25 04:11:11 +08:00
{
2014-09-05 23:29:14 +08:00
nusersLabel = new QLabel();
tabber = new TabCompleter(msgEdit, group);
fileButton->setEnabled(false);
2014-11-13 20:11:23 +08:00
if (group->avGroupchat)
{
videoButton->setEnabled(false);
videoButton->setObjectName("grey");
}
else
{
videoButton->setVisible(false);
callButton->setVisible(false);
volButton->setVisible(false);
micButton->setVisible(false);
}
2014-10-01 17:13:49 +08:00
nameLabel->setText(group->widget->getName());
nusersLabel->setFont(Style::getFont(Style::Medium));
2014-09-05 23:29:14 +08:00
nusersLabel->setText(GroupChatForm::tr("%1 users in chat","Number of users in chat").arg(group->peers.size()));
2014-10-10 03:20:06 +08:00
nusersLabel->setObjectName("statusLabel");
avatar->setPixmap(QPixmap(":/img/group_dark.png"), Qt::transparent);
msgEdit->setObjectName("group");
namesListLayout = new FlowLayout(0,5,0);
QStringList names(group->peers.values());
for (const QString& name : names)
namesListLayout->addWidget(new QLabel(name));
2014-09-05 23:29:14 +08:00
headTextLayout->addWidget(nusersLabel);
headTextLayout->addLayout(namesListLayout);
2014-06-25 04:11:11 +08:00
headTextLayout->addStretch();
nameLabel->setMinimumHeight(12);
nusersLabel->setMinimumHeight(12);
2014-06-25 04:11:11 +08:00
connect(sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
connect(msgEdit, SIGNAL(enterPressed()), this, SLOT(onSendTriggered()));
connect(msgEdit, &ChatTextEdit::tabPressed, tabber, &TabCompleter::complete);
connect(msgEdit, &ChatTextEdit::keyPressed, tabber, &TabCompleter::reset);
2014-11-13 20:11:23 +08:00
connect(callButton, &QPushButton::clicked, this, &GroupChatForm::onCallClicked);
connect(micButton, SIGNAL(clicked()), this, SLOT(onMicMuteToggle()));
connect(volButton, SIGNAL(clicked()), this, SLOT(onVolMuteToggle()));
2014-06-25 04:11:11 +08:00
2014-09-26 00:03:25 +08:00
setAcceptDrops(true);
2014-06-25 04:11:11 +08:00
}
void GroupChatForm::onSendTriggered()
{
QString msg = msgEdit->toPlainText();
if (msg.isEmpty())
return;
2014-11-01 21:00:43 +08:00
2014-06-25 04:11:11 +08:00
msgEdit->clear();
2014-11-01 21:00:43 +08:00
if (msg.startsWith("/me "))
{
msg = msg.right(msg.length() - 4);
emit sendAction(group->groupId, msg);
} else {
emit sendMessage(group->groupId, msg);
}
2014-06-25 04:11:11 +08:00
}
void GroupChatForm::onUserListChanged()
{
2014-11-02 20:38:00 +08:00
nusersLabel->setText(tr("%1 users in chat").arg(group->nPeers));
QLayoutItem *child;
while ((child = namesListLayout->takeAt(0)))
{
child->widget()->hide();
delete child->widget();
delete child;
}
QStringList names(group->peers.values());
2014-11-02 20:38:00 +08:00
unsigned nNames = names.size();
for (unsigned i=0; i<nNames; ++i)
{
QString nameStr = names[i];
2014-11-02 20:38:00 +08:00
if (i!=nNames-1)
nameStr+=", ";
QLabel* nameLabel = new QLabel(nameStr);
nameLabel->setObjectName("peersLabel");
namesListLayout->addWidget(nameLabel);
}
2014-06-25 04:11:11 +08:00
}
2014-09-26 00:03:25 +08:00
void GroupChatForm::dragEnterEvent(QDragEnterEvent *ev)
{
if (ev->mimeData()->hasFormat("friend"))
ev->acceptProposedAction();
}
void GroupChatForm::dropEvent(QDropEvent *ev)
{
if (ev->mimeData()->hasFormat("friend"))
{
int friendId = ev->mimeData()->data("friend").toInt();
Core::getInstance()->groupInviteFriend(friendId, group->groupId);
}
}
2014-11-13 20:11:23 +08:00
void GroupChatForm::onMicMuteToggle()
{
if (audioInputFlag == true)
{
if (micButton->objectName() == "red")
{
Core::getInstance()->enableGroupCallMic(group->groupId);
micButton->setObjectName("green");
}
else
{
Core::getInstance()->disableGroupCallMic(group->groupId);
micButton->setObjectName("red");
}
Style::repolish(micButton);
}
}
void GroupChatForm::onVolMuteToggle()
{
if (audioOutputFlag == true)
{
if (volButton->objectName() == "red")
{
Core::getInstance()->enableGroupCallVol(group->groupId);
volButton->setObjectName("green");
}
else
{
Core::getInstance()->disableGroupCallVol(group->groupId);
volButton->setObjectName("red");
}
Style::repolish(volButton);
}
}
void GroupChatForm::onCallClicked()
{
if (!inCall)
{
Core::getInstance()->joinGroupCall(group->groupId);
audioInputFlag = true;
audioOutputFlag = true;
callButton->setObjectName("red");
callButton->style()->polish(callButton);
inCall = true;
}
else
{
Core::getInstance()->leaveGroupCall(group->groupId);
audioInputFlag = false;
audioOutputFlag = false;
micButton->setObjectName("green");
micButton->style()->polish(micButton);
volButton->setObjectName("green");
volButton->style()->polish(volButton);
callButton->setObjectName("green");
callButton->style()->polish(callButton);
inCall = false;
}
}
void GroupChatForm::keyPressEvent(QKeyEvent* ev)
{
if (msgEdit->hasFocus())
return;
// Push to talk
if (ev->key() == Qt::Key_P && inCall)
{
Core* core = Core::getInstance();
if (!core->isGroupCallMicEnabled(group->groupId))
{
core->enableGroupCallMic(group->groupId);
micButton->setObjectName("green");
micButton->style()->polish(micButton);
Style::repolish(micButton);
}
}
}
void GroupChatForm::keyReleaseEvent(QKeyEvent* ev)
{
if (msgEdit->hasFocus())
return;
// Push to talk
if (ev->key() == Qt::Key_P && inCall)
{
Core* core = Core::getInstance();
if (core->isGroupCallMicEnabled(group->groupId))
{
core->disableGroupCallMic(group->groupId);
micButton->setObjectName("red");
micButton->style()->polish(micButton);
Style::repolish(micButton);
}
}
}