From c00775b2d19da50c5065626c1fad707f63cb2c7e Mon Sep 17 00:00:00 2001 From: Neil Horne Date: Thu, 29 Jun 2017 06:09:47 +1000 Subject: [PATCH] Add model checklist edit to Companion (#5047) * Add model checklist edit to Companion * minor fix * Add only write file if text changed. Add @bsongis feedback. Minor code changes. --- companion/src/modeledit/CMakeLists.txt | 1 + companion/src/modeledit/checklistdialog.cpp | 116 ++++++++++++++ companion/src/modeledit/checklistdialog.h | 50 ++++++ companion/src/modeledit/checklistdialog.ui | 156 +++++++++++++++++++ companion/src/modeledit/setup.cpp | 10 ++ companion/src/modeledit/setup.h | 1 + companion/src/modeledit/setup.ui | 161 +++++++++++--------- 7 files changed, 421 insertions(+), 74 deletions(-) create mode 100644 companion/src/modeledit/checklistdialog.cpp create mode 100644 companion/src/modeledit/checklistdialog.h create mode 100644 companion/src/modeledit/checklistdialog.ui diff --git a/companion/src/modeledit/CMakeLists.txt b/companion/src/modeledit/CMakeLists.txt index 73f9208e3..3855d5437 100644 --- a/companion/src/modeledit/CMakeLists.txt +++ b/companion/src/modeledit/CMakeLists.txt @@ -12,6 +12,7 @@ set(modeledit_NAMES telemetry expodialog mixerdialog + checklistdialog ) set(modeledit_SRCS diff --git a/companion/src/modeledit/checklistdialog.cpp b/companion/src/modeledit/checklistdialog.cpp new file mode 100644 index 000000000..74a84ced1 --- /dev/null +++ b/companion/src/modeledit/checklistdialog.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "checklistdialog.h" +#include "ui_checklistdialog.h" + +#include "appdata.h" +#include "helpers.h" + +#include +#include + +extern AppData g; + +ChecklistDialog::ChecklistDialog(QWidget *parent, const QString modelName): + QDialog(parent), + ui(new Ui::ChecklistDialog), + mDirty(false) +{ + ui->setupUi(this); + setWindowIcon(CompanionIcon("edit.png")); + + mChecklistFolder = g.profile[g.id()].sdPath() + "/MODELS/"; + mModelChecklist = mChecklistFolder + modelName + ".txt"; + ui->file->setText("File: " + mModelChecklist); + ui->pteCheck->setPlainText(readFile(mModelChecklist,QFile::exists(mModelChecklist))); + + connect(ui->pteCheck, SIGNAL(textChanged()), this, SLOT(changed())); + connect(ui->pbImport, SIGNAL(clicked()), this, SLOT(import())); + connect(ui->pbCancel, SIGNAL(clicked()), this, SLOT(reject())); + connect(ui->pbOK, SIGNAL(clicked()), this, SLOT(update())); +} + +ChecklistDialog::~ChecklistDialog() +{ + delete ui; +} + +void ChecklistDialog::changed() +{ + mDirty = true; +} + +void ChecklistDialog::import() +{ + QString filename = QFileDialog::getOpenFileName(this, tr("Open Checklist"), mChecklistFolder, tr("Checklist Files (*.txt)")); + if (!filename.isNull()) + ui->pteCheck->setPlainText(readFile(filename,true)); + mDirty = true; +} + +void ChecklistDialog::update() +{ + if (mDirty) { + QFile file(mModelChecklist); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::critical(this, tr("Model Checklist"), tr("Cannot open file for writing %1:\n%2.").arg(QDir::toNativeSeparators(mModelChecklist), file.errorString())); + } + else { + QTextStream out(&file); + if (out.status()==QTextStream::Ok) { + out << ui->pteCheck->toPlainText(); + if (!(out.status()==QTextStream::Ok)) { + QMessageBox::critical(this, tr("Model Checklist"), tr("Cannot write to file %1:\n%2.").arg(QDir::toNativeSeparators(mModelChecklist), file.errorString())); + if (!file.flush()) { + QMessageBox::critical(this, tr("Model Checklist"), tr("Cannot write file %1:\n%2.").arg(QDir::toNativeSeparators(mModelChecklist), file.errorString())); + } + } + } + } + file.close(); + } + emit accept(); +} + +QString ChecklistDialog::readFile(const QString & filepath, const bool exists) +{ + QString data = ""; + if (!filepath.isNull()) { + QFile file(filepath); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + if (exists) { + QMessageBox::critical(this, tr("Model Checklist"), tr("Cannot open file %1:\n%2.").arg(QDir::toNativeSeparators(filepath), file.errorString())); + } + } + else { + QTextStream in(&file); + if (in.status()==QTextStream::Ok) { + data = in.readAll(); + if (!(in.status()==QTextStream::Ok)) { + QMessageBox::critical(this, tr("Model Checklist"), tr("Cannot read file %1:\n%2.").arg(QDir::toNativeSeparators(filepath), file.errorString())); + data = ""; + } + } + } + file.close(); + } + return data; +} diff --git a/companion/src/modeledit/checklistdialog.h b/companion/src/modeledit/checklistdialog.h new file mode 100644 index 000000000..1005dc236 --- /dev/null +++ b/companion/src/modeledit/checklistdialog.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) OpenTX + * + * Based on code named + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _CHECKLISTDIALOG_H_ +#define _CHECKLISTDIALOG_H_ + +#include + +namespace Ui { + class ChecklistDialog; +} + +class ChecklistDialog : public QDialog { + Q_OBJECT + +public: + ChecklistDialog(QWidget *parent, const QString modelName); + ~ChecklistDialog(); + +private slots: + void import(); + void update(); + void changed(); + +private: + Ui::ChecklistDialog *ui; + QString mModelChecklist; + QString mChecklistFolder; + QString readFile(const QString & filepath, const bool exists); + bool mDirty; +}; + +#endif // _CHECKLISTDIALOG_H_ diff --git a/companion/src/modeledit/checklistdialog.ui b/companion/src/modeledit/checklistdialog.ui new file mode 100644 index 000000000..624ccfbdf --- /dev/null +++ b/companion/src/modeledit/checklistdialog.ui @@ -0,0 +1,156 @@ + + + ChecklistDialog + + + + 0 + 0 + 630 + 373 + + + + Edit Checklist + + + + + + Qt::Vertical + + + QSizePolicy::Fixed + + + + 20 + 3 + + + + + + + + + 0 + 1 + + + + Qt::ScrollBarAsNeeded + + + Qt::ScrollBarAsNeeded + + + true + + + QPlainTextEdit::NoWrap + + + + + + + 6 + + + QLayout::SetMinimumSize + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + &Import... + + + + + + + + 0 + 0 + + + + &Cancel + + + + + + + + 0 + 0 + + + + &OK + + + + + + false + + + + + + + + + Please note, the maximum width displayable is radio model limited. Also, renaming the model will break the link to this checklist file. + + + true + + + + + + + File: unknown + + + true + + + + + + + pteCheck + pbOK + pbCancel + pbImport + + + + diff --git a/companion/src/modeledit/setup.cpp b/companion/src/modeledit/setup.cpp index 149d69d75..1a8e4e475 100644 --- a/companion/src/modeledit/setup.cpp +++ b/companion/src/modeledit/setup.cpp @@ -26,6 +26,7 @@ #include "appdata.h" #include "modelprinter.h" #include "multiprotocols.h" +#include "checklistdialog.h" TimerPanel::TimerPanel(QWidget *parent, ModelData & model, TimerData & timer, GeneralSettings & generalSettings, Firmware * firmware, QWidget * prevFocus): ModelPanel(parent, model, generalSettings, firmware), @@ -891,6 +892,7 @@ SetupPanel::SetupPanel(QWidget * parent, ModelData & model, GeneralSettings & ge if (!firmware->getCapability(HasDisplayText)) { ui->displayText->hide(); + ui->editText->hide(); } if (!firmware->getCapability(GlobalFunctions)) { @@ -1134,6 +1136,7 @@ void SetupPanel::update() ui->extendedLimits->setChecked(model->extendedLimits); ui->extendedTrims->setChecked(model->extendedTrims); ui->displayText->setChecked(model->displayChecklist); + ui->editText->setEnabled(model->displayChecklist); ui->gfEnabled->setChecked(!model->noGlobalFunctions); updateBeepCenter(); @@ -1279,6 +1282,7 @@ void SetupPanel::on_potWarningMode_currentIndexChanged(int index) void SetupPanel::on_displayText_toggled(bool checked) { model->displayChecklist = checked; + ui->editText->setEnabled(checked); emit modified(); } @@ -1306,3 +1310,9 @@ void SetupPanel::onBeepCenterToggled(bool checked) emit modified(); } } + +void SetupPanel::on_editText_clicked() +{ + ChecklistDialog *g = new ChecklistDialog(this, ui->name->text()); + g->exec(); +} diff --git a/companion/src/modeledit/setup.h b/companion/src/modeledit/setup.h index 66e819c2e..20a81d930 100644 --- a/companion/src/modeledit/setup.h +++ b/companion/src/modeledit/setup.h @@ -140,6 +140,7 @@ class SetupPanel : public ModelPanel void startupSwitchToggled(bool checked); void potWarningToggled(bool checked); void on_potWarningMode_currentIndexChanged(int index); + void on_editText_clicked(); private: Ui::Setup *ui; diff --git a/companion/src/modeledit/setup.ui b/companion/src/modeledit/setup.ui index b94f55ac0..9ad725ce1 100644 --- a/companion/src/modeledit/setup.ui +++ b/companion/src/modeledit/setup.ui @@ -6,8 +6,8 @@ 0 0 - 840 - 371 + 893 + 405 @@ -343,16 +343,6 @@ - - - - Qt::LeftToRight - - - Extended Trims - - - @@ -363,40 +353,29 @@ - - - - Qt::LeftToRight - - - Extended Limits - - - - - - - Reverse throttle operation. -If this is checked the throttle will be reversed. Idle will be forward, trim will also be reversed and the throttle warning will be reversed as well. - - - - - Qt::LeftToRight - - - Reverse Throttle - - - - - + + 0 0 + + + Never + + + + + On change + + + + + Always + + @@ -409,6 +388,13 @@ If this is checked the throttle will be reversed. Idle will be forward, trim wi + + + + Global Functions + + + @@ -419,18 +405,15 @@ If this is checked the throttle will be reversed. Idle will be forward, trim wi - - - - Qt::Horizontal + + + + + 0 + 0 + - - - 40 - 20 - - - + @@ -467,38 +450,68 @@ If this is checked the throttle will be reversed. Idle will be forward, trim wi - - + + + + Reverse throttle operation. +If this is checked the throttle will be reversed. Idle will be forward, trim will also be reversed and the throttle warning will be reversed as well. + + + + + Qt::LeftToRight + + + Reverse Throttle + + + + + - + 0 0 - - - Never - - - - - On change - - - - - Always - - - - - - - Global Functions + Edit Checklist... + + + + Qt::LeftToRight + + + Extended Trims + + + + + + + Qt::LeftToRight + + + Extended Limits + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + +