#include "mainwindow.h" #include "ui_mainwindow.h" #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->centralWidget->setMouseTracking(true); this->setMouseTracking(true); this->showMaximized(); QPoint p1(100,70); const QRect& r = this->geometry(); initWidth = r.width(); initHeight = r.height(); QPoint p2(r.width()-100,r.height()-70); scale = Scale(5,8,4000,3000,p1,p2); bitmap = Bitmap(p1,p2); scaleCount = 8; } MainWindow::~MainWindow() { delete ui; } void MainWindow::mousePressEvent(QMouseEvent *event){ if(ui->checkBox4->checkState()==Qt::Checked && event->button()==Qt::LeftButton){ rect.setTopLeft(event->pos()); } if(ui->checkBox3->checkState()==Qt::Checked && event->button()==Qt::LeftButton){ circular.setCenter(event->posF()); } if(ui->checkBox->checkState()==Qt::Unchecked){ for(int i=0;iposF())){ isPressed = true; iPoint = f.dotContains(event->posF()); f.pick(event); break; } if(event->button()==Qt::RightButton){ f.setMove(false); } } } if(event->button()==Qt::RightButton){ c.setMove(false); } 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){ if(!isPressed&&c.contains(event->posF())){ isPressed = true; iPoint = c.dotContains(event->posF()); c.pick(event); } } if(event->button()==Qt::RightButton){ ui->checkBox4->setCheckState(Qt::Unchecked); ui->checkBox3->setCheckState(Qt::Unchecked); } this->update(); } void MainWindow::resizeEvent(QResizeEvent *event) { QPoint p1(100,70); const QRect& r = this->geometry(); // sx = (double)event->size().width()/initWidth; // sy = (double)event->size().height()/initHeight; QPoint p2(r.width()-100,r.height()-70); scale.setTopLeft(p1); scale.setBottomRight(p2); bitmap.setTopLeft(p1); bitmap.setBottomRight(p2); this->update(); } void MainWindow::wheelEvent(QWheelEvent *event) { int d = event->delta(); if(d>0){ scaleCount+=2; }else if(d<0){ scaleCount-=2; } scale.setScaleCount(qAbs(scaleCount)%24); this->update(); } void MainWindow::mouseReleaseEvent(QMouseEvent *event){ isPressed = false; this->update(); } void MainWindow::mouseMoveEvent(QMouseEvent *event){ if(ui->checkBox3->checkState()==Qt::Checked){ QPointF p = circular.center(); QPointF p2 = event->posF(); double r = qSqrt(qPow(p.x()-p2.x(), 2) + qPow(p.y()-p2.y(), 2)); circular.setRadius(r); } if(ui->checkBox4->checkState()==Qt::Checked){ rect.setBottomRight(event->pos()); } 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;iposF().x(),event->posF().y()); }else{ f.move(event); } } } } this->update(); } void MainWindow::paintEvent(QPaintEvent *event){ QPixmap mp(size()); mp.fill(Qt::white); scale.paint(mp,event); bitmap.paint(mp,event); c.paint(mp,event); f.paint(mp,event); for(int i=0;i