36 : QGraphicsView(parent)
41 setMinimumSize(64, 64);
42 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
43 setScene(
new QGraphicsScene(
this));
44 setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
46 m_renderer =
new QSvgRenderer();
64 connect(&dialTimer, &QTimer::timeout,
this, &DialGadgetWidget::rotateNeedles);
76 QString nfield2, QString object3, QString nfield3)
89 if (!(object1.isEmpty() || nfield1.isEmpty())) {
94 if (nfield1.contains(
"-")) {
95 QStringList fieldSubfield = nfield1.split(
"-", QString::SkipEmptyParts);
96 field1 = fieldSubfield.at(0);
97 subfield1 = fieldSubfield.at(1);
101 haveSubField1 =
false;
104 qDebug() <<
"Error: Object is unknown (" << object1 <<
").";
109 if (!(object2.isEmpty() || nfield2.isEmpty())) {
114 if (nfield2.contains(
"-")) {
115 QStringList fieldSubfield = nfield2.split(
"-", QString::SkipEmptyParts);
116 field2 = fieldSubfield.at(0);
117 subfield2 = fieldSubfield.at(1);
118 haveSubField2 =
true;
121 haveSubField2 =
false;
124 qDebug() <<
"Error: Object is unknown (" << object2 <<
").";
129 if (!(object3.isEmpty() || nfield3.isEmpty())) {
134 if (nfield3.contains(
"-")) {
135 QStringList fieldSubfield = nfield3.split(
"-", QString::SkipEmptyParts);
136 field3 = fieldSubfield.at(0);
137 subfield3 = fieldSubfield.at(1);
138 haveSubField3 =
true;
141 haveSubField3 =
false;
144 qDebug() <<
"Error: Object is unknown (" << object3 <<
").";
160 QRegExp(subfield1, Qt::CaseSensitive, QRegExp::FixedString));
161 value = field->
getDouble(indexOfSubField);
164 if (value != value) {
165 qDebug() <<
"Dial widget: encountered NaN !!";
170 qDebug() <<
"Wrong field, maybe an issue with object disconnection ?";
184 QRegExp(subfield2, Qt::CaseSensitive, QRegExp::FixedString));
185 value = field->
getDouble(indexOfSubField);
188 if (value != value) {
189 qDebug() <<
"Dial widget: encountered NaN !!";
194 qDebug() <<
"Wrong field, maybe an issue with object disconnection ?";
208 QRegExp(subfield3, Qt::CaseSensitive, QRegExp::FixedString));
209 value = field->
getDouble(indexOfSubField);
212 if (value != value) {
213 qDebug() <<
"Dial widget: encountered NaN !!";
218 qDebug() <<
"Wrong field, maybe an issue with object disconnection ?";
227 QString n3, QString n1Move, QString n2Move, QString n3Move)
232 QGraphicsScene *l_scene = scene();
234 if (QFile::exists(dfn) && m_renderer->load(dfn) && m_renderer->isValid()) {
236 m_background =
new QGraphicsSvgItem();
238 m_background->setFlags(QGraphicsItem::ItemClipsChildrenToShape
239 | QGraphicsItem::ItemClipsToShape);
240 m_foreground =
new QGraphicsSvgItem();
241 m_needle1 =
new QGraphicsSvgItem();
242 m_needle2 =
new QGraphicsSvgItem();
243 m_needle3 =
new QGraphicsSvgItem();
244 m_needle1->setParentItem(m_background);
245 m_needle2->setParentItem(m_background);
246 m_needle3->setParentItem(m_background);
247 m_foreground->setParentItem(m_background);
251 m_background->setSharedRenderer(m_renderer);
252 m_background->setElementId(bg);
253 l_scene->addItem(m_background);
255 m_needle1->setSharedRenderer(m_renderer);
256 m_needle1->setElementId(n1);
266 m_needle2 = m_needle1;
269 if (m_renderer->elementExists(n2)) {
270 m_needle2->setSharedRenderer(m_renderer);
271 m_needle2->setElementId(n2);
277 if (m_renderer->elementExists(n3)) {
278 m_needle3->setSharedRenderer(m_renderer);
279 m_needle3->setElementId(n3);
284 if (m_renderer->elementExists(fg)) {
285 m_foreground->setSharedRenderer(m_renderer);
286 m_foreground->setElementId(fg);
288 QRectF rectB = m_background->boundingRect();
289 QRectF rectF = m_foreground->boundingRect();
290 m_foreground->setPos(rectB.width() / 2 - rectF.width() / 2,
291 rectB.height() / 2 - rectF.height() / 2);
308 if (n1Move.contains(
"Rotate")) {
310 }
else if (n1Move.contains(
"Horizontal")) {
312 }
else if (n1Move.contains(
"Vertical")) {
316 if (n2Move.contains(
"Rotate")) {
318 }
else if (n2Move.contains(
"Horizontal")) {
320 }
else if (n2Move.contains(
"Vertical")) {
324 if (n3Move.contains(
"Rotate")) {
326 }
else if (n3Move.contains(
"Horizontal")) {
328 }
else if (n3Move.contains(
"Vertical")) {
332 l_scene->setSceneRect(m_background->boundingRect());
337 QRectF rectB = m_background->boundingRect();
338 QRectF rectN = m_needle1->boundingRect();
339 m_needle1->setPos(rectB.width() / 2 - rectN.width() / 2,
340 rectB.height() / 2 - rectN.height() / 2);
342 m_needle1->setTransformOriginPoint(rectN.width() / 2, rectN.height() / 2);
345 if (m_renderer->elementExists(n1 +
"-text")) {
346 QMatrix textMatrix = m_renderer->matrixForElement(n1 +
"-text");
347 qreal startX = textMatrix.mapRect(m_renderer->boundsOnElement(n1 +
"-text")).
x();
348 qreal startY = textMatrix.mapRect(m_renderer->boundsOnElement(n1 +
"-text")).
y();
350 matrix.translate(startX, startY);
351 m_text1 =
new QGraphicsTextItem(
"0.00");
352 m_text1->setDefaultTextColor(QColor(
"White"));
353 m_text1->setTransform(matrix,
false);
354 l_scene->addItem(m_text1);
359 if ((n1 != n2) && n2enabled) {
361 rectN = m_needle2->boundingRect();
362 m_needle2->setPos(rectB.width() / 2 - rectN.width() / 2,
363 rectB.height() / 2 - rectN.height() / 2);
364 m_needle2->setTransformOriginPoint(rectN.width() / 2, rectN.height() / 2);
366 if (m_renderer->elementExists(n2 +
"-text")) {
367 QMatrix textMatrix = m_renderer->matrixForElement(n2 +
"-text");
368 qreal startX = textMatrix.mapRect(m_renderer->boundsOnElement(n2 +
"-text")).
x();
369 qreal startY = textMatrix.mapRect(m_renderer->boundsOnElement(n2 +
"-text")).
y();
371 matrix.translate(startX, startY);
372 m_text2 =
new QGraphicsTextItem(
"0.00");
373 m_text2->setDefaultTextColor(QColor(
"White"));
374 m_text2->setTransform(matrix,
false);
375 l_scene->addItem(m_text2);
381 rectN = m_needle3->boundingRect();
382 m_needle3->setPos(rectB.width() / 2 - rectN.width() / 2,
383 rectB.height() / 2 - rectN.height() / 2);
384 m_needle3->setTransformOriginPoint(rectN.width() / 2, rectN.height() / 2);
386 if (m_renderer->elementExists(n3 +
"-text")) {
387 QMatrix textMatrix = m_renderer->matrixForElement(n3 +
"-text");
388 qreal startX = textMatrix.mapRect(m_renderer->boundsOnElement(n3 +
"-text")).
x();
389 qreal startY = textMatrix.mapRect(m_renderer->boundsOnElement(n3 +
"-text")).
y();
391 matrix.translate(startX, startY);
392 m_text3 =
new QGraphicsTextItem(
"0.00");
393 m_text3->setDefaultTextColor(QColor(
"White"));
394 m_text3->setTransform(matrix,
false);
395 l_scene->addItem(m_text3);
409 if (!dialTimer.isActive())
413 qDebug() <<
"no file: display default background.";
414 m_renderer->load(QString(
":/dial/images/empty.svg"));
416 m_background =
new QGraphicsSvgItem();
417 m_background->setSharedRenderer(m_renderer);
418 l_scene->addItem(m_background);
437 if (!m_renderer->isValid()) {
438 qDebug() <<
"Dial file not loaded, not rendering";
441 QGraphicsView::paintEvent(event);
450 fitInView(m_background, Qt::KeepAspectRatio);
455 QFont font = QFont(
"Arial", 12);
456 font.fromString(fontProps);
458 m_text1->setFont(font);
467 needle1Target = 360 * (value * n1Factor) / (n1MaxValue - n1MinValue);
470 needle1Target = (value * n1Factor) / (n1MaxValue - n1MinValue);
473 needle1Target = (value * n1Factor) / (n1MaxValue - n1MinValue);
475 if (!dialTimer.isActive())
479 s.sprintf(
"%.2f", value * n1Factor);
480 m_text1->setPlainText(s);
487 needle2Target = 360 * (value * n2Factor) / (n2MaxValue - n2MinValue);
490 needle2Target = (value * n2Factor) / (n2MaxValue - n2MinValue);
493 needle2Target = (value * n2Factor) / (n2MaxValue - n2MinValue);
495 if (!dialTimer.isActive())
499 s.sprintf(
"%.2f", value * n2Factor);
500 m_text2->setPlainText(s);
507 needle3Target = 360 * (value * n3Factor) / (n3MaxValue - n3MinValue);
510 needle3Target = (value * n3Factor) / (n3MaxValue - n3MinValue);
513 needle3Target = (value * n3Factor) / (n3MaxValue - n3MinValue);
515 if (!dialTimer.isActive())
519 s.sprintf(
"%.2f", value * n3Factor);
520 m_text3->setPlainText(s);
531 void DialGadgetWidget::rotateNeedles()
541 if (fabs((needle2Value - needle2Target) * 10) > 5 && beSmooth) {
542 needle2Diff = (needle2Target - needle2Value) / 5;
544 needle2Diff = needle2Target - needle2Value;
548 m_needle2->setRotation(m_needle2->rotation() + needle2Diff);
550 QPointF opd = QPointF(0, 0);
552 opd = QPointF(needle2Diff, 0);
555 opd = QPointF(0, needle2Diff);
557 m_needle2->setTransform(QTransform::fromTranslate(opd.x(), opd.y()),
true);
563 QPointF oop = m_needle2->transformOriginPoint();
564 m_needle2->setTransformOriginPoint(oop.x() - opd.x(), oop.y() - opd.y());
566 needle2Value += needle2Diff;
573 if ((fabs((needle1Value - needle1Target) * 10) > 5) && beSmooth) {
574 needle1Diff = (needle1Target - needle1Value) / 5;
576 needle1Diff = needle1Target - needle1Value;
580 m_needle1->setRotation(m_needle1->rotation() + needle1Diff);
582 QPointF opd = QPointF(0, 0);
584 opd = QPointF(needle1Diff, 0);
587 opd = QPointF(0, needle1Diff);
589 m_needle1->setTransform(QTransform::fromTranslate(opd.x(), opd.y()),
true);
590 QPointF oop = m_needle1->transformOriginPoint();
591 m_needle1->setTransformOriginPoint((oop.x() - opd.x()), (oop.y() - opd.y()));
593 needle1Value += needle1Diff;
597 if ((fabs((needle3Value - needle3Target) * 10) > 5) && beSmooth) {
598 needle3Diff = (needle3Target - needle3Value) / 5;
600 needle3Diff = needle3Target - needle3Value;
604 m_needle3->setRotation(m_needle3->rotation() + needle3Diff);
606 QPointF opd = QPointF(0, 0);
608 opd = QPointF(needle3Diff, 0);
611 opd = QPointF(0, needle3Diff);
613 m_needle3->setTransform(QTransform::fromTranslate(opd.x(), opd.y()),
true);
614 QPointF oop = m_needle3->transformOriginPoint();
615 m_needle3->setTransformOriginPoint((oop.x() - opd.x()), (oop.y() - opd.y()));
617 needle3Value += needle3Diff;
DialGadgetWidget(QWidget *parent=nullptr)
void updateNeedle2(UAVObject *object2)
Called by the UAVObject which got updated.
void setNeedle3(double value)
double getDouble(int index=0) const
void setDialFont(QString fontProps)
Core plugin system that manages the plugins, their life cycle and their registered objects...
void objectUpdated(UAVObject *obj)
Signal sent whenever any field of the object is updated.
void connectNeedles(QString object1, QString field1, QString object2, QString field2, QString object3, QString field3)
Connects the widget to the relevant UAVObjects.
void paintEvent(QPaintEvent *event)
void setDialFile(QString dfn, QString bg, QString fg, QString n1, QString n2, QString n3, QString n1Move, QString n2Move, QString n3Move)
void setNeedle1(double value)
void updateNeedle3(UAVObject *object3)
Called by the UAVObject which got updated.
UAVObjectField * getField(const QString &name)
void updateNeedle1(UAVObject *object1)
Called by the UAVObject which got updated.
void setNeedle2(double value)
QStringList getElementNames() const
void resizeEvent(QResizeEvent *event)
UAVObject * getObject(const QString &name, quint32 instId=0)