mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-12-03 11:24:10 +01:00
OP-1763 Extracted template selector functionality as a widget for reuse.
This commit is contained in:
parent
5b08f02aae
commit
2b0d357df3
@ -35,58 +35,48 @@
|
|||||||
|
|
||||||
AirframeInitialTuningPage::AirframeInitialTuningPage(SetupWizard *wizard, QWidget *parent) :
|
AirframeInitialTuningPage::AirframeInitialTuningPage(SetupWizard *wizard, QWidget *parent) :
|
||||||
AbstractWizardPage(wizard, parent),
|
AbstractWizardPage(wizard, parent),
|
||||||
ui(new Ui::AirframeInitialTuningPage), m_dir(NULL), m_photoItem(NULL)
|
ui(new Ui::AirframeInitialTuningPage)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
ui->templateImage->setScene(new QGraphicsScene());
|
|
||||||
connect(ui->templateList, SIGNAL(itemSelectionChanged()), this, SLOT(templateSelectionChanged()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AirframeInitialTuningPage::~AirframeInitialTuningPage()
|
AirframeInitialTuningPage::~AirframeInitialTuningPage()
|
||||||
{
|
{
|
||||||
ui->templateList->clear();
|
|
||||||
foreach(QJsonObject * templ, m_templates.values()) {
|
|
||||||
delete templ;
|
|
||||||
}
|
|
||||||
m_templates.clear();
|
|
||||||
|
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AirframeInitialTuningPage::initializePage()
|
void AirframeInitialTuningPage::initializePage()
|
||||||
{
|
{
|
||||||
|
const char* path;
|
||||||
switch (getWizard()->getVehicleType()) {
|
switch (getWizard()->getVehicleType()) {
|
||||||
case VehicleConfigurationSource::VEHICLE_FIXEDWING:
|
case VehicleConfigurationSource::VEHICLE_FIXEDWING:
|
||||||
m_dir = VehicleTemplateExportDialog::EXPORT_FIXEDWING_NAME;
|
path = VehicleTemplateExportDialog::EXPORT_FIXEDWING_NAME;
|
||||||
break;
|
break;
|
||||||
case VehicleConfigurationSource::VEHICLE_MULTI:
|
case VehicleConfigurationSource::VEHICLE_MULTI:
|
||||||
m_dir = VehicleTemplateExportDialog::EXPORT_MULTI_NAME;
|
path = VehicleTemplateExportDialog::EXPORT_MULTI_NAME;
|
||||||
break;
|
break;
|
||||||
case VehicleConfigurationSource::VEHICLE_HELI:
|
case VehicleConfigurationSource::VEHICLE_HELI:
|
||||||
m_dir = VehicleTemplateExportDialog::EXPORT_HELI_NAME;
|
path = VehicleTemplateExportDialog::EXPORT_HELI_NAME;
|
||||||
break;
|
break;
|
||||||
case VehicleConfigurationSource::VEHICLE_SURFACE:
|
case VehicleConfigurationSource::VEHICLE_SURFACE:
|
||||||
m_dir = VehicleTemplateExportDialog::EXPORT_SURFACE_NAME;
|
path = VehicleTemplateExportDialog::EXPORT_SURFACE_NAME;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
m_dir = NULL;
|
path = NULL;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
loadValidFiles();
|
ui->selectorWidget->setTemplateInfo(path, getWizard()->getVehicleType(), getWizard()->getVehicleSubType());
|
||||||
setupTemplateList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AirframeInitialTuningPage::validatePage()
|
bool AirframeInitialTuningPage::validatePage()
|
||||||
{
|
{
|
||||||
QJsonObject *templ = NULL;
|
QJsonObject *templ = ui->selectorWidget->selectedTemplate();
|
||||||
|
|
||||||
if (ui->templateList->currentRow() >= 0) {
|
|
||||||
templ = ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 1).value<QJsonObject *>();
|
|
||||||
}
|
|
||||||
if (getWizard()->getVehicleTemplate() != NULL) {
|
if (getWizard()->getVehicleTemplate() != NULL) {
|
||||||
delete getWizard()->getVehicleTemplate();
|
delete getWizard()->getVehicleTemplate();
|
||||||
}
|
}
|
||||||
getWizard()->setVehicleTemplate(templ != NULL ? new QJsonObject(*templ) : NULL);
|
getWizard()->setVehicleTemplate(templ != NULL ? new QJsonObject(*templ) : NULL);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,159 +84,3 @@ bool AirframeInitialTuningPage::isComplete() const
|
|||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AirframeInitialTuningPage::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 AirframeInitialTuningPage::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 AirframeInitialTuningPage::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 AirframeInitialTuningPage::airframeIsCompatible(int vehicleType, int vehicleSubType)
|
|
||||||
{
|
|
||||||
if (vehicleType != getWizard()->getVehicleType()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
int wizSubType = getWizard()->getVehicleSubType();
|
|
||||||
switch (vehicleType) {
|
|
||||||
case VehicleConfigurationSource::MULTI_ROTOR_QUAD_X:
|
|
||||||
{
|
|
||||||
return wizSubType == VehicleConfigurationSource::MULTI_ROTOR_QUAD_X;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
return vehicleSubType == wizSubType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void AirframeInitialTuningPage::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 AirframeInitialTuningPage::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_dir));
|
|
||||||
loadFilesInDir(QString("%1/%2/").arg(Utils::PathUtils().InsertStoragePath("%%STOREPATH%%cloudconfig")).arg(m_dir));
|
|
||||||
}
|
|
||||||
|
|
||||||
void AirframeInitialTuningPage::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 AirframeInitialTuningPage::getTemplateKey(QJsonObject *templ)
|
|
||||||
{
|
|
||||||
return QString(templ->value("name").toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
void AirframeInitialTuningPage::resizeEvent(QResizeEvent *)
|
|
||||||
{
|
|
||||||
ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
|
|
||||||
ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AirframeInitialTuningPage::showEvent(QShowEvent *)
|
|
||||||
{
|
|
||||||
ui->templateImage->setSceneRect(ui->templateImage->scene()->itemsBoundingRect());
|
|
||||||
ui->templateImage->fitInView(ui->templateImage->scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
|
|
||||||
}
|
|
||||||
|
@ -44,28 +44,9 @@ public:
|
|||||||
void initializePage();
|
void initializePage();
|
||||||
bool validatePage();
|
bool validatePage();
|
||||||
bool isComplete() const;
|
bool isComplete() const;
|
||||||
public slots:
|
|
||||||
void templateSelectionChanged();
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void resizeEvent(QResizeEvent *);
|
|
||||||
void showEvent(QShowEvent *);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Ui::AirframeInitialTuningPage *ui;
|
Ui::AirframeInitialTuningPage *ui;
|
||||||
const char *m_dir;
|
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QJsonObject *)
|
|
||||||
|
|
||||||
#endif // AIRFRAMEINITIALTUNINGPAGE_H
|
#endif // AIRFRAMEINITIALTUNINGPAGE_H
|
||||||
|
@ -33,72 +33,25 @@ p, li { white-space: pre-wrap; }
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<widget class="VehicleTemplateSelectorWidget" name="selectorWidget" native="true">
|
||||||
<item>
|
|
||||||
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="2,0">
|
|
||||||
<item>
|
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,4">
|
|
||||||
<item>
|
|
||||||
<widget class="QListWidget" name="templateList">
|
|
||||||
<property name="verticalScrollBarPolicy">
|
|
||||||
<enum>Qt::ScrollBarAlwaysOn</enum>
|
|
||||||
</property>
|
|
||||||
<property name="editTriggers">
|
|
||||||
<set>QAbstractItemView::NoEditTriggers</set>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QGraphicsView" name="templateImage">
|
|
||||||
<property name="minimumSize">
|
|
||||||
<size>
|
|
||||||
<width>250</width>
|
|
||||||
<height>250</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
<property name="styleSheet">
|
|
||||||
<string notr="true">background-color: rgba(254, 254, 254, 0);</string>
|
|
||||||
</property>
|
|
||||||
<property name="interactive">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="renderHints">
|
|
||||||
<set>QPainter::Antialiasing|QPainter::HighQualityAntialiasing|QPainter::TextAntialiasing</set>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QTextEdit" name="templateDescription">
|
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
|
||||||
<horstretch>0</horstretch>
|
<horstretch>0</horstretch>
|
||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<property name="font">
|
|
||||||
<font>
|
|
||||||
<pointsize>10</pointsize>
|
|
||||||
</font>
|
|
||||||
</property>
|
|
||||||
<property name="undoRedoEnabled">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="readOnly">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
<property name="placeholderText">
|
|
||||||
<string>Information about the Vehicle in short.</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</item>
|
|
||||||
</layout>
|
|
||||||
</widget>
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>VehicleTemplateSelectorWidget</class>
|
||||||
|
<extends>QWidget</extends>
|
||||||
|
<header>vehicletemplateselectorwidget.h</header>
|
||||||
|
<container>1</container>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
</ui>
|
</ui>
|
||||||
|
@ -36,7 +36,7 @@
|
|||||||
|
|
||||||
VehicleTemplateSelectorWidget::VehicleTemplateSelectorWidget(QWidget *parent) :
|
VehicleTemplateSelectorWidget::VehicleTemplateSelectorWidget(QWidget *parent) :
|
||||||
QWidget(parent),
|
QWidget(parent),
|
||||||
ui(new Ui::VehicleTemplateSelectorWidget)
|
ui(new Ui::VehicleTemplateSelectorWidget), m_photoItem(NULL)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
ui->templateImage->setScene(new QGraphicsScene());
|
ui->templateImage->setScene(new QGraphicsScene());
|
||||||
@ -54,6 +54,21 @@ VehicleTemplateSelectorWidget::~VehicleTemplateSelectorWidget()
|
|||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VehicleTemplateSelectorWidget::setTemplateInfo(QString path, int vehicleType, int vehicleSubType) {
|
||||||
|
m_templateFolder = path;
|
||||||
|
m_vehicleType = vehicleType;
|
||||||
|
m_vehicleSubType = vehicleSubType;
|
||||||
|
updateTemplates();
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject *VehicleTemplateSelectorWidget::selectedTemplate() const
|
||||||
|
{
|
||||||
|
if (ui->templateList->currentRow() >= 0) {
|
||||||
|
return ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 1).value<QJsonObject *>();
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
void VehicleTemplateSelectorWidget::updateTemplates()
|
void VehicleTemplateSelectorWidget::updateTemplates()
|
||||||
{
|
{
|
||||||
loadValidFiles();
|
loadValidFiles();
|
||||||
@ -111,7 +126,7 @@ void VehicleTemplateSelectorWidget::updateDescription(QJsonObject *templ)
|
|||||||
void VehicleTemplateSelectorWidget::templateSelectionChanged()
|
void VehicleTemplateSelectorWidget::templateSelectionChanged()
|
||||||
{
|
{
|
||||||
if (ui->templateList->currentRow() >= 0) {
|
if (ui->templateList->currentRow() >= 0) {
|
||||||
QJsonObject *templ = ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 1).value<QJsonObject *>();
|
QJsonObject *templ = selectedTemplate();
|
||||||
updatePhoto(templ);
|
updatePhoto(templ);
|
||||||
updateDescription(templ);
|
updateDescription(templ);
|
||||||
}
|
}
|
||||||
|
@ -43,12 +43,8 @@ class VehicleTemplateSelectorWidget : public QWidget
|
|||||||
public:
|
public:
|
||||||
explicit VehicleTemplateSelectorWidget(QWidget *parent = 0);
|
explicit VehicleTemplateSelectorWidget(QWidget *parent = 0);
|
||||||
~VehicleTemplateSelectorWidget();
|
~VehicleTemplateSelectorWidget();
|
||||||
void setTemplateInfo(QString path, int vehicleType, int vehicleSubType) {
|
void setTemplateInfo(QString path, int vehicleType, int vehicleSubType);
|
||||||
m_templateFolder = path;
|
QJsonObject *selectedTemplate() const;
|
||||||
m_vehicleType = vehicleType;
|
|
||||||
m_vehicleSubType = vehicleSubType;
|
|
||||||
updateTemplates();
|
|
||||||
}
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void templateSelectionChanged();
|
void templateSelectionChanged();
|
||||||
|
Loading…
Reference in New Issue
Block a user