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.

302 lines
6.7 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#ifndef FAULT_H
#define FAULT_H
//
#include "basicgeometryoperator.h"
//Qt
#include <QColor>
#include <QPainter>
#include <QPainterPath>
#include <QPaintEvent>
#include <QVector>
#include <QPainterPathStroker>
#include <QPen>
#include <QWidget>
#include <QPaintEngine>
class Fault
{
public:
Fault(double scalex,double scaley,bool bmove,double tolerance, QString title = "Fault_0"):_scalex(scalex),_scaley(scaley),
_move(bmove),_tolerance(tolerance){
_title = title;
_isGenerated = false;
_painter = new QPainter();
}
Fault(QString title = "Fault_0"):_scalex(1),_scaley(1),
_move(false),_tolerance(30){
_title = title;
_isGenerated = false;
_painter = new QPainter();
}
~Fault(){
clear();
}
void addPoint(qreal x,qreal y){
if(_rectPath.elementCount()==0){
_rectPath.moveTo(x,y);
}else{
_rectPath.lineTo(x,y);
}
_points.push_back(QPointF(x,y));
}
void setPoint(int i,qreal x,qreal y){
_rectPath.setElementPositionAt(i,x,y);
_points[i] = QPointF(x,y);
if(_move){
setDotline();
setTitle(_title);
}
}
int pointCount(){
return _points.count();
}
int elementCount(){
return _rectPath.elementCount();
}
void setColor(int red,int green,int blue,int alpha = 50){
_faultColor.setRgb(red,green,blue,alpha);
}
QColor getColor() const{
return _faultColor;
}
QString getTitle() const{
return _title;
}
void setTitle(const QString& title){
_title = title;
_titlePath = QPainterPath();
_titlePath.addText(_rectPath.elementAt(_rectPath.elementCount()-1).x+1,
_rectPath.elementAt(_rectPath.elementCount()-1).y+1,
QFont(),_title);
}
void saveData(){
// TODO
}
void setScalex(double scalex){
_scalex = scalex;
}
double getScalex() const{
return _scalex;
}
double getScaley() const{
return _scaley;
}
void setScaley(double scaley){
_scaley = scaley;
}
bool isMove() const
{
return _move;
}
void setMove(bool move)
{
_move = move;
}
QPainterPath getRectPath() const
{
return _rectPath;
}
void addRectPath(const QPainterPath &rectPath)
{
_rectPath.addPath(rectPath);
}
QVector<QPointF> getPoints() const
{
return _points;
}
double getTolerance() const
{
return _tolerance;
}
void setTolerance(double value)
{
_tolerance = value;
}
void clear(){
_titlePath = QPainterPath();
_rectPath = QPainterPath();
_dotPath = QPainterPath();
_points.clear();
_isGenerated = false;
_move = false;
_title = "Fault_0";
last = QPointF();
_tolerance = 30;
}
void generate(){
_isGenerated = true;
_points.pop_back();
int n = _points.count();
if(n < 2){
clear();
return;
}
_rectPath = QPainterPath();
_rectPath.addPolygon(_points);
_titlePath = QPainterPath();
_titlePath.addText(_rectPath.elementAt(_rectPath.elementCount()-1).x+1,
_rectPath.elementAt(_rectPath.elementCount()-1).y+1,
QFont(),_title);
}
void move(QMouseEvent *event){
qreal x = event->posF().x();
qreal y = event->posF().y();
qreal tx = x - last.x();
qreal ty = y - last.y();
_rectPath.translate(tx,ty);
_dotPath.translate(tx,ty);
_titlePath.translate(tx,ty);
int n = _points.count();
for(int i=0;i<n;++i){
qreal rx = _points[i].x();
qreal ry = _points[i].y();
_points[i].setX(rx+tx);
_points[i].setY(ry+ty);
}
last = event->posF();
}
bool isGenerated(){
return _isGenerated;
}
void pick(QMouseEvent* event){
QPointF f = event->posF();
setDotline();
if(!contains(f)){
return;
}
setMove(true);
last = f;
}
void setDotline(){
QPainterPathStroker stroker;
stroker.setCapStyle(Qt::RoundCap);
stroker.setDashPattern(Qt::DashLine);
stroker.setJoinStyle(Qt::RoundJoin);
stroker.setWidth(2);
_dotPath = stroker.createStroke(_rectPath);
}
void clearDotPath(){
_dotPath = QPainterPath();
}
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))||
_dotPath.contains(f+QPointF(0,-i))||
_dotPath.contains(f+QPointF(0,i))){
return true;
}
}
return false;
}
int dotContains(const QPointF& f){
qreal x = f.x();
qreal y = f.y();
for(int j=0;j<_points.size();++j) {
qreal x1 = _points[j].x();
qreal y1 = _points[j].y();
for(int i=0;i<=_tolerance;++i){
if(x1 <= (x+i)&&
x1 >= (x-i)&&
y1 >= (y-i)&&
y1 <= (y+i)){
return j;
}
}
}
return -1;
}
void paint(QWidget *w, QPaintEvent *event){
_painter->begin(w);
_painter->scale(_scalex, _scaley);
_painter->setRenderHint(QPainter::Antialiasing);
QPen pen;
pen.setWidth(2);
pen.setColor(_faultColor);
_painter->setPen(pen);
int n = _points.count();
if(_move){
for(int i=0;i<n;++i){
_painter->drawEllipse(QPointF(_rectPath.elementAt(i).x,_rectPath.elementAt(i).y),3,3);
}
_painter->drawPath(_dotPath);
}
else{
_painter->drawPath(_rectPath);
}
if(_titlePath.elementCount()>0){
pen.setWidth(1);
_painter->setPen(pen);
_painter->drawPath(_titlePath);
}
_painter->end();
}
private:
QColor _faultColor;
// 记录标题比如fault_1
QString _title;
QVector<QPointF> _points;
QPainter *_painter;
QPointF last;
QWidget *w;
// 缩放
double _scalex;
double _scaley;
// 移动
bool _move;
bool _isGenerated;
// 容差
double _tolerance;
// fault的路径
QPainterPath _rectPath;
// title
QPainterPath _titlePath;
// fault的虚线框
QPainterPath _dotPath;
};
#endif // FAULT_H