From 61804f91ddbc5ef54d3a0c0386da0ab70398825f Mon Sep 17 00:00:00 2001 From: edouard Date: Sun, 2 May 2010 14:07:00 +0000 Subject: [PATCH] Settings for SVG analog dials all finalized except actual connection to UAVObjects. The test code randomly assigns values to the dials to demonstrate smooth needle rotation. Let me know if you like it! git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@570 ebee16cc-31ac-478f-84a7-5cbb03baadba --- .../src/plugins/airspeed/airspeedgadget.cpp | 7 +- .../airspeed/airspeedgadgetoptionspage.cpp | 12 +++ .../airspeed/airspeedgadgetoptionspage.ui | 81 +++++++++++++--- .../plugins/airspeed/airspeedgadgetwidget.cpp | 92 +++++++++++-------- .../plugins/airspeed/airspeedgadgetwidget.h | 33 +++++-- 5 files changed, 169 insertions(+), 56 deletions(-) diff --git a/ground/src/plugins/airspeed/airspeedgadget.cpp b/ground/src/plugins/airspeed/airspeedgadget.cpp index 27fc71eb9..efa528a8b 100644 --- a/ground/src/plugins/airspeed/airspeedgadget.cpp +++ b/ground/src/plugins/airspeed/airspeedgadget.cpp @@ -48,6 +48,11 @@ AirspeedGadget::~AirspeedGadget() void AirspeedGadget::loadConfiguration(IUAVGadgetConfiguration* config) { AirspeedGadgetConfiguration *m = qobject_cast(config); - m_widget->setDialFile(m->dialFile()); + m_widget->setDialFile(m->dialFile(), m->dialBackground(), m->dialForeground(), m->dialNeedle1(), + m->dialNeedle2()); + m_widget->setN1Min(m->getN1Min()); + m_widget->setN1Max(m->getN1Max()); + m_widget->setN2Min(m->getN2Min()); + m_widget->setN2Max(m->getN2Max()); } diff --git a/ground/src/plugins/airspeed/airspeedgadgetoptionspage.cpp b/ground/src/plugins/airspeed/airspeedgadgetoptionspage.cpp index 320eb6c85..a20549f80 100644 --- a/ground/src/plugins/airspeed/airspeedgadgetoptionspage.cpp +++ b/ground/src/plugins/airspeed/airspeedgadgetoptionspage.cpp @@ -55,6 +55,10 @@ QWidget *AirspeedGadgetOptionsPage::createPage(QWidget *parent) options_page->foregroundID->setText(m_config->dialForeground()); options_page->needle1ID->setText(m_config->dialNeedle1()); options_page->needle2ID->setText(m_config->dialNeedle2()); + options_page->needle1Min->setValue(m_config->getN1Min()); + options_page->needle1Max->setValue(m_config->getN1Max()); + options_page->needle2Min->setValue(m_config->getN2Min()); + options_page->needle2Max->setValue(m_config->getN2Max()); connect(options_page->loadFile, SIGNAL(clicked()), this, SLOT(on_loadFile_clicked())); @@ -70,6 +74,14 @@ QWidget *AirspeedGadgetOptionsPage::createPage(QWidget *parent) void AirspeedGadgetOptionsPage::apply() { m_config->setDialFile(options_page->svgSourceFile->text()); + m_config->setDialBackgroundID(options_page->backgroundID->text()); + m_config->setDialForegroundID(options_page->foregroundID->text()); + m_config->setDialNeedleID1(options_page->needle1ID->text()); + m_config->setDialNeedleID2(options_page->needle2ID->text()); + m_config->setN1Min(options_page->needle1Min->value()); + m_config->setN1Max(options_page->needle1Max->value()); + m_config->setN2Min(options_page->needle2Min->value()); + m_config->setN2Max(options_page->needle2Max->value()); } diff --git a/ground/src/plugins/airspeed/airspeedgadgetoptionspage.ui b/ground/src/plugins/airspeed/airspeedgadgetoptionspage.ui index 6daad099e..7c3f4dec6 100644 --- a/ground/src/plugins/airspeed/airspeedgadgetoptionspage.ui +++ b/ground/src/plugins/airspeed/airspeedgadgetoptionspage.ui @@ -6,7 +6,7 @@ 0 0 - 513 + 486 300 @@ -24,11 +24,14 @@ -1 -1 - 511 + 481 339 + + QLayout::SetMinimumSize + 10 @@ -137,7 +140,14 @@ - + + + + 0 + 0 + + + @@ -147,7 +157,17 @@ - + + + + 0 + 0 + + + + 100000.000000000000000 + + @@ -157,7 +177,17 @@ - + + + + 0 + 0 + + + + 100000.000000000000000 + + @@ -174,7 +204,7 @@ - + @@ -184,7 +214,7 @@ - + @@ -225,7 +255,14 @@ - + + + + 0 + 0 + + + @@ -235,7 +272,17 @@ - + + + + 0 + 0 + + + + 100000.000000000000000 + + @@ -245,7 +292,17 @@ - + + + + 0 + 0 + + + + 100000.000000000000000 + + @@ -262,7 +319,7 @@ - + @@ -272,7 +329,7 @@ - + diff --git a/ground/src/plugins/airspeed/airspeedgadgetwidget.cpp b/ground/src/plugins/airspeed/airspeedgadgetwidget.cpp index 7cdd9aec6..7fc6f7499 100644 --- a/ground/src/plugins/airspeed/airspeedgadgetwidget.cpp +++ b/ground/src/plugins/airspeed/airspeedgadgetwidget.cpp @@ -40,17 +40,22 @@ AirspeedGadgetWidget::AirspeedGadgetWidget(QWidget *parent) : QGraphicsView(pare m_renderer = new QSvgRenderer(); m_background = new QGraphicsSvgItem(); m_foreground = new QGraphicsSvgItem(); - m_desired = new QGraphicsSvgItem(); - m_actual = new QGraphicsSvgItem(); - //setDialFile(QFileDialog::getOpenFileName(qobject_cast(this), - // tr("Airspeed Dial"), "../artwork/", tr("SVG File (*.svg)")) ); - //setDialFile(QString("/usr/src/openpilot/artwork/Dials/extracts/speed-complete.svg")); + m_needle1 = new QGraphicsSvgItem(); + m_needle2 = new QGraphicsSvgItem(); paint(); + needle1Target = 0; + needle2Target = 0; + needle1Value = 0; + needle2Value = 0; + + connect(&dialTimer, SIGNAL(timeout()), this, SLOT(rotateNeedles())); + dialTimer.start(30); + // Test code for timer to rotate the needle testSpeed=0; connect(&m_testTimer, SIGNAL(timeout()), this, SLOT(testRotate())); - m_testTimer.start(60); + m_testTimer.start(4000); } AirspeedGadgetWidget::~AirspeedGadgetWidget() @@ -58,24 +63,26 @@ AirspeedGadgetWidget::~AirspeedGadgetWidget() // Do nothing } -void AirspeedGadgetWidget::setDialFile(QString dfn) +void AirspeedGadgetWidget::setDialFile(QString dfn, QString bg, QString fg, QString n1, QString n2) { if (QFile::exists(dfn)) { m_renderer->load(dfn); if(m_renderer->isValid()) { + m_background->setSharedRenderer(m_renderer); - m_background->setElementId(QString("background")); + m_background->setElementId(bg); m_foreground->setSharedRenderer(m_renderer); - m_foreground->setElementId(QString("foreground")); + m_foreground->setElementId(fg); - m_desired->setSharedRenderer(m_renderer); - m_desired->setElementId(QString("desired")); + m_needle1->setSharedRenderer(m_renderer); + m_needle1->setElementId(n1); + + m_needle2->setSharedRenderer(m_renderer); + m_needle2->setElementId(n2); - m_actual->setSharedRenderer(m_renderer); - m_actual->setElementId(QString("needle")); std::cout<<"Dial file loaded"<clear(); l_scene->addItem(m_background); - l_scene->addItem(m_desired); - l_scene->addItem(m_actual); + l_scene->addItem(m_needle1); + l_scene->addItem(m_needle2); l_scene->addItem(m_foreground); l_scene->setSceneRect(m_background->boundingRect()); @@ -120,33 +127,46 @@ void AirspeedGadgetWidget::resizeEvent(QResizeEvent *event) fitInView(m_background, Qt::KeepAspectRatio ); } -// Take a unitless speed value and sets the actual needle accordingly -// scale fixed at 0-90 for now -void AirspeedGadgetWidget::setActual(int speed) +void AirspeedGadgetWidget::setNeedle1(double value) { + needle1Target = value; +} + +void AirspeedGadgetWidget::setNeedle2(double value) { + needle2Target = value; +} + + +// Take an input value and rotate the dial accordingly +void AirspeedGadgetWidget::rotateNeedles() { - m_actual->resetTransform(); - QRectF rect = m_actual->boundingRect(); - m_actual->translate(rect.width()/2,rect.height()/2); - m_actual->rotate(speed*3); - m_actual->translate(-rect.width()/2,-rect.height()/2); + if (needle2Value != needle2Target) { + (needle2Value > needle2Target) ? needle2Value-- : needle2Value++; + m_needle2->resetTransform(); + QRectF rect = m_needle2->boundingRect(); + m_needle2->translate(rect.width()/2,rect.height()/2); + m_needle2->rotate(360*needle2Value/n2MaxValue); + m_needle2->translate(-rect.width()/2,-rect.height()/2); + } + + if (needle1Value != needle1Target) { + (needle1Value > needle1Target) ? needle1Value-- : needle1Value++; + m_needle1->resetTransform(); + QRectF rect = m_needle1->boundingRect(); + m_needle1->translate(rect.width()/2,rect.height()/2); + m_needle1->rotate(360*needle1Value/n1MaxValue); + m_needle1->translate(-rect.width()/2,-rect.height()/2); + } + update(); } -// Take a unitless speed value and sets the desired needle accordingly -// scale fixed at 0-90 for now -void AirspeedGadgetWidget::setDesired(int speed) -{ - m_desired->resetTransform(); - QRectF rect = m_desired->boundingRect(); - m_desired->translate(rect.width()/2,rect.height()/2); - m_desired->rotate(speed*3); - m_desired->translate(-rect.width()/2,-rect.height()/2); - update(); -} // Test function for timer to rotate needles void AirspeedGadgetWidget::testRotate() { - if(testSpeed==90) testSpeed=0; - setActual(testSpeed++); + int testVal1 = rand() % (int)n1MaxValue; + int testVal2 = rand() % (int)n2MaxValue; + setNeedle1((double)testVal1); + setNeedle2((double)testVal2); + } diff --git a/ground/src/plugins/airspeed/airspeedgadgetwidget.h b/ground/src/plugins/airspeed/airspeedgadgetwidget.h index af1c47c64..73dda3352 100644 --- a/ground/src/plugins/airspeed/airspeedgadgetwidget.h +++ b/ground/src/plugins/airspeed/airspeedgadgetwidget.h @@ -28,6 +28,7 @@ #ifndef AIRSPEEDGADGETWIDGET_H_ #define AIRSPEEDGADGETWIDGET_H_ +#include "airspeedgadgetconfiguration.h" #include #include #include @@ -43,10 +44,16 @@ class AirspeedGadgetWidget : public QGraphicsView public: AirspeedGadgetWidget(QWidget *parent = 0); ~AirspeedGadgetWidget(); - void setDialFile(QString dfn); + void setDialFile(QString dfn, QString bg, QString fg, QString n1, QString n2); void paint(); - void setActual(int speed); - void setDesired(int speed); + // setNeedle1 and setNeedle2 use a timer to simulate + // needle inertia + void setNeedle1(double value); + void setNeedle2(double value); + void setN1Min(double value) {n1MinValue = value;} + void setN1Max(double value) {n1MaxValue = value;} + void setN2Min(double value) {n1MinValue = value;} + void setN2Max(double value) {n2MaxValue = value;} protected: void paintEvent(QPaintEvent *event); @@ -54,17 +61,29 @@ protected: private: -// Test functions private slots: + // Test function void testRotate(); -// End test functions + void rotateNeedles(); private: QSvgRenderer *m_renderer; QGraphicsSvgItem *m_background; QGraphicsSvgItem *m_foreground; - QGraphicsSvgItem *m_desired; - QGraphicsSvgItem *m_actual; + QGraphicsSvgItem *m_needle1; + QGraphicsSvgItem *m_needle2; + double n1MinValue; + double n1MaxValue; + double n2MinValue; + double n2MaxValue; + + double needle1Target; + double needle1Value; + double needle2Target; + double needle2Value; + + // Rotation timer + QTimer dialTimer; // Test variables int testSpeed;