Skip to content

Commit

Permalink
Rework device selection
Browse files Browse the repository at this point in the history
* Use own QDialog class SelectDevice and clean up main.cpp.
* USBDevice: Introduce a unique usb id for a connected device
* USBDevice: Introduce iteration field, to remember in which find round a device was found
  • Loading branch information
David Graeff committed Dec 24, 2017
1 parent 3ccfb5b commit 4a1617f
Show file tree
Hide file tree
Showing 15 changed files with 434 additions and 154 deletions.
18 changes: 11 additions & 7 deletions cmake/CPackInfos.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ else()
)

set(ENV{LANG} "en_US")
execute_process(
COMMAND ${GIT_EXECUTABLE} log -n 10 "--date=format:%a %b %d %Y" "--pretty=format:* %ad %aN <%aE> %h - %s"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE CMD_RESULT
OUTPUT_VARIABLE CHANGELOG
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(GIT_VERSION_STRING VERSION_LESS 2.6)
set(CHANGELOG "")
else()
execute_process(
COMMAND ${GIT_EXECUTABLE} log -n 10 "--date=format:%a %b %d %Y" "--pretty=format:* %ad %aN <%aE> %h - %s"
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
RESULT_VARIABLE CMD_RESULT
OUTPUT_VARIABLE CHANGELOG
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
file(WRITE "${CMAKE_BINARY_DIR}/changelog" "${CHANGELOG}")
endif()

Expand Down
1 change: 1 addition & 0 deletions openhantek/res/application.qrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<RCC>
<qresource prefix="/">
<file alias="openhantek.png">images/openhantek.png</file>
<file alias="switch.png">images/switch.png</file>
</qresource>
<qresource prefix="/actions">
<file alias="open.png">images/actions/open.png</file>
Expand Down
Binary file added openhantek/res/images/switch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 9 additions & 100 deletions openhantek/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
// SPDX-License-Identifier: GPL-2.0+

#include <QApplication>
#include <QDebug>
#include <QDesktopWidget>
#include <QApplication>
#include <QLibraryInfo>
#include <QListWidget>
#include <QLocale>
#include <QMessageBox>
#include <QPushButton>
#include <QTimer>
#include <QTranslator>
#include <QVBoxLayout>

#include <iostream>
#include <memory>

#include <libusb-1.0/libusb.h>

#include "analyse/dataanalyzer.h"
#include "hantekdsocontrol.h"
#include "mainwindow.h"
#include "settings.h"
#include "usb/finddevices.h"
#include "usb/uploadFirmware.h"
#include "usb/usbdevice.h"
#include "dsomodel.h"
#include "selectdevice/selectdevice.h"

using namespace Hantek;

void showMessage(const QString &message) {
QMessageBox::information(nullptr, QCoreApplication::translate("", "No connection established!"), message);
}

/// \brief Initialize resources and translations and show the main window.
int main(int argc, char *argv[]) {
//////// Set application information ////////
Expand All @@ -53,97 +42,17 @@ int main(int argc, char *argv[]) {
}

//////// Find matching usb devices ////////
libusb_context *context;
libusb_context *context = nullptr;
int error = libusb_init(&context);

if (error) {
showMessage(QCoreApplication::translate("", "Can't initalize USB: %1").arg(libUsbErrorString(error)));
SelectDevice().showLibUSBFailedDialogModel(error);
return -1;
}
std::unique_ptr<USBDevice> device = SelectDevice().showSelectDeviceModal(context);

FindDevices findDevices;
std::list<std::unique_ptr<USBDevice>> devices = findDevices.findDevices();

if (devices.empty()) {
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()));
return -1;
}

//////// Upload firmwares for all connected devices ////////
for (const auto &i : devices) {
QString modelName = QString::fromStdString(i->getModel()->name);
if (i->needsFirmware()) {
UploadFirmware uf;
uf.startUpload(i.get());
}
}
devices.clear();

#define TR(str) QCoreApplication::translate("Firmware upload dialog", str)

//////// Select device - Autoselect if only one device is ready ////////
std::unique_ptr<QDialog> dialog = std::unique_ptr<QDialog>(new QDialog);
QListWidget *w = new QListWidget(dialog.get());

devices = findDevices.findDevices();
for (auto &i : devices) {
QString modelName = QString::fromStdString(i->getModel()->name);

if (i->needsFirmware()) {
if (UploadFirmware().startUpload(&*i)) {
w->addItem(TR("%1: Upload failed").arg(modelName));
} else {
w->addItem(TR("%1: Upload failed").arg(modelName));

}
continue;
}
QString errorMessage;
if (i->connectDevice(errorMessage)) {
w->addItem(TR("%1: Ready").arg(modelName));
w->setCurrentRow(w->count() - 1);
} else {
w->addItem(TR("%1: %2").arg(modelName).arg(findDevices.getErrorMessage()));
}
}

if (w->currentRow() == -1 || devices.size() > 1) {
QPushButton *btn = new QPushButton(QCoreApplication::translate("", "Connect to first device"), dialog.get());
dialog->move(QApplication::desktop()->screen()->rect().center() - w->rect().center());
dialog->setWindowTitle(QCoreApplication::translate("", "Firmware upload"));
dialog->setLayout(new QVBoxLayout());
dialog->layout()->addWidget(w);
dialog->layout()->addWidget(btn);
btn->connect(btn, &QPushButton::clicked, QCoreApplication::instance(), &QCoreApplication::quit);
dialog->show();
openHantekApplication.exec();
dialog->close();
}
int selectedDevice = w->currentRow();
dialog.reset(nullptr);

std::unique_ptr<USBDevice> device;
int indexCounter = 0;
for (auto &i : devices) {
if (indexCounter == selectedDevice) {
device = std::move(i);
break;
}
}
devices.clear();

if (device == nullptr || device->needsFirmware() || !device->isConnected()) {
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()));
QString errorMessage;
if (device == nullptr || !device->connectDevice(errorMessage)) {
libusb_exit(context);
return -1;
}

