-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPlanarSelect.cpp
347 lines (297 loc) · 10.5 KB
/
PlanarSelect.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) 2014-2021 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Framework.hpp>
#include <QGroupBox>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsObject>
#include <QHBoxLayout>
#include <QResizeEvent>
#include <QMouseEvent>
#include <QPainter>
#include <vector>
#include <complex>
#include <iostream>
#include <algorithm> //min/max
/***********************************************************************
* Draggable crosshairs for point selection
**********************************************************************/
class PlanarSelectCrossHairs : public QGraphicsObject
{
Q_OBJECT
public:
PlanarSelectCrossHairs(void):
_length(10)
{
this->setFlag(QGraphicsItem::ItemIsMovable);
this->setFlag(QGraphicsItem::ItemIsSelectable);
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges);
}
QRectF boundingRect(void) const
{
return QRectF(QPointF(-_length/2, -_length/2), QSizeF(_length, _length));
}
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->setPen(Qt::black);
painter->drawLine(QPointF(0, -_length/2), QPointF(0, _length/2));
painter->drawLine(QPointF(-_length/2, 0), QPointF(_length/2, 0));
}
signals:
void positionChanged(const QPointF &);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value)
{
if (change == ItemPositionChange) emit this->positionChanged(value.toPointF());
return QGraphicsObject::itemChange(change, value);
}
private:
qreal _length;
};
/***********************************************************************
* Custom scene with axis background
**********************************************************************/
class PlanarSelectGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
PlanarSelectGraphicsScene(QObject *parent):
QGraphicsScene(parent)
{
return;
}
void drawBackground(QPainter *painter, const QRectF &rect)
{
QGraphicsScene::drawBackground(painter, rect);
static const QColor lightGray("#D0D0D0");
painter->setPen(lightGray);
qreal x = this->sceneRect().center().x();
qreal y = this->sceneRect().center().y();
qreal width = this->sceneRect().width();
qreal height = this->sceneRect().height();
//main center lines
painter->drawLine(QPointF(x, y-height/2), QPointF(x, y+height/2));
painter->drawLine(QPointF(x-width/2, y), QPointF(x+width/2, y));
//half-way lines
qreal length = 5;
painter->drawLine(QPointF(x-width/4, y-length), QPointF(x-width/4, y+length));
painter->drawLine(QPointF(x+width/4, y-length), QPointF(x+width/4, y+length));
painter->drawLine(QPointF(x-length, y-height/4), QPointF(x+length, y-height/4));
painter->drawLine(QPointF(x-length, y+height/4), QPointF(x+length, y+height/4));
}
};
/***********************************************************************
* Custom view with scene resize
**********************************************************************/
class PlanarSelectGraphicsView : public QGraphicsView
{
Q_OBJECT
public:
PlanarSelectGraphicsView(QWidget *parent):
QGraphicsView(parent),
_crossHairs(new PlanarSelectCrossHairs())
{
this->setScene(new PlanarSelectGraphicsScene(this));
this->scene()->setBackgroundBrush(Qt::white);
this->scene()->addItem(_crossHairs);
//set high quality rendering
this->setRenderHint(QPainter::Antialiasing);
this->setRenderHint(QPainter::SmoothPixmapTransform);
//forward position changed signal
connect(_crossHairs, &PlanarSelectCrossHairs::positionChanged, this, &PlanarSelectGraphicsView::handleCrossHairsPointChanged);
}
QPointF getPosition(void) const
{
return this->scenePosToRelPos(_crossHairs->pos());
}
public slots:
void setPosition(const QPointF &rel_)
{
//clip to 0.0 -> 1.0 to keep in bounds
QPointF rel(
std::max(std::min(rel_.x(), 1.0), 0.0),
std::max(std::min(rel_.y(), 1.0), 0.0));
const auto sr = this->scene()->sceneRect();
const auto p = QPointF(rel.x()*sr.width(), (1.0-rel.y())*sr.height());
_crossHairs->setPos(p + sr.topLeft());
}
signals:
void positionChanged(const QPointF &);
private slots:
void handleCrossHairsPointChanged(const QPointF &pos)
{
emit this->positionChanged(this->scenePosToRelPos(pos));
}
protected:
void resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
bool oldState = this->blockSignals(true);
const auto p = this->getPosition();
this->scene()->setSceneRect(QRectF(QPointF(), event->size()));
this->setPosition(p);
this->blockSignals(oldState);
}
void mousePressEvent(QMouseEvent *mouseEvent)
{
QGraphicsView::mousePressEvent(mouseEvent);
if (mouseEvent->button() == Qt::LeftButton)
{
mouseEvent->accept();
const auto pos = this->scenePosToRelPos(this->mapToScene(mouseEvent->pos()));
this->setPosition(pos);
emit this->positionChanged(pos);
}
}
private:
QPointF scenePosToRelPos(const QPointF &scenePos) const
{
const auto sr = this->scene()->sceneRect();
auto p = scenePos - sr.topLeft();
return QPointF(p.x()/sr.width(), 1.0-(p.y()/sr.height()));
}
PlanarSelectCrossHairs *_crossHairs;
};
/***********************************************************************
* |PothosDoc Planar Select
*
* A two-dimensional point selection widget.
* The point is changed graphically by dragging a crosshair across a rectangular region.
* When the crosshair point is changed, the new value is emitted
* as a two dimensional vector of doubles through the "valueChanged" signal,
* and as a complex number through the "complexValueChanged" signal.
*
* |category /Widgets
* |keywords 2d plane cartesian complex
*
* |param title The name of the value displayed by this widget
* |default "My Coordinate"
* |widget StringEntry()
*
* |param value The initial value of this slider.
* |default [0.0, 0.0]
*
* |param minimum The smallest X and Y bounds of the selection.
* |default [-1.0, -1.0]
*
* |param maximum The largest X and Y bounds of the selection.
* |default [1.0, 1.0]
*
* |mode graphWidget
* |factory /widgets/planar_select()
* |setter setTitle(title)
* |setter setMinimum(minimum)
* |setter setMaximum(maximum)
* |setter setValue(value)
**********************************************************************/
class PlanarSelect : public QGroupBox, public Pothos::Block
{
Q_OBJECT
public:
static Block *make(void)
{
return new PlanarSelect();
}
PlanarSelect(void):
_view(new PlanarSelectGraphicsView(this)),
_layout(new QHBoxLayout(this))
{
this->setStyleSheet("QGroupBox {font-weight: bold;}");
this->registerCall(this, POTHOS_FCN_TUPLE(PlanarSelect, widget));
this->registerCall(this, POTHOS_FCN_TUPLE(PlanarSelect, value));
this->registerCall(this, POTHOS_FCN_TUPLE(PlanarSelect, setTitle));
this->registerCall(this, POTHOS_FCN_TUPLE(PlanarSelect, setValue));
this->registerCall(this, POTHOS_FCN_TUPLE(PlanarSelect, setMinimum));
this->registerCall(this, POTHOS_FCN_TUPLE(PlanarSelect, setMaximum));
this->registerSignal("valueChanged");
this->registerSignal("complexValueChanged");
_layout->addWidget(_view);
_layout->setContentsMargins(QMargins());
_layout->setSpacing(0);
connect(_view, &PlanarSelectGraphicsView::positionChanged, this, &PlanarSelect::handlePositionChanged);
}
QWidget *widget(void)
{
return this;
}
void setTitle(const QString &title)
{
QMetaObject::invokeMethod(this, "handleSetTitle", Qt::QueuedConnection, Q_ARG(QString, title));
}
std::vector<double> value(void) const
{
std::vector<double> vals(2);
vals[0] = _value.x();
vals[1] = _value.y();
return vals;
}
std::complex<double> complexValue(void) const
{
return std::complex<double>(_value.x(), _value.y());
}
void setValue(const std::vector<double> &value)
{
if (value.size() != 2) throw Pothos::RangeException("PlanarSelect::setValue()", "value size must be 2");
_value = QPointF(value[0], value[1]);
const auto pos = _value - _minimum;
const auto range = _maximum - _minimum;
const QPointF viewPos(pos.x()/range.x(), pos.y()/range.y());
QMetaObject::invokeMethod(_view, "setPosition", Qt::QueuedConnection, Q_ARG(QPointF, viewPos));
}
void setMinimum(const std::vector<double> &minimum)
{
if (minimum.size() != 2) throw Pothos::RangeException("PlanarSelect::setMinimum()", "minimum size must be 2");
_minimum = QPointF(minimum[0], minimum[1]);
}
void setMaximum(const std::vector<double> &maximum)
{
if (maximum.size() != 2) throw Pothos::RangeException("PlanarSelect::setMaximum()", "maximum size must be 2");
_maximum = QPointF(maximum[0], maximum[1]);
}
void activate(void)
{
//emit current value when design becomes active
this->emitValuesChanged();
}
public slots:
QVariant saveState(void) const
{
return _value;
}
void restoreState(const QVariant &state)
{
_value = state.toPointF();
this->setValue(this->value());
}
private slots:
void handlePositionChanged(const QPointF &pos)
{
const auto range = _maximum - _minimum;
_value = QPointF(pos.x()*range.x(), pos.y()*range.y()) + _minimum;
this->emitValuesChanged();
}
void handleSetTitle(const QString &title)
{
QGroupBox::setTitle(title);
}
protected:
void mousePressEvent(QMouseEvent *event)
{
QGroupBox::mousePressEvent(event);
event->ignore(); //allows for dragging from QGroupBox title
}
private:
void emitValuesChanged(void)
{
this->emitSignal("valueChanged", this->value());
this->emitSignal("complexValueChanged", this->complexValue());
}
QPointF _minimum;
QPointF _maximum;
QPointF _value;
PlanarSelectGraphicsView *_view;
QHBoxLayout *_layout;
};
static Pothos::BlockRegistry registerPlanarSelect(
"/widgets/planar_select", &PlanarSelect::make);
#include "PlanarSelect.moc"