2010-06-23 03:59:12 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file lineardialgadgetwidget.cpp
|
|
|
|
* @author Edouard Lafargue Copyright (C) 2010.
|
|
|
|
* @brief
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
* @defgroup lineardialplugin
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lineardialgadgetwidget.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <QtGui/QFileDialog>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
LineardialGadgetWidget::LineardialGadgetWidget(QWidget *parent) : QGraphicsView(parent)
|
|
|
|
{
|
|
|
|
setMinimumSize(32,32);
|
|
|
|
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
|
|
|
|
setScene(new QGraphicsScene(this));
|
2010-06-28 18:32:40 +02:00
|
|
|
setRenderHints(QPainter::Antialiasing);
|
2010-06-23 03:59:12 +02:00
|
|
|
m_renderer = new QSvgRenderer();
|
|
|
|
verticalDial = false;
|
|
|
|
|
|
|
|
paint();
|
|
|
|
|
|
|
|
obj1 = NULL;
|
2010-06-28 18:32:40 +02:00
|
|
|
fieldName = NULL;
|
|
|
|
fieldValue = NULL;
|
2010-06-23 03:59:12 +02:00
|
|
|
indexTarget = 0;
|
|
|
|
indexValue = 0;
|
2010-07-05 00:00:50 +02:00
|
|
|
places = 0;
|
|
|
|
factor = 1;
|
2010-06-23 03:59:12 +02:00
|
|
|
|
|
|
|
// This timer mechanism makes the index rotate smoothly
|
|
|
|
connect(&dialTimer, SIGNAL(timeout()), this, SLOT(moveIndex()));
|
|
|
|
dialTimer.start(30);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LineardialGadgetWidget::~LineardialGadgetWidget()
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Connects the widget to the relevant UAVObjects
|
|
|
|
*/
|
|
|
|
void LineardialGadgetWidget::connectInput(QString object1, QString nfield1) {
|
|
|
|
|
|
|
|
if (obj1 != NULL)
|
|
|
|
disconnect(obj1,SIGNAL(objectUpdated(UAVObject*)),this,SLOT(updateIndex(UAVObject*)));
|
|
|
|
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
|
|
|
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
|
|
|
|
|
2010-07-10 09:15:37 +02:00
|
|
|
qDebug() << "Lineardial Connect needles - " << object1 << "-"<< nfield1;
|
2010-06-23 03:59:12 +02:00
|
|
|
|
|
|
|
// Check validity of arguments first, reject empty args and unknown fields.
|
|
|
|
if (!(object1.isEmpty() || nfield1.isEmpty())) {
|
|
|
|
obj1 = dynamic_cast<UAVDataObject*>( objManager->getObject(object1) );
|
|
|
|
if (obj1 != NULL ) {
|
|
|
|
connect(obj1, SIGNAL(objectUpdated(UAVObject*)), this, SLOT(updateIndex(UAVObject*)));
|
|
|
|
field1 = nfield1;
|
2010-06-28 18:32:40 +02:00
|
|
|
if (fieldName)
|
|
|
|
fieldName->setPlainText(nfield1);
|
2010-06-29 17:00:23 +02:00
|
|
|
updateIndex(obj1);
|
2010-06-29 11:02:54 +02:00
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
} else {
|
2010-07-10 09:15:37 +02:00
|
|
|
qDebug() << "Error: Object is unknown (" << object1 << ") this should not happen.";
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Called by the UAVObject which got updated
|
2010-06-29 17:00:23 +02:00
|
|
|
|
|
|
|
Updates the numeric value and/or the icon if the dial wants this.
|
2010-06-23 03:59:12 +02:00
|
|
|
*/
|
|
|
|
void LineardialGadgetWidget::updateIndex(UAVObject *object1) {
|
|
|
|
// Double check that the field exists:
|
|
|
|
UAVObjectField* field = object1->getField(field1);
|
|
|
|
if (field) {
|
|
|
|
QString s;
|
2010-06-29 17:00:23 +02:00
|
|
|
if (field->isNumeric()) {
|
2010-07-05 00:00:50 +02:00
|
|
|
double v = field->getDouble()*factor;
|
2010-06-29 17:00:23 +02:00
|
|
|
setIndex(v);
|
2010-07-05 00:00:50 +02:00
|
|
|
s.sprintf("%.*f",places,v);
|
2010-06-29 17:00:23 +02:00
|
|
|
}
|
|
|
|
if (field->isText()) {
|
|
|
|
s = field->getValue().toString();
|
|
|
|
if (fieldSymbol) {
|
|
|
|
// If we defined a symbol, we will look for a matching
|
|
|
|
// SVG element to display:
|
|
|
|
if (m_renderer->elementExists("symbol-" + s)) {
|
|
|
|
fieldSymbol->setElementId("symbol-" + s);
|
|
|
|
} else {
|
|
|
|
fieldSymbol->setElementId("symbol");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-28 18:32:40 +02:00
|
|
|
if (fieldValue)
|
|
|
|
fieldValue->setPlainText(s);
|
2010-06-29 17:00:23 +02:00
|
|
|
|
2010-06-28 18:32:40 +02:00
|
|
|
if (index && !dialTimer.isActive())
|
2010-06-23 11:04:36 +02:00
|
|
|
dialTimer.start();
|
2010-06-23 03:59:12 +02:00
|
|
|
} else {
|
2010-07-10 09:15:37 +02:00
|
|
|
qDebug() << "Wrong field, maybe an issue with object disconnection ?";
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-29 17:00:23 +02:00
|
|
|
/*!
|
|
|
|
\brief Setup dial using its master SVG template.
|
|
|
|
|
|
|
|
Should only be called after the min/max ranges have been set.
|
|
|
|
|
2010-06-28 18:32:40 +02:00
|
|
|
*/
|
2010-06-23 03:59:12 +02:00
|
|
|
void LineardialGadgetWidget::setDialFile(QString dfn)
|
|
|
|
{
|
|
|
|
if (QFile::exists(dfn))
|
|
|
|
{
|
2010-06-28 18:32:40 +02:00
|
|
|
QGraphicsScene *l_scene = scene();
|
2010-06-23 03:59:12 +02:00
|
|
|
m_renderer->load(dfn);
|
|
|
|
if(m_renderer->isValid())
|
|
|
|
{
|
2010-06-28 18:32:40 +02:00
|
|
|
l_scene->clear(); // Beware: clear also deletes all objects
|
|
|
|
// which are currently in the scene
|
|
|
|
background = new QGraphicsSvgItem();
|
|
|
|
background->setSharedRenderer(m_renderer);
|
|
|
|
background->setElementId("background");
|
|
|
|
background->setFlags(QGraphicsItem::ItemClipsChildrenToShape|
|
|
|
|
QGraphicsItem::ItemClipsToShape);
|
|
|
|
l_scene->addItem(background);
|
2010-06-29 11:02:54 +02:00
|
|
|
|
|
|
|
// The red/yellow/green zones are optional, we just
|
|
|
|
// test on the presence of "red"
|
|
|
|
if (m_renderer->elementExists("red")) {
|
|
|
|
// Order is important: red, then yellow then green
|
|
|
|
// overlayed on top of each other
|
|
|
|
red = new QGraphicsSvgItem();
|
|
|
|
red->setSharedRenderer(m_renderer);
|
|
|
|
red->setElementId("red");
|
|
|
|
red->setParentItem(background);
|
|
|
|
yellow = new QGraphicsSvgItem();
|
|
|
|
yellow->setSharedRenderer(m_renderer);
|
|
|
|
yellow->setElementId("yellow");
|
|
|
|
yellow->setParentItem(background);
|
|
|
|
green = new QGraphicsSvgItem();
|
|
|
|
green->setSharedRenderer(m_renderer);
|
|
|
|
green->setElementId("green");
|
|
|
|
green->setParentItem(background);
|
|
|
|
// In order to properly render the Green/Yellow/Red graphs, we need to find out
|
|
|
|
// the starting location of the bargraph rendering area:
|
|
|
|
QMatrix textMatrix = m_renderer->matrixForElement("bargraph");
|
|
|
|
qreal bgX = textMatrix.mapRect(m_renderer->boundsOnElement("bargraph")).x();
|
|
|
|
qreal bgY = textMatrix.mapRect(m_renderer->boundsOnElement("bargraph")).y();
|
|
|
|
bargraphSize = textMatrix.mapRect(m_renderer->boundsOnElement("bargraph")).width();
|
|
|
|
// Detect if the bargraph is vertical or horizontal.
|
|
|
|
qreal bargraphHeight = textMatrix.mapRect(m_renderer->boundsOnElement("bargraph")).height();
|
|
|
|
if (bargraphHeight > bargraphSize) {
|
|
|
|
verticalDial = true;
|
|
|
|
bargraphSize = bargraphHeight;
|
|
|
|
} else {
|
|
|
|
verticalDial = false;
|
|
|
|
}
|
|
|
|
// Now adjust the red/yellow/green zones:
|
|
|
|
double range = maxValue-minValue;
|
|
|
|
|
|
|
|
green->resetTransform();
|
|
|
|
double greenScale = (greenMax-greenMin)/range;
|
|
|
|
double greenStart = verticalDial ? (maxValue-greenMax)/range*green->boundingRect().height() :
|
|
|
|
(greenMin-minValue)/range*green->boundingRect().width();
|
|
|
|
QTransform matrix;
|
|
|
|
matrix.reset();
|
|
|
|
if (verticalDial) {
|
|
|
|
matrix.scale(1,greenScale);
|
|
|
|
matrix.translate(bgX,(greenStart+bgY)/greenScale);
|
|
|
|
} else {
|
|
|
|
matrix.scale(greenScale,1);
|
|
|
|
matrix.translate((greenStart+bgX)/greenScale,bgY);
|
|
|
|
}
|
|
|
|
green->setTransform(matrix,false);
|
|
|
|
|
|
|
|
yellow->resetTransform();
|
|
|
|
double yellowScale = (yellowMax-yellowMin)/range;
|
|
|
|
double yellowStart = verticalDial ? (maxValue-yellowMax)/range*yellow->boundingRect().height() :
|
|
|
|
(yellowMin-minValue)/range*yellow->boundingRect().width();
|
|
|
|
matrix.reset();
|
|
|
|
if (verticalDial) {
|
|
|
|
matrix.scale(1,yellowScale);
|
|
|
|
matrix.translate(bgX,(yellowStart+bgY)/yellowScale);
|
|
|
|
} else {
|
|
|
|
matrix.scale(yellowScale,1);
|
|
|
|
matrix.translate((yellowStart+bgX)/yellowScale,bgY);
|
|
|
|
}
|
|
|
|
yellow->setTransform(matrix,false);
|
|
|
|
|
|
|
|
red->resetTransform();
|
|
|
|
double redScale = (redMax-redMin)/range;
|
|
|
|
double redStart = verticalDial ? (maxValue-redMax)/range*red->boundingRect().height() :
|
|
|
|
(redMin-minValue)/range*red->boundingRect().width();
|
|
|
|
matrix.reset();
|
|
|
|
if (verticalDial) {
|
|
|
|
matrix.scale(1,redScale);
|
|
|
|
matrix.translate(bgX,(redStart+bgY)/redScale);
|
|
|
|
} else {
|
|
|
|
matrix.scale(redScale,1);
|
|
|
|
matrix.translate((redStart+bgX)/redScale,bgY);
|
|
|
|
}
|
|
|
|
red->setTransform(matrix,false);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
red = NULL;
|
|
|
|
yellow = NULL;
|
|
|
|
green = NULL;
|
|
|
|
}
|
2010-06-28 18:32:40 +02:00
|
|
|
|
|
|
|
// Check whether the dial wants to display a moving index:
|
|
|
|
if (m_renderer->elementExists("needle")) {
|
|
|
|
QMatrix textMatrix = m_renderer->matrixForElement("needle");
|
2010-07-05 00:00:50 +02:00
|
|
|
QRectF nRect = textMatrix.mapRect(m_renderer->boundsOnElement("needle"));
|
|
|
|
startX = nRect.x();
|
|
|
|
startY = nRect.y();
|
|
|
|
//indexWidth = nRect.width();
|
|
|
|
//indexHeight = nRect.height();
|
2010-06-28 18:32:40 +02:00
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(startX,startY);
|
|
|
|
index = new QGraphicsSvgItem();
|
|
|
|
index->setSharedRenderer(m_renderer);
|
|
|
|
index->setElementId("needle");
|
|
|
|
index->setTransform(matrix,false);
|
|
|
|
index->setParentItem(background);
|
|
|
|
} else {
|
|
|
|
index = NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-29 17:00:23 +02:00
|
|
|
// Check whether the dial wants display its field name:
|
2010-06-28 18:32:40 +02:00
|
|
|
if (m_renderer->elementExists("field")) {
|
|
|
|
QMatrix textMatrix = m_renderer->matrixForElement("field");
|
|
|
|
qreal startX = textMatrix.mapRect(m_renderer->boundsOnElement("field")).x();
|
|
|
|
qreal startY = textMatrix.mapRect(m_renderer->boundsOnElement("field")).y();
|
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(startX,startY);
|
|
|
|
fieldName = new QGraphicsTextItem("0.00");
|
|
|
|
fieldName->setDefaultTextColor(QColor("White"));
|
|
|
|
fieldName->setTransform(matrix,false);
|
|
|
|
fieldName->setParentItem(background);
|
|
|
|
} else {
|
|
|
|
fieldName = NULL;
|
|
|
|
}
|
2010-06-29 17:00:23 +02:00
|
|
|
|
2010-06-28 18:32:40 +02:00
|
|
|
// Check whether the dial wants display the numeric value:
|
|
|
|
if (m_renderer->elementExists("value")) {
|
|
|
|
QMatrix textMatrix = m_renderer->matrixForElement("value");
|
|
|
|
qreal startX = textMatrix.mapRect(m_renderer->boundsOnElement("value")).x();
|
|
|
|
qreal startY = textMatrix.mapRect(m_renderer->boundsOnElement("value")).y();
|
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(startX,startY);
|
2010-06-29 11:02:54 +02:00
|
|
|
fieldValue = new QGraphicsTextItem("00");
|
2010-06-28 18:32:40 +02:00
|
|
|
fieldValue->setDefaultTextColor(QColor("White"));
|
|
|
|
fieldValue->setTransform(matrix,false);
|
|
|
|
fieldValue->setParentItem(background);
|
|
|
|
} else {
|
|
|
|
fieldValue = NULL;
|
|
|
|
}
|
2010-06-23 03:59:12 +02:00
|
|
|
|
2010-06-29 17:00:23 +02:00
|
|
|
// Check whether the dial wants to display the value as a
|
|
|
|
// symbol (only works for text values):
|
|
|
|
if (m_renderer->elementExists("symbol")) {
|
|
|
|
QMatrix textMatrix = m_renderer->matrixForElement("symbol");
|
|
|
|
qreal startX = textMatrix.mapRect(m_renderer->boundsOnElement("symbol")).x();
|
|
|
|
qreal startY = textMatrix.mapRect(m_renderer->boundsOnElement("symbol")).y();
|
|
|
|
QTransform matrix;
|
|
|
|
matrix.translate(startX,startY);
|
|
|
|
fieldSymbol = new QGraphicsSvgItem();
|
|
|
|
fieldSymbol->setElementId("symbol");
|
|
|
|
fieldSymbol->setSharedRenderer(m_renderer);
|
|
|
|
fieldSymbol->setTransform(matrix,false);
|
|
|
|
fieldSymbol->setParentItem(background);
|
|
|
|
} else {
|
|
|
|
fieldSymbol = NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-23 03:59:12 +02:00
|
|
|
if (m_renderer->elementExists("foreground")) {
|
2010-06-28 18:32:40 +02:00
|
|
|
foreground = new QGraphicsSvgItem();
|
2010-06-23 03:59:12 +02:00
|
|
|
foreground->setSharedRenderer(m_renderer);
|
|
|
|
foreground->setElementId("foreground");
|
2010-06-28 18:32:40 +02:00
|
|
|
//l_scene->addItem(foreground);
|
|
|
|
foreground->setParentItem(background);
|
2010-06-23 03:59:12 +02:00
|
|
|
fgenabled = true;
|
2010-06-28 18:32:40 +02:00
|
|
|
} else {
|
|
|
|
fgenabled = false;
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l_scene->setSceneRect(background->boundingRect());
|
|
|
|
|
2010-06-23 11:04:36 +02:00
|
|
|
// Reset the current index value:
|
|
|
|
indexValue = 0;
|
2010-06-29 11:02:54 +02:00
|
|
|
if (!dialTimer.isActive() && index)
|
2010-06-23 11:04:36 +02:00
|
|
|
dialTimer.start();
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2010-07-10 09:15:37 +02:00
|
|
|
{ qDebug()<<"no file "; }
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LineardialGadgetWidget::setDialFont(QString fontProps)
|
|
|
|
{
|
|
|
|
QFont font = QFont("Arial",12);
|
|
|
|
font.fromString(fontProps);
|
2010-06-29 11:02:54 +02:00
|
|
|
if (fieldName)
|
2010-06-28 18:32:40 +02:00
|
|
|
fieldName->setFont(font);
|
2010-06-29 11:02:54 +02:00
|
|
|
if (fieldValue)
|
2010-06-28 18:32:40 +02:00
|
|
|
fieldValue->setFont(font);
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void LineardialGadgetWidget::paint()
|
|
|
|
{
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LineardialGadgetWidget::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
// Skip painting until the dial file is loaded
|
|
|
|
if (! m_renderer->isValid()) {
|
2010-07-10 09:15:37 +02:00
|
|
|
qDebug()<<"Dial file not loaded, not rendering";
|
2010-06-23 03:59:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QGraphicsView::paintEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This event enables the dial to be dynamically resized
|
|
|
|
// whenever the gadget is resized, taking advantage of the vector
|
|
|
|
// nature of SVG dials.
|
|
|
|
void LineardialGadgetWidget::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
fitInView(background, Qt::KeepAspectRatio );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the value into an percentage:
|
|
|
|
// this enables smooth movement in moveIndex below
|
|
|
|
void LineardialGadgetWidget::setIndex(double value) {
|
|
|
|
if (verticalDial) {
|
|
|
|
indexTarget = 100*(maxValue-value)/(maxValue-minValue);
|
|
|
|
} else {
|
|
|
|
indexTarget = 100*(value-minValue)/(maxValue-minValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take an input value and move the index accordingly
|
|
|
|
// Move is smooth, starts fast and slows down when
|
|
|
|
// approaching the target.
|
|
|
|
void LineardialGadgetWidget::moveIndex()
|
|
|
|
{
|
2010-06-29 17:00:23 +02:00
|
|
|
if (!index) { // Safeguard
|
|
|
|
dialTimer.stop();
|
|
|
|
return;
|
|
|
|
}
|
2010-06-23 03:59:12 +02:00
|
|
|
if ((abs((indexValue-indexTarget)*10) > 3)) {
|
2010-06-28 18:32:40 +02:00
|
|
|
indexValue += (indexTarget - indexValue)/5;
|
2010-06-23 11:04:36 +02:00
|
|
|
} else {
|
2010-06-28 18:32:40 +02:00
|
|
|
indexValue = indexTarget;
|
|
|
|
dialTimer.stop();
|
|
|
|
}
|
|
|
|
QTransform matrix;
|
|
|
|
index->resetTransform();
|
2010-07-05 00:00:50 +02:00
|
|
|
qreal trans = indexValue*bargraphSize/100;
|
2010-06-28 18:32:40 +02:00
|
|
|
if (verticalDial) {
|
2010-07-05 00:00:50 +02:00
|
|
|
//matrix.translate(startX-indexWidth/2,trans+startY-indexHeight/2);
|
|
|
|
matrix.translate(startX,trans+startY);
|
2010-06-28 18:32:40 +02:00
|
|
|
} else {
|
2010-07-05 00:00:50 +02:00
|
|
|
//matrix.translate(trans+startX-indexWidth/2,startY-indexHeight/2);
|
|
|
|
matrix.translate(trans+startX,startY);
|
2010-06-28 18:32:40 +02:00
|
|
|
}
|
|
|
|
index->setTransform(matrix,false);
|
2010-07-05 00:00:50 +02:00
|
|
|
|
2010-06-28 18:32:40 +02:00
|
|
|
update();
|
2010-06-23 03:59:12 +02:00
|
|
|
}
|