mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-11-30 08:24:11 +01:00
OP-1763 Moving code to widget from form.
This commit is contained in:
parent
389577e62a
commit
bb880b451e
@ -27,20 +27,183 @@
|
|||||||
|
|
||||||
#include "vehicletemplateselectorwidget.h"
|
#include "vehicletemplateselectorwidget.h"
|
||||||
#include "ui_vehicletemplateselectorwidget.h"
|
#include "ui_vehicletemplateselectorwidget.h"
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QDebug>
|
||||||
|
#include "vehicletemplateexportdialog.h"
|
||||||
|
#include "utils/pathutils.h"
|
||||||
|
|
||||||
VehicleTemplateSelectorWidget::VehicleTemplateSelectorWidget(QWidget *parent) :
|
VehicleTemplateSelectorWidget::VehicleTemplateSelectorWidget(QWidget *parent) :
|
||||||
QWidget(parent),
|
QWidget(parent),
|
||||||
ui(new Ui::VehicleTemplateSelectorWidget)
|
ui(new Ui::VehicleTemplateSelectorWidget)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
ui->templateImage->setScene(new QGraphicsScene());
|
||||||
|
connect(ui->templateList, SIGNAL(itemSelectionChanged()), this, SLOT(templateSelectionChanged()));
|
||||||
}
|
}
|
||||||
|
|
||||||
VehicleTemplateSelectorWidget::~VehicleTemplateSelectorWidget()
|
VehicleTemplateSelectorWidget::~VehicleTemplateSelectorWidget()
|
||||||
{
|
{
|
||||||
|
ui->templateList->clear();
|
||||||
|
foreach(QJsonObject * templ, m_templates.values()) {
|
||||||
|
delete templ;
|
||||||
|
}
|
||||||
|
m_templates.clear();
|
||||||
|
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
void VehicleTemplateSelectorWidget::updateTemplates()
|
void VehicleTemplateSelectorWidget::updateTemplates()
|
||||||
{
|
{
|
||||||
|
loadValidFiles();
|
||||||
|
setupTemplateList();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::updatePhoto(QJsonObject *templ)
|
||||||
|
{
|
||||||
|
QPixmap photo;
|
||||||
|
|
||||||
|
if (m_photoItem != NULL) {
|
||||||
|
ui->templateImage->scene()->removeItem(m_photoItem);
|
||||||
|
}
|
||||||
|
if (templ != NULL && !templ->value("photo").isUndefined()) {
|
||||||
|
QByteArray imageData = QByteArray::fromBase64(templ->value("photo").toString().toLatin1());
|
||||||
|
photo.loadFromData(imageData, "PNG");
|
||||||
|
} else {
|
||||||
|
photo.load(":/core/images/openpilot_logo_500.png");
|
||||||
|
}
|
||||||
|
m_photoItem = ui->templateImage->scene()->addPixmap(photo);
|
||||||
|
ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
|
||||||
|
ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::updateDescription(QJsonObject *templ)
|
||||||
|
{
|
||||||
|
if (templ != NULL) {
|
||||||
|
QString description;
|
||||||
|
description.append("<b>").append(tr("Name of Vehicle: ")).append("</b>").append(templ->value("name").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Name of Owner: ")).append("</b>").append(templ->value("owner").toString());
|
||||||
|
if (templ->value("nick") != QStringLiteral("")) {
|
||||||
|
description.append(" (").append(templ->value("nick").toString()).append(")");
|
||||||
|
}
|
||||||
|
description.append("<br>");
|
||||||
|
description.append("<b>").append(tr("Size: ")).append("</b>").append(templ->value("size").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Weight: ")).append("</b>").append(templ->value("weight").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Motor(s): ")).append("</b>").append(templ->value("motor").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("ESC(s): ")).append("</b>").append(templ->value("esc").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Servo(s): ")).append("</b>").append(templ->value("servo").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Battery: ")).append("</b>").append(templ->value("battery").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Propellers(s): ")).append("</b>").append(templ->value("propeller").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Controller: ")).append("</b>").append(templ->value("controller").toString()).append("<br>");
|
||||||
|
description.append("<b>").append(tr("Comments: ")).append("</b>").append(templ->value("comment").toString());
|
||||||
|
ui->templateDescription->setText(description);
|
||||||
|
} else {
|
||||||
|
ui->templateDescription->setText(tr("This option will use the current tuning settings saved on the controller, if your controller "
|
||||||
|
"is currently unconfigured, then the OpenPilot firmware defaults will be used.\n\n"
|
||||||
|
"It is suggested that if this is a first time configuration of your controller, rather than "
|
||||||
|
"use this option, instead select a tuning set that matches your own airframe as close as "
|
||||||
|
"possible from the list above or if you are not able to fine one, then select the generic item "
|
||||||
|
"from the list."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::templateSelectionChanged()
|
||||||
|
{
|
||||||
|
if (ui->templateList->currentRow() >= 0) {
|
||||||
|
QJsonObject *templ = ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 1).value<QJsonObject *>();
|
||||||
|
updatePhoto(templ);
|
||||||
|
updateDescription(templ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool VehicleTemplateSelectorWidget::airframeIsCompatible(int vehicleType, int vehicleSubType)
|
||||||
|
{
|
||||||
|
if (vehicleType != m_vehicleType) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return vehicleSubType == m_vehicleSubType;
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::loadFilesInDir(QString templateBasePath)
|
||||||
|
{
|
||||||
|
QDir templateDir(templateBasePath);
|
||||||
|
|
||||||
|
qDebug() << "Loading templates from base path:" << templateBasePath;
|
||||||
|
QStringList names;
|
||||||
|
names << "*.optmpl";
|
||||||
|
templateDir.setNameFilters(names);
|
||||||
|
templateDir.setSorting(QDir::Name);
|
||||||
|
QStringList files = templateDir.entryList();
|
||||||
|
foreach(QString fileName, files) {
|
||||||
|
QFile file(QString("%1/%2").arg(templateDir.absolutePath()).arg(fileName));
|
||||||
|
|
||||||
|
if (file.open(QFile::ReadOnly)) {
|
||||||
|
QByteArray jsonData = file.readAll();
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument templateDoc = QJsonDocument::fromJson(jsonData, &error);
|
||||||
|
if (error.error == QJsonParseError::NoError) {
|
||||||
|
QJsonObject json = templateDoc.object();
|
||||||
|
if (airframeIsCompatible(json["type"].toInt(), json["subtype"].toInt())) {
|
||||||
|
QString uuid = json["uuid"].toString();
|
||||||
|
if (!m_templates.contains(uuid)) {
|
||||||
|
m_templates[json["uuid"].toString()] = new QJsonObject(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
qDebug() << "Error parsing json file: "
|
||||||
|
<< fileName << ". Error was:" << error.errorString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::loadValidFiles()
|
||||||
|
{
|
||||||
|
ui->templateList->clear();
|
||||||
|
foreach(QJsonObject * templ, m_templates.values()) {
|
||||||
|
delete templ;
|
||||||
|
}
|
||||||
|
m_templates.clear();
|
||||||
|
|
||||||
|
loadFilesInDir(QString("%1/%2/").arg(Utils::PathUtils().InsertDataPath("%%DATAPATH%%cloudconfig")).arg(m_templateFolder));
|
||||||
|
loadFilesInDir(QString("%1/%2/").arg(Utils::PathUtils().InsertStoragePath("%%STOREPATH%%cloudconfig")).arg(m_templateFolder));
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::setupTemplateList()
|
||||||
|
{
|
||||||
|
QListWidgetItem *item;
|
||||||
|
|
||||||
|
foreach(QString templ, m_templates.keys()) {
|
||||||
|
QJsonObject *json = m_templates[templ];
|
||||||
|
|
||||||
|
item = new QListWidgetItem(json->value("name").toString(), ui->templateList);
|
||||||
|
item->setData(Qt::UserRole + 1, QVariant::fromValue(json));
|
||||||
|
}
|
||||||
|
ui->templateList->sortItems(Qt::AscendingOrder);
|
||||||
|
|
||||||
|
item = new QListWidgetItem(tr("Current Tuning"));
|
||||||
|
item->setData(Qt::UserRole + 1, QVariant::fromValue((QJsonObject *)NULL));
|
||||||
|
ui->templateList->insertItem(0, item);
|
||||||
|
ui->templateList->setCurrentRow(0);
|
||||||
|
// TODO Add generics to top under item Current tuning
|
||||||
|
}
|
||||||
|
|
||||||
|
QString VehicleTemplateSelectorWidget::getTemplateKey(QJsonObject *templ)
|
||||||
|
{
|
||||||
|
return QString(templ->value("name").toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::resizeEvent(QResizeEvent *)
|
||||||
|
{
|
||||||
|
ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
|
||||||
|
ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::showEvent(QShowEvent *)
|
||||||
|
{
|
||||||
|
ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
|
||||||
|
ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,8 @@
|
|||||||
#ifndef VEHICLETEMPLATESELECTORWIDGET_H
|
#ifndef VEHICLETEMPLATESELECTORWIDGET_H
|
||||||
#define VEHICLETEMPLATESELECTORWIDGET_H
|
#define VEHICLETEMPLATESELECTORWIDGET_H
|
||||||
|
|
||||||
|
#include <QGraphicsItem>
|
||||||
|
#include <QJsonObject>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
@ -48,14 +50,34 @@ public:
|
|||||||
updateTemplates();
|
updateTemplates();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void templateSelectionChanged();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void resizeEvent(QResizeEvent *);
|
||||||
|
void showEvent(QShowEvent *);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::VehicleTemplateSelectorWidget *ui;
|
Ui::VehicleTemplateSelectorWidget *ui;
|
||||||
QString m_templateFolder;
|
QString m_templateFolder;
|
||||||
int m_vehicleType;
|
int m_vehicleType;
|
||||||
int m_vehicleSubType;
|
int m_vehicleSubType;
|
||||||
|
|
||||||
|
QMap<QString, QJsonObject *> m_templates;
|
||||||
|
QGraphicsPixmapItem *m_photoItem;
|
||||||
|
|
||||||
|
void loadValidFiles();
|
||||||
|
void loadFilesInDir(QString templateBasePath);
|
||||||
|
void setupTemplateList();
|
||||||
|
QString getTemplateKey(QJsonObject *templ);
|
||||||
|
void updatePhoto(QJsonObject *templ);
|
||||||
|
void updateDescription(QJsonObject *templ);
|
||||||
|
bool airframeIsCompatible(int vehicleType, int vehicleSubType);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void updateTemplates();
|
void updateTemplates();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Q_DECLARE_METATYPE(QJsonObject *)
|
||||||
|
|
||||||
#endif // VEHICLETEMPLATESELECTORWIDGET_H
|
#endif // VEHICLETEMPLATESELECTORWIDGET_H
|
||||||
|
Loading…
Reference in New Issue
Block a user