You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.6 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setMouseTracking(true);
ui->centralWidget->setMouseTracking(true);
rectPath.moveTo(20.0, 30.0);
rectPath.lineTo(80.0, 30.0);
rectPath.lineTo(80.0, 70.0);
rectPath.lineTo(20.0, 70.0);
scalex = 1;
scaley = 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event){
for(int i=0;i<vf.size();++i){
Fault&f = vf[i];
if(!f.isGenerated()){
continue;
}
if(f.contains(event->posF())){
isPressed = true;
iPoint = f.dotContains(event->posF());
}else if(c.contains(event->posF())){
isPressed = true;
iPoint = c.dotContains(event->posF());
}
if(event->button()==Qt::RightButton){
f.setMove(false);
c.setMove(false);
}
if(ui->checkBox->checkState()==Qt::Unchecked && event->button()==Qt::LeftButton){
f.pick(event);
}
}
if(ui->checkBox->checkState()==Qt::Checked){
// Fault 测试集
int n = f.elementCount();
if(event->button()==Qt::LeftButton){
qreal x = event->posF().x();
qreal y = event->posF().y();
if(n>1){
f.setPoint(n-1,x+0.01,y+0.01);
}
f.addPoint(x,y);
if(n==0){
f.addPoint(x+3,y+3);
}
}
else if(event->button()==Qt::RightButton){
f.generate();
vf.push_back(f);
f.clear();
ui->checkBox->setCheckState(Qt::Unchecked);
}
}
else if(ui->checkBox2->checkState()==Qt::Checked){
// contour测试集
int n = c.elementCount();
if(event->button()==Qt::LeftButton){
qreal x = event->posF().x();
qreal y = event->posF().y();
c.addPoint(x,y);
if(n==0){
c.addPoint(x+3,y+3);
}
}else if(event->button()==Qt::RightButton){
c.generate();
ui->checkBox2->setCheckState(Qt::Unchecked);
}
}
if(ui->checkBox2->checkState()==Qt::Unchecked && event->button()==Qt::LeftButton){
c.pick(event);
}
this->update();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event){
isPressed = false;
this->update();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event){
if(ui->checkBox->checkState()==Qt::Checked){
int n = f.elementCount();
qreal x1 = event->posF().x();
qreal y1 = event->posF().y();
if(n > 1){
f.setPoint(n-1,x1,y1);
}
}
else if(ui->checkBox2->checkState()==Qt::Checked){
int n = c.elementCount();
qreal x1 = event->posF().x();
qreal y1 = event->posF().y();
if(n > 1){
c.setPoint(n-1,x1,y1);
}
}
if(ui->checkBox2->checkState()==Qt::Unchecked&&ui->checkBox->checkState()==Qt::Unchecked&&isPressed){
if(c.isMove()){
if(iPoint!=-1){
c.setPoint(iPoint,event->posF().x(),event->posF().y());
}else{
c.move(event);
}
}
}
if(ui->checkBox->checkState()==Qt::Unchecked&&ui->checkBox2->checkState()==Qt::Unchecked&&isPressed){
for(int i=0;i<vf.size();++i){
Fault&f = vf[i];
if(f.isMove()){
if(iPoint!=-1){
f.setPoint(iPoint,event->posF().x(),event->posF().y());
}else{
f.move(event);
}
}
}
}
this->update();
}
void MainWindow::paintEvent(QPaintEvent *event){
c.paint(this,event);
f.paint(this,event);
for(int i=0;i<vf.size();++i){
vf[i].paint(this,event);
}
// scalex = this->width() / 100.0;
// scaley = this->height() / 100.0;
// painter.begin(this);
// painter.setRenderHint(QPainter::Antialiasing);
// painter.scale(scalex, scaley);
// QColor color(255,30,1);
// QPen pen;
// pen.setColor(color);
// painter.setPen(pen);
// int n = rectPath.elementCount();
// if(ismove){
// painter.save();
// for(int i=0;i<n;++i){
// painter.setPen(Qt::NoPen);
// painter.setBrush(color);
// painter.drawEllipse(QPointF(rectPath.elementAt(i).x,rectPath.elementAt(i).y),3,3);
// }
// painter.restore();
// painter.drawPath(outline);
// }
// else{
// painter.drawPath(rectPath);
// }
// painter.end();
}