BDHServerController::BDHServerController(QObject *parent)
    :QObject(parent)
{
    m_dataStorage = new SQLiteDatabase(this);
    m_ahpServer = new AHP::AHPServer(this,this);
    m_dataAcquisition = new DataAcquisition(this);
    m_realtimeCache = new RealtimeCache(this);
    m_collectorInterface = nullptr;
    m_configurationLoader = new XmlLoader(this);

    m_alarmCounter = 0;
    m_myIP = selectHostAddress().toString();
    m_myPort = 9999;

    connect(this,SIGNAL(newVariablePoint(quint32,quint8,qulonglong,QVariant)),m_realtimeCache,SLOT(storeVariablePoint(quint32,quint8,quint64,QVariant)));

    //! Acquisition Simulation
    connect(m_dataAcquisition,SIGNAL(incommingAlarmPoint(quint32,AlarmState,quint64)),m_dataStorage,SLOT(storeIncommingAlarmPoint(quint32,AlarmState,quint64)));
    connect(m_dataAcquisition,SIGNAL(incommingVariablePoint(quint32,quint8,quint64,QVariant)),m_realtimeCache,SLOT(storeVariablePoint(quint32,quint8,quint64,QVariant)));

    //! Connection between RealtimeCache and SQLiteDatabase to persist data
    connect(m_realtimeCache,SIGNAL(persistData(quint32,quint8,quint64,QVariant)),m_dataStorage,SLOT(storeIncommingVariablePoint(quint32,quint8,quint64,QVariant)));

    //! Configuration connects
    connect(m_configurationLoader,SIGNAL(loadFinished()),SLOT(configurationLoadFinish()));

    connect(m_configurationLoader,SIGNAL(newCollector(QXmlAttributes)),this,SLOT(addConfigurationCollector(QXmlAttributes)));
    connect(m_configurationLoader,SIGNAL(newVariable(QXmlAttributes)),this,SLOT(addConfigurationVariable(QXmlAttributes)));
    connect(m_configurationLoader,SIGNAL(newAlarm(QXmlAttributes)),this,SLOT(addConfigurationAlarm(QXmlAttributes)));
    connect(m_configurationLoader,SIGNAL(newBDH(QXmlAttributes)),this,SLOT(addConfigurationBDH(QXmlAttributes)));
}
void UBExportDocumentSetAdaptor::persist(UBDocumentProxy* pDocumentProxy)
{
    QModelIndex treeViewParentIndex;
    UBPersistenceManager *persistenceManager = UBPersistenceManager::persistenceManager();
    UBDocumentTreeModel *treeModel = persistenceManager->mDocumentTreeStructureModel;
    QString filename;

    if (pDocumentProxy) {
        treeViewParentIndex = treeModel->indexForProxy(pDocumentProxy);
        if (!treeViewParentIndex.isValid()) {
            qDebug() << "failed to export";
            UBApplication::showMessage(tr("Failed to export..."));
            return;
        }
        filename = askForFileName(pDocumentProxy, tr("Export as UBX File"));

    } else {
        treeViewParentIndex = UBApplication::documentController->firstSelectedTreeIndex();
        if (!treeViewParentIndex.isValid()) {
            qDebug() << "failed to export";
            UBApplication::showMessage(tr("Failed to export..."));
            return;
        }

        UBDocumentTreeNode* node = treeModel->nodeFromIndex(treeViewParentIndex);
        UBDocumentProxy proxy;
        proxy.setMetaData(UBSettings::documentName,node->displayName());
        filename = askForFileName(&proxy, tr("Export as UBX File"));
    }

    if (filename.length() > 0)
    {
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));

        if (mIsVerbose)
            UBApplication::showMessage(tr("Exporting document..."));

         if (persistData(treeViewParentIndex, filename)) {
             if (mIsVerbose) {
                 UBApplication::showMessage(tr("Export successful."));
             }
         } else {
             if (mIsVerbose) {
                 UBApplication::showMessage(tr("Export failed."));
             }
         }

         QApplication::restoreOverrideCursor();
    }
}
void UBTeacherGuidePageZeroWidget::switchToMode(tUBTGZeroPageMode mode)
{
    if (mode == tUBTGZeroPageMode_EDITION) {
        QString inputStyleSheet("QTextEdit { background: white; border-radius: 10px; border: 2px;}");
        mpModePushButton->hide();
        mpSessionTitle->setReadOnly(false);
        mpSessionTitle->managePlaceholder(true);
        mpSessionTitle->setStyleSheet(inputStyleSheet);
        QFont titleFont(QApplication::font().family(), 11, -1);
        mpSessionTitle->document()->setDefaultFont(titleFont);
        mpAuthors->setReadOnly(false);
        mpAuthors->managePlaceholder(false);
        mpAuthors->setStyleSheet(inputStyleSheet);
        mpObjectives->setReadOnly(false);
        mpObjectives->managePlaceholder(false);
        mpObjectives->setStyleSheet(inputStyleSheet);
        mpKeywords->setReadOnly(false);
        mpKeywords->managePlaceholder(false);
        mpKeywords->setStyleSheet(inputStyleSheet);
        mpSchoolLevelValueLabel->hide();
        mpSchoolLevelBox->show();
        mpSchoolSubjectsValueLabel->hide();
        mpSchoolSubjectsBox->show();
        mpSchoolTypeValueLabel->hide();
        mpSchoolTypeBox->show();
        mpLicenceIcon->hide();
        mpLicenceValueLabel->hide();
        mpLicenceBox->show();
    }
    else {
        QString inputStyleSheet( "QTextEdit { background: transparent; border: none;}");
        mpModePushButton->show();
        mpSessionTitle->showText(mpSessionTitle->text());
        mpSessionTitle->setStyleSheet(inputStyleSheet);
        updateSceneTitle();
        QFont titleFont(QApplication::font().family(), 14, 1);
        mpSessionTitle->document()->setDefaultFont(titleFont);
        mpAuthors->setStyleSheet(inputStyleSheet);
        mpAuthors->setTextColor(QColor(Qt::black));
        mpAuthors->showText(mpAuthors->text());
        mpObjectives->setStyleSheet(inputStyleSheet);
        mpObjectives->setTextColor(QColor(Qt::black));
        mpObjectives->showText(mpObjectives->text());
        mpKeywords->setStyleSheet(inputStyleSheet);
        mpKeywords->setTextColor(QColor(Qt::black));
        mpKeywords->showText(mpKeywords->text());
        mpSchoolLevelValueLabel->setText(mpSchoolLevelBox->currentText());
        mpSchoolLevelValueLabel->show();
        mpSchoolLevelBox->hide();
        mpSchoolSubjectsValueLabel->setText(mpSchoolSubjectsBox->currentText());
        mpSchoolSubjectsValueLabel->show();
        mpSchoolSubjectsBox->hide();
        mpSchoolTypeValueLabel->setText(mpSchoolTypeBox->currentText());
        mpSchoolTypeValueLabel->show();
        mpSchoolTypeBox->hide();
        mpLicenceValueLabel->setText(mpLicenceBox->currentText());
        QString licenceIconPath = mpLicenceBox->itemData(mpLicenceBox->currentIndex()).toString();
        if (!licenceIconPath.isEmpty()) {
            mpLicenceIcon->setPixmap(QPixmap(licenceIconPath));
            mpLicenceIcon->show();
        }
        mpLicenceValueLabel->show();
        mpLicenceBox->hide();
        persistData();
    }
    update();
}
void UBTeacherGuidePageZeroWidget::hideEvent(QHideEvent * event)
{
    persistData();
    QWidget::hideEvent(event);
}