1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-31 16:52:10 +01:00

LP-16 Added code to delete 'local' vehicle templates.

This commit is contained in:
m_thread 2015-07-15 15:34:15 +02:00
parent da6b0715f7
commit 72938ec954
4 changed files with 214 additions and 55 deletions

View File

@ -19,17 +19,11 @@
<height>700</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>610</width>
<height>700</height>
</size>
</property>
<property name="windowTitle">
<string>Vehicle Templates</string>
</property>
<property name="sizeGripEnabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="modal">
<bool>true</bool>

View File

@ -31,6 +31,7 @@
#include <QJsonArray>
#include <QDir>
#include <QDebug>
#include <QMessageBox>
#include "vehicletemplateexportdialog.h"
#include "utils/pathutils.h"
@ -41,12 +42,13 @@ VehicleTemplateSelectorWidget::VehicleTemplateSelectorWidget(QWidget *parent) :
ui->setupUi(this);
ui->templateImage->setScene(new QGraphicsScene());
connect(ui->templateList, SIGNAL(itemSelectionChanged()), this, SLOT(templateSelectionChanged()));
connect(ui->deleteTemplateButton, SIGNAL(clicked()), this, SLOT(deleteSelectedTemplate()));
}
VehicleTemplateSelectorWidget::~VehicleTemplateSelectorWidget()
{
ui->templateList->clear();
foreach(QJsonObject * templ, m_templates.values()) {
foreach(VehicleTemplate * templ, m_templates.values()) {
delete templ;
}
m_templates.clear();
@ -69,12 +71,47 @@ QJsonObject *VehicleTemplateSelectorWidget::selectedTemplate() const
return NULL;
}
bool VehicleTemplateSelectorWidget::selectedTemplateEditable() const
{
if (ui->templateList->currentRow() >= 0) {
return ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 2).value<bool>();
}
return NULL;
}
QString VehicleTemplateSelectorWidget::selectedTemplatePath() const
{
if (ui->templateList->currentRow() >= 0) {
return ui->templateList->item(ui->templateList->currentRow())->data(Qt::UserRole + 3).value<QString>();
}
return NULL;
}
void VehicleTemplateSelectorWidget::updateTemplates()
{
loadValidFiles();
setupTemplateList();
}
void VehicleTemplateSelectorWidget::deleteSelectedTemplate()
{
if (selectedTemplateEditable()) {
if (QMessageBox::question(this, tr("Delete Vehicle Template"),
"Are you sure you want to delete the selected template?",
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
QFile fileToDelete(selectedTemplatePath());
if (fileToDelete.remove()) {
QJsonObject* templObj = selectedTemplate();
if (templObj) {
VehicleTemplate *templ = m_templates[templObj->value("uuid").toString()];
m_templates.remove(templObj->value("uuid").toString());
delete templ;
}
delete ui->templateList->item(ui->templateList->currentRow());
}
}
}
}
void VehicleTemplateSelectorWidget::updatePhoto(QJsonObject *templ)
{
QPixmap photo;
@ -129,6 +166,7 @@ void VehicleTemplateSelectorWidget::templateSelectionChanged()
QJsonObject *templ = selectedTemplate();
updatePhoto(templ);
updateDescription(templ);
ui->deleteTemplateButton->setEnabled(selectedTemplateEditable());
}
}
@ -173,7 +211,8 @@ void VehicleTemplateSelectorWidget::loadFilesInDir(QString templateBasePath, boo
templateDir.setSorting(QDir::Name);
QStringList files = templateDir.entryList();
foreach(QString fileName, files) {
QFile file(QString("%1/%2").arg(templateDir.absolutePath()).arg(fileName));
QString fullPathName = QString("%1/%2").arg(templateDir.absolutePath()).arg(fileName);
QFile file(fullPathName);
if (file.open(QFile::ReadOnly)) {
QByteArray jsonData = file.readAll();
@ -184,7 +223,7 @@ void VehicleTemplateSelectorWidget::loadFilesInDir(QString templateBasePath, boo
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);
m_templates[json["uuid"].toString()] = new VehicleTemplate(new QJsonObject(json), local, fullPathName);
}
}
} else {
@ -199,7 +238,7 @@ void VehicleTemplateSelectorWidget::loadFilesInDir(QString templateBasePath, boo
void VehicleTemplateSelectorWidget::loadValidFiles()
{
ui->templateList->clear();
foreach(QJsonObject * templ, m_templates.values()) {
foreach(VehicleTemplate * templ, m_templates.values()) {
delete templ;
}
m_templates.clear();
@ -213,10 +252,15 @@ void VehicleTemplateSelectorWidget::setupTemplateList()
QListWidgetItem *item;
foreach(QString templ, m_templates.keys()) {
QJsonObject *json = m_templates[templ];
VehicleTemplate *vtemplate = m_templates[templ];
item = new QListWidgetItem(json->value("name").toString(), ui->templateList);
item->setData(Qt::UserRole + 1, QVariant::fromValue(json));
item = new QListWidgetItem(vtemplate->templateObject()->value("name").toString(), ui->templateList);
item->setData(Qt::UserRole + 1, QVariant::fromValue(vtemplate->templateObject()));
item->setData(Qt::UserRole + 2, QVariant::fromValue(vtemplate->editable()));
if (vtemplate->editable()) {
item->setData(Qt::ForegroundRole, QVariant::fromValue(QColor(Qt::darkGreen)));
}
item->setData(Qt::UserRole + 3, QVariant::fromValue(vtemplate->templatePath()));
}
ui->templateList->sortItems(Qt::AscendingOrder);

View File

@ -36,6 +36,36 @@ namespace Ui {
class VehicleTemplateSelectorWidget;
}
class VehicleTemplate {
public:
VehicleTemplate(QJsonObject *templateObject, bool editable, QString templatePath) :
m_templateObject(templateObject), m_editable(editable), m_templatePath(templatePath) {
}
~VehicleTemplate() {
if (m_templateObject) {
delete m_templateObject;
}
}
QJsonObject *templateObject() {
return m_templateObject;
}
bool editable() {
return m_editable;
}
QString templatePath() {
return m_templatePath;
}
private:
QJsonObject *m_templateObject;
bool m_editable;
QString m_templatePath;
};
class VehicleTemplateSelectorWidget : public QWidget {
Q_OBJECT
@ -44,7 +74,6 @@ public:
~VehicleTemplateSelectorWidget();
void setTemplateInfo(int vehicleType, int vehicleSubType);
QJsonObject *selectedTemplate() const;
public slots:
void templateSelectionChanged();
@ -57,7 +86,7 @@ private:
int m_vehicleType;
int m_vehicleSubType;
QMap<QString, QJsonObject *> m_templates;
QMap<QString, VehicleTemplate *> m_templates;
QGraphicsPixmapItem *m_photoItem;
void loadValidFiles();
@ -68,9 +97,12 @@ private:
void updateDescription(QJsonObject *templ);
bool airframeIsCompatible(int vehicleType, int vehicleSubType);
QString getTemplatePath();
bool selectedTemplateEditable() const;
QString selectedTemplatePath() const;
private slots:
void updateTemplates();
void deleteSelectedTemplate();
};
Q_DECLARE_METATYPE(QJsonObject *)

View File

@ -13,71 +13,160 @@
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_3" stretch="2,0">
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QWidget" name="">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="bottomMargin">
<number>6</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2" stretch="0,4">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="templateList">
<property name="minimumSize">
<size>
<width>0</width>
<height>300</height>
</size>
</property>
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="alternatingRowColors">
<bool>false</bool>
</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>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QFrame" name="buttonFrame">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="addTemplateButton">
<property name="text">
<string>Add...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="deleteTemplateButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QTextEdit" name="templateDescription">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<widget class="QGraphicsView" name="templateImage">
<property name="minimumSize">
<size>
<width>200</width>
<height>200</height>
</size>
</property>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
<property name="styleSheet">
<string notr="true">background-color: rgba(254, 254, 254, 0);</string>
</property>
<property name="undoRedoEnabled">
<property name="interactive">
<bool>false</bool>
</property>
<property name="readOnly">
<bool>true</bool>
<property name="renderHints">
<set>QPainter::Antialiasing|QPainter::HighQualityAntialiasing|QPainter::SmoothPixmapTransform|QPainter::TextAntialiasing</set>
</property>
<property name="placeholderText">
<string>Information about the Vehicle in short.</string>
<property name="resizeAnchor">
<enum>QGraphicsView::AnchorViewCenter</enum>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QTextEdit" name="templateDescription">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</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>
</layout>
</widget>