From 9f2976be4376b57db9f68f881fbb0b27ee978939 Mon Sep 17 00:00:00 2001 From: abc Date: Tue, 8 Oct 2024 18:56:36 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=AE=8C=E5=96=84=E4=BA=86=E8=BD=AE?= =?UTF-8?q?=E5=BB=93=E5=92=8C=E6=96=AD=E5=B1=82=202.=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E4=BD=8D=E5=9B=BE=EF=BC=8C=E5=9C=86=E5=BD=A2=EF=BC=8C=E7=9F=A9?= =?UTF-8?q?=E5=BD=A2=EF=BC=8C=E6=AF=94=E4=BE=8B=E5=B0=BA=E7=9A=84=E9=9B=8F?= =?UTF-8?q?=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aoi.h | 27 ------ basicgeometry.h | 36 -------- basicgeometryoperator.h | 14 --- bitmap.h | 122 +++++++++++++++++++++++++ circular.h | 84 +++++++++++++++++ contour.h | 124 +++++++++---------------- fault.h | 57 ++++++------ mainwindow.cpp | 163 +++++++++++++++++++++------------ mainwindow.h | 43 +++++++-- mainwindow.ui | 55 +++++++++-- oilpool.pro | 14 +-- readme.md | 9 ++ rectangle.h | 106 ++++++++++++++++++++++ scale.h | 196 ++++++++++++++++++++++++++++++++++++++++ 14 files changed, 782 insertions(+), 268 deletions(-) delete mode 100644 aoi.h delete mode 100644 basicgeometry.h delete mode 100644 basicgeometryoperator.h create mode 100644 bitmap.h create mode 100644 circular.h create mode 100644 readme.md create mode 100644 rectangle.h create mode 100644 scale.h diff --git a/aoi.h b/aoi.h deleted file mode 100644 index f3d6aa9..0000000 --- a/aoi.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef AOI_H -#define AOI_H - -// -#include "basicgeometryoperator.h" -#include "contour.h" - - -//std -#include -#include - - -//Qt -#include -#include - -class aoi : public BasicGeometryOperator -{ -public: - aoi(); - -private: -// Outline outline; -}; - -#endif // AOI_H diff --git a/basicgeometry.h b/basicgeometry.h deleted file mode 100644 index ac95ef9..0000000 --- a/basicgeometry.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef BASICGEOMETRY_H -#define BASICGEOMETRY_H - - -class BasicGeometry -{ -public: - BasicGeometry(); - virtual void set_color(int red,int green, int blue)=0; - - virtual void set_unit(QString unit)=0; - - virtual void set_move(bool isMove)=0; - - - // points - virtual void add_points(int x,int y)=0; - virtual int get_point_id()=0; - virtual void remove_by_id()=0;// 删除一个点之后,是否遍历拓扑表删除和这个点相关的所有数据 - - // topology - virtual void add_topology()=0; - virtual int get_topology_id()=0; - virtual void remove_by_id()=0; - - // rgb - virtual int red()=0; - virtual int green()=0; - virtual int blue()=0; - - virtual QString unit()=0; - virtual bool isMove()=0; - -}; - -#endif // BASICGEOMETRY_H diff --git a/basicgeometryoperator.h b/basicgeometryoperator.h deleted file mode 100644 index df21807..0000000 --- a/basicgeometryoperator.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef BASICGEOMETRYOPERATOR_H -#define BASICGEOMETRYOPERATOR_H - - -class BasicGeometryOperator -{ -public: - virtual void paint()=0; - virtual void saveData()=0; - virtual void pick()=0;// 点、线、单元,区域 四种拾取 - virtual void move()=0; -}; - -#endif // BASICGEOMETRYOPERATOR_H diff --git a/bitmap.h b/bitmap.h new file mode 100644 index 0000000..5c0a22b --- /dev/null +++ b/bitmap.h @@ -0,0 +1,122 @@ +#ifndef BITMAP_H +#define BITMAP_H + + + +//Qt +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +class Bitmap +{ +public: + Bitmap(const QPoint& topLeft,const QPoint& bottomRight,QString fileName=nullptr){ + _topLeft = topLeft; + _bottomRight = bottomRight; + _scaley = 1; + _scalex = 1; + + // TODO 需要加入文件检测判断文件是否存在 + _pixmap = QPixmap(fileName); + + } + + Bitmap(){ + _scaley = 1; + _scalex = 1; + _pixmap = QPixmap(); + } + + void paint(QPixmap& w, QPaintEvent *event=nullptr){ + + QPainter painter; + painter.begin(&w); + QPoint center = QPoint((_topLeft.x()+_bottomRight.x())/2/_scalex,(_topLeft.y()+_bottomRight.y())/2/_scaley); + QPixmap p; + center -= QPoint(_pixmap.width()/2,_pixmap.height()/2); + painter.scale(_scalex,_scaley); + painter.drawPixmap(center,_pixmap); + painter.end(); + } + + QPoint topLeft() const + { + return _topLeft; + } + + void setTopLeft(const QPoint &topLeft) + { + _topLeft = topLeft; + } + + QPoint bottomRight() const + { + return _bottomRight; + } + + void setBottomRight(const QPoint &bottomRight) + { + _bottomRight = bottomRight; + } + + double scalex() const + { + return _scalex; + } + + void setScalex(double scalex) + { + _scalex = scalex; + } + + double scaley() const + { + return _scaley; + } + + void setScaley(double scaley) + { + _scaley = scaley; + } + + QPixmap getPixmap() const + { + return _pixmap; + } + + void setPixmap(const QPixmap &value) + { + _pixmap = value; + } + void setPixmap(const QString& fileName) + { + _pixmap = QPixmap(fileName);; + } + +private: + QPixmap _pixmap; + QPoint _topLeft; + QPoint _bottomRight; + QColor _scaleColor; + + // 缩放 + double _scalex; + double _scaley; +}; + +#endif // BITMAP_H + + diff --git a/circular.h b/circular.h new file mode 100644 index 0000000..424f936 --- /dev/null +++ b/circular.h @@ -0,0 +1,84 @@ +#ifndef CIRCULAR_H +#define CIRCULAR_H + +//Qt +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +class Circular +{ +public: + Circular(){ + _scalex = 1; + _scaley = 1; + } + Circular(const QPointF& center){ + _center = center; + _scalex = 1; + _scaley = 1; + } + + void paint(QPixmap& w, QPaintEvent* event=nullptr){ + QPainter painter; + painter.begin(&w); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.scale(_scalex,_scaley); + painter.setPen(QPen(Qt::black, 0.8)); + painter.drawEllipse(_center,_radius,_radius); + painter.end(); + } + + QPointF center() const + { + return _center; + } + + void setCenter(const QPointF ¢er) + { + _center = center; + } + + QColor color() const + { + return _color; + } + + void setColor(const QColor &color) + { + _color = color; + } + + double radius() const + { + return _radius; + } + + void setRadius(double radius) + { + _radius = radius; + } + +private: + QPointF _center; + double _radius; + double _scalex; + double _scaley; + QColor _color; +}; + +#endif // CIRCULAR_H + + + + diff --git a/contour.h b/contour.h index 6ce13da..911c026 100644 --- a/contour.h +++ b/contour.h @@ -1,9 +1,6 @@ #ifndef OUTLINE_H #define OUTLINE_H -// -#include "basicgeometryoperator.h" - //Qt #include @@ -17,23 +14,27 @@ #include #include #include -#define d(x) qDebug()<<__FUNCTION__<<(x) +#include + class Contour { public: - Contour(double scalex,double scaley,bool bmove,double tolerance, QString title = "Fault_0"):_scalex(scalex),_scaley(scaley), - _move(bmove),_tolerance(tolerance){ + Contour(double scalex,double scaley,bool bmove,double tolerance, const QString& title){ _title = title; - _painter = new QPainter(); + _scalex = scalex; + _scaley = scaley; + _move = bmove; + _tolerance = tolerance; } - Contour(QString title = "contour_0"):_scalex(1),_scaley(1), - _move(false),_tolerance(15){ + Contour(QString title = "contour_0"){ _title = title; - _painter = new QPainter(); + _scalex = 1; + _scaley = 1; + _move = false; + _tolerance = 10; } - ~Contour(){} void addPoint(qreal x,qreal y){ QPointF f(x,y); @@ -53,8 +54,6 @@ public: for(int i=0;i=0 || cdca*cdcb >=0); } @@ -100,39 +89,10 @@ public: _points[i] = f; _points[_rectPath.elementCount()-1] = f; } - setDotline(); + getDotline(); } } - bool isInside(const QPointF& a){ - int n = _points.count(); - for(int i=1;i=qMin(_points[i].x(),_points[i+1].x())){ - return true; - } - } - } - return false; - } - int pointCount(){ return _points.count(); } @@ -153,7 +113,7 @@ public: return _title; } - void setTitle(QString title){ + void setTitle(const QString& title){ _title = title; } @@ -195,7 +155,7 @@ public: _rectPath.addPath(rectPath); } - QVector getPoints() const + QVector Points() const { return _points; } @@ -228,7 +188,7 @@ public: _rectPath.addPolygon(_points); _rectPath.closeSubpath(); _rectPath.setFillRule(Qt::OddEvenFill); - setDotline(); + getDotline(); } @@ -258,7 +218,7 @@ public: last = f; } - void setDotline(){ + void getDotline(){ QPainterPathStroker stroker; stroker.setCapStyle(Qt::RoundCap); stroker.setDashPattern(Qt::DashLine); @@ -301,28 +261,29 @@ public: } - void paint(QWidget *w, QPaintEvent *event){ - _painter->begin(w); - _painter->scale(_scalex, _scaley); - _painter->setRenderHint(QPainter::Antialiasing); + void paint(QPixmap& w, QPaintEvent *event=nullptr){ + QPainter painter; + painter.begin(&w); + painter.scale(_scalex, _scaley); + painter.setRenderHint(QPainter::Antialiasing); pen.setColor(_contourColor); pen.setWidth(2); - _painter->setPen(pen); - _painter->setBrush(_contourColor); + painter.setPen(pen); + painter.setBrush(_contourColor); int n = _points.count(); if(_move){ - _painter->save(); + painter.save(); for(int i=0;isetPen(Qt::NoPen); - _painter->setBrush(_contourColor); - _painter->drawEllipse(QPointF(_rectPath.elementAt(i).x,_rectPath.elementAt(i).y),5,5); + painter.setPen(Qt::NoPen); + painter.setBrush(_contourColor); + painter.drawEllipse(QPointF(_rectPath.elementAt(i).x,_rectPath.elementAt(i).y),5,5); } - _painter->restore(); - _painter->drawPath(_dotPath); + painter.restore(); + painter.drawPath(_dotPath); }else{ - _painter->drawPath(_rectPath); + painter.drawPath(_rectPath); } - _painter->end(); + painter.end(); } private: @@ -331,7 +292,6 @@ private: // 记录标题,比如contour_1 QString _title; QVector _points; - QPainter *_painter; QPointF last; diff --git a/fault.h b/fault.h index e64f60e..598680d 100644 --- a/fault.h +++ b/fault.h @@ -1,8 +1,6 @@ #ifndef FAULT_H #define FAULT_H -// -#include "basicgeometryoperator.h" //Qt @@ -15,22 +13,27 @@ #include #include #include +#include class Fault { public: - Fault(double scalex,double scaley,bool bmove,double tolerance, QString title = "Fault_0"):_scalex(scalex),_scaley(scaley), - _move(bmove),_tolerance(tolerance){ + Fault(double scalex,double scaley,bool bmove,double tolerance, const QString& title){ _title = title; _isGenerated = false; - _painter = new QPainter(); + _scalex = scalex; + _scaley = scaley; + _move = bmove; + _tolerance = tolerance; } - Fault(QString title = "Fault_0"):_scalex(1),_scaley(1), - _move(false),_tolerance(30){ + Fault(QString title = "Fault_0"){ + _scalex = 1; + _scaley = 1; _title = title; _isGenerated = false; - _painter = new QPainter(); + _tolerance = 15; + _move = false; } ~Fault(){ @@ -51,7 +54,7 @@ public: _points[i] = QPointF(x,y); if(_move){ - setDotline(); + getDotline(); setTitle(_title); } } @@ -161,6 +164,8 @@ public: _titlePath.addText(_rectPath.elementAt(_rectPath.elementCount()-1).x+1, _rectPath.elementAt(_rectPath.elementCount()-1).y+1, QFont(),_title); + getDotline(); + } void move(QMouseEvent *event){ @@ -186,7 +191,7 @@ public: void pick(QMouseEvent* event){ QPointF f = event->posF(); - setDotline(); + getDotline(); if(!contains(f)){ return; } @@ -194,12 +199,12 @@ public: last = f; } - void setDotline(){ + void getDotline(){ QPainterPathStroker stroker; stroker.setCapStyle(Qt::RoundCap); stroker.setDashPattern(Qt::DashLine); stroker.setJoinStyle(Qt::RoundJoin); - stroker.setWidth(2); + stroker.setWidth(1); _dotPath = stroker.createStroke(_rectPath); } @@ -208,8 +213,6 @@ public: } bool contains(const QPointF& f){ - qreal x = f.x(); - qreal y = f.y(); for(int i=0;i<=_tolerance;++i){ if(_dotPath.contains(f+QPointF(i,0))|| _dotPath.contains(f+QPointF(-i,0))|| @@ -239,30 +242,31 @@ public: return -1; } - void paint(QWidget *w, QPaintEvent *event){ - _painter->begin(w); - _painter->scale(_scalex, _scaley); - _painter->setRenderHint(QPainter::Antialiasing); + void paint(QPixmap& w, QPaintEvent *event=nullptr){ + QPainter painter; + painter.begin(&w); + painter.scale(_scalex, _scaley); + painter.setRenderHint(QPainter::Antialiasing); QPen pen; pen.setWidth(2); pen.setColor(_faultColor); - _painter->setPen(pen); + painter.setPen(pen); int n = _points.count(); if(_move){ for(int i=0;idrawEllipse(QPointF(_rectPath.elementAt(i).x,_rectPath.elementAt(i).y),3,3); + painter.drawEllipse(QPointF(_rectPath.elementAt(i).x,_rectPath.elementAt(i).y),3,3); } - _painter->drawPath(_dotPath); + painter.drawPath(_dotPath); } else{ - _painter->drawPath(_rectPath); + painter.drawPath(_rectPath); } if(_titlePath.elementCount()>0){ pen.setWidth(1); - _painter->setPen(pen); - _painter->drawPath(_titlePath); + painter.setPen(pen); + painter.drawPath(_titlePath); } - _painter->end(); + painter.end(); } private: @@ -271,10 +275,9 @@ private: // 记录标题,比如fault_1 QString _title; QVector _points; - QPainter *_painter; +// QPainter *_painter; QPointF last; - QWidget *w; // 缩放 double _scalex; diff --git a/mainwindow.cpp b/mainwindow.cpp index 9ed7ad4..0a4c245 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -2,20 +2,25 @@ #include "ui_mainwindow.h" #include +#include 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; + 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() @@ -24,26 +29,35 @@ MainWindow::~MainWindow() } void MainWindow::mousePressEvent(QMouseEvent *event){ - for(int i=0;iposF())){ - 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->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(); @@ -82,16 +96,65 @@ void MainWindow::mousePressEvent(QMouseEvent *event){ } } if(ui->checkBox2->checkState()==Qt::Unchecked && event->button()==Qt::LeftButton){ - c.pick(event); + 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(); @@ -134,37 +197,21 @@ void MainWindow::mouseMoveEvent(QMouseEvent *event){ } void MainWindow::paintEvent(QPaintEvent *event){ - c.paint(this,event); - f.paint(this,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;iwidth() / 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 #include "QtGui" - #include - #include #include #include #include +#include +#include +#include +#include +#include +#include +// oil #include "fault.h" #include "contour.h" +#include "scale.h" +#include "bitmap.h" +#include "circular.h" +#include "rectangle.h" namespace Ui { class MainWindow; @@ -31,23 +41,42 @@ private: QPainterPath rectPath; QPainterPath outline; QPainter painter; - int x; - int y; - double scalex; - double scaley; - Contour c; + bool isPressed; + int iPoint; QVectorvf; Fault f; + Contour c; + + Scale scale; + Bitmap bitmap; + Circular circular; + Rectangle rect; + QPixmap *mp; + int initWidth; + int initHeight; + + int scaleCount; // QWidget interface protected: void mouseReleaseEvent(QMouseEvent *); void mouseMoveEvent(QMouseEvent *); void paintEvent(QPaintEvent *event); void mousePressEvent(QMouseEvent *event); + + + // QWidget interface +protected: + void resizeEvent(QResizeEvent *); + + + + // QWidget interface +protected: + void wheelEvent(QWheelEvent *); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index cc4f0bd..087d812 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,20 +6,42 @@ 0 0 - 560 - 482 + 1152 + 800 + + + 600 + 800 + + MainWindow + + true + + + + + 510 + 20 + 51 + 18 + + + + 轮廓 + + - 220 - 10 - 79 + 340 + 20 + 51 18 @@ -33,17 +55,30 @@ false - + - 370 - 10 + 660 + 20 79 18 - 轮廓 + 圆形 + + + + + + 800 + 20 + 79 + 18 + + + + 矩形 @@ -61,7 +96,7 @@ 0 0 - 560 + 1152 26 diff --git a/oilpool.pro b/oilpool.pro index 37cfce2..21055f9 100644 --- a/oilpool.pro +++ b/oilpool.pro @@ -6,7 +6,7 @@ QT += core gui -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets concurrent TARGET = oilpool TEMPLATE = app @@ -25,17 +25,17 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ - mainwindow.cpp \ - scalegraphicsview.cpp + mainwindow.cpp HEADERS += \ mainwindow.h \ - basicgeometry.h \ - basicgeometryoperator.h \ fault.h \ - aoi.h \ contour.h \ - scalegraphicsview.h + scale.h \ + bitmap.h \ + circular.h \ + rectangle.h FORMS += \ mainwindow.ui + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..fbedd9f --- /dev/null +++ b/readme.md @@ -0,0 +1,9 @@ +1. 几何部分的 fault.h 和contour.h 完成度较高 + +2. contour.h 画轮廓 + fault.h 画断层 + bitmap.h 画位图 + circular.h 画圆形 + rectangle.h 画矩形 + scale.h 画比例尺 + mainwindow.h和mainwindow.cpp用于测试 \ No newline at end of file diff --git a/rectangle.h b/rectangle.h new file mode 100644 index 0000000..8fa50f2 --- /dev/null +++ b/rectangle.h @@ -0,0 +1,106 @@ +#ifndef RECTANGLE_H +#define RECTANGLE_H + + +//Qt +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class Rectangle +{ +public: + Rectangle(){ + _scalex = 1; + _scaley = 1; + } + + Rectangle(double scalex,double scaley){ + _scalex = scalex; + _scaley = scaley; + } + + void paint(QPixmap&mp, QPaintEvent *event = nullptr){ + QPainter painter; + painter.begin(&mp); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.scale(_scalex,_scaley); + painter.setPen(QPen(Qt::black, 0.8)); + QRect r(_topLeft,_bottomRight); + _rect = r; + painter.drawRect(_rect); + painter.end(); + } + + + double scalex() const + { + return _scalex; + } + + void setScalex(double scalex) + { + _scalex = scalex; + } + + double scaley() const + { + return _scaley; + } + + void setScaley(double scaley) + { + _scaley = scaley; + } + + QPoint topLeft() const + { + return _topLeft; + } + + void setTopLeft(const QPoint &topLeft) + { + _topLeft = topLeft; + } + + QPoint bottomRight() const + { + return _bottomRight; + } + + void setBottomRight(const QPoint &bottomRight) + { + _bottomRight = bottomRight; + } + + QColor rectColor() const + { + return _rectColor; + } + + void setRectColor(const QColor &rectColor) + { + _rectColor = rectColor; + } + +private: + QPoint _topLeft; + QPoint _bottomRight; + QColor _rectColor; + double _scalex; + double _scaley; + QRect _rect; +}; + +#endif // RECTANGLE_H + + diff --git a/scale.h b/scale.h new file mode 100644 index 0000000..7978443 --- /dev/null +++ b/scale.h @@ -0,0 +1,196 @@ +#ifndef SCALE_H +#define SCALE_H + +//Qt +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class Scale +{ +public: + Scale(int scaleLength,int scaleCount,int width,int height,const QPoint& topleft,const QPoint& bottomright){ + _scaleLength = scaleLength; + _scaleCount = scaleCount; + _topLeft = topleft; + _bottomRight = bottomright; + _scaleColor = QColor(192,192,192); + _scalex = 1; + _scaley = 1; + _width = width; + _height = height; + } + Scale(){ + _scaleLength = 6; + _scaleCount = 8; + _topLeft = QPoint(90,90); + _bottomRight = QPoint(300,300); + _scaleColor = QColor(192,192,192); + _scalex = 1; + _scaley = 1; + _width = 0; + _height = 0; + } + + void paint(QPixmap& w, QPaintEvent* event=nullptr){ + _scaleCount = _scaleCount > 8? _scaleCount : 8; + QPainter painter; + painter.begin(&w); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.scale(_scalex,_scaley); + _rect = QRect(QPoint(_topLeft.x()/_scalex,_topLeft.y()/_scaley),QPoint(_bottomRight.x()/_scalex,_bottomRight.y()/_scaley)); + painter.setPen(QPen(Qt::black, 0.8)); + painter.setBrush(_scaleColor); + painter.drawRect(_rect); + int width = _rect.width(); + int height = _rect.height(); + int tmp = _scaleCount/2; + if(_width==0){ + _width = width; + } + if(_height==0){ + _height = height; + } + + // 上 + for (int i = 1; i < _scaleCount; ++i) { + int x = _rect.left() + (i * width / _scaleCount); + painter.drawLine(x, _rect.y(), x, _rect.y() - _scaleLength); + const QRect rectangle = QRect(QPoint(x-25, _rect.y()-50), QPoint(x+25, _rect.y()- _scaleLength)); + painter.drawText(rectangle,Qt::AlignBottom,QString::number((i-tmp) * _width / _scaleCount)); + } + + // 下 + for (int i = 1; i < _scaleCount; ++i) { + int x = _rect.left() + (i * width / _scaleCount); + painter.drawLine(x, _rect.y() + height, x, _rect.y() + height + _scaleLength); + const QRect rectangle = QRect(x, _rect.y() + height + _scaleLength,x, _rect.y() + height + 100); + painter.drawText(rectangle,Qt::AlignTop,QString::number((i-tmp) * _width / _scaleCount)); + } + + //左 + for (int i = 1; i < _scaleCount; ++i) { + int y = _rect.top() + (i * height / _scaleCount); + painter.drawLine(_rect.x() - _scaleLength, y, _rect.x(), y); + const QRect rectangle = QRect(_rect.x() -_scaleLength-100, y, _rect.x() -_scaleLength, y); + painter.drawText(rectangle,Qt::AlignRight,QString::number((i-tmp) * _height / _scaleCount)); + } + + + //右 + for (int i = 1; i < _scaleCount; ++i) { + int y = _rect.top() + (i * height / _scaleCount); + painter.drawLine(_rect.x() + width, y, _rect.x() + width + _scaleLength, y); + const QRect rectangle = QRect(_rect.x() + width + _scaleLength, y, _rect.x() + width + _scaleLength+100, y); + painter.drawText(rectangle,Qt::AlignLeft,QString::number((i-tmp) * _height / _scaleCount)); + } + painter.end(); + + } + + int scaleLength() const{ + return _scaleLength; + } + void setScaleLength(int scaleLength){ + _scaleLength = scaleLength; + } + + double scalex() const + { + return _scalex; + } + + void setScalex(double scalex) + { + _scalex = scalex; + } + + double scaley() const + { + return _scaley; + } + + void setScaley(double scaley) + { + _scaley = scaley; + } + + QRect rect() const{ + return _rect; + } + + QPoint topLeft() const + { + return _topLeft; + } + + void setTopLeft(const QPoint &topLeft) + { + _topLeft = topLeft; + } + + QPoint bottomRight() const + { + return _bottomRight; + } + + void setBottomRight(const QPoint &bottomRight) + { + _bottomRight = bottomRight; + } + + int width() const + { + return _width; + } + + void setWidth(int width) + { + _width = width; + } + + int height() const + { + return _height; + } + + void setHeight(int height) + { + _height = height; + } + + int scaleCount() const + { + return _scaleCount; + } + + void setScaleCount(int scaleCount) + { + _scaleCount = scaleCount; + } + +private: + int _scaleLength; + int _scaleCount; + QPoint _topLeft; + QPoint _bottomRight; + QRect _rect; + QColor _scaleColor; + double _scalex; + double _scaley; + int _width; + int _height; + +}; + +#endif // SCALE_H +