From 453b584d86b17c0495439fed8e22ba608f8c0edb Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 26 Jul 2021 22:48:42 -0500 Subject: [PATCH] Cleanup connection status check and reset SlideView on disconnect --- mainwindow.cpp | 37 +++++++++++++++---------------------- mainwindow.hpp | 6 ++---- openlpclient.cpp | 6 +++++- slideview.cpp | 11 +++++++++-- slideview.hpp | 2 ++ 5 files changed, 33 insertions(+), 29 deletions(-) diff --git a/mainwindow.cpp b/mainwindow.cpp index a6792f6..0600282 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -5,6 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #include #include #include @@ -58,6 +59,7 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) { connect(&m_openlpClient, &OpenLPClient::pollUpdate, slideView, &SlideView::pollUpdate); connect(&m_openlpClient, &OpenLPClient::songListUpdate, slideView, &SlideView::songListUpdate); connect(&m_openlpClient, &OpenLPClient::slideListUpdate, slideView, &SlideView::slideListUpdate); + connect(&m_openlpClient, &OpenLPClient::pollFailed, slideView, &SlideView::reset); connect(slideView, &SlideView::songChanged, &m_openlpClient, &OpenLPClient::changeSong); connect(slideView, &SlideView::slideChanged, &m_openlpClient, &OpenLPClient::changeSlide); // setup scene selector @@ -72,14 +74,9 @@ MainWindow::MainWindow(QWidget *parent): QMainWindow(parent) { connect(btnObsShowSlides, &QPushButton::clicked, &m_obsClient, &OBSClient::showSlides); connect(btnObsShowSlides, &QPushButton::clicked, &m_openlpClient, &OpenLPClient::showSlides); // setup status bar - auto statusBar = new QStatusBar(this); - setStatusBar(statusBar); + setStatusBar(new QStatusBar(this)); connect(&m_openlpClient, &OpenLPClient::pollUpdate, this, &MainWindow::openLpConnectionInit); - connect(&m_openlpClient, &OpenLPClient::pollUpdate, [this] { ++m_openLPUpdates; }); connect(&m_obsClient, &OBSClient::pollUpdate, this, &MainWindow::obsConnectionInit); - connect(&m_obsClient, &OBSClient::pollUpdate, [this] { ++m_obsUpdates; }); - m_statusBarTimer.start(5000); - connect(&m_statusBarTimer, &QTimer::timeout, this, &MainWindow::refreshStatusBar); refreshStatusBar(); } @@ -88,38 +85,34 @@ MainWindow::~MainWindow() { void MainWindow::openLpConnectionInit() { disconnect(&m_openlpClient, &OpenLPClient::pollUpdate, this, &MainWindow::openLpConnectionInit); - ++m_openLPUpdates; + connect(&m_openlpClient, &OpenLPClient::pollFailed, this, &MainWindow::openLpConnectionLost); + m_openLpConnected = true; refreshStatusBar(); } void MainWindow::openLpConnectionLost() { + disconnect(&m_openlpClient, &OpenLPClient::pollFailed, this, &MainWindow::openLpConnectionLost); connect(&m_openlpClient, &OpenLPClient::pollUpdate, this, &MainWindow::openLpConnectionInit); + m_openLpConnected = false; + refreshStatusBar(); } void MainWindow::obsConnectionInit() { disconnect(&m_obsClient, &OBSClient::pollUpdate, this, &MainWindow::obsConnectionInit); - ++m_obsUpdates; + connect(&m_obsClient, &OBSClient::pollFailed, this, &MainWindow::obsConnectionLost); + m_obsConnected = true; refreshStatusBar(); } void MainWindow::obsConnectionLost() { + disconnect(&m_obsClient, &OBSClient::pollFailed, this, &MainWindow::obsConnectionLost); connect(&m_obsClient, &OBSClient::pollUpdate, this, &MainWindow::obsConnectionInit); + m_obsConnected = false; + refreshStatusBar(); } void MainWindow::refreshStatusBar() { - QString openLpStatus; - QString obsStatus; - if (m_openLPUpdates > 0) { - openLpStatus = tr("OpenLP: Connected"); - } else { - openLpStatus = tr("OpenLP: Not Connected"); - } - if (m_obsUpdates > 0) { - obsStatus = tr("OBS: Connected"); - } else { - obsStatus = tr("OBS: Not Connected"); - } - m_openLPUpdates = 0; - m_obsUpdates = 0; + const auto openLpStatus = m_openLpConnected ? tr("OpenLP: Connected") : tr("OpenLP: Not Connected"); + const auto obsStatus = m_obsConnected ? tr("OBS: Connected") : tr("OBS: Not Connected"); statusBar()->showMessage(openLpStatus + " | " + obsStatus); } diff --git a/mainwindow.hpp b/mainwindow.hpp index 78bad07..0a2d20a 100644 --- a/mainwindow.hpp +++ b/mainwindow.hpp @@ -10,7 +10,6 @@ #include #include -#include #include "obsclient.hpp" #include "openlpclient.hpp" @@ -21,9 +20,8 @@ class MainWindow: public QMainWindow { private: OBSClient m_obsClient; OpenLPClient m_openlpClient; - QTimer m_statusBarTimer; - uint64_t m_openLPUpdates = 0; - uint64_t m_obsUpdates = 0; + bool m_openLpConnected = false; + bool m_obsConnected = false; public: MainWindow(QWidget *parent = nullptr); diff --git a/openlpclient.cpp b/openlpclient.cpp index fec2795..6652fef 100644 --- a/openlpclient.cpp +++ b/openlpclient.cpp @@ -5,6 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #include #include #include @@ -15,7 +16,7 @@ OpenLPClient::OpenLPClient(QObject *parent): QObject(parent) { poll(); - m_pollTimer.start(500); + m_pollTimer.start(250); connect(&m_pollTimer, &QTimer::timeout, this, &OpenLPClient::poll); connect(m_nam, &QNetworkAccessManager::finished, this, &OpenLPClient::handleGeneralResponse); connect(m_songListNam, &QNetworkAccessManager::finished, this, &OpenLPClient::handleSongListResponse); @@ -93,6 +94,9 @@ void OpenLPClient::handlePollResponse(QNetworkReply *reply) { if (reply->error()) { qDebug() << reply->errorString(); emit pollFailed(); + m_currentServiceId = -1; + m_currentSongId = ""; + m_songNameMap.clear(); return; } auto data = reply->readAll(); diff --git a/slideview.cpp b/slideview.cpp index d7941bb..223cdae 100644 --- a/slideview.cpp +++ b/slideview.cpp @@ -5,6 +5,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #include #include #include @@ -23,8 +24,6 @@ SlideView::SlideView(QWidget *parent): QWidget(parent) { m_slideTable->setSelectionBehavior(QTableWidget::SelectionBehavior::SelectRows); m_slideTable->setSelectionMode(QTableWidget::SelectionMode::SingleSelection); m_slideTable->setColumnCount(1); - connect(header, &QHeaderView::sectionResized, m_slideTable, &QTableWidget::resizeRowsToContents); - m_slideTable->resizeRowsToContents(); m_slideTable->verticalHeader()->setDefaultSectionSize(300); #ifndef _WIN32 m_slideTable->setAlternatingRowColors(true); @@ -61,6 +60,14 @@ void SlideView::slideListUpdate(QStringList tagList, QStringList slideList) { m_slideTable->setItem(i, 0, item); } m_slideTable->setVerticalHeaderLabels(tagList); + m_slideTable->resizeRowsToContents(); +} + +void SlideView::reset() { + m_songSelector->clear(); + m_slideTable->setRowCount(0); + m_currentSong = ""; + m_currentSlide = -1; } void SlideView::songListUpdate(QStringList songList) { diff --git a/slideview.hpp b/slideview.hpp index 54a7661..95b4fbe 100644 --- a/slideview.hpp +++ b/slideview.hpp @@ -27,6 +27,8 @@ class SlideView : public QWidget void slideListUpdate(QStringList tagList, QStringList songList); + void reset(); + private slots: void changeSong(int song);