Repository navigation
Expand file tree
/
Copy pathwidget.cpp
More file actions
67 lines (56 loc) · 1.45 KB
/
Copy pathwidget.cpp
File metadata and controls
67 lines (56 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// widget.cpp
#include "widget.h"
#include "web.h"
#include <QPainter>
#include <QMouseEvent>
#include <QCursor>
#include <QtMath>
#include <cmath>
Widget::Widget(QWidget *parent) : QWidget(parent), isdrawing_(false) {
setMouseTracking(true);
}
Widget::~Widget() {}
void Widget::updateThreshold(int value) {
lineColor_.setRed(value);
setCursorStyle();
update();
}
void Widget::updateLineWidth(int value) {
lineWidth_ = value;
update();
}
void Widget::setCursorStyle() {
if (isdrawing_) {
setCursor(Qt::CrossCursor);
} else {
setCursor(Qt::ArrowCursor);
}
}
void Widget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
web_.draw(&painter, center_, isdrawing_, lineWidth_, lineColor_);
}
void Widget::mousePressEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton) {
isdrawing_ = true;
center_ = event->pos();
setCursorStyle();
update();
}
}
void Widget::mouseMoveEvent(QMouseEvent *event) {
if (isdrawing_) {
center_ = event->pos();
QPoint globalPos = mapToGlobal(center_);
int newX = qBound(0, globalPos.x(), width() - 1);
int newY = qBound(0, globalPos.y(), height() - 1);
QCursor::setPos(newX, newY);
update();
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event) {
if (event->button() == Qt::LeftButton && isdrawing_) {
isdrawing_ = false;
setCursorStyle();
}
}