#include "danmu_display.h"
#include <QLabel>
#include "ui_danmu_window.h"
DanmuDisplay::DanmuDisplay(Danmuku *parent)
: Module("danmu_display", {}, parent) {
config = nullptr;
updateFollowersCountTimer = nullptr;
// Module UI
QHBoxLayout *layout = new QHBoxLayout(widget); // deleted by QT
btn_startDisplay =
new QPushButton(tr("弹幕显示"), widget); // deleted by QT
btn_startDisplay->setEnabled(false);
connect(btn_startDisplay, SIGNAL(clicked()), this, SLOT(startDisplay()));
layout->addWidget(btn_startDisplay);
btn_startPanel = new QPushButton(tr("配置"), widget); // deleted by QT
btn_startPanel->setEnabled(false);
connect(btn_startPanel, SIGNAL(clicked()), this, SLOT(startPanel()));
layout->addWidget(btn_startPanel);
btn_toggleLockPosition =
new QPushButton(tr("锁定窗口"), widget); // deleted by QT
btn_toggleLockPosition->setEnabled(false);
connect(btn_toggleLockPosition, SIGNAL(clicked()), this,
SLOT(toggleLockPosition()));
layout->addWidget(btn_toggleLockPosition);
Module *moduleLiveRoom = getModule("live_room");
connect(moduleLiveRoom, SIGNAL(started()), this, SLOT(enable()));
connect(moduleLiveRoom, SIGNAL(stopped()), this, SLOT(disable()));
}
DanmuDisplay::~DanmuDisplay() {
qDebug("Enter ~DanmuDisplay");
stop();
qDebug("Exit ~DanmuDisplay");
}
// slots
void DanmuDisplay::enable() { btn_startDisplay->setEnabled(true); }
void DanmuDisplay::disable() {
btn_startDisplay->setEnabled(false);
stop();
}
void DanmuDisplay::startDisplay() {
if (!window.isNull()) {
stop();
return;
}
Q_ASSERT(window == nullptr);
window = new DanmuWindow(); // deleted in stop()
Q_ASSERT(danmuLoader == nullptr);
danmuLoader =
new DanmuLoader(window->ui->list_danmu, this); // deleted in stop()
Q_ASSERT(config == nullptr);
config = new DanmuConfig(this); // deleted in stop()
connect(config, SIGNAL(changed()), this, SLOT(applyConfig()));
btn_toggleLockPosition->setEnabled(true);
setLockPosition(config->lockPosition);
btn_startPanel->setEnabled(true);
applyConfig();
danmuLoader->start();
// Followers count
Module *moduleLiveRoom = getModule("live_room");
connect(moduleLiveRoom, SIGNAL(followersCountUpdated(const int &)), this,
SLOT(updateFollowersCount(const int &)));
QMetaObject::invokeMethod(moduleLiveRoom, "updateFollowersCount");
Q_ASSERT(updateFollowersCountTimer == nullptr);
updateFollowersCountTimer = new QTimer(this); // deleted in stop()
connect(updateFollowersCountTimer, SIGNAL(timeout()), moduleLiveRoom,
SLOT(updateFollowersCount()));
updateFollowersCountTimer->start(UPDATE_FOLLOWERS_COUNT_INTERVAL_MS);
QObject *protocal;
QMetaObject::invokeMethod(moduleLiveRoom, "getProtocal",
Q_RETURN_ARG(QObject *, protocal));
connect(protocal, SIGNAL(viewersCountUpdated(const int &)), this,
SLOT(updateViewersCount(const int &)));
connect(protocal,
SIGNAL(recvDanmu(const int, const QString &, const QString &,
const bool, const bool, const int)),
this,
SLOT(recvDanmu(const int, const QString &, const QString &,
const bool, const bool, const int)));
connect(
protocal,
SIGNAL(
recvGift(const int, const QString &, const QString &, const int)),
this,
SLOT(recvGift(const int, const QString &, const QString &, const int)));
}
void DanmuDisplay::startPanel() {
if (!panel.isNull()) {
panel->deleteLater();
return;
}
Q_ASSERT(panel == nullptr);
panel = new DanmuPanel(config); // deleted in stop()
connect(panel, &DanmuPanel::testDanmu, this, &DanmuDisplay::recvDanmu);
connect(panel, &DanmuPanel::testGift, this, &DanmuDisplay::recvGift);
}
void DanmuDisplay::stop() {
qDebug("Enter stop");
Module *moduleLiveRoom = getModule("live_room");
if (moduleLiveRoom != nullptr) {
disconnect(moduleLiveRoom, SIGNAL(followersCountUpdated(const int &)),
this, SLOT(updateFollowersCount(const int &)));
}
if (updateFollowersCountTimer != nullptr) {
updateFollowersCountTimer->stop();
updateFollowersCountTimer->deleteLater();
updateFollowersCountTimer = nullptr;
}
if (!danmuLoader.isNull()) {
danmuLoader->deleteLater();
}
if (!giftLoader.isNull()) {
giftLoader->deleteLater();
}
if (!window.isNull()) {
window->deleteLater();
}
if (!panel.isNull()) {
panel->deleteLater();
}
if (config != nullptr) {
config->deleteLater();
config = nullptr;
}
btn_startPanel->setEnabled(false);
btn_toggleLockPosition->setEnabled(false);
qDebug("Exit stop");
}
void DanmuDisplay::recvDanmu(const int uid, const QString &username,
const QString &text, const bool isAdmin,
const bool isVIP, const int userGuardLevel) {
qDebug() << "Display danmu: " << uid << username << text << isAdmin << isVIP
<< userGuardLevel;
QString prefix = "";
QString suffix = "";
if (isVIP) {
suffix.append("<span style=\"color:yellow\">[VIP]</span>");
}
if (isAdmin) {
suffix.append("<span style=\"color:yellow\">[房]</span>");
}
QLabel *label =
new QLabel(danmuContentFormat.arg(prefix, username, suffix, text),
window->ui->list_danmu); // deleted by QT
label->setFont(config->font);
label->setFixedWidth(window->ui->list_danmu->width());
label->setWordWrap(true);
QListWidgetItem *item =
new QListWidgetItem(window->ui->list_danmu); // deleted by QT
window->ui->list_danmu->addItem(item);
window->ui->list_danmu->setItemWidget(item, label);
item->setSizeHint(label->sizeHint());
danmuLoader->loadItem(item);
}
void DanmuDisplay::recvGift(const int uid, const QString &username,
const QString &giftName, const int giftCount) {
if (giftLoader.isNull()) {
return;
}
qDebug() << "Display gift:" << uid << username << giftName << giftCount;
QLabel *label = new QLabel(
giftContentFormat.arg(username, giftName, QString::number(giftCount)));
label->setFont(config->font);
label->setFixedWidth(window->ui->list_gift->width());
label->setWordWrap(true);
QListWidgetItem *item =
new QListWidgetItem(window->ui->list_gift); // deleted by QT
window->ui->list_gift->addItem(item);
window->ui->list_gift->setItemWidget(item, label);
item->setSizeHint(label->sizeHint());
giftLoader->loadItem(item);
}
void DanmuDisplay::updateViewersCount(const int &viewersCount) {
window->ui->label_viewersCount->setText(QString::number(viewersCount));
qDebug() << "Update viewers count (display):" << viewersCount;
}
void DanmuDisplay::updateFollowersCount(const int &followersCount) {
window->ui->label_followersCount->setText(QString::number(followersCount));
qDebug() << "Update followers count (display):" << followersCount;
}
void DanmuDisplay::applyConfig() {
danmuContentFormat = QString(
"%1<span style=\"color:%2\">%3</span>%4: <span "
"style=\"color:%5\">%6</span>")
.arg("%1", config->usernameColor.name(), "%2",
"%3", config->contentColor.name(), "%4");
giftContentFormat = QString(
"<span style=\"color:%1\">%2</span> 送出了礼物 "
"<span style=\"color:%3\">%4</span> x%5")
.arg(config->usernameColor.name(), "%1",
config->contentColor.name(), "%2",
没有合适的资源?快使用搜索试试~ 我知道了~
一个基于 [Qt 6]的 Bilibili 直播弹幕库.zip
共55个文件
h:17个
cpp:13个
lib:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 172 浏览量
2024-04-26
16:31:38
上传
评论
收藏 771KB ZIP 举报
温馨提示
基于QT的系统
资源推荐
资源详情
资源评论
收起资源包目录
一个基于 [Qt 6]的 Bilibili 直播弹幕库.zip (55个子文件)
bilibili-live-danmuku-main
.github
workflows
windows-mingw.yml 2KB
windows-msvc.yml 2KB
src
danmuku.h 611B
bilibili-live-danmuku_zh_CN.ts 5KB
modules
danmu_display
danmu_config.h 1KB
danmu_panel.cpp 7KB
danmu_config.cpp 3KB
danmu_loader.h 1KB
danmu_window.h 731B
danmu_window.cpp 3KB
stylesheet
danmu_window.qss 50B
stylesheet.qrc 126B
danmu_display.h 2KB
danmu_loader.cpp 3KB
danmu_panel.h 1KB
ui
danmu_panel.ui 18KB
danmu_window.ui 4KB
danmu_display.cpp 10KB
live_room
live_room.h 863B
protocal.h 2KB
live_room.cpp 3KB
protocal.cpp 9KB
global.h 205B
utils
byte_convert.cpp 868B
network.cpp 647B
decompress.cpp 2KB
network.h 192B
decompress.h 274B
byte_convert.h 322B
main.cpp 584B
stylesheet
danmuku.qss 66B
stylesheet.qrc 99B
danmuku.cpp 2KB
module.cpp 675B
ui
danmuku.ui 789B
module.h 598B
3rdparty
include
brotli
port.h 10KB
types.h 3KB
decode.h 14KB
encode.h 17KB
lib
brotlienc.lib 4KB
brotlidec-static.lib 76KB
pkgconfig
libbrotlienc.pc 271B
libbrotlicommon.pc 244B
libbrotlidec.pc 271B
brotlidec.lib 4KB
brotlicommon-static.lib 130KB
brotlienc-static.lib 769KB
brotlicommon.lib 3KB
bin
brotlienc.dll 568KB
brotlicommon.dll 134KB
brotlidec.dll 46KB
bilibili-live-danmuku.pro 2KB
scripts
windows-mingw-package.ps1 908B
windows-msvc-package.ps1 2KB
共 55 条
- 1
资源评论
我慢慢地也过来了
- 粉丝: 6338
- 资源: 3975
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功