#include "dialogsetup.hpp"
#include <qprocess.h>
#include <thread>
#include <qmessagebox.h>
#include <QCryptographicHash>
#include <QIntValidator>
#include "common.h"
#include <iostream>
#include <fstream>
#include <QFileDialog>
#include <QInputDialog>
#include "basecamera.h"
#include  <windows.h>	//用于显示桌面
#include  <shldisp.h>	//用于显示桌面

#include <QThread>
#include "Cleanthread.h"

extern SysConf g_sys_conf;						//系统配置参数
extern QString g_admin_pswd;	//管理员密码
extern QString g_op_pswd;	//操作员密码

void task_osk()
{
	system("start /b C:\\Windows\\system32\\osk.exe");
}

DialogSetup::DialogSetup(QWidget * parent) : QDialog(parent) {
	ui.setupUi(this);
	InitPtrMat();
	//this->setWindowFlags(Qt::FramelessWindowHint);//窗口可移动

	for(int i=0;i<NumberOfSupportedCameras;i++)
	{
		lineEdit_expo_mat[i]->setValidator(new QIntValidator(0, 1000000, this));
		lineEdit_expo_mat[i]->setText(QString::number(g_sys_conf.expo[i]));
		lineEdit_gain_mat[i]->setValidator(new QIntValidator(0, 64, this));
		lineEdit_gain_mat[i]->setText(QString::number(g_sys_conf.gain[i]));
		lineEdit_filter_mat[i]->setValidator(new QIntValidator(0, 1000000, this));
		lineEdit_filter_mat[i]->setText(QString::number(g_sys_conf.filter[i]));
	}

	if (g_sys_conf.auto_open == 1)
	{
		ui.checkBox_auto_open->setChecked(true);
	}
	else {
		ui.checkBox_auto_open->setChecked(false);
	}
	if (g_sys_conf.auto_work == 1)
	{
		ui.checkBox_auto_work->setChecked(true);
	}
	else {
		ui.checkBox_auto_work->setChecked(false);
	}
	
	if (!g_sys_conf.model_path.isEmpty()) {
		// 如果曾经选择过模型文件夹
		// 查看model_path路径下文件夹详情
		QDir* dirinfo = new QDir(g_sys_conf.model_path);
		if (!dirinfo->exists())
			delete dirinfo, dirinfo = nullptr;
		dirinfo->setNameFilters(QStringList("*.weights"));  // 设置过滤器
		QStringList fileList = dirinfo->entryList(QDir::Files);
		fileList.removeOne(".");
		fileList.removeOne("..");
		ui.comboBox->clear();
		ui.comboBox->addItems(fileList);
		if (!g_sys_conf.model_name.isEmpty()) {
			// 如果曾选择过模型，则在comboBox显示选择模型的文件名
			ui.comboBox->setCurrentText(g_sys_conf.model_name);
		}
		// 更换模型事件
		connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboBoxSelect(int)));
		delete dirinfo, dirinfo = nullptr;
	}
	else {
		// 如果未曾选择过模型文件夹
		if (!g_sys_conf.model_name.isEmpty()) {
			// 如果曾经选择过模型文件
			ui.comboBox->addItem(g_sys_conf.model_name);
		}
	}

	switch (g_sys_conf.save)
	{
	case 0:
		ui.radioButton_none->setChecked(true);
		break;
	case 1:
		ui.radioButton_ng->setChecked(true);
		break;
	case 2:
		ui.radioButton_all->setChecked(true);
		break;
	default:
		break;
	}
	//
#ifndef __DEBUG
	ui.pushButton_testimg->setVisible(false);
	ui.pushButton_testimgs->setVisible(false);
#endif
}

DialogSetup::~DialogSetup() {
	
}

void DialogSetup::onComboBoxSelect(int index) {
	// 获取comboBox当前内容
	QString ct = ui.comboBox->currentText();
	/// QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("您选择的项为")+ct, NULL);
	// 保存到配置文件中
	g_sys_conf.model_name = ct;
}

