dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
connectiondiagram.cpp
Go to the documentation of this file.
1 
15 /*
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful, but
22  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24  * for more details.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  *
29  * Additional note on redistribution: The copyright and license notices above
30  * must be maintained in each individual source file that is a derivative work
31  * of this source file; otherwise redistribution is prohibited.
32  */
33 
34 #include "connectiondiagram.h"
35 #include "ui_connectiondiagram.h"
36 
40 #include <coreplugin/iboardtype.h>
41 #include <systemsettings.h>
42 
43 #include <QDebug>
44 #include <QFile>
45 #include <QFileDialog>
46 #include <QMessageBox>
47 
49  : QDialog(parent)
50  , ui(new Ui::ConnectionDiagram)
51  , m_background(nullptr)
52 {
53  ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
54  utilMngr = pm->getObject<UAVObjectUtilManager>();
55  Q_ASSERT(utilMngr);
56  if (!utilMngr)
57  return;
58  uavoMngr = utilMngr->getObjectManager();
59  Q_ASSERT(uavoMngr);
60  if (!uavoMngr)
61  return;
62 
63  ui->setupUi(this);
64  setWindowTitle(tr("Connection Diagram"));
65  setupGraphicsScene();
66 
67  connect(ui->saveButton, &QAbstractButton::clicked, this, &ConnectionDiagram::saveToFile);
68 }
69 
71 {
72  delete ui;
73 }
74 
75 void ConnectionDiagram::resizeEvent(QResizeEvent *event)
76 {
77  QWidget::resizeEvent(event);
78 
79  if (m_background != NULL)
80  ui->connectionDiagram->fitInView(m_background, Qt::KeepAspectRatio);
81 }
82 
83 void ConnectionDiagram::showEvent(QShowEvent *event)
84 {
85  QWidget::showEvent(event);
86 
87  if (m_background != NULL)
88  ui->connectionDiagram->fitInView(m_background, Qt::KeepAspectRatio);
89 }
90 
91 void ConnectionDiagram::setupGraphicsScene()
92 {
93  Core::IBoardType *board = utilMngr->getBoardType();
94  if (!board)
95  return;
96 
97  QString diagram = board->getConnectionDiagram();
98  m_renderer = new QSvgRenderer(this);
99  if (QFile::exists(diagram) && m_renderer->load(diagram) && m_renderer->isValid()) {
100 
101  m_scene = new QGraphicsScene(this);
102  ui->connectionDiagram->setScene(m_scene);
103 
104  m_background = new QGraphicsSvgItem();
105  m_background->setSharedRenderer(m_renderer);
106  m_background->setElementId("background");
107  if (!m_background->boundingRect().isValid()) {
108  qWarning() << "\"background\" element seems to be missing from connection diagram";
109  return;
110  }
111  m_background->setOpacity(0);
112  m_background->setZValue(-1);
113  m_scene->addItem(m_background);
114 
115  QStringList elementsToShow;
116  elementsToShow << QString("controller-").append(board->shortName().toLower());
117 
118  QStringList uavosToShow;
119  uavosToShow << "SystemSettings";
120  uavosToShow << "ModuleSettings";
121  uavosToShow << "OnScreenDisplaySettings";
122  uavosToShow << board->getHwUAVO();
123  for (const auto &uavoName : uavosToShow) {
124  if (uavoName.length()) {
125  UAVObject *obj = uavoMngr->getObject(uavoName);
126  if (obj)
127  addUavoFieldElements(elementsToShow, obj);
128  }
129  }
130 
131  setupGraphicsSceneItems(elementsToShow);
132 
133  ui->connectionDiagram->setSceneRect(m_background->boundingRect());
134  ui->connectionDiagram->fitInView(m_background, Qt::KeepAspectRatio);
135 
136  ui->saveButton->setEnabled(true);
137 
138  qDebug() << "Scene complete";
139  }
140 }
141 
142 void ConnectionDiagram::setupGraphicsSceneItems(QStringList elementsToShow)
143 {
144  qreal z = 0;
145 
146  for (const QString &elementId : elementsToShow) {
147  if (m_renderer->elementExists(elementId)) {
148  QGraphicsSvgItem *element = new QGraphicsSvgItem();
149  element->setSharedRenderer(m_renderer);
150  element->setElementId(elementId);
151  element->setZValue(z++);
152  element->setOpacity(1.0);
153 
154  QMatrix matrix = m_renderer->matrixForElement(elementId);
155  QRectF orig = matrix.mapRect(m_renderer->boundsOnElement(elementId));
156  element->setPos(orig.x(), orig.y());
157 
158  m_scene->addItem(element);
159  qDebug() << "Adding " << elementId << " to scene at " << element->pos();
160  } else {
161  qDebug() << "Element with id: " << elementId << " not found.";
162  }
163  }
164 }
165 
166 void ConnectionDiagram::saveToFile()
167 {
168  if (!m_background)
169  return;
170 
171  QImage image(m_background->boundingRect().size().toSize(), QImage::Format_ARGB32);
172  image.fill(0);
173 
174  if (m_scene) {
175  QPainter painter(&image);
176  painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing
177  | QPainter::SmoothPixmapTransform);
178  m_scene->render(&painter);
179  }
180 
181  bool success = false;
182  QString fileName = QFileDialog::getSaveFileName(this, tr("Save Connection Diagram"), "",
183  tr("Images (*.png *.xpm *.jpg)"));
184  if (!fileName.isEmpty()) {
185  QFileInfo fn(fileName);
186  if (!fn.fileName().contains('.'))
187  fileName += ".png";
188  success = image.save(fileName);
189  }
190 
191  if (!success)
192  QMessageBox::warning(this, tr("Save Failed"),
193  tr("Invalid filename provided, diagram was not saved."));
194 }
195 
196 void ConnectionDiagram::addUavoFieldElements(QStringList &elements, UAVObject *obj,
197  const QString &prefix)
198 {
199  if (!obj) {
200  Q_ASSERT(false);
201  qWarning() << "Invalid object!";
202  return;
203  }
204 
205  for (const auto field : obj->getFields()) {
206  if (!field) {
207  Q_ASSERT(false);
208  qWarning() << "Invalid object field!";
209  continue;
210  }
211  if (field->getType() != UAVObjectField::ENUM)
212  continue;
213 
214  quint32 nelements = field->getNumElements();
215  if (nelements > 1) {
216  for (quint32 i = 0; i < nelements; i++) {
217  QString val = field->getValue(i)
218  .toString()
219  .replace(QRegExp(ENUM_SPECIAL_CHARS), "")
220  .toLower();
221  elements << QString("%0%1-%2-%3-%4")
222  .arg(prefix)
223  .arg(obj->getName().toLower())
224  .arg(field->getName().toLower())
225  .arg(field->getElementName(i)
226  .replace(QRegExp(ENUM_SPECIAL_CHARS), "")
227  .toLower())
228  .arg(val);
229  }
230  } else {
231  QString val =
232  field->getValue().toString().replace(QRegExp(ENUM_SPECIAL_CHARS), "").toLower();
233  elements << QString("%0%1-%2-%3")
234  .arg(prefix)
235  .arg(obj->getName().toLower())
236  .arg(field->getName().toLower())
237  .arg(val);
238  }
239  }
240 }
241 
Core plugin system that manages the plugins, their life cycle and their registered objects...
Definition: pluginmanager.h:53
virtual QString getConnectionDiagram()
getConnectionDiagram get the connection diagram for this board
Definition: iboardtype.h:209
for i
Definition: OPPlots.m:140
void showEvent(QShowEvent *event)
UAVObjectManager * getObjectManager()
virtual QString getHwUAVO()=0
z
Definition: OPPlots.m:102
QList< UAVObjectField * > getFields()
Definition: uavobject.cpp:196
ConnectionDiagram(QWidget *parent=nullptr)
QString getName()
Definition: uavobject.cpp:131
virtual QString shortName()=0
void resizeEvent(QResizeEvent *event)
UAVObject * getObject(const QString &name, quint32 instId=0)
Core::IBoardType * getBoardType()
Get the IBoardType corresponding to the connected board.