diff --git a/src/blackgui/components/infobarwebreadersstatuscomponent.cpp b/src/blackgui/components/infobarwebreadersstatuscomponent.cpp new file mode 100644 index 000000000..2d84c09f4 --- /dev/null +++ b/src/blackgui/components/infobarwebreadersstatuscomponent.cpp @@ -0,0 +1,140 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "infobarwebreadersstatuscomponent.h" +#include "ui_infobarwebreadersstatuscomponent.h" +#include "blackcore/webreaderflags.h" +#include "blackmisc/icons.h" + +using namespace BlackGui; +using namespace BlackMisc; +using namespace BlackCore; +using namespace BlackMisc::Network; + +namespace BlackGui +{ + namespace Components + { + CInfoBarWebReadersStatusComponent::CInfoBarWebReadersStatusComponent(QWidget *parent) : + QFrame(parent), ui(new Ui::CInfoBarWebReadersStatusComponent) + { + ui->setupUi(this); + this->initLeds(); + connect(&m_timer, &QTimer::timeout, this, &CInfoBarWebReadersStatusComponent::ps_checkServerAndData); + m_timer.setInterval(30 * 1000); + m_timer.start(); + m_timer.setObjectName("CInfoBarWebReadersStatusComponent::CheckSwiftDbTimer"); + } + + CInfoBarWebReadersStatusComponent::~CInfoBarWebReadersStatusComponent() + { + m_timer.stop(); + } + + void CInfoBarWebReadersStatusComponent::initLeds() + { + CLedWidget::LedShape shape = CLedWidget::Circle; + this->ui->led_DataReady->setValues(CLedWidget::Yellow, CLedWidget::Black, shape, "all data ready", "data missing", 14); + + this->ui->led_IcaoAircraft->setValues(CLedWidget::Yellow, CLedWidget::Black, CLedWidget::Red, shape, "reading", "idle", "failed", 14); + this->ui->led_IcaoAirline->setValues(CLedWidget::Yellow, CLedWidget::Black, CLedWidget::Red, shape, "reading", "idle", "failed", 14); + this->ui->led_Countries->setValues(CLedWidget::Yellow, CLedWidget::Black, CLedWidget::Red, shape, "reading", "idle", "failed", 14); + + this->ui->led_Models->setValues(CLedWidget::Yellow, CLedWidget::Black, CLedWidget::Red, shape, "reading", "idle", "failed", 14); + this->ui->led_Liveries->setValues(CLedWidget::Yellow, CLedWidget::Black, CLedWidget::Red, shape, "reading", "idle", "failed", 14); + this->ui->led_Distributors->setValues(CLedWidget::Yellow, CLedWidget::Black, CLedWidget::Red, shape, "reading", "idle", "failed", 14); + } + + void CInfoBarWebReadersStatusComponent::setProvider(IWebDataServicesProvider *webDataReaderProvider) + { + CWebDataServicesAware::setProvider(webDataReaderProvider); + connectSwiftDatabaseSignals( + this, + std::bind(&CInfoBarWebReadersStatusComponent::ps_dataRead, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) + ); + } + + void CInfoBarWebReadersStatusComponent::ps_dataRead(CDbFlags::Entity entity, CDbFlags::ReadState readState, int count) + { + if (readState == CDbFlags::ReadFinished) + { + bool swift = CWebReaderFlags::isFromSwiftDb(entity); + if (swift && count > 0) + { + // avoids unnecessary checks + this->ui->led_SwiftDb->setOn(true); + this->m_timer.start(); // restart + } + } + + QList leds = this->entityToLeds(entity); + if (!leds.isEmpty()) { this->setLedReadStates(leds, readState); } + } + + void CInfoBarWebReadersStatusComponent::ps_checkServerAndData() + { + bool swift = + this->hasProvider() && + this->canConnectSwiftDb(); + this->ui->led_SwiftDb->setOn(swift); + + bool allData = hasAllData(); + this->ui->led_DataReady->setOn(allData); + } + + void CInfoBarWebReadersStatusComponent::setLedReadStates(const QList &leds, CDbFlags::ReadState readState) + { + for (CLedWidget *led : leds) + { + setLedReadState(led, readState); + } + } + + void CInfoBarWebReadersStatusComponent::setLedReadState(CLedWidget *led, CDbFlags::ReadState readState) + { + Q_ASSERT_X(led, Q_FUNC_INFO, "no LED"); + int blinkTime = 2.5 * 1000; + switch (readState) + { + case CDbFlags::ReadFinished: + led->setOn(true, blinkTime); + break; + case CDbFlags::StartRead: + led->setOn(true); + break; + case CDbFlags::ReadFailed: + led->setTriState(2 * blinkTime); + break; + } + } + + QList CInfoBarWebReadersStatusComponent::entityToLeds(CDbFlags::Entity entity) const + { + QList leds; + if (entity.testFlag(CDbFlags::CountryEntity)) { leds << ui->led_Countries; } + if (entity.testFlag(CDbFlags::DistributorEntity)) { leds << ui->led_Distributors; } + if (entity.testFlag(CDbFlags::AircraftIcaoEntity)) { leds << ui->led_IcaoAircraft; } + if (entity.testFlag(CDbFlags::AirlineIcaoEntity)) { leds << ui->led_IcaoAirline; } + if (entity.testFlag(CDbFlags::LiveryEntity)) { leds << ui->led_Liveries; } + if (entity.testFlag(CDbFlags::ModelEntity)) { leds << ui->led_Models; } + return leds; + } + + bool CInfoBarWebReadersStatusComponent::hasAllData() const + { + if (!hasProvider()) { return false; } + return getAirlineIcaoCodesCount() > 0 && + getAircraftIcaoCodesCount() > 0 && + getDistributorsCount() > 0 && + getModelsCount() > 0 && + getLiveriesCount() > 0; + } + + } // namespace +} // namespace diff --git a/src/blackgui/components/infobarwebreadersstatuscomponent.h b/src/blackgui/components/infobarwebreadersstatuscomponent.h new file mode 100644 index 000000000..a6faf64af --- /dev/null +++ b/src/blackgui/components/infobarwebreadersstatuscomponent.h @@ -0,0 +1,72 @@ +/* Copyright (C) 2015 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKGUI_INFOBARWEBREADERSSTATUSCOMPONENT_H +#define BLACKGUI_INFOBARWEBREADERSSTATUSCOMPONENT_H + +#include "blackgui/blackguiexport.h" +#include "blackgui/led.h" +#include "blackmisc/network/webdataservicesprovider.h" +#include +#include +#include + +namespace Ui { class CInfoBarWebReadersStatusComponent; } +namespace BlackGui +{ + namespace Components + { + //! Info bar displaying status of web readers(swift DB, ...) + class BLACKGUI_EXPORT CInfoBarWebReadersStatusComponent : + public QFrame, + public BlackMisc::Network::CWebDataServicesAware + { + Q_OBJECT + + public: + //! Constructor + explicit CInfoBarWebReadersStatusComponent(QWidget *parent = nullptr); + + //!Destructor + ~CInfoBarWebReadersStatusComponent(); + + //! Init the LEDs + void initLeds(); + + //! Set the provider + virtual void setProvider(BlackMisc::Network::IWebDataServicesProvider *webDataReaderProvider) override; + + private slots: + //! Data have been read + void ps_dataRead(BlackMisc::Network::CDbFlags::Entity entity, BlackMisc::Network::CDbFlags::ReadState readState, int count); + + //! Check server status + void ps_checkServerAndData(); + + private: + QScopedPointer ui; + QTimer m_timer { this }; + + //! Set LED states + void setLedReadStates(const QList &leds, BlackMisc::Network::CDbFlags::ReadState readState); + + //! Set the LED read state + void setLedReadState(CLedWidget *led, BlackMisc::Network::CDbFlags::ReadState readState); + + //! Maps entity to its id + QList entityToLeds(BlackMisc::Network::CDbFlags::Entity entity) const; + + //! All data read + bool hasAllData() const; + }; + } +} +#endif // guard diff --git a/src/blackgui/components/infobarwebreadersstatuscomponent.ui b/src/blackgui/components/infobarwebreadersstatuscomponent.ui new file mode 100644 index 000000000..b676bba75 --- /dev/null +++ b/src/blackgui/components/infobarwebreadersstatuscomponent.ui @@ -0,0 +1,169 @@ + + + CInfoBarWebReadersStatusComponent + + + + 0 + 0 + 632 + 22 + + + + Frame + + + QFrame::StyledPanel + + + QFrame::Raised + + + + 10 + + + 0 + + + 0 + + + 0 + + + + + swift DB + + + + + + + + + + Mappings ready + + + data + + + 6 + + + + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 20 + 20 + + + + + + + + loading: + + + + + + + models + + + + + + + + + + | liveries + + + + + + + + + + | distributors + + + + + + + + + + | aircraft + + + + + + + + + + | airlines + + + + + + + + + + | countries + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + BlackGui::CLedWidget + QWidget +
blackgui/led.h
+ 1 +
+
+ + +