Skip to content

Commit 5d72ea9

Browse files
author
David Graeff
committed
Show summary dialog with firmware update status instead of single confirmation box for every device.
1 parent e461eff commit 5d72ea9

File tree

3 files changed

+120
-90
lines changed

3 files changed

+120
-90
lines changed

openhantek/src/hantek/hantekdsocontrol.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ void HantekDsoControl::stopSampling() {
3232

3333
/// \brief Get a list of the names of the special trigger sources.
3434
const QStringList *HantekDsoControl::getSpecialTriggerSources() {
35-
return &(specialTriggerSources);
35+
return &(specialTriggerSources);
36+
}
37+
38+
const USBDevice *HantekDsoControl::getDevice() const
39+
{
40+
return device;
3641
}
3742

3843
/// \brief Initializes the command buffers and lists.

openhantek/src/hantek/hantekdsocontrol.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class HantekDsoControl : public QObject {
4343
double getMaxSamplerate();
4444

4545
const QStringList *getSpecialTriggerSources();
46+
const USBDevice* getDevice() const;
4647

4748
signals:
4849
void samplingStarted(); ///< The oscilloscope started sampling/waiting for trigger

openhantek/src/main.cpp

+113-89
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// SPDX-License-Identifier: GPL-2.0+
22

33
#include <QApplication>
4+
#include <QDesktopWidget>
5+
#include <QPushButton>
6+
#include <QVBoxLayout>
47
#include <QLibraryInfo>
58
#include <QLocale>
69
#include <QDebug>
710
#include <QMessageBox>
811
#include <QTranslator>
912
#include <QTimer>
13+
#include <QListWidget>
1014
#include <iostream>
1115
#include <memory>
1216

@@ -27,93 +31,113 @@ void showMessage(const QString& message) {
2731

2832
/// \brief Initialize resources and translations and show the main window.
2933
int main(int argc, char *argv[]) {
30-
// Set application information
31-
QCoreApplication::setOrganizationName("OpenHantek");
32-
QCoreApplication::setOrganizationDomain("www.openhantek.org");
33-
QCoreApplication::setApplicationName("OpenHantek");
34-
QCoreApplication::setApplicationVersion(VERSION);
35-
36-
QApplication openHantekApplication(argc, argv);
37-
38-
QTranslator qtTranslator;
39-
if (qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
40-
openHantekApplication.installTranslator(&qtTranslator);
41-
42-
QTranslator openHantekTranslator;
43-
if (openHantekTranslator.load(QLocale(), QLatin1String("openhantek"),
44-
QLatin1String("_"),
45-
QLatin1String(":/translations"))) {
46-
openHantekApplication.installTranslator(&openHantekTranslator);
47-
}
48-
49-
libusb_context* context;
50-
int error = libusb_init(&context);
51-
52-
if (error){
53-
showMessage(QCoreApplication::translate("","Can't initalize USB: %1").arg(libUsbErrorString(error)));
54-
return -1;
55-
}
56-
57-
FindDevices findDevices;
58-
std::list<std::unique_ptr<USBDevice>> devices = findDevices.findDevices();
59-
60-
if (devices.empty()) {
61-
showMessage(QCoreApplication::translate("","No Hantek oscilloscope found. Please check if your device is supported by this software, is connected, in the right mode (oscilloscope mode) and if the driver is correctly installed. Refer to the <a href='https://github.com/OpenHantek/openhantek/'>website</a> for help: %1").arg(findDevices.getErrorMessage()));
62-
return -1;
63-
}
64-
65-
// Upload firmwares for all connected devices
66-
for (auto i = devices.begin(); i != devices.end(); ++i) {
67-
if (i->get()->needsFirmware()) {
68-
UploadFirmware uf;
69-
if (!uf.startUpload(i->get())) {
70-
showMessage(QCoreApplication::translate("","Uploading firmware to %1: failed").arg(uf.getErrorMessage()));
71-
} else {
72-
showMessage(QCoreApplication::translate("","Uploading firmware to %1: done").arg(QString::fromStdString(i->get()->getModel().name)));
73-
}
74-
}
75-
}
76-
77-
devices.clear();
78-
79-
// Find first ready device
80-
devices = findDevices.findDevices();
81-
std::unique_ptr<USBDevice> device;
82-
for (auto i = devices.begin(); i != devices.end(); ++i) {
83-
if (i->get()->needsFirmware()) continue;
84-
QString errorMessage;
85-
if (i->get()->connectDevice(errorMessage)) {
86-
device = std::move(*i);
87-
break;
88-
} else {
89-
showMessage(QCoreApplication::translate("","A device was found, but the connection could not be established: %1").arg(findDevices.getErrorMessage()));
90-
}
91-
}
92-
93-
if (device == nullptr) {
94-
showMessage(QCoreApplication::translate("","A device was found, but the firmware upload seem to have failed or the connection could not be established: %1").arg(findDevices.getErrorMessage()));
95-
return -1;
96-
}
97-
98-
// USB device
99-
QObject::connect(device.get(), &USBDevice::deviceDisconnected, QCoreApplication::instance(), &QCoreApplication::quit);
100-
101-
QThread dsoControlThread;
102-
std::shared_ptr<HantekDsoControl> dsoControl(new HantekDsoControl(device.get()));
103-
dsoControl->moveToThread(&dsoControlThread);
104-
QObject::connect(&dsoControlThread,&QThread::started,dsoControl.get(),&HantekDsoControl::run);
105-
QObject::connect(dsoControl.get(), &HantekDsoControl::communicationError, QCoreApplication::instance(), &QCoreApplication::quit);
106-
107-
std::shared_ptr<DataAnalyzer> dataAnalyser(new DataAnalyzer());
108-
109-
OpenHantekMainWindow *openHantekMainWindow = new OpenHantekMainWindow(dsoControl, dataAnalyser);
110-
openHantekMainWindow->show();
111-
112-
dsoControlThread.start();
113-
114-
int res = openHantekApplication.exec();
115-
116-
dsoControlThread.quit();
117-
dsoControlThread.wait(10000);
118-
return res;
34+
//////// Set application information ////////
35+
QCoreApplication::setOrganizationName("OpenHantek");
36+
QCoreApplication::setOrganizationDomain("www.openhantek.org");
37+
QCoreApplication::setApplicationName("OpenHantek");
38+
QCoreApplication::setApplicationVersion(VERSION);
39+
40+
QApplication openHantekApplication(argc, argv);
41+
42+
//////// Load translations ////////
43+
QTranslator qtTranslator;
44+
if (qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
45+
openHantekApplication.installTranslator(&qtTranslator);
46+
47+
QTranslator openHantekTranslator;
48+
if (openHantekTranslator.load(QLocale(), QLatin1String("openhantek"),
49+
QLatin1String("_"),
50+
QLatin1String(":/translations"))) {
51+
openHantekApplication.installTranslator(&openHantekTranslator);
52+
}
53+
54+
//////// Find matching usb devices ////////
55+
libusb_context* context;
56+
int error = libusb_init(&context);
57+
58+
if (error){
59+
showMessage(QCoreApplication::translate("","Can't initalize USB: %1").arg(libUsbErrorString(error)));
60+
return -1;
61+
}
62+
63+
FindDevices findDevices;
64+
std::list<std::unique_ptr<USBDevice>> devices = findDevices.findDevices();
65+
66+
if (devices.empty()) {
67+
showMessage(QCoreApplication::translate("","No Hantek oscilloscope found. Please check if your device is supported by this software, is connected, in the right mode (oscilloscope mode) and if the driver is correctly installed. Refer to the <a href='https://github.com/OpenHantek/openhantek/'>website</a> for help: %1").arg(findDevices.getErrorMessage()));
68+
return -1;
69+
}
70+
71+
//////// Upload firmwares for all connected devices ////////
72+
std::unique_ptr<QDialog> dialog = std::unique_ptr<QDialog>(new QDialog);
73+
QListWidget* w = new QListWidget(dialog.get());
74+
QPushButton* btn = new QPushButton(QCoreApplication::translate("","Connect to first device"),dialog.get());
75+
dialog->move(QApplication::desktop()->screen()->rect().center() - w->rect().center());
76+
dialog->setWindowTitle(QCoreApplication::translate("","Firmware upload"));
77+
for (const auto& i: devices) {
78+
QString modelName = QString::fromStdString(i->getModel().name);
79+
if (i->needsFirmware()) {
80+
UploadFirmware uf;
81+
if (!uf.startUpload(i.get())) {
82+
w->addItem(QCoreApplication::translate("Firmware upload dialog, failed","%1: Failed (%2)").arg(modelName).arg(uf.getErrorMessage()));
83+
} else {
84+
w->addItem(QCoreApplication::translate("Firmware upload dialog, success","%1: Uploaded").arg(modelName));
85+
}
86+
} else {
87+
w->addItem(QCoreApplication::translate("Firmware upload dialog, success","%1: Ready").arg(modelName));
88+
}
89+
}
90+
devices.clear();
91+
dialog->setLayout(new QVBoxLayout());
92+
dialog->layout()->addWidget(w);
93+
dialog->layout()->addWidget(btn);
94+
btn->connect(btn, &QPushButton::clicked, QCoreApplication::instance(), &QCoreApplication::quit);
95+
dialog->show();
96+
openHantekApplication.exec();
97+
dialog->close();
98+
dialog.reset(nullptr);
99+
100+
//////// Find first ready device ////////
101+
devices = findDevices.findDevices();
102+
std::unique_ptr<USBDevice> device;
103+
for (auto& i: devices) {
104+
if (i->needsFirmware()) continue;
105+
QString modelName = QString::fromStdString(i->getModel().name);
106+
QString errorMessage;
107+
if (i->connectDevice(errorMessage)) {
108+
device = std::move(i);
109+
break;
110+
} else {
111+
showMessage(QCoreApplication::translate("","The connection to %1 can not be established: %2").arg(modelName).arg(findDevices.getErrorMessage()));
112+
}
113+
}
114+
115+
if (device == nullptr) {
116+
showMessage(QCoreApplication::translate("","A device was found, but the firmware upload seem to have failed or the connection could not be established: %1").arg(findDevices.getErrorMessage()));
117+
return -1;
118+
}
119+
120+
//////// Create DSO control object and move it to a separate thread ////////
121+
QThread dsoControlThread;
122+
std::shared_ptr<HantekDsoControl> dsoControl(new HantekDsoControl(device.get()));
123+
dsoControl->moveToThread(&dsoControlThread);
124+
QObject::connect(&dsoControlThread,&QThread::started,dsoControl.get(),&HantekDsoControl::run);
125+
QObject::connect(dsoControl.get(), &HantekDsoControl::communicationError, QCoreApplication::instance(), &QCoreApplication::quit);
126+
QObject::connect(device.get(), &USBDevice::deviceDisconnected, QCoreApplication::instance(), &QCoreApplication::quit);
127+
128+
//////// Create data analyser object ////////
129+
std::shared_ptr<DataAnalyzer> dataAnalyser(new DataAnalyzer());
130+
131+
//////// Create main window ////////
132+
OpenHantekMainWindow *openHantekMainWindow = new OpenHantekMainWindow(dsoControl, dataAnalyser);
133+
openHantekMainWindow->show();
134+
135+
//////// Start DSO thread and go into GUI main loop
136+
dsoControlThread.start();
137+
int res = openHantekApplication.exec();
138+
139+
//////// Clean up ////////
140+
dsoControlThread.quit();
141+
dsoControlThread.wait(10000);
142+
return res;
119143
}

0 commit comments

Comments
 (0)