2 ******************************************************************************
4 * @file connectiondiagram.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
8 * @addtogroup ConnectionDiagram
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <QFileDialog>
31 #include "connectiondiagram.h"
32 #include "ui_connectiondiagram.h"
34 const char *ConnectionDiagram::FILE_NAME
= ":/setupwizard/resources/connection-diagrams.svg";
35 const int ConnectionDiagram::IMAGE_PADDING
= 10;
37 ConnectionDiagram::ConnectionDiagram(QWidget
*parent
, VehicleConfigurationSource
*configSource
) :
38 QDialog(parent
), ui(new Ui::ConnectionDiagram
), m_configSource(configSource
)
41 setWindowTitle(tr("Connection Diagram"));
45 ConnectionDiagram::~ConnectionDiagram()
50 void ConnectionDiagram::resizeEvent(QResizeEvent
*event
)
52 QWidget::resizeEvent(event
);
57 void ConnectionDiagram::showEvent(QShowEvent
*event
)
59 QWidget::showEvent(event
);
64 void ConnectionDiagram::fitInView()
66 ui
->connectionDiagram
->setSceneRect(m_scene
->itemsBoundingRect());
67 ui
->connectionDiagram
->fitInView(
68 m_scene
->itemsBoundingRect().adjusted(-IMAGE_PADDING
, -IMAGE_PADDING
, IMAGE_PADDING
, IMAGE_PADDING
),
72 void ConnectionDiagram::setupGraphicsScene()
74 m_renderer
= new QSvgRenderer();
75 if (QFile::exists(QString(FILE_NAME
)) &&
76 m_renderer
->load(QString(FILE_NAME
)) &&
77 m_renderer
->isValid()) {
78 m_scene
= new QGraphicsScene(this);
79 ui
->connectionDiagram
->setScene(m_scene
);
81 QList
<QString
> elementsToShow
;
83 switch (m_configSource
->getControllerType()) {
84 case VehicleConfigurationSource::CONTROLLER_CC
:
85 case VehicleConfigurationSource::CONTROLLER_CC3D
:
86 elementsToShow
<< "controller-cc";
88 case VehicleConfigurationSource::CONTROLLER_REVO
:
89 elementsToShow
<< "controller-revo";
91 case VehicleConfigurationSource::CONTROLLER_NANO
:
92 elementsToShow
<< "controller-nano";
94 case VehicleConfigurationSource::CONTROLLER_OPLINK
:
96 elementsToShow
<< "controller-cc";
100 switch (m_configSource
->getVehicleType()) {
101 case VehicleConfigurationSource::VEHICLE_MULTI
:
102 switch (m_configSource
->getVehicleSubType()) {
103 case VehicleConfigurationSource::MULTI_ROTOR_TRI_Y
:
104 elementsToShow
<< "tri";
106 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_X
:
107 elementsToShow
<< "quad-x";
109 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_PLUS
:
110 elementsToShow
<< "quad-p";
112 case VehicleConfigurationSource::MULTI_ROTOR_HEXA
:
113 elementsToShow
<< "hexa";
115 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_X
:
116 elementsToShow
<< "hexa-x";
118 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_COAX_Y
:
119 elementsToShow
<< "hexa-y";
121 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_H
:
122 elementsToShow
<< "hexa-h";
128 case VehicleConfigurationSource::VEHICLE_FIXEDWING
:
129 switch (m_configSource
->getVehicleSubType()) {
130 case VehicleConfigurationSource::FIXED_WING_DUAL_AILERON
:
131 elementsToShow
<< "aileron";
133 case VehicleConfigurationSource::FIXED_WING_AILERON
:
134 elementsToShow
<< "aileron-single";
136 case VehicleConfigurationSource::FIXED_WING_ELEVON
:
137 elementsToShow
<< "elevon";
139 case VehicleConfigurationSource::FIXED_WING_VTAIL
:
140 elementsToShow
<< "vtail";
145 case VehicleConfigurationSource::VEHICLE_SURFACE
:
146 switch (m_configSource
->getVehicleSubType()) {
147 case VehicleConfigurationSource::GROUNDVEHICLE_CAR
:
148 elementsToShow
<< "car";
150 case VehicleConfigurationSource::GROUNDVEHICLE_DIFFERENTIAL
:
151 elementsToShow
<< "tank";
153 case VehicleConfigurationSource::GROUNDVEHICLE_MOTORCYCLE
:
154 elementsToShow
<< "motorbike";
159 case VehicleConfigurationSource::VEHICLE_HELI
:
166 switch (m_configSource
->getControllerType()) {
167 case VehicleConfigurationSource::CONTROLLER_CC
:
168 case VehicleConfigurationSource::CONTROLLER_CC3D
:
170 if (m_configSource
->getEscType() == VehicleConfigurationSource::ESC_ONESHOT
||
171 m_configSource
->getEscType() == VehicleConfigurationSource::ESC_SYNCHED
) {
175 case VehicleConfigurationSource::CONTROLLER_REVO
:
178 case VehicleConfigurationSource::CONTROLLER_NANO
:
185 switch (m_configSource
->getInputType()) {
186 case VehicleConfigurationSource::INPUT_PWM
:
187 elementsToShow
<< QString("%1pwm").arg(prefix
);
189 case VehicleConfigurationSource::INPUT_PPM
:
190 elementsToShow
<< QString("%1ppm%2").arg(prefix
).arg(suffix
);
192 case VehicleConfigurationSource::INPUT_SBUS
:
193 elementsToShow
<< QString("%1sbus").arg(prefix
);
195 case VehicleConfigurationSource::INPUT_SRXL
:
196 elementsToShow
<< QString("%1srxl").arg(prefix
);
198 case VehicleConfigurationSource::INPUT_DSM
:
199 elementsToShow
<< QString("%1satellite").arg(prefix
);
205 if (m_configSource
->getVehicleType() == VehicleConfigurationSource::VEHICLE_FIXEDWING
&&
206 m_configSource
->getAirspeedType() != VehicleConfigurationSource::AIRSPEED_ESTIMATE
) {
207 switch (m_configSource
->getAirspeedType()) {
208 case VehicleConfigurationSource::AIRSPEED_EAGLETREE
:
209 elementsToShow
<< QString("%1eagletree-speed-sensor").arg(prefix
);
211 case VehicleConfigurationSource::AIRSPEED_MS4525
:
212 elementsToShow
<< QString("%1ms4525-speed-sensor").arg(prefix
);
219 if (m_configSource
->getInputType() == VehicleConfigurationSource::INPUT_SBUS
) {
220 prefix
= QString("flexi-%1").arg(prefix
);
222 switch (m_configSource
->getGpsType()) {
223 case VehicleConfigurationSource::GPS_DISABLED
:
225 case VehicleConfigurationSource::GPS_NMEA
:
226 elementsToShow
<< QString("%1generic-nmea").arg(prefix
);
228 case VehicleConfigurationSource::GPS_PLATINUM
:
229 elementsToShow
<< QString("%1OPGPS-v9").arg(prefix
);
231 case VehicleConfigurationSource::GPS_UBX
:
232 elementsToShow
<< QString("%1OPGPS-v8-ublox").arg(prefix
);
238 setupGraphicsSceneItems(elementsToShow
);
240 qDebug() << "Scene complete";
244 void ConnectionDiagram::setupGraphicsSceneItems(QList
<QString
> elementsToShow
)
248 foreach(QString elementId
, elementsToShow
) {
249 if (m_renderer
->elementExists(elementId
)) {
250 QGraphicsSvgItem
*element
= new QGraphicsSvgItem();
251 element
->setSharedRenderer(m_renderer
);
252 element
->setElementId(elementId
);
253 element
->setZValue(z
++);
254 element
->setOpacity(1.0);
256 QMatrix matrix
= m_renderer
->matrixForElement(elementId
);
257 QRectF orig
= matrix
.mapRect(m_renderer
->boundsOnElement(elementId
));
258 element
->setPos(orig
.x(), orig
.y());
259 m_scene
->addItem(element
);
260 qDebug() << "Adding " << elementId
<< " to scene at " << element
->pos();
262 qDebug() << "Element with id: " << elementId
<< " not found.";
267 void ConnectionDiagram::on_saveButton_clicked()
269 QImage
image(2200, 1100, QImage::Format_ARGB32
);
271 image
.fill(Qt::white
);
272 QPainter
painter(&image
);
273 m_scene
->render(&painter
);
274 QString fileName
= QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Images (*.png *.xpm *.jpg)"));
275 if (!fileName
.isEmpty()) {
276 image
.save(fileName
);