1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-10 20:52:11 +01:00

248 lines
8.7 KiB
C++
Raw Normal View History

/**
******************************************************************************
*
* @file connectiondiagram.cpp
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
* @addtogroup
* @{
* @addtogroup ConnectionDiagram
* @{
* @brief
*****************************************************************************/
/*
* 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 <QDebug>
#include <QFile>
#include <QFileDialog>
#include "connectiondiagram.h"
#include "ui_connectiondiagram.h"
2014-09-16 19:27:18 +02:00
const char *ConnectionDiagram::FILE_NAME = ":/setupwizard/resources/connection-diagrams.svg";
const int ConnectionDiagram::IMAGE_PADDING = 10;
ConnectionDiagram::ConnectionDiagram(QWidget *parent, VehicleConfigurationSource *configSource) :
QDialog(parent), ui(new Ui::ConnectionDiagram), m_configSource(configSource)
{
ui->setupUi(this);
setWindowTitle(tr("Connection Diagram"));
setupGraphicsScene();
}
ConnectionDiagram::~ConnectionDiagram()
{
delete ui;
}
void ConnectionDiagram::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
2014-09-16 19:27:18 +02:00
fitInView();
}
void ConnectionDiagram::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
2014-09-16 19:27:18 +02:00
fitInView();
}
void ConnectionDiagram::fitInView()
{
ui->connectionDiagram->setSceneRect(m_scene->itemsBoundingRect());
ui->connectionDiagram->fitInView(
2014-09-16 19:27:18 +02:00
m_scene->itemsBoundingRect().adjusted(-IMAGE_PADDING, -IMAGE_PADDING, IMAGE_PADDING, IMAGE_PADDING),
Qt::KeepAspectRatio);
}
void ConnectionDiagram::setupGraphicsScene()
{
m_renderer = new QSvgRenderer();
if (QFile::exists(QString(FILE_NAME)) &&
m_renderer->load(QString(FILE_NAME)) &&
m_renderer->isValid()) {
m_scene = new QGraphicsScene(this);
ui->connectionDiagram->setScene(m_scene);
QList<QString> elementsToShow;
switch (m_configSource->getControllerType()) {
case VehicleConfigurationSource::CONTROLLER_CC:
case VehicleConfigurationSource::CONTROLLER_CC3D:
elementsToShow << "controller-cc";
break;
case VehicleConfigurationSource::CONTROLLER_REVO:
elementsToShow << "controller-revo";
break;
case VehicleConfigurationSource::CONTROLLER_NANO:
elementsToShow << "controller-nano";
break;
case VehicleConfigurationSource::CONTROLLER_OPLINK:
default:
elementsToShow << "controller-cc";
break;
}
switch (m_configSource->getVehicleType()) {
case VehicleConfigurationSource::VEHICLE_MULTI:
switch (m_configSource->getVehicleSubType()) {
case VehicleConfigurationSource::MULTI_ROTOR_TRI_Y:
elementsToShow << "tri";
break;
case VehicleConfigurationSource::MULTI_ROTOR_QUAD_X:
elementsToShow << "quad-x";
break;
case VehicleConfigurationSource::MULTI_ROTOR_QUAD_PLUS:
elementsToShow << "quad-p";
break;
case VehicleConfigurationSource::MULTI_ROTOR_QUAD_H:
elementsToShow << "quad-h";
break;
case VehicleConfigurationSource::MULTI_ROTOR_HEXA:
elementsToShow << "hexa";
break;
case VehicleConfigurationSource::MULTI_ROTOR_HEXA_X:
elementsToShow << "hexa-x";
break;
case VehicleConfigurationSource::MULTI_ROTOR_HEXA_COAX_Y:
elementsToShow << "hexa-y";
break;
case VehicleConfigurationSource::MULTI_ROTOR_HEXA_H:
elementsToShow << "hexa-h";
break;
default:
break;
}
break;
case VehicleConfigurationSource::VEHICLE_FIXEDWING:
Changed the element id's in one of the SVG files that I incorrectly edited. This prevents the error: Could not resolve property : SVGID_1_ Error comes from qsvghandler.cpp or qsvgstyle.cpp in the QT libraries Qt5.2.0//5.2.0-beta1/Src/qtsvg/src/svg/ The fixedwing-shapes.svg and flyingwing-shapes.svg files need modified to prevent the errors caused by qsvgtinydocument.cpp in the QT libraries. Couldn't find node aileron. Skipping rendering. Couldn't find node vtail. Skipping rendering. These errors come from the case statement that was added to updateImageAnd Description() for FixedWingPage + switch (type) { + case SetupWizard::FIXED_WING_AILERON: + elementId = "aileron"; + break; + case SetupWizard::FIXED_WING_VTAIL: + elementId = "vtail"; + break; + default: + elementId = ""; + break; + } A similar error needs to be resolved in connectiondiagram.cpp + switch (m_configSource->getVehicleSubType()) { + case VehicleConfigurationSource::FIXED_WING_AILERON: + elementsToShow << "aileron"; + break; + case VehicleConfigurationSource::FIXED_WING_VTAIL: + elementsToShow << "vtail"; + break; + default: + break; + } Likewise outputcalibrationpage.cpp will need to reverence the elements inside the SVG file properly. + m_vehicleElementIds << "fixed-aileron" << "aileron"; + m_vehicleHighlightElementIndexes << 0 << 1; ... + m_vehicleElementIds << "fixed-vtail" << "vtail"; + m_vehicleHighlightElementIndexes << 0 << 1; Until these elements are fixed in the SVG files the wizard will not render properly and allow the user to click *next*.
2013-11-24 17:29:38 -05:00
switch (m_configSource->getVehicleSubType()) {
case VehicleConfigurationSource::FIXED_WING_DUAL_AILERON:
Changed the element id's in one of the SVG files that I incorrectly edited. This prevents the error: Could not resolve property : SVGID_1_ Error comes from qsvghandler.cpp or qsvgstyle.cpp in the QT libraries Qt5.2.0//5.2.0-beta1/Src/qtsvg/src/svg/ The fixedwing-shapes.svg and flyingwing-shapes.svg files need modified to prevent the errors caused by qsvgtinydocument.cpp in the QT libraries. Couldn't find node aileron. Skipping rendering. Couldn't find node vtail. Skipping rendering. These errors come from the case statement that was added to updateImageAnd Description() for FixedWingPage + switch (type) { + case SetupWizard::FIXED_WING_AILERON: + elementId = "aileron"; + break; + case SetupWizard::FIXED_WING_VTAIL: + elementId = "vtail"; + break; + default: + elementId = ""; + break; + } A similar error needs to be resolved in connectiondiagram.cpp + switch (m_configSource->getVehicleSubType()) { + case VehicleConfigurationSource::FIXED_WING_AILERON: + elementsToShow << "aileron"; + break; + case VehicleConfigurationSource::FIXED_WING_VTAIL: + elementsToShow << "vtail"; + break; + default: + break; + } Likewise outputcalibrationpage.cpp will need to reverence the elements inside the SVG file properly. + m_vehicleElementIds << "fixed-aileron" << "aileron"; + m_vehicleHighlightElementIndexes << 0 << 1; ... + m_vehicleElementIds << "fixed-vtail" << "vtail"; + m_vehicleHighlightElementIndexes << 0 << 1; Until these elements are fixed in the SVG files the wizard will not render properly and allow the user to click *next*.
2013-11-24 17:29:38 -05:00
elementsToShow << "aileron";
break;
case VehicleConfigurationSource::FIXED_WING_AILERON:
elementsToShow << "aileron-single";
break;
2014-06-07 23:04:12 -04:00
case VehicleConfigurationSource::FIXED_WING_ELEVON:
elementsToShow << "elevon";
Changed the element id's in one of the SVG files that I incorrectly edited. This prevents the error: Could not resolve property : SVGID_1_ Error comes from qsvghandler.cpp or qsvgstyle.cpp in the QT libraries Qt5.2.0//5.2.0-beta1/Src/qtsvg/src/svg/ The fixedwing-shapes.svg and flyingwing-shapes.svg files need modified to prevent the errors caused by qsvgtinydocument.cpp in the QT libraries. Couldn't find node aileron. Skipping rendering. Couldn't find node vtail. Skipping rendering. These errors come from the case statement that was added to updateImageAnd Description() for FixedWingPage + switch (type) { + case SetupWizard::FIXED_WING_AILERON: + elementId = "aileron"; + break; + case SetupWizard::FIXED_WING_VTAIL: + elementId = "vtail"; + break; + default: + elementId = ""; + break; + } A similar error needs to be resolved in connectiondiagram.cpp + switch (m_configSource->getVehicleSubType()) { + case VehicleConfigurationSource::FIXED_WING_AILERON: + elementsToShow << "aileron"; + break; + case VehicleConfigurationSource::FIXED_WING_VTAIL: + elementsToShow << "vtail"; + break; + default: + break; + } Likewise outputcalibrationpage.cpp will need to reverence the elements inside the SVG file properly. + m_vehicleElementIds << "fixed-aileron" << "aileron"; + m_vehicleHighlightElementIndexes << 0 << 1; ... + m_vehicleElementIds << "fixed-vtail" << "vtail"; + m_vehicleHighlightElementIndexes << 0 << 1; Until these elements are fixed in the SVG files the wizard will not render properly and allow the user to click *next*.
2013-11-24 17:29:38 -05:00
break;
default:
break;
}
case VehicleConfigurationSource::VEHICLE_HELI:
case VehicleConfigurationSource::VEHICLE_SURFACE:
default:
break;
}
QString prefix = "";
if (m_configSource->getControllerType() == VehicleConfigurationSource::CONTROLLER_NANO) {
prefix = "nano-";
}
switch (m_configSource->getInputType()) {
case VehicleConfigurationSource::INPUT_PWM:
elementsToShow << QString("%1pwm").arg(prefix);
break;
case VehicleConfigurationSource::INPUT_PPM:
elementsToShow << QString("%1ppm").arg(prefix);
break;
case VehicleConfigurationSource::INPUT_SBUS:
elementsToShow << QString("%1sbus").arg(prefix);
break;
case VehicleConfigurationSource::INPUT_DSMX10:
case VehicleConfigurationSource::INPUT_DSMX11:
case VehicleConfigurationSource::INPUT_DSM2:
elementsToShow << QString("%1satellite").arg(prefix);
break;
default:
break;
}
if (m_configSource->getVehicleType() == VehicleConfigurationSource::VEHICLE_FIXEDWING &&
m_configSource->getAirspeedType() != VehicleConfigurationSource::AIRSPEED_ESTIMATE) {
switch (m_configSource->getAirspeedType()) {
case VehicleConfigurationSource::AIRSPEED_EAGLETREE:
elementsToShow << QString("%1eagletree-speed-sensor").arg(prefix);
break;
case VehicleConfigurationSource::AIRSPEED_MS4525:
elementsToShow << QString("%1ms4525-speed-sensor").arg(prefix);
break;
default:
break;
}
}
if (m_configSource->getInputType() == VehicleConfigurationSource::INPUT_SBUS) {
prefix = QString("flexi-%1").arg(prefix);
}
switch (m_configSource->getGpsType()) {
case VehicleConfigurationSource::GPS_DISABLED:
break;
case VehicleConfigurationSource::GPS_NMEA:
elementsToShow << QString("%1generic-nmea").arg(prefix);
break;
case VehicleConfigurationSource::GPS_PLATINUM:
elementsToShow << QString("%1OPGPS-v9").arg(prefix);
break;
case VehicleConfigurationSource::GPS_UBX:
elementsToShow << QString("%1OPGPS-v8-ublox").arg(prefix);
break;
default:
break;
}
setupGraphicsSceneItems(elementsToShow);
fitInView();
qDebug() << "Scene complete";
}
}
void ConnectionDiagram::setupGraphicsSceneItems(QList<QString> elementsToShow)
{
qreal z = 0;
foreach(QString elementId, elementsToShow) {
if (m_renderer->elementExists(elementId)) {
QGraphicsSvgItem *element = new QGraphicsSvgItem();
element->setSharedRenderer(m_renderer);
element->setElementId(elementId);
element->setZValue(z++);
element->setOpacity(1.0);
QMatrix matrix = m_renderer->matrixForElement(elementId);
QRectF orig = matrix.mapRect(m_renderer->boundsOnElement(elementId));
element->setPos(orig.x(), orig.y());
m_scene->addItem(element);
qDebug() << "Adding " << elementId << " to scene at " << element->pos();
} else {
qDebug() << "Element with id: " << elementId << " not found.";
}
}
}
void ConnectionDiagram::on_saveButton_clicked()
{
QImage image(2200, 1100, QImage::Format_ARGB32);
image.fill(Qt::white);
QPainter painter(&image);
m_scene->render(&painter);
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Images (*.png *.xpm *.jpg)"));
if (!fileName.isEmpty()) {
image.save(fileName);
}
}