2 ******************************************************************************
4 * @file connectiondiagram.cpp
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2015-2016.
6 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
9 * @addtogroup ConnectionDiagram
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <QFileDialog>
32 #include "connectiondiagram.h"
33 #include "ui_connectiondiagram.h"
35 const char *ConnectionDiagram::FILE_NAME
= ":/setupwizard/resources/connection-diagrams.svg";
36 const int ConnectionDiagram::IMAGE_PADDING
= 10;
38 ConnectionDiagram::ConnectionDiagram(QWidget
*parent
, VehicleConfigurationSource
*configSource
) :
39 QDialog(parent
), ui(new Ui::ConnectionDiagram
), m_configSource(configSource
)
42 setWindowTitle(tr("Connection Diagram"));
46 ConnectionDiagram::~ConnectionDiagram()
51 void ConnectionDiagram::resizeEvent(QResizeEvent
*event
)
53 QWidget::resizeEvent(event
);
58 void ConnectionDiagram::showEvent(QShowEvent
*event
)
60 QWidget::showEvent(event
);
65 void ConnectionDiagram::fitInView()
67 ui
->connectionDiagram
->setSceneRect(m_scene
->itemsBoundingRect());
68 ui
->connectionDiagram
->fitInView(
69 m_scene
->itemsBoundingRect().adjusted(-IMAGE_PADDING
, -IMAGE_PADDING
, IMAGE_PADDING
, IMAGE_PADDING
),
73 void ConnectionDiagram::setupGraphicsScene()
75 m_renderer
= new QSvgRenderer();
76 if (QFile::exists(QString(FILE_NAME
)) &&
77 m_renderer
->load(QString(FILE_NAME
)) &&
78 m_renderer
->isValid()) {
79 m_scene
= new QGraphicsScene(this);
80 ui
->connectionDiagram
->setScene(m_scene
);
82 QList
<QString
> elementsToShow
;
84 switch (m_configSource
->getControllerType()) {
85 case VehicleConfigurationSource::CONTROLLER_CC
:
86 elementsToShow
<< "controller-cc";
88 case VehicleConfigurationSource::CONTROLLER_CC3D
:
89 elementsToShow
<< "controller-cc3d";
91 case VehicleConfigurationSource::CONTROLLER_REVO
:
92 elementsToShow
<< "controller-revo";
94 case VehicleConfigurationSource::CONTROLLER_NANO
:
95 elementsToShow
<< "controller-nano";
97 case VehicleConfigurationSource::CONTROLLER_SPARKY2
:
98 elementsToShow
<< "controller-sparky2";
100 case VehicleConfigurationSource::CONTROLLER_OPLINK
:
102 elementsToShow
<< "controller-cc";
106 switch (m_configSource
->getVehicleType()) {
107 case VehicleConfigurationSource::VEHICLE_MULTI
:
108 switch (m_configSource
->getVehicleSubType()) {
109 case VehicleConfigurationSource::MULTI_ROTOR_TRI_Y
:
110 elementsToShow
<< "tri";
112 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_X
:
113 elementsToShow
<< "quad-x";
115 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_PLUS
:
116 elementsToShow
<< "quad-p";
118 case VehicleConfigurationSource::MULTI_ROTOR_HEXA
:
119 elementsToShow
<< "hexa";
121 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_X
:
122 elementsToShow
<< "hexa-x";
124 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_COAX_Y
:
125 elementsToShow
<< "hexa-y";
127 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_H
:
128 elementsToShow
<< "hexa-h";
134 case VehicleConfigurationSource::VEHICLE_FIXEDWING
:
135 switch (m_configSource
->getVehicleSubType()) {
136 case VehicleConfigurationSource::FIXED_WING_DUAL_AILERON
:
137 elementsToShow
<< "aileron";
139 case VehicleConfigurationSource::FIXED_WING_AILERON
:
140 elementsToShow
<< "aileron-single";
142 case VehicleConfigurationSource::FIXED_WING_ELEVON
:
143 elementsToShow
<< "elevon";
145 case VehicleConfigurationSource::FIXED_WING_VTAIL
:
146 elementsToShow
<< "vtail";
151 case VehicleConfigurationSource::VEHICLE_SURFACE
:
152 switch (m_configSource
->getVehicleSubType()) {
153 case VehicleConfigurationSource::GROUNDVEHICLE_CAR
:
154 elementsToShow
<< "car";
156 case VehicleConfigurationSource::GROUNDVEHICLE_DIFFERENTIAL
:
157 elementsToShow
<< "tank";
159 case VehicleConfigurationSource::GROUNDVEHICLE_MOTORCYCLE
:
160 elementsToShow
<< "motorbike";
162 case VehicleConfigurationSource::GROUNDVEHICLE_BOAT
:
163 elementsToShow
<< "boat";
165 case VehicleConfigurationSource::GROUNDVEHICLE_DIFFERENTIAL_BOAT
:
166 elementsToShow
<< "boat_diff";
171 case VehicleConfigurationSource::VEHICLE_HELI
:
178 switch (m_configSource
->getControllerType()) {
179 case VehicleConfigurationSource::CONTROLLER_CC
:
180 case VehicleConfigurationSource::CONTROLLER_CC3D
:
182 if (m_configSource
->getEscType() == VehicleConfigurationSource::ESC_ONESHOT125
||
183 m_configSource
->getEscType() == VehicleConfigurationSource::ESC_ONESHOT42
||
184 m_configSource
->getEscType() == VehicleConfigurationSource::ESC_MULTISHOT
||
185 m_configSource
->getEscType() == VehicleConfigurationSource::ESC_SYNCHED
) {
189 case VehicleConfigurationSource::CONTROLLER_REVO
:
192 case VehicleConfigurationSource::CONTROLLER_NANO
:
195 case VehicleConfigurationSource::CONTROLLER_SPARKY2
:
202 switch (m_configSource
->getInputType()) {
203 case VehicleConfigurationSource::INPUT_PWM
:
204 elementsToShow
<< QString("%1pwm").arg(prefix
);
206 case VehicleConfigurationSource::INPUT_PPM
:
207 elementsToShow
<< QString("%1ppm%2").arg(prefix
).arg(suffix
);
209 case VehicleConfigurationSource::INPUT_SBUS
:
210 elementsToShow
<< QString("%1sbus").arg(prefix
);
212 case VehicleConfigurationSource::INPUT_SRXL
:
213 elementsToShow
<< QString("%1srxl").arg(prefix
);
215 case VehicleConfigurationSource::INPUT_DSM
:
216 elementsToShow
<< QString("%1satellite").arg(prefix
);
218 case VehicleConfigurationSource::INPUT_HOTT_SUMD
:
219 elementsToShow
<< QString("%1hott").arg(prefix
);
221 case VehicleConfigurationSource::INPUT_EXBUS
:
222 elementsToShow
<< QString("%1exbus").arg(prefix
);
224 case VehicleConfigurationSource::INPUT_IBUS
:
225 elementsToShow
<< QString("%1ibus").arg(prefix
);
231 if (m_configSource
->getVehicleType() == VehicleConfigurationSource::VEHICLE_FIXEDWING
&&
232 m_configSource
->getAirspeedType() != VehicleConfigurationSource::AIRSPEED_ESTIMATE
) {
233 switch (m_configSource
->getAirspeedType()) {
234 case VehicleConfigurationSource::AIRSPEED_EAGLETREE
:
235 elementsToShow
<< QString("%1eagletree-speed-sensor").arg(prefix
);
237 case VehicleConfigurationSource::AIRSPEED_MS4525
:
238 elementsToShow
<< QString("%1ms4525-speed-sensor").arg(prefix
);
245 if ((m_configSource
->getInputType() == VehicleConfigurationSource::INPUT_SBUS
) &&
246 (m_configSource
->getControllerType() != VehicleConfigurationSource::CONTROLLER_SPARKY2
)) {
247 prefix
= QString("flexi-%1").arg(prefix
);
249 switch (m_configSource
->getGpsType()) {
250 case VehicleConfigurationSource::GPS_DISABLED
:
252 case VehicleConfigurationSource::GPS_NMEA
:
253 elementsToShow
<< QString("%1generic-nmea").arg(prefix
);
255 case VehicleConfigurationSource::GPS_PLATINUM
:
256 elementsToShow
<< QString("%1OPGPS-v9").arg(prefix
);
258 case VehicleConfigurationSource::GPS_NAZA
:
259 elementsToShow
<< QString("%1NazaGPS").arg(prefix
);
261 case VehicleConfigurationSource::GPS_UBX
:
262 elementsToShow
<< QString("%1OPGPS-v8-ublox").arg(prefix
);
264 case VehicleConfigurationSource::GPS_UBX_FLEXI_I2CMAG
:
265 elementsToShow
<< QString("%1generic-ublox-mag").arg(prefix
);
271 setupGraphicsSceneItems(elementsToShow
);
273 qDebug() << "Scene complete";
277 void ConnectionDiagram::setupGraphicsSceneItems(QList
<QString
> elementsToShow
)
281 foreach(QString elementId
, elementsToShow
) {
282 if (m_renderer
->elementExists(elementId
)) {
283 QGraphicsSvgItem
*element
= new QGraphicsSvgItem();
284 element
->setSharedRenderer(m_renderer
);
285 element
->setElementId(elementId
);
286 element
->setZValue(z
++);
287 element
->setOpacity(1.0);
289 QMatrix matrix
= m_renderer
->matrixForElement(elementId
);
290 QRectF orig
= matrix
.mapRect(m_renderer
->boundsOnElement(elementId
));
291 element
->setPos(orig
.x(), orig
.y());
292 m_scene
->addItem(element
);
293 qDebug() << "Adding " << elementId
<< " to scene at " << element
->pos();
295 qDebug() << "Element with id: " << elementId
<< " not found.";
300 void ConnectionDiagram::on_saveButton_clicked()
302 QImage
image(2200, 1100, QImage::Format_ARGB32
);
304 image
.fill(Qt::white
);
305 QPainter
painter(&image
);
306 m_scene
->render(&painter
);
307 QString fileName
= QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Images (*.png *.xpm *.jpg)"));
308 if (!fileName
.isEmpty()) {
309 image
.save(fileName
);