void DialogSetup::on_toolButton_keyboard_released()
{
	std::thread osk_thread = std::thread(task_osk);
	osk_thread.detach();
}
void DialogSetup::on_pushButton_exit_released()
{
	if (QMessageBox::Yes == QMessageBox::critical(NULL, QStringLiteral("退出系统"), QStringLiteral("确定退出系统?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes))
	{
		emit system_exit();
		this->close();
	}
}

void DialogSetup::on_pushButton_desktop_released()
{
	CoInitialize(0);
	IShellDispatch4 * pdisp = NULL;
	CoCreateInstance(CLSID_Shell, NULL, CLSCTX_ALL, __uuidof(IShellDispatch4), (void **)&pdisp);
	pdisp->ToggleDesktop();  //  这句是用来切换桌面的
	pdisp->Release();
	CoUninitialize();
}

void DialogSetup::on_pushButton_expo_released()
{
	for(int i=0;i<NumberOfSupportedCameras;i++)
	{
		g_sys_conf.expo[i] = (lineEdit_expo_mat[i]->text()).toInt();
		g_sys_conf.gain[i] = (lineEdit_gain_mat[i]->text()).toInt();
	}
	QMessageBox::information(NULL, QStringLiteral("提示"), QStringLiteral("设置成功"), QMessageBox::Ok);
}

void DialogSetup::on_checkBox_auto_open_clicked(bool checked)
{
	if (checked)
	{
		g_sys_conf.auto_open = 1;
	}
	else
	{
		g_sys_conf.auto_open = 0;
	}
}

void DialogSetup::on_checkBox_auto_work_clicked(bool checked)
{
	if (checked)
	{
		g_sys_conf.auto_work = 1;
	}
	else
	{
		g_sys_conf.auto_work = 0;
	}
}

void DialogSetup::on_pushButton_filter_released()
{
	for(int i=0;i<NumberOfSupportedCameras;i++)
	{
		g_sys_conf.filter[i] = (lineEdit_filter_mat[i]->text()).toInt();
	}
	QMessageBox::information(NULL, QStringLiteral("提示"), QStringLiteral("设置成功"), QMessageBox::Ok);
}

void DialogSetup::on_pushButton_image_released()
{
	if (ui.radioButton_none->isChecked())
	{
		g_sys_conf.save = 0;
	}
	if (ui.radioButton_ng->isChecked())
	{
		g_sys_conf.save = 1;
	}
	if (ui.radioButton_all->isChecked())
	{
		g_sys_conf.save = 2;
	}
	QMessageBox::information(NULL, QStringLiteral("提示"), QStringLiteral("设置成功"), QMessageBox::Ok);
}

void DialogSetup::on_pushButton_pswd_released()
{
	QString md5Str = QCryptographicHash::hash((ui.lineEdit_old->text()).toLatin1(), QCryptographicHash::Md5).toHex();
	if (md5Str.compare(g_admin_pswd, Qt::CaseInsensitive) == 0)
	{
		QString pswd_new = ui.lineEdit_new->text();
		QString pswd_confirm = ui.lineEdit_confirm->text();
		if (pswd_new.compare(pswd_confirm, Qt::CaseInsensitive) == 0)
		{
			g_admin_pswd = QCryptographicHash::hash(pswd_new.toLatin1(), QCryptographicHash::Md5).toHex();
			write_pswd();
			QMessageBox::information(NULL, QStringLiteral("提示"), QStringLiteral("管理员密码修改成功"), QMessageBox::Ok);
		}
		else {
			QMessageBox::information(NULL, QStringLiteral("提示消息"), QStringLiteral("新密码两次输入不一致，请重新输入"), QMessageBox::Ok);
		}
	}
	else {
		QMessageBox::information(NULL, QStringLiteral("提示消息"), QStringLiteral("原密码错误，请重新输入"), QMessageBox::Ok);
	}
}
void DialogSetup::on_pushButton_pswd_op_released()
{
	QString md5Str = QCryptographicHash::hash((ui.lineEdit_old_op->text()).toLatin1(), QCryptographicHash::Md5).toHex();
	if (md5Str.compare(g_op_pswd, Qt::CaseInsensitive) == 0)
	{
		QString pswd_new = ui.lineEdit_new_op->text();
		QString pswd_confirm = ui.lineEdit_confirm_op->text();
		if (pswd_new.compare(pswd_confirm, Qt::CaseInsensitive) == 0)
		{
			g_op_pswd = QCryptographicHash::hash(pswd_new.toLatin1(), QCryptographicHash::Md5).toHex();
			write_pswd_op();
			QMessageBox::information(NULL, QStringLiteral("提示"), QStringLiteral("操作员密码修改成功"), QMessageBox::Ok);
		}
		else {
			QMessageBox::information(NULL, QStringLiteral("提示消息"), QStringLiteral("新密码两次输入不一致，请重新输入"), QMessageBox::Ok);
		}
	}
	else {
		QMessageBox::information(NULL, QStringLiteral("提示消息"), QStringLiteral("原密码错误，请重新输入"), QMessageBox::Ok);
	}
}
void DialogSetup::on_pushButton_save_released()
{
	//存图设置
	if (ui.radioButton_none->isChecked())
	{
		g_sys_conf.save = 0;
	}
	if (ui.radioButton_ng->isChecked())
	{
		g_sys_conf.save = 1;
	}
	if (ui.radioButton_all->isChecked())
	{
		g_sys_conf.save = 2;
	}
	for(int i=0;i<NumberOfSupportedCameras;i++)
	{
		g_sys_conf.expo[i] = (lineEdit_expo_mat[i]->text()).toInt();
		g_sys_conf.gain[i] = (lineEdit_gain_mat[i]->text()).toInt();
		g_sys_conf.filter[i] = (lineEdit_filter_mat[i]->text()).toInt();
	}
	
	write_config();
	QMessageBox::information(NULL, QStringLiteral("提示"), QStringLiteral("保存参数成功,部分配置需要重启程序后生效"), QMessageBox::Ok);
}

void DialogSetup::on_pushButton_close_released()
{
	this->close();
}

void DialogSetup::on_pushButton_clear_pic_released()
{
	QThread* handleThread = new QThread();
	CleanWorkThread* cleanWorkThread = new CleanWorkThread();

	cleanWorkThread->moveToThread(handleThread);

	connect(handleThread, &QThread::started, cleanWorkThread, &CleanWorkThread::startWork);
	connect(cleanWorkThread, &CleanWorkThread::workStart, cleanWorkThread, &CleanWorkThread::setSel);
	connect(cleanWorkThread, &CleanWorkThread::workFinished, cleanWorkThread, &CleanWorkThread::deleteLater);
	connect(cleanWorkThread, &CleanWorkThread::destroyed, handleThread, &QThread::quit);
	connect(handleThread, &QThread::finished, handleThread, &QThread::deleteLater);
	handleThread->start();
}

void DialogSetup::on_toolButton_choose_path_released() {
	// 点击浏览文件按钮后触发的事件
	QString dirName = QFileDialog::getExistingDirectory(this, QStringLiteral("请选择模型所在文件夹"), "./");
	//std::cout << "dirName is " << dirName.toStdString().c_str() << std::endl;
	//std::cout << "g_sys_conf.dir_path is " << g_sys_conf.dir_path.toStdString().c_str() << std::endl;
	//std::cout << "g_sys_conf.model_name is " << g_sys_conf.model_name.toStdString().c_str() << std::endl;

	if (dirName.isEmpty()) {
		dirName = g_sys_conf.model_path;
		ui.comboBox->setCurrentText(g_sys_conf.model_name);
		return;
	}
	QDir* dirinfo = new QDir(dirName);
	if (!dirinfo->exists()) {
		delete dirinfo, dirinfo = nullptr;
	}
	dirinfo->setNameFilters(QStringList("*.weights"));

	QStringList fileList = dirinfo->entryList(QDir::Files);
	if (fileList.count() == 0)
		g_sys_conf.model_path = "D:/model";
	else
		g_sys_conf.model_path = dirName;

	fileList.removeOne(".");
	fileList.removeOne("..");

	ui.comboBox->clear();
	ui.comboBox->addItems(fileList);

	delete dirinfo, dirinfo = nullptr;
}

void DialogSetup::on_toolButton_choose_path_jpg_released() {
	QString JpgPath = QFileDialog::getOpenFileName(this, "选择jpg文件", "/", "jpg files(*.jpg)");
	if (JpgPath.isEmpty()) {
		return;
	}
	else {
		if (ui.comboBox_2->findText(JpgPath) == -1) {
			ui.comboBox_2->addItem(JpgPath);	// 在comboBox中显示文件路径
			g_sys_conf.jpg_path = JpgPath;	// 将选择的路径写入conf配置文件中
		}
	}
}

void DialogSetup::write_pswd()
{
	std::fstream cfg_file;
	cfg_file.open("D:/Release/pswd.txt", std::ios::out | std::ios::trunc);
	if (cfg_file.good())
	{
		char buf[256];
		memset(buf, 0, 256);
		sprintf(buf, "%s", g_admin_pswd.toStdString().c_str());
		cfg_file.write(buf, strlen(buf));
	}
	cfg_file.close();
}
void DialogSetup::write_pswd_op()
{
	std::fstream cfg_file;
	cfg_file.open("D:/Release/pswd_op.txt", std::ios::out | std::ios::trunc);
	if (cfg_file.good())
	{
		char buf[256];
		memset(buf, 0, 256);
		sprintf(buf, "%s", g_op_pswd.toStdString().c_str());
		cfg_file.write(buf, strlen(buf));
	}
	cfg_file.close();
}
void DialogSetup::write_config()
{
	std::fstream cfg_file;
	cfg_file.open("D:/conf/conf.txt", std::ios::out | std::ios::trunc);
	if (cfg_file.good())
	{
		char buf[256];
		memset(buf, 0, 256);
		sprintf(buf, "SAVE=%d\n", g_sys_conf.save);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "MISMATCHACT=%d\n", g_sys_conf.MisMatchAct);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);///
		sprintf(buf, "SAVE_DAYS=%d\n", g_sys_conf.save_days);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);///
		sprintf(buf, "FREESIZE=%d\n", g_sys_conf.freesize);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);///
		sprintf(buf, "ComPort=%s\n", g_sys_conf.ComPort);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);///
		sprintf(buf, "CONFTHRESHOLD=%d\n", g_sys_conf.ConfThreshold);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "AUTO_OPEN=%d\n", g_sys_conf.auto_open);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "AUTO_WORK=%d\n", g_sys_conf.auto_work);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "AUTO_SHIFT=%d\n", g_sys_conf.auto_shift);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "TIMING_SHIFT=%d\n", g_sys_conf.timing_shift);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHIFT_BYHAND=%d\n", g_sys_conf.shift_byhand);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHIFT_A=%d|%d\n", g_sys_conf.shiftA.hour(), g_sys_conf.shiftA.minute());
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHIFT_B=%d|%d\n", g_sys_conf.shiftB.hour(), g_sys_conf.shiftB.minute());
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHIFT_C=%d|%d\n", g_sys_conf.shiftC.hour(), g_sys_conf.shiftC.minute());
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "LOCATION=%s\n", g_sys_conf.location.toStdString().c_str());
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "MODELPATH=%s\n", g_sys_conf.model_path.toStdString().c_str());
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "MODELNAME=%s\n", g_sys_conf.model_name.toStdString().c_str());
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "JPGPATH=%s\n", g_sys_conf.jpg_path.toStdString().c_str());
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>0)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO1=%d\n", g_sys_conf.expo[0]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN1=%d\n", g_sys_conf.gain[0]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "FILTER1=%d\n", g_sys_conf.filter[0]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "USERID1=%d\n", g_sys_conf.UserID[0]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO1=%d|%d|%d\n", g_sys_conf.no[0][0],g_sys_conf.no[0][1],g_sys_conf.no[0][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT1=%d\n", g_sys_conf.shoot[0]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>1)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO2=%d\n", g_sys_conf.expo[1]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN2=%d\n", g_sys_conf.gain[1]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "FILTER2=%d\n", g_sys_conf.filter[1]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "USERID2=%d\n", g_sys_conf.UserID[1]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO2=%d|%d|%d\n", g_sys_conf.no[1][0],g_sys_conf.no[1][1],g_sys_conf.no[1][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT2=%d\n", g_sys_conf.shoot[1]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>2)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO3=%d\n", g_sys_conf.expo[2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN3=%d\n", g_sys_conf.gain[2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "FILTER3=%d\n", g_sys_conf.filter[2]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "USERID3=%d\n", g_sys_conf.UserID[2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO3=%d|%d|%d\n", g_sys_conf.no[2][0],g_sys_conf.no[2][1],g_sys_conf.no[2][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT3=%d\n", g_sys_conf.shoot[2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>3)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO4=%d\n", g_sys_conf.expo[3]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN4=%d\n", g_sys_conf.gain[3]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "FILTER4=%d\n", g_sys_conf.filter[3]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "USERID4=%d\n", g_sys_conf.UserID[3]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO4=%d|%d|%d\n", g_sys_conf.no[3][0],g_sys_conf.no[3][1],g_sys_conf.no[3][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT4=%d\n", g_sys_conf.shoot[3]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>4)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO5=%d\n", g_sys_conf.expo[4]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN5=%d\n", g_sys_conf.gain[4]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "FILTER5=%d\n", g_sys_conf.filter[4]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "USERID5=%d\n", g_sys_conf.UserID[4]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO5=%d|%d|%d\n", g_sys_conf.no[4][0],g_sys_conf.no[4][1],g_sys_conf.no[4][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT5=%d\n", g_sys_conf.shoot[4]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>5)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO6=%d\n", g_sys_conf.expo[5]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN6=%d\n", g_sys_conf.gain[5]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "FILTER6=%d\n", g_sys_conf.filter[5]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "USERID6=%d\n", g_sys_conf.UserID[5]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO6=%d|%d|%d\n", g_sys_conf.no[5][0],g_sys_conf.no[5][1],g_sys_conf.no[5][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT6=%d\n", g_sys_conf.shoot[5]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>6)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO7=%d\n", g_sys_conf.expo[6]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN7=%d\n", g_sys_conf.gain[6]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "FILTER7=%d\n", g_sys_conf.filter[6]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "USERID7=%d\n", g_sys_conf.UserID[6]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO7=%d|%d|%d\n", g_sys_conf.no[6][0],g_sys_conf.no[6][1],g_sys_conf.no[6][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT7=%d\n", g_sys_conf.shoot[6]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>7)
		memset(buf, 0, 256);
		sprintf(buf, "EXPO8=%d\n", g_sys_conf.expo[7]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "GAIN8=%d\n", g_sys_conf.gain[7]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "FILTER8=%d\n", g_sys_conf.filter[7]);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "USERID8=%d\n", g_sys_conf.UserID[7]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "NO8=%d|%d|%d\n", g_sys_conf.no[7][0],g_sys_conf.no[7][1],g_sys_conf.no[7][2]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "SHOOT8=%d\n", g_sys_conf.shoot[7]);
		cfg_file.write(buf, strlen(buf));
		memset(buf, 0, 256);
		sprintf(buf, "*****************************************\n");
		cfg_file.write(buf, strlen(buf));
