ThothWindow::ThothWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ThothWindow)
{
    ui->setupUi(this);
    // ALERT! Use the following code to get keyboard focus at your OpenGL widget
    ui->contextGL->setFocusPolicy(Qt::StrongFocus);
    ui->contextGL->setFocus();
    message = new QLabel(this);
    popUp = new QLabel(this);
    mode = new QLabel(this);
    picked = new QLabel(this);

    ui->statusBar->addWidget(popUp);
    ui->statusBar->addPermanentWidget(picked);
    ui->statusBar->addPermanentWidget(mode);
    ui->statusBar->addPermanentWidget(message);
    mode->setText("Selecting");
    message->setText("No model selected");
    ui->statusBar->showMessage("Ready", 5000);
    //Tree file.

    //treeview of tabs
    TreeFile *m_build = new TreeFile(ui->treeViewBuild, "Build");
    m_buildModel = m_build->getQFileSystemModel();
    TreeFile *m_decorate = new TreeFile(ui->treeViewDecorate, "Decorate");
    m_decorateModel = m_decorate->getQFileSystemModel();
    TreeFile *m_furnish = new TreeFile(ui->treeViewFurnish, "Furnish");
    m_furnishModel = m_furnish->getQFileSystemModel();
    connect(ui->actionSave_project, SIGNAL(triggered()), this, SLOT(actionSave_project_triggered()));
    QTimer *tim = new QTimer(this);
    connect(tim, SIGNAL(timeout()), ui->contextGL, SLOT(updateGL()));
    tim->start(TICK_PER_SECOND);
    connect(tim, SIGNAL(timeout()), ui->contextGL, SLOT(checkActiveModel()));
    connect(ui->SldR, SIGNAL(valueChanged(int)), SLOT(onColorChanged()));
    connect(ui->SldG, SIGNAL(valueChanged(int)), SLOT(onColorChanged()));
    connect(ui->SldB, SIGNAL(valueChanged(int)), SLOT(onColorChanged()));
    onColorChanged();
    connect(CScenary::getInstance(), SIGNAL(setNameModel(string)), SLOT(getModelName(string)));
    connect(RenderManager::GetInstance(), SIGNAL(sendChangeTab()), SLOT(changeTab()));
    RenderManager::GetInstance()->GetRenderMode(EDITOR_2D)->SetCameraProjection(INIT_WIDTH, INIT_HEIGHT);
    connect(this, SIGNAL(changeMouseMove(bool)), RenderManager::GetInstance()->GetRenderMode(EXPLORER), SLOT(setMouseMove(bool)));
    connect(this, SIGNAL(changeCursor(Qt::CursorShape)), ui->contextGL, SLOT(changeCursor(Qt::CursorShape)));
    connect(ui->ColorButton, SIGNAL(clicked(bool)), SLOT(on_colorButton_clicked()));
    connect(this, SIGNAL(newModel(ModelInfo, int)), RenderManager::GetInstance()->GetRenderMode(EXPLORER), SLOT(changeModelTexture(ModelInfo, int)));
    connect(ui->actionOpen_project, SIGNAL(triggered()), this, SLOT(actionOpen_project_triggered()));
    connect(CScenary::getInstance(), SIGNAL(setNameModel(string)), SLOT(getModelName(string)));
    connect(RenderManager::GetInstance(), SIGNAL(sendChangeTab()), SLOT(changeTab()));
    connect(CScenary::getInstance(), SIGNAL(activeFloorChanged(int)), ui->floorBox, SLOT(setValue(int)));
    connect(RenderManager::GetInstance()->GetRenderMode(EDITOR_2D), SIGNAL(changedMode(string)), SLOT(getModeName(string)));
    connect(RenderManager::GetInstance()->GetRenderMode(EDITOR_2D), SIGNAL(pickedInfo(string)), SLOT(getPicked(string)));
    connect(RenderManager::GetInstance()->GetRenderMode(EXPLORER), SIGNAL(pickedInfo(string)), SLOT(getPicked(string)));
    connect(RenderManager::GetInstance()->GetRenderMode(EXPLORER), SIGNAL(setMessage(string, int)), SLOT(getMessage(string, int)));
    connect(RenderManager::GetInstance()->GetRenderMode(EDITOR_2D), SIGNAL(setMessage(string, int)), SLOT(getMessage(string, int)));
    connect(CScenary::getInstance(), SIGNAL(setMessage(string, int)), SLOT(getMessage(string, int)));
}
void CatManager::PerformCmd(QString str)
{
	short cmd = GetNameCmd(str);
	QString tmpstr1, tmpstr2, tmpFreq, tmpStep, tmpMod;
	int Freq, Tmp;
	QString SendStr;
	QByteArray Data;
	switch(cmd)
	{
	case 0x4741:
		if(str.size() == 3)
		{
			if(str.right(1).toInt() == 0)
			{
                Tmp = (pSdr->ui.slVol->value()*255)/100;
                tmpstr1.setNum((pSdr->ui.slVol->value()*255)/100);
				if(Tmp < 10)
					SendStr = "AG000" + tmpstr1 + ";";
				else if(Tmp < 100)
					SendStr = "AG00" + tmpstr1 + ";";
				else
					SendStr = "AG0" + tmpstr1 + ";";

                Data = SendStr.toLatin1();
				pCom->write(Data);
			}
		}
        else if(str.size() == 6)
		{
        	emit changedVolume(str.right(3).toInt());
            pSdr->ui.slVol->setValue((str.right(3).toInt()*100)/255);
            pSdr->OnVolume((str.right(3).toInt()*100)/255);



		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4941:
		SentErrorCode(ERROR_CMD);
		break;
	case 0x4442:
		if(str.size() == 2)
		{
			emit changedBand(-1);
			Tmp = pSdr->pBandBut->checkedId();
			if(--Tmp < 0)
				Tmp = 13;
			pSdr->OnChangeBand(Tmp);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5542:
		if(str.size() == 2)
		{
			emit changedBand(1);
			Tmp = pSdr->pBandBut->checkedId();
			if(++Tmp > 13)
				Tmp = 0;
			pSdr->OnChangeBand(Tmp);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4E44:
		if(str.size() == 2)
		{
			emit changedIncrDecrVFO_A(-1);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4146:
	case 0x5246:
	case 0x5446:
		if(str.size() == 2)
		{
			Freq = pSdr->FreqNum + pSdr->pGraph->pGl->GetPitch();
			tmpstr1.setNum(Freq);

			if(Freq < 10)
				tmpFreq = "0000000000" + tmpstr1;
			else if(Freq < 100)
				tmpFreq = "000000000" + tmpstr1;
			else if(Freq < 1000)
				tmpFreq = "00000000" + tmpstr1;
			else if(Freq < 10000)
				tmpFreq = "0000000" + tmpstr1;
			else if(Freq < 100000)
				tmpFreq = "000000" + tmpstr1;
			else if(Freq < 1000000)
				tmpFreq = "00000" + tmpstr1;
			else if(Freq < 10000000)
				tmpFreq = "0000" + tmpstr1;
			else if(Freq < 100000000)
				tmpFreq = "000" + tmpstr1;
			else if(Freq < 1000000000)
				tmpFreq = "00" + tmpstr1;

			if(cmd == 0x4146)
			{
				SendStr = "FA" + tmpFreq + ";";
			}
			else if(cmd == 0x5246)
			{
				SendStr = "FR" + tmpFreq + ";";
			}
			else if(cmd == 0x5446)
			{
                if(pSdr->ui.pbSplit->isChecked())
				{
					SendStr = "FT" + tmpFreq + ";";
				}
				else
				{
					Freq = pSdr->pVfoB->value() + pSdr->pGraph->pGl->GetPitch();
					tmpstr1.setNum(Freq);
					SendStr = "FT" + tmpFreq + ";";
				}
			}
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 13)
		{
			emit changedVFO_A(str.right(11).toInt() - pSdr->pGraph->pGl->GetPitch());
			pSdr->OnChangeMainFreqWithWdgMem(str.right(11).toInt() - pSdr->pGraph->pGl->GetPitch(), pSdr->MainMod());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4246:
		if(str.size() == 2)
		{
			Freq = pSdr->pVfoB->value() + pSdr->pGraph->pGl->GetPitch();//FreqNum;
			tmpstr1.setNum(Freq);

			if(Freq < 10)
				tmpFreq = "0000000000" + tmpstr1;
			else if(Freq < 100)
				tmpFreq = "000000000" + tmpstr1;
			else if(Freq < 1000)
				tmpFreq = "00000000" + tmpstr1;
			else if(Freq < 10000)
				tmpFreq = "0000000" + tmpstr1;
			else if(Freq < 100000)
				tmpFreq = "000000" + tmpstr1;
			else if(Freq < 1000000)
				tmpFreq = "00000" + tmpstr1;
			else if(Freq < 10000000)
				tmpFreq = "0000" + tmpstr1;
			else if(Freq < 100000000)
				tmpFreq = "000" + tmpstr1;
			else if(Freq < 1000000000)
				tmpFreq = "00" + tmpstr1;

			SendStr = "FB" + tmpFreq + ";";

            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 13)
		{
			emit changedVFO_B(str.right(11).toInt() - pSdr->pGraph->pGl->GetPitch());
			pSdr->pVfoB->setValue(str.right(11).toInt() - pSdr->pGraph->pGl->GetPitch());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
		}
		break;
	case 0x5746:
		SentErrorCode(ERROR_CMD);
		break;
	case 0x5447:
		if(str.size() == 2)
		{
			Tmp = pSdr->MainAGC();
			tmpstr1.setNum(Tmp);
			SendStr = "GT00" + tmpstr1 + ";";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 5)
		{
			Tmp = str.right(3).toInt();
			if(Tmp >= 0 && Tmp < 6)
			{
				emit changedAGC(str.right(3).toInt());
				pSdr->OnAgc(Tmp);
			}
			else
			{
				SentErrorCode(ERROR_PARAM);
			}
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4449:
		if(str.size() == 2)
		{
			SendStr = "ID019;";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4649:
		if(str.size() != 2)
		{
			SentErrorCode(ERROR_CMD);
			return;
		}
		tmpMod = ModToCat[pSdr->MainMod()];
		if(tmpMod == "0")
		{
			SentErrorCode(ERROR_PARAM);
			return;
		}

		Freq = pSdr->FreqNum + pSdr->pGraph->pGl->GetPitch();
		tmpstr1.setNum(Freq);

		if(Freq < 10)
			tmpFreq = "0000000000" + tmpstr1;
		else if(Freq < 100)
			tmpFreq = "000000000" + tmpstr1;
		else if(Freq < 1000)
			tmpFreq = "00000000" + tmpstr1;
		else if(Freq < 10000)
			tmpFreq = "0000000" + tmpstr1;
		else if(Freq < 100000)
			tmpFreq = "000000" + tmpstr1;
		else if(Freq < 1000000)
			tmpFreq = "00000" + tmpstr1;
		else if(Freq < 10000000)
			tmpFreq = "0000" + tmpstr1;
		else if(Freq < 100000000)
			tmpFreq = "000" + tmpstr1;
		else if(Freq < 1000000000)
			tmpFreq = "00" + tmpstr1;

		tmpStep = StepToCat[pSdr->MainStep()];

		tmpstr1 = "IF" + tmpFreq + tmpStep + "000000" + "0" + "0" + "0" + "00";

        tmpstr2.setNum((int)(pSdr->ui.pbMox->isChecked()));

		tmpstr1 += tmpstr2 + tmpMod + "0" + "0" + "0" + "0" + "00" + "0;";

                Data = tmpstr1.toLatin1();
		pCom->write(Data);

		break;
	case 0x534B:
		if(str.size() == 2)
		{
			SendStr = "KS000;";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 5)
		{
			emit changetCWLSpeed(str.right(3).toInt());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x594B:
		SentErrorCode(ERROR_CMD);
		break;
	case 0x444D:
		if(str.size() == 2)
		{
			tmpMod = ModToCat[pSdr->MainMod()];
			if(tmpMod == "0")
			{
				SentErrorCode(ERROR_PARAM);
				return;
			}

			SendStr = "MD" + tmpMod + ";";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 3)
		{
			emit changedMode(str.right(1).toInt());
			Tmp = CatToMod[str.right(1).toInt()];
			if(Tmp >= 0)
				pSdr->OnChangeMode(Tmp);
			else
				SentErrorCode(ERROR_PARAM);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x474D:
		if(str.size() == 2)
		{
            Tmp = pSdr->ui.slMic->value();
			if(Tmp < 10)
			{
				tmpstr1.setNum(Tmp);
				SendStr = "MG00" + tmpstr1 + ";";
			}
			else if(Tmp < 100)
			{
				tmpstr1.setNum(Tmp);
				SendStr = "MG0" + tmpstr1 + ";";
			}
			else
			{
				tmpstr1.setNum(Tmp);
				SendStr = "MG" + tmpstr1 + ";";
			}
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 5)
		{
			emit changedGainMicrophone(str.right(3).toInt());
			Tmp = str.right(3).toInt();
			pSdr->OnMic(Tmp);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4F4D:
		SentErrorCode(ERROR_CMD);
		break;
	case 0x424E:
		if(str.size() == 2)
		{
            if(pSdr->ui.pbNb->isChecked())
				SendStr = "NB0;";
			else
				SendStr = "NB1;";

            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 3)
		{
			if(str.right(1).toInt() != 0)
				pSdr->OnNb(false);
			else
				pSdr->OnNb(true);

			emit changedNB((bool)str.right(1).toInt());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x544E:
		if(str.size() == 2)
		{
            if(pSdr->ui.pbAnf->isChecked())
				SendStr = "NT0;";
			else
				SendStr = "NT1;";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 3)
		{
			if(str.right(1).toInt() != 0)
				pSdr->OnAnf(false);
			else
				pSdr->OnAnf(true);

			emit changedANF((bool)str.right(1).toInt());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4350:
		if(str.size() == 2)
		{
            Tmp = pSdr->ui.slDrive->value();
			tmpstr1.setNum(Tmp);
			if(Tmp < 10)
				SendStr = "PC00" + tmpstr1 + ";";
			else if(Tmp < 100)
				SendStr = "PC0" + tmpstr1 + ";";
			else
				SendStr = "PC" + tmpstr1 + ";";

			SendStr = "PC" + tmpstr1 + ";";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 5)
		{
			Tmp = str.right(3).toInt();
			if(Tmp >= 0 && Tmp <= 100)
			{
				pSdr->OnDrive(Tmp);
				emit changedPWR(str.right(3).toInt());
			}
			else
				SentErrorCode(ERROR_PARAM);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5250:
		if(str.size() == 2)
		{
            if(pSdr->ui.pbComp->isChecked())
				SendStr = "PR0;";
			else
				SendStr = "PR1;";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 3)
		{
			if(str.right(1).toInt() != 0)
				pSdr->OnComp(false);
			else
				pSdr->OnComp(true);

			emit changedCOMP((bool)str.right(1).toInt());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5350:
		if(str.size() == 2)
		{
            if(pSdr->ui.pbStart->isChecked())
				SendStr = "PS1;";
			else
				SendStr = "PS0;";

            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 3)
		{
			if(str.right(1).toInt() != 0)
				pSdr->OnStart(true);
			else
				pSdr->OnStart(false);
			emit changedOnOff((bool)str.right(1).toInt());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4352:
		if(str.size() == 2)
		{

		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5452:
		if(str.size() == 2)
		{
			SendStr = "RT0;";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 3)
		{
			emit changedOnOff((bool)str.right(1).toInt());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5852:
		if(str.size() == 2)
		{
			emit rxMode();
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x4853:
		SentErrorCode(ERROR_CMD);
		break;
	case 0x4C53:
		SentErrorCode(ERROR_CMD);
		break;
	case 0x4D53:
		if(str.size() == 3)
		{
			Tmp = pSdr->MainSmeter();
			Tmp += 73;
			Tmp /= 4.0;
			Tmp += 15;
			if(Tmp < 0)
				Tmp = 0;
			else if(Tmp > 30)
				Tmp = 30;

			tmpstr1.setNum(Tmp);
			if(Tmp < 10)
				SendStr = "SM0000" + tmpstr1 + ";";
			else
				SendStr = "SM000" + tmpstr1 + ";";

            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5153:
		if(str.size() == 3)
		{
            Tmp = pSdr->ui.slSql->value();
			Tmp *= -255.0/160.0;
			tmpstr1.setNum(Tmp);
			if(Tmp < 10)
				SendStr = "SQ000" + tmpstr1 + ";";
			else if(Tmp < 100)
				SendStr = "SQ00" + tmpstr1 + ";";
			else
				SendStr = "SQ0" + tmpstr1 + ";";

            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 6)
		{
			Tmp = str.right(3).toInt();
			Tmp *= -160.0/255.0;
			pSdr->OnSql(Tmp);
			emit changedSQLval(Tmp);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5854:
		if((str.size() == 2) || (str.size() == 3))
		{
			emit txMode();
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5055:
		if(str.size() == 2)
		{
			emit changedIncrDecrVFO_A(1);
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;
	case 0x5458:
		if(str.size() == 2)
		{
			SendStr = "XT0;";
            Data = SendStr.toLatin1();
			pCom->write(Data);
		}
		else if(str.size() == 3)
		{
			emit changedXIT((bool)str.right(1).toInt());
		}
		else
		{
			SentErrorCode(ERROR_PARAM);
			break;
		}
		break;

	default:
		SentErrorCode(ERROR_CMD);
		break;
	}
}