void DlgMaterialsManager::on_pushButtonAdd_clicked()
{
    stuffData();
    bool ok;
    QString sMatID = QInputDialog::getText(this, tr("Material ID"),
                                         tr("Enter a short unique Identifier for this Material:"), QLineEdit::Normal,
                                         "MaterialID", &ok);
    if (!(ok && !sMatID.isEmpty())) return;
    if(ui->comboBoxMaterial->findText(sMatID,Qt::MatchFixedString)!=-1){
        QMessageBox::warning(this,tr("Duplicate Material ID!"),sMatID+" already exists in the Catalog.  Please enter a unique ID.",QMessageBox::Ok);
        return;
    }

    QString sDecrip = QInputDialog::getText(this, tr("Material ID"),
                                             tr("Enter a short, unique Identifier for this Material:"), QLineEdit::Normal,
                                             "Material Description", &ok);
    if (!ok) return;

    m_pCatalog->addMaterial(sMatID, sDecrip);
    m_bLoading=true;
    ui->comboBoxMaterial->addItem(m_pCatalog->getMaterialLabel(m_pCatalog->getMaterialCount()-1));
    ui->comboBoxMaterial->setCurrentIndex(m_pCatalog->getMaterialCount());
    m_bLoading=false;
    fillData(m_pCatalog->getMaterialCount()-1,m_pCatalog->getCurXYIndex());
}
void DlgMaterialsManager::on_comboBoxXY_currentIndexChanged(int index)
{
    if(index<0)return;
    if(m_pCatalog->getMaterialCount()<1)return;
    stuffData();
    fillData(m_pCatalog->getCurMatIndex(), index);
}
void DlgMaterialsManager::on_comboBoxMaterial_currentIndexChanged(int index)
{
    if(index<0)return;
    if(m_pCatalog->getMaterialCount()<1)return;

    ui->lineEditDescription->setText(m_pCatalog->getMaterialDescription(index));
    stuffData();
    fillData(index, m_pCatalog->getCurXYIndex());
    if(m_pCatalog->getMaterialCount()<1){
        m_pCatalog->setCurMatIndex(-1);
        ui->groupBox_ExposureSettings->setEnabled(false);
        ui->pushButtonDuplicate->setEnabled(false);
        ui->pushButtonDelete->setEnabled(false);
        ui->doubleSpinBox->setEnabled(false);
        ui->spinBoxNumberOfAttachLayers->setEnabled(false);
    }
    else if(m_pCatalog->isFactoryEntry(m_pCatalog->getCurMatIndex())){
        ui->groupBox_ExposureSettings->setEnabled(true);
        ui->tableWidget->setEnabled(false);
        ui->pushButtonDuplicate->setEnabled(true);
        ui->pushButtonDelete->setEnabled(false);
        ui->doubleSpinBox->setEnabled(false);
        ui->spinBoxNumberOfAttachLayers->setEnabled(false);
    }
    else
    {
        ui->groupBox_ExposureSettings->setEnabled(true);
        ui->tableWidget->setEnabled(true);
        ui->pushButtonDuplicate->setEnabled(true);
        ui->pushButtonDelete->setEnabled(true);
        ui->doubleSpinBox->setEnabled(true);
        ui->spinBoxNumberOfAttachLayers->setEnabled(true);
    }
}
void DlgMaterialsManager::on_buttonBoxSaveCancel_accepted()
{
    stuffData();
    m_pCatalog->save();
    QSettings settings;
    settings.setValue("CurrentMaterialLabel",ui->comboBoxMaterial->currentText());
    settings.setValue("CurrentXYLabel",ui->comboBoxXY->currentText());
}
void DlgMaterialsManager::on_pushButtonDuplicate_clicked()
{
    stuffData();
    QString sID = m_pCatalog->getMaterialLabel(ui->comboBoxMaterial->currentIndex())+"-Duplicate";
    bool ok;
    QString sMatID = QInputDialog::getText(this, tr("Material ID"),
                                         tr("Enter a short, unique Identifier for this Material:"), QLineEdit::Normal,
                                         sID, &ok);
    if (!(ok && !sMatID.isEmpty())) return;
    if(ui->comboBoxMaterial->findText(sMatID,Qt::MatchFixedString)!=-1){
        QMessageBox::warning(this,tr("Duplicate Material ID!"),sMatID+" already exists in the Catalog.  Please enter a unique ID.",QMessageBox::Ok);
        return;
    }

    if(sMatID=="DELETEFACTORY"){
        if(QMessageBox::warning(this, tr("Delete Factory Material"),tr("Are you SURE you wish to delete this factory material from the Catalog?"),QMessageBox::Yes|QMessageBox::No)==QMessageBox::No)return;
        m_bLoading=true;
        int iC = m_pCatalog->getCurMatIndex();
        m_pCatalog->deleteMaterial(iC);
        m_pCatalog->setCurMatIndex(0);
        ui->comboBoxMaterial->setCurrentIndex(0);
        ui->comboBoxMaterial->removeItem(iC);
        m_bLoading=false;
        fillData(0,m_pCatalog->getCurXYIndex());
        return;
    }

    QString sDecrip = QInputDialog::getText(this, tr("Material ID"),
                                             tr("Enter a short unique Identifier for this Material:"), QLineEdit::Normal,
                                             m_pCatalog->getMaterialDescription(ui->comboBoxMaterial->currentIndex()), &ok);
        if (!ok) return;


    m_pCatalog->addDupMat(sMatID, sDecrip, ui->comboBoxMaterial->currentIndex());
    m_bLoading=true;
    ui->comboBoxMaterial->addItem(m_pCatalog->getMaterialLabel(m_pCatalog->getMaterialCount()-1));
    ui->comboBoxMaterial->setCurrentIndex(m_pCatalog->getMaterialCount()-1);
    updateEnabledStates();
    m_bLoading=false;
    fillData(m_pCatalog->getMaterialCount()-1,m_pCatalog->getCurXYIndex());
}
u16  prepareForSend(u8 * bufSrc, u16 nSize, u8 flags, u8 cmd, u8 nrLong)
{
    u8 * pBuf = bufSrc;
    u16 lenTemp = nSize;
	u16 index = 0;
	//u8 res = 0;
    cmd_info wrap;
	if((flags & 0x01) == 0)
	  {
		u8  temp = SIZE_WRAP_BLOCKID + SIZE_WRAP_CMD + SIZE_WRAP_AUX + SIZE_WRAP_LEN;
		if(nSize)
		   memmove(&bufSrc[temp],bufSrc,nSize);
	  }
    /* Insert UID of the block*/
    *(u32*)pBuf = config.idBlock;
    pBuf     += SIZE_WRAP_BLOCKID;
    lenTemp  += SIZE_WRAP_BLOCKID;
    /* Insert CMD of the answer */
	wrap.cmd = cmd;
    *pBuf  = wrap.cmd;
    pBuf     += SIZE_WRAP_CMD;
    lenTemp  += SIZE_WRAP_CMD;

    /* Insert AUX byte of the answer*/
	wrap.aux.reply  = AUX_SELF_INTV;
	if(flags & 0x01)
	   wrap.aux.reply  = AUX_REPLY;
	wrap.aux.cypher = AUX_OPENED;
	wrap.aux.longPack = AUX_SHORT;
    if(nrLong)
	  {
	   wrap.aux.longPack = AUX_LONG;
	   memmove(&bufSrc[9],&bufSrc[8],nSize);
	   bufSrc[8] = nrLong;
	   nSize += 1;
	   lenTemp += 1;
	  }
    *pBuf  = *(u8 *)&wrap.aux;
    pBuf     += SIZE_WRAP_AUX;
    lenTemp  += SIZE_WRAP_AUX;
    /* Insert Length of the block of pure data (without cmd, id, crc ...)*/
	wrap.len = nSize;
    *(u16*)pBuf  = wrap.len;
    pBuf     += SIZE_WRAP_LEN;
    lenTemp  += SIZE_WRAP_LEN;
   /* Add CRC to the trail of the packet, crc is calculating on whole packet*/
    wrap.crc = calcBlock(bufSrc, lenTemp);
    //wrap.crc = 0x10040502;
    //*(u32*)&bufSrc[lenTemp]  = wrap.crc;
	//bufSrc += temp + nSize;
	pBuf     += nSize;
	
    *(u32*)pBuf  = wrap.crc;

    //bufSrc   += sizeof(wrap.crc);
    lenTemp  += SIZE_WRAP_CRC;
	/* Stuffing and so on*/
    lenTemp = stuffData(bufSrc, lenTemp);
    memmove(&bufSrc[1],&bufSrc[0],lenTemp);
    bufSrc[0] = 0x40;
    lenTemp++;
    bufSrc[lenTemp++] = 0x24;
	/* Here comes check on max length of SIM900 packet(1360 bytes)*/
	while(lenTemp > (MAX_SIZE_PACKET_GPRS_SIM900))
	  {
	    /* put handled packet into the send packet buffer*/
	  	fifoPacketPut(&fifoNaviPack,&bufRxOperGSM[index], (MAX_SIZE_PACKET_GPRS_SIM900),0);
		lenTemp -= (MAX_SIZE_PACKET_GPRS_SIM900 );
		index += ( MAX_SIZE_PACKET_GPRS_SIM900 );
	  }
	/* put handled packet into the send packet buffer*/
	if(!(flags & 0x02))
		fifoPacketPut(&fifoNaviPack,&bufRxOperGSM[index], lenTemp,0);
	else if(flags & 0x02)
		saveSDInfo(&bufRxOperGSM[index], lenTemp, SD_NOTSEND, SD_TYPE_MSG_GSM);
//    if(innerState.flagDebug)
//	 {
//	  USBWriteStr("cmd:");
//	  USBDebWrite(wrap.cmd);
//	  USBWriteStr("\r\n");
//	 }
    return lenTemp;
}