1
#include "hoverpoints.h"
5
HoverPoints::HoverPoints(QWidget *widget, PointShape shape)
9
widget->installEventFilter(this);
11
m_connectionType = CurveConnection;
14
m_pointPen = QPen(QColor(255, 255, 255, 191), 1);
15
m_connectionPen = QPen(QColor(255, 255, 255, 127), 2);
16
m_pointBrush = QBrush(QColor(191, 191, 191, 127));
17
m_pointSize = QSize(11, 11);
22
connect(this, SIGNAL(pointsChanged(const QPolygonF &)),
23
m_widget, SLOT(update()));
27
void HoverPoints::setEnabled(bool enabled)
29
if (m_enabled != enabled) {
36
bool HoverPoints::eventFilter(QObject *object, QEvent *event)
38
if (object == m_widget && m_enabled) {
39
switch (event->type()) {
41
case QEvent::MouseButtonPress:
43
QMouseEvent *me = (QMouseEvent *) event;
45
QPointF clickPos = me->pos();
47
for (int i=0; i<m_points.size(); ++i) {
49
if (m_shape == CircleShape)
50
path.addEllipse(pointBoundingRect(i));
52
path.addRect(pointBoundingRect(i));
54
if (path.contains(clickPos)) {
60
if (me->button() == Qt::LeftButton) {
65
// Insert sort for x or y
66
if (m_sortType == XSort) {
67
for (int i=0; i<m_points.size(); ++i)
68
if (m_points.at(i).x() > clickPos.x()) {
72
} else if (m_sortType == YSort) {
73
for (int i=0; i<m_points.size(); ++i)
74
if (m_points.at(i).y() > clickPos.y()) {
80
m_points.insert(pos, clickPos);
81
m_locks.insert(pos, 0);
85
m_currentIndex = index;
89
} else if (me->button() == Qt::RightButton) {
90
if (index >= 0 && m_editable) {
91
if (m_locks[index] == 0) {
92
m_locks.remove(index);
93
m_points.remove(index);
103
case QEvent::MouseButtonRelease:
107
case QEvent::MouseMove:
108
if (m_currentIndex >= 0)
109
movePoint(m_currentIndex, ((QMouseEvent *)event)->pos());
114
QResizeEvent *e = (QResizeEvent *) event;
115
double stretch_x = e->size().width() / double(e->oldSize().width());
116
double stretch_y = e->size().height() / double(e->oldSize().height());
117
for (int i=0; i<m_points.size(); ++i) {
118
QPointF p = m_points[i];
119
movePoint(i, QPointF(p.x() * stretch_x, p.y() * stretch_y), false);
128
QWidget *that_widget = m_widget;
130
QApplication::sendEvent(object, event);
131
m_widget = that_widget;
144
void HoverPoints::paintPoints()
146
QPainter p(m_widget);
148
p.setRenderHint(QPainter::Antialiasing);
150
if (m_connectionPen.style() != Qt::NoPen && m_connectionType != NoConnection) {
151
p.setPen(m_connectionPen);
153
if (m_connectionType == CurveConnection) {
155
path.moveTo(m_points.at(0));
156
for (int i=1; i<m_points.size(); ++i) {
157
QPointF p1 = m_points.at(i-1);
158
QPointF p2 = m_points.at(i);
159
double distance = p2.x() - p1.x();
161
path.cubicTo(p1.x() + distance / 2, p1.y(),
162
p1.x() + distance / 2, p2.y(),
167
p.drawPolyline(m_points);
171
p.setPen(m_pointPen);
172
p.setBrush(m_pointBrush);
174
for (int i=0; i<m_points.size(); ++i) {
175
QRectF bounds = pointBoundingRect(i);
176
if (m_shape == CircleShape)
177
p.drawEllipse(bounds);
183
static QPointF bound_point(const QPointF &point, const QRectF &bounds, int lock)
187
double left = bounds.left();
188
double right = bounds.right();
189
double top = bounds.top();
190
double bottom = bounds.bottom();
192
if (p.x() < left || (lock & HoverPoints::LockToLeft)) p.setX(left);
193
else if (p.x() > right || (lock & HoverPoints::LockToRight)) p.setX(right);
195
if (p.y() < top || (lock & HoverPoints::LockToTop)) p.setY(top);
196
else if (p.y() > bottom || (lock & HoverPoints::LockToBottom)) p.setY(bottom);
201
void HoverPoints::setPoints(const QPolygonF &points)
204
for (int i=0; i<points.size(); ++i)
205
m_points << bound_point(points.at(i), boundingRect(), 0);
208
if (m_points.size() > 0) {
209
m_locks.resize(m_points.size());
216
void HoverPoints::movePoint(int index, const QPointF &point, bool emitUpdate)
218
m_points[index] = bound_point(point, boundingRect(), m_locks.at(index));
224
inline static bool x_less_than(const QPointF &p1, const QPointF &p2)
226
return p1.x() < p2.x();
230
inline static bool y_less_than(const QPointF &p1, const QPointF &p2)
232
return p1.y() < p2.y();
235
void HoverPoints::firePointChange()
237
// printf("HoverPoints::firePointChange(), current=%d\n", m_currentIndex);
239
if (m_sortType != NoSort) {
242
if (m_currentIndex != -1) {
243
oldCurrent = m_points[m_currentIndex];
246
if (m_sortType == XSort)
247
qSort(m_points.begin(), m_points.end(), x_less_than);
248
else if (m_sortType == YSort)
249
qSort(m_points.begin(), m_points.end(), y_less_than);
251
// Compensate for changed order...
252
if (m_currentIndex != -1) {
253
for (int i=0; i<m_points.size(); ++i) {
254
if (m_points[i] == oldCurrent) {
261
// printf(" - firePointChange(), current=%d\n", m_currentIndex);
265
// for (int i=0; i<m_points.size(); ++i) {
266
// printf(" - point(%2d)=[%.2f, %.2f], lock=%d\n",
267
// i, m_points.at(i).x(), m_points.at(i).y(), m_locks.at(i));
270
emit pointsChanged(m_points);