matrixion/src/ActiveCallBar.cpp

161 lines
5.3 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);
2020-08-01 21:31:10 +03:00
layout_->setContentsMargins(2 * widgetMargin, widgetMargin, 2 * widgetMargin, widgetMargin);
2020-07-11 02:19:48 +03:00
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-08-01 21:31:10 +03:00
connect(muteBtn_, &FlatButton::clicked, this, [this]() {
2020-07-23 04:15:45 +03:00
if (WebRTCSession::instance().toggleMuteAudioSrc(muted_))
2020-08-01 21:31:10 +03:00
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);
2020-08-01 21:31:10 +03:00
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);
2020-07-23 04:15:45 +03:00
});
2020-08-01 21:31:10 +03:00
connect(
&WebRTCSession::instance(), &WebRTCSession::stateChanged, this, &ActiveCallBar::update);
2020-07-23 04:15:45 +03:00
}
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-08-01 21:31:10 +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-08-01 21:31:10 +03:00
callPartyLabel_->setText(" " + (displayName.isEmpty() ? userid : displayName) + " ");
2020-07-23 04:15:45 +03:00
if (!avatarUrl.isEmpty())
2020-08-01 21:31:10 +03:00
avatar_->setImage(avatarUrl);
2020-07-11 02:19:48 +03:00
else
2020-08-01 21:31:10 +03:00
avatar_->setLetter(utils::firstChar(roomName));
2020-07-23 04:15:45 +03:00
}
void
ActiveCallBar::update(WebRTCSession::State state)
{
switch (state) {
2020-08-01 21:31:10 +03:00
case WebRTCSession::State::INITIATING:
show();
stateLabel_->setText("Initiating call...");
break;
case WebRTCSession::State::INITIATED:
show();
stateLabel_->setText("Call initiated...");
break;
case WebRTCSession::State::OFFERSENT:
show();
stateLabel_->setText("Calling...");
break;
case WebRTCSession::State::CONNECTING:
show();
stateLabel_->setText("Connecting...");
break;
case WebRTCSession::State::CONNECTED:
show();
callStartTime_ = QDateTime::currentSecsSinceEpoch();
timer_->start(1000);
stateLabel_->setPixmap(
QIcon(":/icons/icons/ui/place-call.png").pixmap(QSize(buttonSize_, buttonSize_)));
durationLabel_->setText("00:00");
durationLabel_->show();
break;
case WebRTCSession::State::ICEFAILED:
case WebRTCSession::State::DISCONNECTED:
hide();
timer_->stop();
callPartyLabel_->setText(QString());
stateLabel_->setText(QString());
durationLabel_->setText(QString());
durationLabel_->hide();
setMuteIcon(false);
break;
default:
break;
2020-07-23 04:15:45 +03:00
}
2020-07-11 02:19:48 +03:00
}