35 #include "ui_connectiondiagram.h"
41 #include <systemsettings.h>
45 #include <QFileDialog>
46 #include <QMessageBox>
51 , m_background(nullptr)
64 setWindowTitle(tr(
"Connection Diagram"));
67 connect(ui->saveButton, &QAbstractButton::clicked,
this, &ConnectionDiagram::saveToFile);
77 QWidget::resizeEvent(event);
79 if (m_background != NULL)
80 ui->connectionDiagram->fitInView(m_background, Qt::KeepAspectRatio);
85 QWidget::showEvent(event);
87 if (m_background != NULL)
88 ui->connectionDiagram->fitInView(m_background, Qt::KeepAspectRatio);
91 void ConnectionDiagram::setupGraphicsScene()
98 m_renderer =
new QSvgRenderer(
this);
99 if (QFile::exists(diagram) && m_renderer->load(diagram) && m_renderer->isValid()) {
101 m_scene =
new QGraphicsScene(
this);
102 ui->connectionDiagram->setScene(m_scene);
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";
111 m_background->setOpacity(0);
112 m_background->setZValue(-1);
113 m_scene->addItem(m_background);
115 QStringList elementsToShow;
116 elementsToShow << QString(
"controller-").append(board->
shortName().toLower());
118 QStringList uavosToShow;
119 uavosToShow <<
"SystemSettings";
120 uavosToShow <<
"ModuleSettings";
121 uavosToShow <<
"OnScreenDisplaySettings";
123 for (
const auto &uavoName : uavosToShow) {
124 if (uavoName.length()) {
127 addUavoFieldElements(elementsToShow, obj);
131 setupGraphicsSceneItems(elementsToShow);
133 ui->connectionDiagram->setSceneRect(m_background->boundingRect());
134 ui->connectionDiagram->fitInView(m_background, Qt::KeepAspectRatio);
136 ui->saveButton->setEnabled(
true);
138 qDebug() <<
"Scene complete";
142 void ConnectionDiagram::setupGraphicsSceneItems(QStringList elementsToShow)
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);
154 QMatrix matrix = m_renderer->matrixForElement(elementId);
155 QRectF orig = matrix.mapRect(m_renderer->boundsOnElement(elementId));
156 element->setPos(orig.x(), orig.y());
158 m_scene->addItem(element);
159 qDebug() <<
"Adding " << elementId <<
" to scene at " << element->pos();
161 qDebug() <<
"Element with id: " << elementId <<
" not found.";
166 void ConnectionDiagram::saveToFile()
171 QImage image(m_background->boundingRect().size().toSize(), QImage::Format_ARGB32);
175 QPainter painter(&image);
176 painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing
177 | QPainter::SmoothPixmapTransform);
178 m_scene->render(&painter);
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(
'.'))
188 success = image.save(fileName);
192 QMessageBox::warning(
this, tr(
"Save Failed"),
193 tr(
"Invalid filename provided, diagram was not saved."));
196 void ConnectionDiagram::addUavoFieldElements(QStringList &elements,
UAVObject *obj,
197 const QString &prefix)
201 qWarning() <<
"Invalid object!";
205 for (
const auto field : obj->
getFields()) {
208 qWarning() <<
"Invalid object field!";
214 quint32 nelements = field->getNumElements();
216 for (quint32
i = 0;
i < nelements;
i++) {
217 QString val = field->getValue(
i)
219 .replace(QRegExp(ENUM_SPECIAL_CHARS),
"")
221 elements << QString(
"%0%1-%2-%3-%4")
224 .arg(field->getName().toLower())
225 .arg(field->getElementName(
i)
226 .replace(QRegExp(ENUM_SPECIAL_CHARS),
"")
232 field->getValue().toString().replace(QRegExp(ENUM_SPECIAL_CHARS),
"").toLower();
233 elements << QString(
"%0%1-%2-%3")
236 .arg(field->getName().toLower())
Core plugin system that manages the plugins, their life cycle and their registered objects...
virtual QString getConnectionDiagram()
getConnectionDiagram get the connection diagram for this board
void showEvent(QShowEvent *event)
UAVObjectManager * getObjectManager()
virtual QString getHwUAVO()=0
QList< UAVObjectField * > getFields()
ConnectionDiagram(QWidget *parent=nullptr)
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.