/*! * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * \copyright \n * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, \n * IN PART OR IN WHOLE, \n * WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n * * \file guicontroller.cpp * \date 2019/09/30 * \author Behrouz NematiPour * */ #include "guicontroller.h" // Qt #include #include // Project #include "logger.h" #include "applicationcontroller.h" // namespace using namespace Gui; /*! * \brief GuiController::GuiController * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ GuiController::GuiController(QObject *parent) : QObject(parent) {} /*! * \brief GuiController::init * \details Initialized the Class by calling the init() method first * And initializes the thread vThread by calling initThread * on success init(). * \param vThread - the thread * \return returns the return value of the init() method */ bool GuiController::init(QThread &vThread) { if ( ! init() ) return false; initThread(vThread); return true; } /*! * \brief GuiController::quit * \details quits the class * Calls quitThread */ void GuiController::quit() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. quitThread(); // validated } // coco end /*! * \brief GuiController::initConnections * \details Initializes the required signal/slot connection between this class and other objects * to be able to communicate. */ void GuiController::initConnections() { // From HD/DG connect(&_ApplicationController, SIGNAL(didActionReceive (GuiActionType, const QVariantList &)), this , SLOT( onActionReceive (GuiActionType, const QVariantList &))); // From OS : USB Drive has been removed physically. connect(&_ApplicationController, SIGNAL(didUSBDriveMount ()), this , SLOT( onUSBDriveMount ())); connect(&_ApplicationController, SIGNAL(didUSBDriveRemove()), this , SLOT( onUSBDriveRemove())); connect(&_ApplicationController, SIGNAL(didExport()), this , SLOT( onExport())); connect(&_ApplicationController, SIGNAL(didFailedTransmit(Sequence)), this , SLOT(didFailedTransmit(Sequence))); // ---- Signal/Slots ACTION_RECEIVE_MODEL_BRIDGE_CONNECTIONS(_ApplicationController) } /*! * \brief ApplicationController::initThread * \details Moves this object into the thread vThread. * And checks that this method is called from main thread. * Also connects quitThread to application aboutToQuit. * \param vThread - the thread */ void GuiController::initThread(QThread &vThread) { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. // runs in main thread Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); _thread = &vThread; _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); _thread->start(); moveToThread(_thread); } // coco end /*! * \brief GuiController::quitThread * \details Moves this object to main thread to be handled by QApplicaiton * And to be destroyed there. */ void GuiController::quitThread() { // coco begin validated: Application termination is not correctly done in coco!!! // it has been tested and works perfectly fine in normal run. if ( ! _thread ) return; // runs in thread moveToThread(qApp->thread()); // validated } // coco end /*! * \brief GuiController initializer */ bool GuiController::init() { if ( _init ) return false; _init = true; initConnections(); LOG_EVENT(QObject::tr("%1 Initialized").arg(metaObject()->className())); return true; } /*! * \brief An Action has been requested * \details This method Confirmed that if the action is accepted or not, * Regarding the current state and the action. * These actions are only user actions and there is only one user interaction, * So no need to capture from which screen this action comes since we have the current state. * Sometimes GuiController requires to investigate with the ApplicationController to get approval from HD device. * \param vAction - User requested Action * \param vData - Message data to be transmitted. */ void GuiController::doActionTransmit(GuiActionType vAction, const QVariantList &vData) { // coco begin validated: This is a sample code and currently does nothing // The handleTransmit is a place holder and currently has not been used. if (! handleTransmit(vAction, vData)) { // coco end emit didActionTransmit(vAction, vData); } } /*! * \brief GuiController::handleTransmit * \details If an action request from Gui can be handled in Gui Controller * without passing to HD, then can be handled here. * \param vAction - the Requested action * \param vData - Data of the action * \return if handled returns true to not to pass to the lower level (Application Controller) * to not to send to HD then. */ bool GuiController::handleTransmit(GuiActionType vAction, const QVariantList &vData) { // coco begin validated: This is a sample code and currently does nothing Q_UNUSED(vAction) Q_UNUSED(vData) // This is an example implementation of how to handle actions // which does not require HD approval in GuiController // Process the GuiView Request. // It can be processed in GuiController take action and notify GuiView switch (vAction) { case GuiActionType::PowerOff: //qApp->quit(); // GUI Controller decides (loop back) //if (vData == GuiActionData::NoData){ // // PowerOff noData is a request // emit didActionReceive (vAction, GuiActionData::Accepted); // return true; //} break; //case Another_Command_Which_Doesn't_Require_HD_Approval: //return true; //break; default: break; } return false; } // coco end /*! * \brief Action commanded by HD * \details An action has been commanded by HD, * Gui requires to be notified to perform the action. * \param vAction - Message Action * \param vData - The data to be translated. */ void GuiController::onActionReceive (GuiActionType vAction, const QVariantList &vData) { // Process the command and notify GuiView // Process ... emit didActionReceive (vAction, vData); } /*! * \brief GuiController::onUSBDriveMount * \details emits didUSBDriveMount signal to notify other classes (GuiView) * , the USB drive has been mounted. */ void GuiController::onUSBDriveMount() { // coco begin validated: This needs user interaction to plug-in USB device // has been tested manually emit didUSBDriveMount(); } // coco end /*! * \brief GuiController::doUSBDriveUmount * \details emits didUSBDriveUmount signal to notify other classes (GuiView) * , the USB drive has been unmounted. */ void GuiController::doUSBDriveUmount() { // coco begin validated: This needs user interaction to plug-out the USB device // has been tested manually emit didUSBDriveUmount(); } // coco end /*! * \brief GuiController::onUSBDriveRemove * \details emits didUSBDriveRemove signal to notify other classes (GuiView) * , the USB drive has been removed. */ void GuiController::onUSBDriveRemove() { // coco begin validated: This needs user interaction to plug-out the USB device // has been tested manually emit didUSBDriveRemove(); } // coco end /*! * \brief GuiController::onExport * \details The slot which will be called to notify the export is done * by emitting the didExport signal. */ void GuiController::onExport() { // coco begin validated: This needs user interaction to export to USB device // has been tested manually emit didExport(); } // coco end /*! * \brief GuiController::doExportLog * \details emits didExportLog signal to notify other classes (ApplicationController) * , the User requested to export the log. */ void GuiController::doExportLog() { // coco begin validated: This needs user interaction to export to USB device // has been tested manually emit didExportLog(); } // coco end /*! * \brief GuiController::didFailedTransmit * Raises an HD communiation timeout alarm if communication with HD is lost. * \param seq - Sequence that failed to transmit */ void GuiController::didFailedTransmit(Sequence seq) { Q_UNUSED(seq); AlarmStatusData data; data.mState = GuiAlarmPriority::ALARM_PRIORITY_HIGH; // Alarm priority data.mTop = GuiAlarmID::ALARM_ID_HD_COMM_TIMEOUT; // Alarm ID data.mMuteTimeout = 0; // Alarm timeout data.mEscalatesIn = 0; // Alarm Escalate Time Period data.mFlags = QBitArray(16, false); // 16 QBitArray of flags, if needed emit didActionReceive(data); }