Корретная отрисовка в Canvas

This commit is contained in:
2025-12-07 20:49:47 +03:00
parent 0cb599e0e4
commit c9d3a73255
4 changed files with 64 additions and 21 deletions

View File

@@ -84,26 +84,29 @@ void Canvas::mousePressEvent(QMouseEvent* event)
current_line = nullptr; current_line = nullptr;
mode = Mode::None; mode = Mode::None;
obj_count++; obj_count++;
}
update(); update();
return; return;
} }
}
else if (mode == Mode::Parallel) { else if (mode == Mode::Parallel) {
Line* found = findAt(scene); Line* found = findAt(scene);
if (!found) { if (!found) {
current_line = nullptr; current_line = nullptr;
update();
return; return;
} }
if (!current_line) { if (!current_line) {
current_line = found; current_line = found;
update();
return; return;
} }
if (found == current_line){ if (found == current_line){
current_line = nullptr; current_line = nullptr;
update();
return; return;
} }
@@ -111,9 +114,9 @@ void Canvas::mousePressEvent(QMouseEvent* event)
sys.addConstraintParallel(*found, *current_line, constraints_count++); sys.addConstraintParallel(*found, *current_line, constraints_count++);
parallelPairs.insert(makeOrderedPair(found, current_line)); parallelPairs.insert(makeOrderedPair(found, current_line));
current_line = nullptr; current_line = nullptr;
update();
mode = Mode::None; mode = Mode::None;
} }
else { else {
#ifdef _DEBUG #ifdef _DEBUG
@@ -125,6 +128,7 @@ void Canvas::mousePressEvent(QMouseEvent* event)
QMessageBox::Ok QMessageBox::Ok
); );
current_line = nullptr; current_line = nullptr;
update();
return; return;
} }
@@ -136,16 +140,19 @@ void Canvas::mousePressEvent(QMouseEvent* event)
if (!clickedPoint) { if (!clickedPoint) {
firstPoint = nullptr; firstPoint = nullptr;
update();
return; return;
} }
if (!firstPoint) { if (!firstPoint) {
firstPoint = clickedPoint; firstPoint = clickedPoint;
update();
return; return;
} }
if (clickedPoint == firstPoint) { if (clickedPoint == firstPoint) {
firstPoint = nullptr; firstPoint = nullptr;
update();
return; return;
} }
@@ -158,6 +165,7 @@ void Canvas::mousePressEvent(QMouseEvent* event)
if (l1 == l2) { if (l1 == l2) {
QMessageBox::warning(this, QString("NO!"), QString("P2P failed")); QMessageBox::warning(this, QString("NO!"), QString("P2P failed"));
firstPoint = nullptr; firstPoint = nullptr;
update();
return; return;
} }
@@ -172,23 +180,66 @@ void Canvas::mousePressEvent(QMouseEvent* event)
void Canvas::paintEvent(QPaintEvent*) void Canvas::paintEvent(QPaintEvent*)
{ {
QPainter p(this); QPainter p(this);
p.setRenderHint(QPainter::Antialiasing); p.setRenderHint(QPainter::Antialiasing, true);
// === Решаем систему один раз ===
if (!params.empty()) { if (!params.empty()) {
int res = sys.solve(params); int res = sys.solve(params);
if (res == SolveStatus::Success) { if (res == SolveStatus::Success || res == SolveStatus::Converged) {
sys.applySolution(); sys.applySolution();
} }
else { else {
#ifdef _DEBUG
qDebug() << "INVAILD LAST CONSTRAINT. REMOVE HIM";
#endif
sys.removeConstraint(sys.get_last_constraint()); sys.removeConstraint(sys.get_last_constraint());
constraints_count--;
} }
} }
for (Line* line : lines) { for (Line* line : lines) {
line->draw(p); bool isSelected = (mode == Mode::Parallel && line == current_line);
QPen linePen = isSelected ? QPen(Qt::red, 4) : QPen(Qt::black, 2);
p.setPen(linePen);
// Рисуем саму линию
p.drawLine(QPointF(*line->p1.x, *line->p1.y),
QPointF(*line->p2.x, *line->p2.y));
// Рисуем концы — с учётом выделения
QBrush pointBrush = isSelected ? QBrush(Qt::red) : QBrush(Qt::darkBlue);
p.setBrush(pointBrush);
p.setPen(Qt::NoPen); // убираем обводку у кружков
p.drawEllipse(QPointF(*line->p1.x, *line->p1.y), 5, 5);
p.drawEllipse(QPointF(*line->p2.x, *line->p2.y), 5, 5);
}
// === Подсветка выбранной точки в режиме Coincident ===
if (mode == Mode::Coincedent && firstPoint) {
QPointF pt(*firstPoint->x, *firstPoint->y);
p.setPen(Qt::NoPen);
p.setBrush(Qt::red);
p.drawEllipse(pt, 12, 12);
p.setBrush(Qt::white);
p.drawEllipse(pt, 8, 8);
p.setBrush(Qt::red);
p.drawEllipse(pt, 4, 4); // маленький центр
}
// === Текущая рисуемая линия (DrawingLine) ===
if (current_line && mode == Mode::DrawingLine) {
QPen pen(Qt::blue, 3, Qt::DashLine);
p.setPen(pen);
p.setBrush(Qt::transparent);
p.drawLine(QPointF(*current_line->p1.x, *current_line->p1.y),
QPointF(*current_line->p2.x, *current_line->p2.y));
// Концы текущей линии — синие точки
p.setBrush(Qt::blue);
p.setPen(Qt::NoPen);
p.drawEllipse(QPointF(*current_line->p1.x, *current_line->p1.y), 6, 6);
p.drawEllipse(QPointF(*current_line->p2.x, *current_line->p2.y), 6, 6);
} }
return;
} }
Canvas::Canvas(QWidget *parent) Canvas::Canvas(QWidget *parent)

