#include "widget.h"
#include "ui_widget.h"
#include<QPen>
#include<QDebug>
#include<math.h>
#include<QPainter>
#include<QMessageBox>
#include<QKeyEvent>
#include<QMouseEvent>
#define pi 3.14159
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->grabKeyboard();
point = new QPointF;
y = 150;
x = 150;
centerx = 150;
centery = 120;
point->setX(x);
point->setY(y);
state = 0;
SecondarySerial = NULL;
ui->horizontalSlider->setMaximum(200);
ui->horizontalSlider->setMinimum(10);
ui->spinBox->setMinimum(10);
ui->spinBox->setMaximum(200);
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->spinBox, SLOT(setValue(int)));
connect(ui->spinBox, SIGNAL(valueChanged(int)), ui->horizontalSlider, SLOT(setValue(int)));
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if (state == 0 && event->button()==Qt::LeftButton)
{
state = 1;
}
repaint();
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if(state == 1 && event->button()==Qt::LeftButton)
{
state = 0;
}
ui->lineEdit->setText("0");
repaint();
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen(Qt::black, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
if(state == 0)
{
point->setX(centerx);
point->setY(centery);
}
else
{
point->setX(x);
point->setY(y);
}
painter.setPen(pen);
painter.drawEllipse(QPointF(centerx, centery), 80, 80);
pen.setColor(Qt::gray);
painter.setPen(pen);
QBrush brush(QColor(0, 0, 255), Qt::Dense4Pattern);
painter.setBrush(brush);
painter.drawEllipse(*point, 20, 20);
painter.setPen(pen);
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
QPoint temp = event->pos();
int tempx = temp.x();
int tempy = temp.y();
double distance = sqrt((tempx-centerx)*(tempx-centerx) + (tempy-centery)*(tempy-centery));
if(distance >= 80)
{
x = 80*(tempx-centerx)/distance + centerx;
y = 80*(tempy-centery)/distance + centery;
ui->spinBox->setValue(200);
}
else if(distance <= 20)
{
x = centerx;
y = centery;
ui->spinBox->setValue(10);
}
else
{
x = tempx;
y = tempy;
double speed = (distance-20)*190/60 + 10;
ui->spinBox->setValue(speed);
}
double angle;
angle = asin(abs(tempy - centery)/distance);
if(tempx > centerx && tempy > centery)
{
angle = 2 * pi - angle;
}
else if(tempx < centerx && tempy > centery)
{
angle += pi;
}
else if(tempx < centerx && tempy < centery)
{
angle = pi - angle;
}
if(x==centerx && y==centery)
{
angle = 0;
}
QString str = QString::number(angle);
ui->lineEdit->setText(str);
if(SecondarySerial != NULL && SecondarySerial->isOpen())
{
QString str = QString("set_speed(%1)(%2)").arg(ui->spinBox->text()).arg(QString::number(angle));
QByteArray s = str.toLatin1();
s.append('\r');
s.append('\n');
SecondarySerial->write(s);
}
repaint();
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_2_clicked()
{
if(ui->pushButton_2->text() =="打开串口"){
SecondarySerial=new QSerialPort(this);
SecondarySerial->setPortName(ui->comboBox->currentText());
SecondarySerial->setBaudRate(ui->comboBox_2->currentText().toInt());
SecondarySerial->setDataBits(QSerialPort::Data8);
//设置校验位
SecondarySerial->setParity(QSerialPort::NoParity);
//设置流控制
SecondarySerial->setFlowControl(QSerialPort::NoFlowControl);
//设置停止位
SecondarySerial->setStopBits(QSerialPort::OneStop);
SecondarySerial->setReadBufferSize(200000);
if(SecondarySerial->isOpen())
{
QMessageBox msgBox;
msgBox.setText("打开成功");
msgBox.exec();
}
if(SecondarySerial->open(QIODevice::ReadWrite)){
ui->pushButton_2->setText("关闭串口");
ui->comboBox_2->setDisabled(true);
ui->comboBox->setDisabled(true);
}
else
{
QMessageBox::about(this, "warning", "打开串口失败");
return;
}
// start_update();
}else{
SecondarySerial->close();
ui->pushButton_2->setText("打开串口");
ui->comboBox_2->setDisabled(false);
ui->comboBox->setDisabled(false);
// stop_update();
}
}