#endif
		memset(buf, 0, 256);
		sprintf(buf, "MonitorIP=%s\n", g_sys_conf.MonitorIP.c_str());
		cfg_file.write(buf, strlen(buf));

		memset(buf, 0, 256);
		cfg_file.write(buf, strlen(buf));
		sprintf(buf, "MonitorPort=%d\n", g_sys_conf.MonitorPort);
		cfg_file.write(buf, strlen(buf));
	}
	cfg_file.close();
}

#define InitPtrMatDef(a,b)\
    lineEdit_expo_mat[a]=ui.lineEdit_expo_##b; \
	lineEdit_gain_mat[a]=ui.lineEdit_gain_##b; \
	lineEdit_filter_mat[a]=ui.lineEdit_filter_##b;
void DialogSetup::InitPtrMat()
{
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>0)
	InitPtrMatDef(0, 1);
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>1)
	InitPtrMatDef(1, 2);
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>2)
	InitPtrMatDef(2, 3);
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>3)
	InitPtrMatDef(3, 4);
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>4)
	InitPtrMatDef(4, 5);
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>5)
	InitPtrMatDef(5, 6);
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>6)
	InitPtrMatDef(6, 7);
#endif
#if defined(NumberOfSupportedCameras) && (NumberOfSupportedCameras>7)
	InitPtrMatDef(7, 8);