View File

@@ -250,6 +250,10 @@ public:
void clearByTag(int tagId, bool is_fixed_point = false); void clearByTag(int tagId, bool is_fixed_point = false);
int addConstraint(Constraint* constr); int addConstraint(Constraint* constr);
void removeConstraint(Constraint* constr); void removeConstraint(Constraint* constr);
std::vector<Constraint*> get_clist(){
return clist;
}
Constraint* get_last_constraint() { Constraint* get_last_constraint() {
return clist[clist.size() - 1]; return clist[clist.size() - 1];
} }

View File

@@ -218,17 +218,6 @@ Line* Line::Copy()
return crv; return crv;
} }
void Line::draw(QPainter& p, bool highlight){
QPen pen = highlight ? QPen(Qt::red, 4) : QPen(Qt::black, 2);
p.setPen(pen);
p.drawLine(QPointF(p1.get_X(), p1.get_Y()), QPointF(p2.get_X(), p2.get_Y()));
p.setBrush(highlight ? Qt::red : Qt::blue);
p.drawEllipse(QPointF(p1.get_X(), p1.get_Y()), 4, 4);
p.drawEllipse(QPointF(p2.get_X(), p2.get_Y()), 4, 4);
}
//---------------circle //---------------circle
DeriVector2 Circle::CalculateNormal(const Point& p, const double* derivparam) const DeriVector2 Circle::CalculateNormal(const Point& p, const double* derivparam) const

View File

@@ -226,7 +226,6 @@ public:
void ReconstructOnNewPvec(VEC_pD& pvec, int& cnt) override; void ReconstructOnNewPvec(VEC_pD& pvec, int& cnt) override;
Line* Copy() override; Line* Copy() override;
void draw(QPainter& p, bool highlight = false);
void set_tag(int); void set_tag(int);
bool contains(QPointF, qreal tol = 10.0) const; bool contains(QPointF, qreal tol = 10.0) const;
}; };