Expand Down
18 changes: 18 additions & 0 deletions openhantek/src/selectdevice/devicelistentry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <QString>
#include "usb/usbdevice.h"

/**
* Represents an entry in the {@link DevicesListModel}.
*/
struct DeviceListEntry {
UniqueUSBid id;
QString name;
bool canConnect;
bool needFirmware;
QString errorMessage;
QString getStatus() const {
return errorMessage.size()? errorMessage : (canConnect?"Ready":(needFirmware?"Firmware upload":"Cannot connect"));
}
};
82 changes: 82 additions & 0 deletions openhantek/src/selectdevice/deviceslistmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "deviceslistmodel.h"
#include "usb/finddevices.h"
#include "usb/uploadFirmware.h"
#include "dsomodel.h"
#include <QColor>

DevicesListModel::DevicesListModel(FindDevices *findDevices) :findDevices(findDevices) {}

int DevicesListModel::rowCount(const QModelIndex &) const
{
return entries.size();
}

int DevicesListModel::columnCount(const QModelIndex &parent) const
{
return 2;
}

QVariant DevicesListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical)
return QAbstractTableModel::headerData(section, orientation, role);
if (role == Qt::DisplayRole) {
switch(section) {
case 0: return tr("Devicename");
case 1: return tr("Status");
default: return QVariant();
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}

QVariant DevicesListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
if (role==Qt::UserRole) return QVariant::fromValue(entries[index.row()].id);
else if (role==Qt::UserRole+1) return QVariant::fromValue(entries[index.row()].canConnect);
else if (role==Qt::UserRole+2) return QVariant::fromValue(entries[index.row()].needFirmware);

if (role == Qt::DisplayRole) {
if (index.column() == 0) {
return entries[index.row()].name;
} else if (index.column() == 1) {
return entries[index.row()].getStatus();
}
}

if (role == Qt::BackgroundRole) {
if (entries[index.row()].canConnect) return QColor(Qt::darkGreen).lighter();
else if (entries[index.row()].needFirmware) return QColor(Qt::yellow).lighter();
}

return QVariant();
}

void DevicesListModel::updateDeviceList()
{
beginResetModel();
entries.clear();
endResetModel();
const FindDevices::DeviceList* devices = findDevices->getDevices();
beginInsertRows(QModelIndex(),0,devices->size());
for (auto &i : *devices) {
DeviceListEntry entry;
entry.name= QString::fromStdString(i.second->getModel()->name);
entry.id = i.first;
if (i.second->needsFirmware()) {
UploadFirmware uf;
if (!uf.startUpload(i.second.get())) {
entry.errorMessage = uf.getErrorMessage();
}
entry.needFirmware = true;
} else if (i.second->connectDevice(entry.errorMessage)) {
entry.canConnect = true;
i.second->disconnectFromDevice();
} else {
entry.canConnect = false;
}
entries.push_back(entry);
}
endInsertRows();
}
24 changes: 24 additions & 0 deletions openhantek/src/selectdevice/deviceslistmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <QAbstractTableModel>
#include "devicelistentry.h"

class FindDevices;

/**
* Provides a Model for the Qt Model/View concept. The {@see FindDevices} is required
* to update the list of available devices.
*/
class DevicesListModel: public QAbstractTableModel {
public:
DevicesListModel(FindDevices* findDevices);
// QAbstractItemModel interface
virtual int rowCount(const QModelIndex &parent) const override;
virtual int columnCount(const QModelIndex &parent) const override;
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
void updateDeviceList();
private:
std::vector<DeviceListEntry> entries;
FindDevices* findDevices;
};
Loading

0 comments on commit 4a1617f

Please sign in to comment.