From 13e0fd91960b2e2b7a4b3c01b5b7fb58125f0f9c Mon Sep 17 00:00:00 2001 From: edouard Date: Tue, 13 Apr 2010 20:51:49 +0000 Subject: [PATCH] New version of the experimental SVG dial widget: I have created a new basicsvgdial widget and updated the main program to use three SVG files: background, needle and top (fixed). Use the default provided SVG files to test (copy them to the debug directory after compiling for quicker tests). basicdial.cpp and basicdial.h are not used anymore git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@495 ebee16cc-31ac-478f-84a7-5cbb03baadba --- ground/src/experimental/DialTest/DialTest.pro | 4 +- .../experimental/DialTest/basicsvgdial.cpp | 102 +++ .../src/experimental/DialTest/basicsvgdial.h | 51 ++ ground/src/experimental/DialTest/bg.svg | 301 ++++++- ground/src/experimental/DialTest/fg.svg | 230 ++++++ .../src/experimental/DialTest/mainwindow.cpp | 30 +- ground/src/experimental/DialTest/mainwindow.h | 8 +- .../src/experimental/DialTest/mainwindow.ui | 390 ++++----- ground/src/experimental/DialTest/need.svg | 760 ++++++++++++++++++ 9 files changed, 1654 insertions(+), 222 deletions(-) create mode 100644 ground/src/experimental/DialTest/basicsvgdial.cpp create mode 100644 ground/src/experimental/DialTest/basicsvgdial.h create mode 100644 ground/src/experimental/DialTest/fg.svg create mode 100644 ground/src/experimental/DialTest/need.svg diff --git a/ground/src/experimental/DialTest/DialTest.pro b/ground/src/experimental/DialTest/DialTest.pro index 052f91ea0..6e7e32224 100644 --- a/ground/src/experimental/DialTest/DialTest.pro +++ b/ground/src/experimental/DialTest/DialTest.pro @@ -6,8 +6,8 @@ TARGET = DialTest TEMPLATE = app SOURCES += main.cpp \ mainwindow.cpp \ - basicdial.cpp + basicsvgdial.cpp HEADERS += mainwindow.h \ - basicdial.h + basicsvgdial.h FORMS += mainwindow.ui RESOURCES += diff --git a/ground/src/experimental/DialTest/basicsvgdial.cpp b/ground/src/experimental/DialTest/basicsvgdial.cpp new file mode 100644 index 000000000..6619e35f3 --- /dev/null +++ b/ground/src/experimental/DialTest/basicsvgdial.cpp @@ -0,0 +1,102 @@ +#include "basicsvgdial.h" + +#include +#include + +BasicSvgDial::BasicSvgDial(QWidget *parent) : QGraphicsView(parent) +{ + m_renderer = Native; + backgroundFile = "bg.svg"; + needleFile = "need.svg"; + foregroundFile = "fg.svg"; + angle = 0; + + setScene(new QGraphicsScene(this)); + +} + +void BasicSvgDial::setRenderer(RendererType type) +{ + m_renderer = type; + setViewport(new QWidget); +} + +void BasicSvgDial::paintEvent(QPaintEvent *event) { + qDebug() << "in painEvent()"; + + QGraphicsScene *s = scene(); + s->clear(); + m_backgroundItem = new QGraphicsSvgItem(backgroundFile); + m_backgroundItem->setZValue(-1); + + m_needleItem = new QGraphicsSvgItem(needleFile); + QRectF rect = m_needleItem->boundingRect(); + m_needleItem->translate(rect.width()/2,rect.height()/2); + m_needleItem->rotate(angle); + m_needleItem->translate(-rect.width()/2,-rect.height()/2); + m_needleItem->setZValue(0); + + m_foregroundItem = new QGraphicsSvgItem(foregroundFile); + m_foregroundItem->setZValue(1); + + s->addItem(m_backgroundItem); + s->addItem(m_needleItem); + s->addItem(m_foregroundItem); + + s->setSceneRect(m_backgroundItem->boundingRect()); + + QGraphicsView::paintEvent(event); + + qDebug() << "out paintEvent()"; +} + + + + + + +void BasicSvgDial::setAngle(int i) { + qDebug() << "BasicDial::setAngle()"; + angle = value2angle(i); + update(); +} + +void BasicSvgDial::setRange(qreal bottom, qreal top) { + bottomValue = bottom; + topValue = top; +} + +void BasicSvgDial::setAngles(qreal bottom, qreal top) { + bottomAngle = bottom; + topAngle = top; + angleSpan = topAngle - bottomAngle; +} + +qreal BasicSvgDial::value2angle(qreal value) { + return bottomAngle + (value/topValue)*angleSpan; +} + +void BasicSvgDial::setValue(qreal value) { + angle = value2angle(value); + currentValue = value; + update(); +} + +qreal BasicSvgDial::getValue(void) { + return currentValue; +} + +void BasicSvgDial::setBackgroundFile(QString file) { + backgroundFile = file; + update(); +} + +void BasicSvgDial::setForegroundFile(QString file) { + foregroundFile = file; + update(); +} + +void BasicSvgDial::setNeedleFile(QString file) { + needleFile = file; + update(); +} diff --git a/ground/src/experimental/DialTest/basicsvgdial.h b/ground/src/experimental/DialTest/basicsvgdial.h new file mode 100644 index 000000000..a996dd253 --- /dev/null +++ b/ground/src/experimental/DialTest/basicsvgdial.h @@ -0,0 +1,51 @@ +#ifndef BASICSVGDIAL_H +#define BASICSVGDIAL_H + +#include + +class BasicSvgDial : public QGraphicsView +{ + Q_OBJECT + +public: + enum RendererType { Native, OpenGL, Image}; + BasicSvgDial(QWidget *parent = 0); + void setRenderer(RendererType type = Native); + + void setAngles(qreal bottom, qreal top); + void setRange(qreal bottom, qreal top); + qreal getValue(void); + void setBackgroundFile(QString file); + void setForegroundFile(QString file); + void setNeedleFile(QString file); + +public slots: + void setAngle(int i); + void setValue(qreal value); + +protected: + void paintEvent(QPaintEvent *event); + +private: + void renderBackground(void); + void renderForeground(void); + void renderNeedle(qreal angle); + qreal value2angle(qreal value); + + RendererType m_renderer; + QGraphicsItem *m_backgroundItem; + QGraphicsItem *m_foregroundItem; + QGraphicsItem *m_needleItem; + + QRectF calculateCenteredFrame(void); + + qreal angle; + QString backgroundFile, foregroundFile, needleFile; + + qreal topValue, topAngle; + qreal bottomValue, bottomAngle; + qreal angleSpan; + qreal currentValue; +}; + +#endif // BASICDIAL_H diff --git a/ground/src/experimental/DialTest/bg.svg b/ground/src/experimental/DialTest/bg.svg index 3b119aa1f..6c48fe9bd 100644 --- a/ground/src/experimental/DialTest/bg.svg +++ b/ground/src/experimental/DialTest/bg.svg @@ -2,26 +2,295 @@ + inkscape:version="0.47 r22583" + sodipodi:docname="heading-002-f.svg" + inkscape:export-filename="H:\Documents\Hobbies\W433\My Gauges\heading-001.png" + inkscape:export-xdpi="103.61" + inkscape:export-ydpi="103.61" + style="display:inline"> + id="defs10070"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + - - + style="display:inline" + inkscape:label="Dark background" + id="g2932" + inkscape:groupmode="layer" + transform="translate(-228.56377,-366.85416)"> + + + + + + + + + + + + diff --git a/ground/src/experimental/DialTest/fg.svg b/ground/src/experimental/DialTest/fg.svg new file mode 100644 index 000000000..169e01195 --- /dev/null +++ b/ground/src/experimental/DialTest/fg.svg @@ -0,0 +1,230 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + diff --git a/ground/src/experimental/DialTest/mainwindow.cpp b/ground/src/experimental/DialTest/mainwindow.cpp index 33dddb8e8..46671face 100644 --- a/ground/src/experimental/DialTest/mainwindow.cpp +++ b/ground/src/experimental/DialTest/mainwindow.cpp @@ -1,12 +1,11 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); - dial = new BasicDial(this); + dial = new BasicSvgDial(this); ui->dialLayout->addWidget(dial); ui->spinBottomAngle->setValue(30); @@ -14,6 +13,8 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi ui->spinBottomValue->setValue(0); ui->spinTopValue->setValue(100); ui->lineFile->setText(QString("bg.svg")); + ui->lineFileForeground->setText(QString("fg.svg")); + ui->lineFileNeedle->setText(QString("need.svg")); on_buttonUpdate_clicked(); dial->setValue(0); @@ -35,15 +36,10 @@ void MainWindow::on_buttonUpdate_clicked() dial->setAngles(ui->spinBottomAngle->value(), ui->spinTopAngle->value()); dial->setRange(ui->spinBottomValue->value(), ui->spinTopValue->value()); dial->setBackgroundFile(ui->lineFile->text()); + dial->setForegroundFile(ui->lineFileForeground->text()); + dial->setNeedleFile(ui->lineFileNeedle->text()); dial->setValue(dial->getValue()); - QPen pen; - if( ui->comboColor->currentText() == "red" ) pen.setColor(Qt::red); - if( ui->comboColor->currentText() == "yellow" ) pen.setColor(Qt::yellow); - if( ui->comboColor->currentText() == "black" ) pen.setColor(Qt::black); - if( ui->comboColor->currentText() == "green" ) pen.setColor(Qt::green); - if( ui->comboColor->currentText() == "blue" ) pen.setColor(Qt::blue); - dial->setPen(pen); ui->sliderValue->setRange(ui->spinBottomValue->value(), ui->spinTopValue->value()); ui->statusBar->showMessage(QString("Dial settings updated!")); @@ -56,8 +52,22 @@ void MainWindow::on_sliderValue_valueChanged(int value) ui->statusBar->showMessage(QString().sprintf("Value: %f", dial->getValue())); } -void MainWindow::on_buttonFile_clicked() + +void MainWindow::on_buttonBackground_clicked() { QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "~", tr("SVG Files (*.svg)")); ui->lineFile->setText(fileName); } + +void MainWindow::on_buttonNeedle_clicked() +{ + QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "~", tr("SVG Files (*.svg)")); + ui->lineFileNeedle->setText(fileName); +} + +void MainWindow::on_buttonForeground_clicked() +{ + QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), "~", tr("SVG Files (*.svg)")); + ui->lineFileForeground->setText(fileName); +} + diff --git a/ground/src/experimental/DialTest/mainwindow.h b/ground/src/experimental/DialTest/mainwindow.h index 3cd891c36..bffca769d 100644 --- a/ground/src/experimental/DialTest/mainwindow.h +++ b/ground/src/experimental/DialTest/mainwindow.h @@ -5,7 +5,7 @@ #include #include -#include "basicdial.h" +#include "basicsvgdial.h" namespace Ui { @@ -22,10 +22,12 @@ public: private: Ui::MainWindow *ui; - BasicDial *dial; + BasicSvgDial *dial; private slots: - void on_buttonFile_clicked(); + void on_buttonBackground_clicked(); + void on_buttonNeedle_clicked(); + void on_buttonForeground_clicked(); void on_sliderValue_valueChanged(int value); void on_buttonUpdate_clicked(); void on_buttonQuit_clicked(); diff --git a/ground/src/experimental/DialTest/mainwindow.ui b/ground/src/experimental/DialTest/mainwindow.ui index 4864c3ba5..9367b8d0a 100644 --- a/ground/src/experimental/DialTest/mainwindow.ui +++ b/ground/src/experimental/DialTest/mainwindow.ui @@ -1,191 +1,199 @@ - - - MainWindow - - - - 0 - 0 - 607 - 406 - - - - MainWindow - - - - - - - - - - - - Top value - - - - - - - - 0 - 0 - - - - - 60 - 0 - - - - 360 - - - - - - - - 80 - 0 - - - - 360 - - - - - - - Top angle - - - - - - - 360 - - - - - - - Bottom angle - - - - - - - 360 - - - - - - - Bottom value - - - - - - - Needle color - - - - - - - - black - - - - - red - - - - - green - - - - - yellow - - - - - blue - - - - - - - - - - - File - - - - - - - 100 - - - Qt::Horizontal - - - - - - - Set value - - - - - - - Update - - - - - - - Quit - - - - - - - - - - TopToolBarArea - - - false - - - - - - - - + + + MainWindow + + + + 0 + 0 + 607 + 438 + + + + MainWindow + + + + + + + + + + 6 + + + + + Top value + + + + + + + + 0 + 0 + + + + + 60 + 0 + + + + 360 + + + + + + + + 80 + 0 + + + + 360 + + + + + + + Top angle + + + + + + + 360 + + + + + + + Bottom angle + + + + + + + 360 + + + + + + + Bottom value + + + + + + + 100 + + + Qt::Horizontal + + + + + + + Set value + + + + + + + Quit + + + + + + + Background + + + + + + + + + + Browse... + + + + + + + Needle + + + + + + + Foreground + + + + + + + + + + + + + Browse... + + + + + + + Browse... + + + + + + + Update + + + + + + + + + + TopToolBarArea + + + false + + + + + + + + diff --git a/ground/src/experimental/DialTest/need.svg b/ground/src/experimental/DialTest/need.svg new file mode 100644 index 000000000..330fbc911 --- /dev/null +++ b/ground/src/experimental/DialTest/need.svg @@ -0,0 +1,760 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + N + 33 + 30 + W + 24 + 21 + S + 15 + 12 + E + 6 + 3 + + +