#endif
}

void DialogSetup::on_pushButton_config_released()
{
	if (m_camera_glue)delete m_camera_glue;
	m_camera_glue = new camera_glue(this);
	connect(m_camera_glue, &camera_glue::sendMsgToDialogSetup,this, &DialogSetup::recMsgFromDialogConfig);
	connect(this, &DialogSetup::sendMsgToConfig,m_camera_glue, &camera_glue::recMsgFromDialogSetup);
	m_camera_glue->show();
	m_camera_glue->move(this->geometry().center() - m_camera_glue->rect().center());//移动父窗口中心位置
	emit sendMsgToConfig(g_sys_conf.no);
}
void DialogSetup::recMsgFromDialogConfig(int ptr[][3])
{
	for(int i=0;i<NumberOfSupportedCameras;i++)
			for(int j=0;j<3;j++)
				g_sys_conf.no[i][j]=ptr[i][j];
}
void DialogSetup::on_pushButton_change_released()
{
	if (m_change_shift)delete m_change_shift;
	m_change_shift = new change_shift(this);
	connect(m_change_shift, &change_shift::sendMsgToDialogSetup, this, &DialogSetup::recMsgFromChangeShift);
	connect(this, &DialogSetup::sendMsgToShift, m_change_shift, &change_shift::recMsgFromDialogSetup);
	m_change_shift->show();
	m_change_shift->move(this->geometry().center() - m_change_shift->rect().center());
	emit sendMsgToShift (g_sys_conf.shiftA, g_sys_conf.shiftB, g_sys_conf.shiftC);
}
void DialogSetup::on_pushButton_statistic_released()
{
	if (m_output_statistic)delete m_output_statistic;
	m_output_statistic = new output_statistic(this);
	connect(this, &DialogSetup::sendMsgToOutput, m_output_statistic, &output_statistic::recMsgFromDialogSetup);
	m_output_statistic->show();
	m_output_statistic->move(this->geometry().center() - m_output_statistic->rect().center());
	emit sendMsgToOutput();
}
void DialogSetup::recMsgFromChangeShift(QTime timeA, QTime timeB, QTime timeC)
{
	g_sys_conf.shiftA.setHMS(timeA.hour(), timeA.minute(), 0);
	g_sys_conf.shiftB.setHMS(timeB.hour(), timeB.minute(), 0);
	g_sys_conf.shiftC.setHMS(timeC.hour(), timeC.minute(), 0);
}
#ifdef __DEBUG
void DialogSetup::on_pushButton_testimg_released()
{
	emit _testimg();
}
void DialogSetup::on_pushButton_testimgs_released()
{
	emit _testimgs();
}
#endif