From f22f91e2e63409aa39e94c31dcb36d9ada23c991 Mon Sep 17 00:00:00 2001 From: Fidel Yin Date: Sat, 9 Apr 2022 12:29:05 +0800 Subject: [PATCH] update qss and fix unhandled heartbeat reply --- bilibili-live-danmuku.pro | 1 + src/danmuku.cpp | 18 +++++++++++++++--- src/main.cpp | 12 ------------ src/module.cpp | 12 ++++++------ src/module.h | 8 +++++--- src/modules/danmu_display/danmu_display.cpp | 2 +- src/modules/danmu_display/danmu_display.h | 2 +- src/modules/danmu_display/danmu_window.cpp | 14 ++++++++++++++ .../danmu_display/stylesheet/danmu_window.qss} | 0 .../danmu_display/stylesheet/stylesheet.qrc | 5 +++++ src/modules/live_room/live_room.cpp | 15 ++++++++------- src/modules/live_room/live_room.h | 2 +- src/modules/live_room/protocal.cpp | 4 ++++ src/stylesheet/danmuku.qss | 7 +++++++ src/stylesheet/stylesheet.qrc | 2 +- 15 files changed, 69 insertions(+), 35 deletions(-) rename src/{stylesheet/display.qss => modules/danmu_display/stylesheet/danmu_window.qss} (100%) create mode 100644 src/modules/danmu_display/stylesheet/stylesheet.qrc create mode 100644 src/stylesheet/danmuku.qss diff --git a/bilibili-live-danmuku.pro b/bilibili-live-danmuku.pro index 5df3e8f..ee1a30c 100644 --- a/bilibili-live-danmuku.pro +++ b/bilibili-live-danmuku.pro @@ -57,6 +57,7 @@ LIBS += \ -L$$PWD/3rdparty/lib -lbrotlidec RESOURCES += \ + src/modules/danmu_display/stylesheet/stylesheet.qrc \ src/stylesheet/stylesheet.qrc TRANSLATIONS += \ diff --git a/src/danmuku.cpp b/src/danmuku.cpp index be0c72f..ef362c0 100644 --- a/src/danmuku.cpp +++ b/src/danmuku.cpp @@ -1,5 +1,7 @@ #include "danmuku.h" +#include +#include #include #include "module.h" @@ -19,9 +21,19 @@ Danmuku::Danmuku(QWidget *parent) layout_modules = new QVBoxLayout(ui->centralWidget); // deleted by QT - Module::danmuku = this; - loadModule(new LiveRoom()); // deleted in unloadModule - loadModule(new DanmuDisplay()); // deleted in unloadModule + loadModule(new LiveRoom(this)); // deleted in unloadModule + loadModule(new DanmuDisplay(this)); // deleted in unloadModule + + // Qss + QFile qss(":/stylesheet/danmuku.qss"); + if (qss.open(QFile::ReadOnly)) { + qDebug("QSS loaded."); + QString stylesheet = QLatin1String(qss.readAll()); + setStyleSheet(stylesheet); + qss.close(); + } else { + qWarning("Failed to load QSS stylesheet (danmuku.qss)."); + } } Danmuku::~Danmuku() { diff --git a/src/main.cpp b/src/main.cpp index 48c6f36..b785e6b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,5 @@ #include -#include #include -#include #include #include "danmuku.h" @@ -20,16 +18,6 @@ int main(int argc, char *argv[]) { } } - QFile qss(":/stylesheet/display.qss"); - if (qss.open(QFile::ReadOnly)) { - qDebug("QSS loaded."); - QString stylesheet = QLatin1String(qss.readAll()); - a.setStyleSheet(stylesheet); - qss.close(); - } else { - qWarning("Failed to load QSS stylesheet (display.qss)."); - } - Danmuku w; w.show(); return a.exec(); diff --git a/src/module.cpp b/src/module.cpp index 4ebe305..5b460e9 100644 --- a/src/module.cpp +++ b/src/module.cpp @@ -4,15 +4,14 @@ #include "danmuku.h" -Danmuku *Module::danmuku = nullptr; - -Module::Module(const QString &name, const QList &dependencies) - : QObject(danmuku) { +Module::Module(const QString &name, const QList &dependencies, + Danmuku *parent) + : QObject(parent) { qDebug() << "Loading module:" << name; moduleMetadata.name = name; moduleMetadata.dependencies = dependencies; - widget = new QWidget(danmuku); // deleted by QT + widget = new QWidget(parent); // deleted by QT // TODO check dependencies } @@ -20,6 +19,7 @@ Module::~Module() { qDebug() << "Unloading module:" << moduleMetadata.name; } QWidget *Module::getWidget() const { return widget; } +// for communication with other modules Module *Module::getModule(const QString &name) { - return danmuku->getModule(name); + return ((Danmuku *)parent())->getModule(name); } \ No newline at end of file diff --git a/src/module.h b/src/module.h index af52812..5d14024 100644 --- a/src/module.h +++ b/src/module.h @@ -16,17 +16,19 @@ class Module : public QObject { Q_OBJECT public: - Module(const QString &name, const QList &dependencies = {}); + Module(const QString &name, const QList &dependencies, + Danmuku *parent); ~Module(); QWidget *getWidget() const; + protected: + Module *getModule(const QString &name); + public: ModuleMetadata moduleMetadata; - static Danmuku *danmuku; protected: QWidget *widget; - static Module *getModule(const QString &name); }; #define MODULE diff --git a/src/modules/danmu_display/danmu_display.cpp b/src/modules/danmu_display/danmu_display.cpp index 46903a5..96efbec 100644 --- a/src/modules/danmu_display/danmu_display.cpp +++ b/src/modules/danmu_display/danmu_display.cpp @@ -4,7 +4,7 @@ #include "ui_danmu_window.h" -DanmuDisplay::DanmuDisplay() : Module("danmu_display") { +DanmuDisplay::DanmuDisplay(Danmuku *parent) : Module("danmu_display", {}, parent) { config = nullptr; updateFollowersCountTimer = nullptr; diff --git a/src/modules/danmu_display/danmu_display.h b/src/modules/danmu_display/danmu_display.h index 062f1a9..8c23479 100644 --- a/src/modules/danmu_display/danmu_display.h +++ b/src/modules/danmu_display/danmu_display.h @@ -20,7 +20,7 @@ class DanmuDisplay : public Module { MODULE public: - DanmuDisplay(); + DanmuDisplay(Danmuku *parent); ~DanmuDisplay(); private: diff --git a/src/modules/danmu_display/danmu_window.cpp b/src/modules/danmu_display/danmu_window.cpp index 0ae0593..e7ca3d8 100644 --- a/src/modules/danmu_display/danmu_window.cpp +++ b/src/modules/danmu_display/danmu_window.cpp @@ -2,11 +2,14 @@ #include +#include #include #include "ui_danmu_window.h" +#ifdef Q_CC_MSVC #pragma comment(lib, "user32.lib") +#endif DanmuWindow::DanmuWindow(QWidget *parent) : QWidget(parent), ui(new Ui::DanmuWindow) { // deleted in ~DanmuWindow @@ -27,6 +30,17 @@ DanmuWindow::DanmuWindow(QWidget *parent) ui->list_danmu->setAttribute(Qt::WA_TransparentForMouseEvents); ui->list_gift->setAttribute(Qt::WA_TransparentForMouseEvents); + // Qss + QFile qss(":/modules/danmu_display/stylesheet/danmu_window.qss"); + if (qss.open(QFile::ReadOnly)) { + qDebug("QSS loaded."); + QString stylesheet = QLatin1String(qss.readAll()); + setStyleSheet(stylesheet); + qss.close(); + } else { + qWarning("Failed to load QSS stylesheet (danmu_window.qss)."); + } + show(); } diff --git a/src/stylesheet/display.qss b/src/modules/danmu_display/stylesheet/danmu_window.qss similarity index 100% rename from src/stylesheet/display.qss rename to src/modules/danmu_display/stylesheet/danmu_window.qss diff --git a/src/modules/danmu_display/stylesheet/stylesheet.qrc b/src/modules/danmu_display/stylesheet/stylesheet.qrc new file mode 100644 index 0000000..0a75f39 --- /dev/null +++ b/src/modules/danmu_display/stylesheet/stylesheet.qrc @@ -0,0 +1,5 @@ + + + danmu_window.qss + + \ No newline at end of file diff --git a/src/modules/live_room/live_room.cpp b/src/modules/live_room/live_room.cpp index 71889b2..44d1226 100644 --- a/src/modules/live_room/live_room.cpp +++ b/src/modules/live_room/live_room.cpp @@ -4,13 +4,14 @@ #include "utils/network.h" -LiveRoom::LiveRoom() : Module("live_room") { +LiveRoom::LiveRoom(Danmuku *parent) : Module("live_room", {}, parent) { protocal = nullptr; // Control UI QHBoxLayout *layout = new QHBoxLayout(widget); // deleted by QT input_roomID = new QLineEdit(widget); // deleted by QT + input_roomID->setPlaceholderText(tr("直播间号")); #ifdef QT_DEBUG input_roomID->setText("14003442"); // debug #endif @@ -76,9 +77,7 @@ void LiveRoom::stop() { qDebug("Exit stop"); } -QObject *LiveRoom::getProtocal() { - return protocal; -} +QObject *LiveRoom::getProtocal() { return protocal; } QJsonObject LiveRoom::requestDanmuInfo() { QJsonObject response = requestJsonResponse( @@ -113,7 +112,9 @@ void LiveRoom::updateFollowersCount() { qWarning("Error occured in followersCountUpdated."); return; } - qDebug() << "followersCountUpdated:" - << response["data"][(QString) "follower"].toInt(); - emit followersCountUpdated(response["data"][(QString) "follower"].toInt()); + qDebug() + << "followersCountUpdated:" + << response[QStringLiteral("data")][QStringLiteral("follower")].toInt(); + emit followersCountUpdated( + response[QStringLiteral("data")][QStringLiteral("follower")].toInt()); } \ No newline at end of file diff --git a/src/modules/live_room/live_room.h b/src/modules/live_room/live_room.h index 6dec239..d83cbd9 100644 --- a/src/modules/live_room/live_room.h +++ b/src/modules/live_room/live_room.h @@ -15,7 +15,7 @@ class LiveRoom : public Module { MODULE public: - LiveRoom(); + LiveRoom(Danmuku* parent); ~LiveRoom(); private: diff --git a/src/modules/live_room/protocal.cpp b/src/modules/live_room/protocal.cpp index 28d0be7..0d8195d 100644 --- a/src/modules/live_room/protocal.cpp +++ b/src/modules/live_room/protocal.cpp @@ -147,6 +147,10 @@ void Protocal::recvData(const QByteArray& data) { while (pack.length() != 0) { qDebug() << "pack:" << pack; + if (pack == "[object Object]") { // heartbeat reply + break; + } + head = pack.left(WS_PACKAGE_HEADER_TOTAL_LENGTH); int packLen = bytesToInt32(head.mid(WS_PACKAGE_OFFSET, 4)); diff --git a/src/stylesheet/danmuku.qss b/src/stylesheet/danmuku.qss new file mode 100644 index 0000000..2e6e0a2 --- /dev/null +++ b/src/stylesheet/danmuku.qss @@ -0,0 +1,7 @@ +QPushButton { + padding: 5px; +} + +QLineEdit { + padding: 4px; +} \ No newline at end of file diff --git a/src/stylesheet/stylesheet.qrc b/src/stylesheet/stylesheet.qrc index 593ac07..cb2c17c 100644 --- a/src/stylesheet/stylesheet.qrc +++ b/src/stylesheet/stylesheet.qrc @@ -1,5 +1,5 @@ - display.qss + danmuku.qss \ No newline at end of file