#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化窗口指针
aboutWindow = NULL;
connectWindow = NULL;
ddsWindow = NULL;
// 主界面模式设置为运行模式
Mode = RUN;
// 实例化USB类
g_USBDevice = new usb();
// 主界面接收线程发送的信号
connect(&usbThread,
SIGNAL(signal_ReviceFinish(int)),
this,
SLOT(updateDisplay(int)) );
// 初始化UI
initUI();
// 参数初始化
ch1_VoltageOffset = 0;
ch1_VoltageRange = 0;
ch1_CouplingMode = 0;
ch2_VoltageOffset = 0;
ch2_VoltageRange = 0;
ch2_CouplingMode = 0;
triggerChannel = 0;
triggerEdge = 0;
triggerLevel = 0;
sampleRate = 100000000;
// 通道设置信号处理
connect(ch1_settings_widget, SIGNAL(voltageOffsetChanged(int, int)), this, SLOT(voltageOffsetChangedHandler(int, int)) );
connect(ch1_settings_widget, SIGNAL(voltageRangeChanged(int, int)) , this, SLOT(voltageRangeChangedHandler(int, int)) );
connect(ch1_settings_widget, SIGNAL(couplingModeChanged(int, int)) , this, SLOT(couplingModeChangedHandler(int, int)) );
connect(ch2_settings_widget, SIGNAL(voltageOffsetChanged(int, int)), this, SLOT(voltageOffsetChangedHandler(int, int)) );
connect(ch2_settings_widget, SIGNAL(voltageRangeChanged(int, int)) , this, SLOT(voltageRangeChangedHandler(int, int)) );
connect(ch2_settings_widget, SIGNAL(couplingModeChanged(int, int)) , this, SLOT(couplingModeChangedHandler(int, int)) );
// 采样率设置信号处理
connect(sampleRateSettingWidget, SIGNAL(sampleRateChanged(int)), this, SLOT(sampleRateChangedHandler(int)) );
// 触发设置信号处理
connect(triggerWidget, SIGNAL(signal_channelChanged(int)), this, SLOT(triggerChannelChangedHandler(int)) );
connect(triggerWidget, SIGNAL(signal_edgeChanged(int)) , this, SLOT(triggerEdgeChangedHandler(int)) );
connect(triggerWidget, SIGNAL(signal_levelChanged(int)) , this, SLOT(triggerLevelChangedHandler(int)) );
// 启动线程
usbThread.start();
}
MainWindow::~MainWindow()
{
if (g_USBDevice)
{
// 关闭USB
g_USBDevice->unconnectDevice();
delete g_USBDevice;
g_USBDevice = NULL;
}
if (aboutWindow)
{
delete aboutWindow;
aboutWindow = NULL;
}
if (connectWindow)
{
delete connectWindow;
connectWindow = NULL;
}
if (ddsWindow)
{
delete ddsWindow;
ddsWindow = NULL;
}
delete ui;
usbThread.stop();
}
// 窗口重定义大小
void MainWindow::resizeEvent(QResizeEvent *)
{
}
// 界面绘制事件
void MainWindow::paintEvent(QPaintEvent *)
{
QPainterPath path;
path.setFillRule(Qt::WindingFill);
path.addRect(0, 0, this->width(), this->height());
QPainter painter(this);
painter.fillPath(path, QBrush(Qt::white));
path.setFillRule(Qt::WindingFill);
path.addRect(0, 0, this->width()-1, this->height()-1);
painter.setPen("#0063B1");
painter.drawPath(path);
}
// 初始化界面
void MainWindow::initUI()
{
QString titleText = MAIN_WINDOW_TITLE;
if (ENABLE_SIMULATION_MODE)
titleText += tr(" - Simulation Mode");
this->setMinimumSize(DEFAULT_MAIN_WINDOW_WIDTH, DEFAULT_MAIN_WINDOW_HEIGHT);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowSystemMenuHint);
// this->setWindowIcon(QIcon(":/img/etc/logo.png"));
this->setWindowIcon(QIcon(":/img/etc/icon.png"));
this->setWindowTitle(titleText);
this->setWindowOpacity(1); //窗体透明度设置
// 标题栏
title_bar_widget = new TitleBarWidget(this, titleText, DEFAULT_TITLE_BAR_HEIGHT, true);
title_bar_widget->initConnect();
connect(title_bar_widget, SIGNAL(closeWindow()), this, SLOT(closeApp()));
// 主界面的下拉菜单
mainMenu = new MainWindowMenu(this);
mainMenu->intConnect();
// 状态栏
status_bar_widget = new StatusBarWidget(this, DEFAULT_STATUS_BAR_HEIGHT);
status_bar_widget->showMessage("未连接设备!");
status_bar_widget->isEnabledShowTime(true);
// 波形显示区域
oscillograph_widget = new OscillographWidget(this, 2);
oscillograph_widget->setMinimumSize(this->width()-160-5*3,
this->height()-title_bar_widget->height()-status_bar_widget->height() - 5*2);
//连接信号槽函数
connect(this, SIGNAL(signal_ModeChanged(int)), oscillograph_widget, SLOT(updateMode(int)));
emit signal_ModeChanged(Mode);
// CH1、CH2控制面板
ch1_settings_widget = new ChannelSettingsWidget(this, 0, "CH1", Qt::yellow);
ch2_settings_widget = new ChannelSettingsWidget(this, 1, "CH2", QColor(0x48, 0x76, 0xFF));
// 设置颜色关联的槽函数
connect(ch1_settings_widget,
SIGNAL(markColoChange(int, QColor)),
oscillograph_widget,
SLOT(setChannelMarkColor(int, QColor))
);
connect(ch2_settings_widget,
SIGNAL(markColoChange(int, QColor)),
oscillograph_widget,
SLOT(setChannelMarkColor(int, QColor))
);
// 直接调用槽函数设置波形颜色
oscillograph_widget->setChannelMarkColor(0, Qt::yellow);
oscillograph_widget->setChannelMarkColor(1, QColor(0x48, 0x76, 0xFF));
// 采样速率设置
sampleRateSettingWidget = new SampleRateSettingWidget(this);
// 触发设置
triggerWidget = new TriggerWidget(this);
// 按键
runButton = new SystemButton(this, "运行/暂停");
// singleButton = new SystemButton(this, "单次采集");
// autoButton = new SystemButton(this, "自动设置");
defalutButton = new SystemButton(this, "默认设置");
// saveDataDeviceButton = new SystemButton(this, "保存数据");
screenshotButton = new SystemButton(this, "截图");
// 设置按键默认状态
disableButton();
// 设置按键事件
connect(runButton, SIGNAL(clicked()), this, SLOT( runButtonHandler()) );
// connect(singleButton, SIGNAL(clicked()), this, SLOT( singleButtonHandler()) );
// connect(autoButton, SIGNAL(clicked()), this, SLOT( autoButtonHandler()) );
connect(defalutButton, SIGNAL(clicked()), this, SLOT( defalutButtonHandler()) );
// connect(saveDataDeviceButton, SIGNAL(clicked()), this, SLOT( saveDataDeviceButtonHandler()) );
connect(screenshotButton, SIGNAL(clicked()), this, SLOT( screenshotButtonHandler()) );
connect(ch1_settings_widget, SIGNAL(switchStateChanged(int, bool)), oscillograph_widget, SLOT(setChannelDisplay(int, bool)));
connect(ch2_settings_widget, SIGNAL(switchStateChanged(int, bool)), oscillograph_widget, SLOT(setChannelDisplay(int, bool)));
// 初始化布局
initLayout();
}
// 初始化布局
void MainWindow::initLayout()
{
QVBoxLayout *mainLayout = new QVBoxLayout();
QHBoxLayout *hLayout1 = new QHBoxLayout();
QVBoxLayout *vLayout1 = new QVBoxLayout();
QGridLayout *gridLayout = new QGridLayout();
QSpacerItem *vSpacer1 = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
// QSpacerItem *vSpacer2 = new QSpacerItem(20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding);
// gridLayout->addWidget(runButton, 0, 0);
// gridLayout->addWidget(singleButton, 0, 1);
// gridLayout->addWidget(autoButton, 1, 0);
// gridLayout->addWidget(defalutButton, 1, 1);
// gridLayout->addWidget(saveDataDeviceButton, 2, 0);
// gridLayout->addWidget(screenshotButton, 2, 1);
// gridLayout->setSpacing(5);
// gridLayout->setContentsMargins(0, 0, 0, 0);
runButton->setFixedSize(160, 32);
defalutButton->