LP-106 Setup Wizard refresh : Add dual servo setup (dual aileron or
[librepilot.git] / ground / gcs / src / plugins / setupwizard / connectiondiagram.cpp
blobc6f8b1d69f3448b785f35e66637f32ee4c6ca27d
1 /**
2 ******************************************************************************
4 * @file connectiondiagram.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
6 * @addtogroup
7 * @{
8 * @addtogroup ConnectionDiagram
9 * @{
10 * @brief
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
21 * for more details.
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
28 #include <QDebug>
29 #include <QFile>
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)
40 ui->setupUi(this);
41 setWindowTitle(tr("Connection Diagram"));
42 setupGraphicsScene();
45 ConnectionDiagram::~ConnectionDiagram()
47 delete ui;
50 void ConnectionDiagram::resizeEvent(QResizeEvent *event)
52 QWidget::resizeEvent(event);
54 fitInView();
57 void ConnectionDiagram::showEvent(QShowEvent *event)
59 QWidget::showEvent(event);
61 fitInView();
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),
69 Qt::KeepAspectRatio);
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";
87 break;
88 case VehicleConfigurationSource::CONTROLLER_REVO:
89 elementsToShow << "controller-revo";
90 break;
91 case VehicleConfigurationSource::CONTROLLER_NANO:
92 elementsToShow << "controller-nano";
93 break;
94 case VehicleConfigurationSource::CONTROLLER_OPLINK:
95 default:
96 elementsToShow << "controller-cc";
97 break;
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";
105 break;
106 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_X:
107 elementsToShow << "quad-x";
108 break;
109 case VehicleConfigurationSource::MULTI_ROTOR_QUAD_PLUS:
110 elementsToShow << "quad-p";
111 break;
112 case VehicleConfigurationSource::MULTI_ROTOR_HEXA:
113 elementsToShow << "hexa";
114 break;
115 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_X:
116 elementsToShow << "hexa-x";
117 break;
118 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_COAX_Y:
119 elementsToShow << "hexa-y";
120 break;
121 case VehicleConfigurationSource::MULTI_ROTOR_HEXA_H:
122 elementsToShow << "hexa-h";
123 break;
124 default:
125 break;
127 break;
128 case VehicleConfigurationSource::VEHICLE_FIXEDWING:
129 switch (m_configSource->getVehicleSubType()) {
130 case VehicleConfigurationSource::FIXED_WING_DUAL_AILERON:
131 elementsToShow << "aileron";
132 break;
133 case VehicleConfigurationSource::FIXED_WING_AILERON:
134 elementsToShow << "aileron-single";
135 break;
136 case VehicleConfigurationSource::FIXED_WING_ELEVON:
137 elementsToShow << "elevon";
138 break;
139 case VehicleConfigurationSource::FIXED_WING_VTAIL:
140 elementsToShow << "vtail";
141 break;
142 default:
143 break;
145 case VehicleConfigurationSource::VEHICLE_SURFACE:
146 switch (m_configSource->getVehicleSubType()) {
147 case VehicleConfigurationSource::GROUNDVEHICLE_CAR:
148 elementsToShow << "car";
149 break;
150 case VehicleConfigurationSource::GROUNDVEHICLE_DIFFERENTIAL:
151 elementsToShow << "tank";
152 break;
153 case VehicleConfigurationSource::GROUNDVEHICLE_MOTORCYCLE:
154 elementsToShow << "motorbike";
155 break;
156 default:
157 break;
159 case VehicleConfigurationSource::VEHICLE_HELI:
160 default:
161 break;
164 QString prefix = "";
165 QString suffix = "";
166 switch (m_configSource->getControllerType()) {
167 case VehicleConfigurationSource::CONTROLLER_CC:
168 case VehicleConfigurationSource::CONTROLLER_CC3D:
169 prefix = "cc-";
170 if (m_configSource->getEscType() == VehicleConfigurationSource::ESC_ONESHOT ||
171 m_configSource->getEscType() == VehicleConfigurationSource::ESC_SYNCHED) {
172 suffix = "-oneshot";
174 break;
175 case VehicleConfigurationSource::CONTROLLER_REVO:
176 prefix = "revo-";
177 break;
178 case VehicleConfigurationSource::CONTROLLER_NANO:
179 prefix = "nano-";
180 break;
181 default:
182 break;
185 switch (m_configSource->getInputType()) {
186 case VehicleConfigurationSource::INPUT_PWM:
187 elementsToShow << QString("%1pwm").arg(prefix);
188 break;
189 case VehicleConfigurationSource::INPUT_PPM:
190 elementsToShow << QString("%1ppm%2").arg(prefix).arg(suffix);
191 break;
192 case VehicleConfigurationSource::INPUT_SBUS:
193 elementsToShow << QString("%1sbus").arg(prefix);
194 break;
195 case VehicleConfigurationSource::INPUT_SRXL:
196 elementsToShow << QString("%1srxl").arg(prefix);
197 break;
198 case VehicleConfigurationSource::INPUT_DSM:
199 elementsToShow << QString("%1satellite").arg(prefix);
200 break;
201 default:
202 break;
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);
210 break;
211 case VehicleConfigurationSource::AIRSPEED_MS4525:
212 elementsToShow << QString("%1ms4525-speed-sensor").arg(prefix);
213 break;
214 default:
215 break;
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:
224 break;
225 case VehicleConfigurationSource::GPS_NMEA:
226 elementsToShow << QString("%1generic-nmea").arg(prefix);
227 break;
228 case VehicleConfigurationSource::GPS_PLATINUM:
229 elementsToShow << QString("%1OPGPS-v9").arg(prefix);
230 break;
231 case VehicleConfigurationSource::GPS_UBX:
232 elementsToShow << QString("%1OPGPS-v8-ublox").arg(prefix);
233 break;
234 default:
235 break;
238 setupGraphicsSceneItems(elementsToShow);
239 fitInView();
240 qDebug() << "Scene complete";
244 void ConnectionDiagram::setupGraphicsSceneItems(QList<QString> elementsToShow)
246 qreal z = 0;
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();
261 } else {
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);