x-tools/Source/Common/CommonUI/xToolsBleDeviceInfoComboBox.cpp
2024-03-26 11:45:54 +08:00

102 lines
2.6 KiB
C++

/***************************************************************************************************
* Copyright 2023 x-tools-author(x-tools@outlook.com). All rights reserved.
*
* The file is encoded using "utf8 with bom", it is a part of xTools project.
*
* xTools is licensed according to the terms in the file LICENCE(GPL V3) in the root of the source
* code directory.
**************************************************************************************************/
#include "xToolsBleDeviceInfoComboBox.h"
#include <QBluetoothDeviceInfo>
#include <QMessageBox>
xToolsBleDeviceInfoComboBox::xToolsBleDeviceInfoComboBox(QWidget* parent)
: xToolsComboBox(parent)
, mScanner(Q_NULLPTR)
{
mScanner = new xToolsBleScanner(this);
connect(mScanner, &xToolsBleScanner::finished, this, &xToolsBleDeviceInfoComboBox::onFinished);
connect(mScanner, &xToolsBleScanner::started, this, &xToolsBleDeviceInfoComboBox::started);
connect(mScanner,
&xToolsBleScanner::deviceDiscovered,
this,
&xToolsBleDeviceInfoComboBox::onDeviceDiscovered);
connect(mScanner,
&xToolsBleScanner::errorOccurred,
this,
&xToolsBleDeviceInfoComboBox::onErrorOccurred);
}
xToolsBleDeviceInfoComboBox::~xToolsBleDeviceInfoComboBox()
{
mScanner->stopDiscover();
}
void xToolsBleDeviceInfoComboBox::startDiscover()
{
clear();
mScanner->startDiscover();
}
void xToolsBleDeviceInfoComboBox::stopDiscover()
{
mScanner->stopDiscover();
}
bool xToolsBleDeviceInfoComboBox::isActive()
{
return mScanner->isActive();
return false;
}
void xToolsBleDeviceInfoComboBox::setTimeoutInterval(int interval)
{
#if 0
mScanner->setTimeoutInterval(interval);
#endif
}
void xToolsBleDeviceInfoComboBox::setNameFiltter(const QString& filtter)
{
mScanner->setNameFiltter(filtter);
}
void xToolsBleDeviceInfoComboBox::changeEvent(QEvent* event)
{
xToolsComboBox::changeEvent(event);
if ((event->type() == QEvent::EnabledChange) && isEnabled()) {
onFinished();
}
}
void xToolsBleDeviceInfoComboBox::onFinished()
{
if (!isEnabled()) {
return;
}
clear();
auto infos = mScanner->devicesInfoList();
for (auto& info : infos) {
QString name = mScanner->deviceName(info);
addItem(name, info);
}
emit finished();
}
void xToolsBleDeviceInfoComboBox::onDeviceDiscovered(const QBluetoothDeviceInfo& info)
{
if (!isEnabled()) {
return;
}
addItem(info.name(), QVariant::fromValue(info));
}
void xToolsBleDeviceInfoComboBox::onErrorOccurred(const QString& errStr)
{
QMessageBox::warning(this, tr("Error Occurred"), errStr);
}