matrixion/src/ActiveCallBar.cpp

163 lines
5 KiB
C++
Raw Normal View History

2020-07-23 04:15:45 +03:00
#include <cstdio>
#include <QDateTime>
2020-07-11 02:19:48 +03:00
#include <QHBoxLayout>
#include <QIcon>
#include <QLabel>
#include <QString>
2020-07-23 04:15:45 +03:00
#include <QTimer>
2020-07-11 02:19:48 +03:00
#include "ActiveCallBar.h"
2020-07-23 04:15:45 +03:00
#include "ChatPage.h"
#include "Utils.h"
2020-07-11 02:19:48 +03:00
#include "WebRTCSession.h"
2020-07-23 04:15:45 +03:00
#include "ui/Avatar.h"
2020-07-11 02:19:48 +03:00
#include "ui/FlatButton.h"
ActiveCallBar::ActiveCallBar(QWidget *parent)
: QWidget(parent)
{
setAutoFillBackground(true);
auto p = palette();
2020-07-23 04:15:45 +03:00
p.setColor(backgroundRole(), QColorConstants::Svg::limegreen);
2020-07-11 02:19:48 +03:00
setPalette(p);
QFont f;
f.setPointSizeF(f.pointSizeF());
const int fontHeight = QFontMetrics(f).height();
const int widgetMargin = fontHeight / 3;
const int contentHeight = fontHeight * 3;
setFixedHeight(contentHeight + widgetMargin);
2020-07-23 04:15:45 +03:00
layout_ = new QHBoxLayout(this);
layout_->setSpacing(widgetMargin);
layout_->setContentsMargins(
2020-07-11 02:19:48 +03:00
2 * widgetMargin, widgetMargin, 2 * widgetMargin, widgetMargin);
QFont labelFont;
2020-07-23 04:15:45 +03:00
labelFont.setPointSizeF(labelFont.pointSizeF() * 1.1);
2020-07-11 02:19:48 +03:00
labelFont.setWeight(QFont::Medium);
2020-07-23 04:15:45 +03:00
avatar_ = new Avatar(this, QFontMetrics(f).height() * 2.5);
2020-07-11 02:19:48 +03:00
callPartyLabel_ = new QLabel(this);
callPartyLabel_->setFont(labelFont);
2020-07-23 04:15:45 +03:00
stateLabel_ = new QLabel(this);
stateLabel_->setFont(labelFont);
durationLabel_ = new QLabel(this);
durationLabel_->setFont(labelFont);
durationLabel_->hide();
2020-07-11 02:19:48 +03:00
muteBtn_ = new FlatButton(this);
2020-07-23 04:15:45 +03:00
setMuteIcon(false);
2020-07-11 02:19:48 +03:00
muteBtn_->setFixedSize(buttonSize_, buttonSize_);
muteBtn_->setCornerRadius(buttonSize_ / 2);
2020-07-23 04:15:45 +03:00
connect(muteBtn_, &FlatButton::clicked, this, [this](){
if (WebRTCSession::instance().toggleMuteAudioSrc(muted_))
setMuteIcon(muted_);
2020-07-11 02:19:48 +03:00
});
2020-07-23 04:15:45 +03:00
layout_->addWidget(avatar_, 0, Qt::AlignLeft);
layout_->addWidget(callPartyLabel_, 0, Qt::AlignLeft);
layout_->addWidget(stateLabel_, 0, Qt::AlignLeft);
layout_->addWidget(durationLabel_, 0, Qt::AlignLeft);
layout_->addStretch();
layout_->addWidget(muteBtn_, 0, Qt::AlignCenter);
layout_->addSpacing(18);
timer_ = new QTimer(this);
connect(timer_, &QTimer::timeout, this,
[this](){
auto seconds = QDateTime::currentSecsSinceEpoch() - callStartTime_;
int s = seconds % 60;
int m = (seconds / 60) % 60;
int h = seconds / 3600;
char buf[12];
if (h)
snprintf(buf, sizeof(buf), "%.2d:%.2d:%.2d", h, m, s);
else
snprintf(buf, sizeof(buf), "%.2d:%.2d", m, s);
durationLabel_->setText(buf);
});
connect(&WebRTCSession::instance(), &WebRTCSession::stateChanged, this, &ActiveCallBar::update);
}
void
ActiveCallBar::setMuteIcon(bool muted)
{
QIcon icon;
if (muted) {
muteBtn_->setToolTip("Unmute Mic");
icon.addFile(":/icons/icons/ui/microphone-unmute.png");
} else {
muteBtn_->setToolTip("Mute Mic");
icon.addFile(":/icons/icons/ui/microphone-mute.png");
}
muteBtn_->setIcon(icon);
muteBtn_->setIconSize(QSize(buttonSize_, buttonSize_));
2020-07-11 02:19:48 +03:00
}
void
2020-07-23 04:15:45 +03:00
ActiveCallBar::setCallParty(
const QString &userid,
const QString &displayName,
const QString &roomName,
const QString &avatarUrl)
2020-07-11 02:19:48 +03:00
{
2020-07-24 00:02:50 +03:00
callPartyLabel_->setText(" " +
2020-07-23 04:15:45 +03:00
(displayName.isEmpty() ? userid : displayName) + " -");
if (!avatarUrl.isEmpty())
avatar_->setImage(avatarUrl);
2020-07-11 02:19:48 +03:00
else
2020-07-23 04:15:45 +03:00
avatar_->setLetter(utils::firstChar(roomName));
}
void
ActiveCallBar::update(WebRTCSession::State state)
{
switch (state) {
case WebRTCSession::State::INITIATING:
2020-07-26 17:59:50 +03:00
show();
2020-07-23 04:15:45 +03:00
stateLabel_->setText("Initiating call...");
break;
case WebRTCSession::State::INITIATED:
2020-07-26 17:59:50 +03:00
show();
2020-07-23 04:15:45 +03:00
stateLabel_->setText("Call initiated...");
break;
case WebRTCSession::State::OFFERSENT:
2020-07-26 17:59:50 +03:00
show();
2020-07-23 04:15:45 +03:00
stateLabel_->setText("Calling...");
break;
case WebRTCSession::State::CONNECTING:
2020-07-26 17:59:50 +03:00
show();
2020-07-23 04:15:45 +03:00
stateLabel_->setText("Connecting...");
break;
case WebRTCSession::State::CONNECTED:
2020-07-26 17:59:50 +03:00
show();
2020-07-23 04:15:45 +03:00
callStartTime_ = QDateTime::currentSecsSinceEpoch();
timer_->start(1000);
2020-07-24 00:02:50 +03:00
stateLabel_->setText("Voice call:");
2020-07-23 04:15:45 +03:00
durationLabel_->setText("00:00");
durationLabel_->show();
break;
2020-07-26 17:59:50 +03:00
case WebRTCSession::State::ICEFAILED:
2020-07-23 04:15:45 +03:00
case WebRTCSession::State::DISCONNECTED:
2020-07-26 17:59:50 +03:00
hide();
2020-07-23 04:15:45 +03:00
timer_->stop();
callPartyLabel_->setText(QString());
stateLabel_->setText(QString());
durationLabel_->setText(QString());
durationLabel_->hide();
setMuteIcon(false);
break;
default:
break;
}
2020-07-11 02:19:48 +03:00
}