1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-03-15 07:29:15 +01:00

OP-1174 added timing out dialog used for board disconnect and connect detection (no need to click ok anymore when disconnecting while rescuing)

This commit is contained in:
Philippe Renon 2014-05-01 16:37:12 +02:00
parent c6dfa25897
commit ef9eaaba35
2 changed files with 94 additions and 38 deletions

View File

@ -37,6 +37,44 @@
const int UploaderGadgetWidget::AUTOUPDATE_CLOSE_TIMEOUT = 7000;
TimedDialog::TimedDialog(const QString &title, const QString &labelText, int timeout, QWidget *parent, Qt::WindowFlags flags) :
QProgressDialog(labelText, tr("Cancel"), 0, timeout, parent, flags), bar(new QProgressBar(this))
{
setWindowTitle(title);
setAutoReset(false);
// open immediately...
setMinimumDuration(0);
// setup progress bar
bar->setRange(0, timeout);
bar->setFormat(tr("Timing out in %1 seconds").arg(timeout));
setBar(bar);
}
int TimedDialog::exec() {
QTimer timer(this);
connect(&timer, SIGNAL(timeout()), this, SLOT(perform()));
setValue(0);
timer.start(1000);
int result = QProgressDialog::exec();
timer.stop();
return result;
}
void TimedDialog::perform()
{
setValue(value() + 1);
int remaining = bar->maximum() - bar->value();
if (remaining > 0) {
bar->setFormat(tr("Timing out in %1 seconds").arg(remaining));
} else {
setResult(TimedDialog::TimedOut);
close();
}
}
UploaderGadgetWidget::UploaderGadgetWidget(QWidget *parent) : QWidget(parent)
{
m_config = new Ui_UploaderWidget();
@ -658,60 +696,67 @@ void UploaderGadgetWidget::systemRescue()
cm->disconnectDevice();
// stop the polling thread: otherwise it will mess up DFU
cm->suspendPolling();
// Delete all previous tabs:
while (m_config->systemElements->count()) {
QWidget *qw = m_config->systemElements->widget(0);
m_config->systemElements->removeTab(0);
delete qw;
}
// Existing DFU objects will have a handle over USB and will
// disturb everything for the rescue process:
if (dfu) {
delete dfu;
dfu = NULL;
}
// Avoid users pressing Rescue twice.
m_config->rescueButton->setEnabled(false);
// Now we're good to go:
// Now we're good to go
clearLog();
log("**********************************************************");
log("** Follow those instructions to attempt a system rescue **");
log("**********************************************************");
log("You will be prompted to first connect USB, then system power");
if (USBMonitor::instance()->availableDevices(0x20a0, -1, -1, -1).length() > 0) {
if (QMessageBox::warning(this, tr("OpenPilot Uploader"), tr("Please disconnect your OpenPilot board"), QMessageBox::Ok, QMessageBox::Cancel) == QMessageBox::Cancel) {
QString labelText = QString("<p align=\"left\">%1</p>").arg(tr("Please disconnect your OpenPilot board."));
TimedDialog progressDlg(tr("System Rescue"), labelText, 20);
connect(USBMonitor::instance(), SIGNAL(deviceRemoved(USBPortInfo)), &progressDlg, SLOT(accept()));
switch(progressDlg.exec()) {
case TimedDialog::Rejected:
// user canceled dialog
m_config->rescueButton->setEnabled(true);
return;
case TimedDialog::TimedOut:
QMessageBox::warning(this, tr("System Rescue"), tr("No board disconnection was detected!"));
m_config->rescueButton->setEnabled(true);
return;
}
}
// Now we're good to go:
// Now we're good to go
clearLog();
log("**********************************************************");
log("** Follow those instructions to attempt a system rescue **");
log("**********************************************************");
log("You will be prompted to first connect USB, then system power");
m_progress = new QProgressDialog(tr("Please connect your OpenPilot board (USB only!)"), tr("Cancel"), 0, 20);
QProgressBar *bar = new QProgressBar(m_progress);
bar->setFormat("Timeout");
m_progress->setBar(bar);
m_progress->setMinimumDuration(0);
m_progress->setRange(0, 20);
connect(m_progress, SIGNAL(canceled()), this, SLOT(cancel()));
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(perform()));
m_timer->start(1000);
connect(USBMonitor::instance(), SIGNAL(deviceDiscovered(USBPortInfo)), &m_eventloop, SLOT(quit()));
m_eventloop.exec();
if (!m_timer->isActive()) {
m_progress->close();
m_timer->stop();
QMessageBox::warning(this, tr("OpenPilot Uploader"), tr("No board connection was detected!"));
QString labelText = QString("<p align=\"left\">%1<br>%2</p>").arg(tr("Please connect your OpenPilot board.")).arg(tr("Board must be connected by USB!"));
TimedDialog progressDlg(tr("System Rescue"), labelText, 20);
connect(USBMonitor::instance(), SIGNAL(deviceDiscovered(USBPortInfo)), &progressDlg, SLOT(accept()));
switch(progressDlg.exec()) {
case TimedDialog::Rejected:
// user canceled dialog
m_config->rescueButton->setEnabled(true);
return;
case TimedDialog::TimedOut:
QMessageBox::warning(this, tr("System Rescue"), tr("No board connection was detected!"));
m_config->rescueButton->setEnabled(true);
return;
}
m_progress->close();
m_timer->stop();
log("... Detecting First Board...");
repaint();
dfu = new DFUObject(DFU_DEBUG, false, QString());
@ -733,6 +778,7 @@ void UploaderGadgetWidget::systemRescue()
return;
}
log(QString("Found ") + QString::number(dfu->numberOfDevices) + QString(" device(s)."));
if (dfu->numberOfDevices > 5) {
log("Inconsistent number of devices, aborting!");
delete dfu;
@ -753,17 +799,10 @@ void UploaderGadgetWidget::systemRescue()
m_config->resetButton->setEnabled(false);
bootButtonsSetEnable(true);
m_config->rescueButton->setEnabled(false);
currentStep = IAP_STATE_BOOTLOADER; // So that we can boot from the GUI afterwards.
// So that we can boot from the GUI afterwards.
currentStep = IAP_STATE_BOOTLOADER;
}
void UploaderGadgetWidget::perform()
{
if (m_progress->value() == 19) {
m_timer->stop();
m_eventloop.exit();
}
m_progress->setValue(m_progress->value() + 1);
}
void UploaderGadgetWidget::performAuto()
{
++autoUpdateConnectTimeout;
@ -773,11 +812,6 @@ void UploaderGadgetWidget::performAuto()
m_eventloop.exit();
}
}
void UploaderGadgetWidget::cancel()
{
m_timer->stop();
m_eventloop.exit();
}
void UploaderGadgetWidget::uploadStarted()
{

View File

@ -56,20 +56,41 @@
#include <QDesktopServices>
#include "uploader_global.h"
#include "enums.h"
using namespace OP_DFU;
using namespace uploader;
class FlightStatus;
class UPLOADER_EXPORT UploaderGadgetWidget : public QWidget {
// A dialog that will time out and close after a given delay
class TimedDialog: public QProgressDialog {
Q_OBJECT
public:
TimedDialog(const QString &title, const QString &labelText, int timeout, QWidget *parent = 0, Qt::WindowFlags flags = 0);
enum DialogCode { Rejected, Accepted, TimedOut };
public slots:
int exec();
private slots:
void perform();
private:
QProgressBar *bar;
};
class UPLOADER_EXPORT UploaderGadgetWidget : public QWidget {
Q_OBJECT
public:
UploaderGadgetWidget(QWidget *parent = 0);
~UploaderGadgetWidget();
void log(QString str);
bool autoUpdateCapable();
public slots:
void onAutopilotConnect();
void onAutopilotDisconnect();
@ -77,8 +98,10 @@ public slots:
void openHelp();
bool autoUpdate();
void autoUpdateProgress(int);
signals:
void autoUpdateSignal(uploader::AutoUpdateStep, QVariant);
private:
Ui_UploaderWidget *m_config;
DFUObject *dfu;
@ -111,9 +134,7 @@ private slots:
void commonSystemBoot(bool safeboot = false, bool erase = false);
void systemRescue();
void getSerialPorts();
void perform();
void performAuto();
void cancel();
void uploadStarted();
void uploadEnded(bool succeed);
void downloadStarted();
@ -122,6 +143,7 @@ private slots:
void finishAutoUpdate();
void closeAutoUpdate();
void autoUpdateStatus(uploader::AutoUpdateStep status, QVariant value);
};
#endif // UPLOADERGADGETWIDGET_H