forked from martonmiklos/sailfish-ble-rc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- ATM broken figuring out exposing the availabledevicesmodel to QML properly
- Loading branch information
1 parent
f9e81b4
commit 24cc6ce
Showing
16 changed files
with
472 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,132 @@ | ||
#include "availabledevicesmodel.h" | ||
|
||
#include "shell_rc_car.h" | ||
|
||
AvailableDevicesModel::AvailableDevicesModel(QObject *parent) | ||
: QAbstractItemModel(parent) | ||
: QAbstractListModel(parent) | ||
{ | ||
//! [les-devicediscovery-1] | ||
m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(); | ||
//discoveryAgent->setLowEnergyDiscoveryTimeout(5000); | ||
connect(m_discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)), | ||
this, SLOT(deviceDiscovered(const QBluetoothDeviceInfo&))); | ||
connect(m_discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), | ||
this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error))); | ||
connect(m_discoveryAgent, SIGNAL(finished()), this, SLOT(deviceScanFinished())); | ||
} | ||
|
||
QVariant AvailableDevicesModel::headerData(int section, Qt::Orientation orientation, int role) const | ||
int AvailableDevicesModel::rowCount(const QModelIndex &parent) const | ||
{ | ||
// FIXME: Implement me! | ||
Q_UNUSED(parent) | ||
return m_devices.count(); | ||
} | ||
|
||
QModelIndex AvailableDevicesModel::index(int row, int column, const QModelIndex &parent) const | ||
int AvailableDevicesModel::columnCount(const QModelIndex &parent) const | ||
{ | ||
// FIXME: Implement me! | ||
Q_UNUSED(parent) | ||
return 1; | ||
} | ||
|
||
QModelIndex AvailableDevicesModel::parent(const QModelIndex &index) const | ||
QVariant AvailableDevicesModel::data(const QModelIndex &index, int role) const | ||
{ | ||
// FIXME: Implement me! | ||
if (!index.isValid()) | ||
return QVariant(); | ||
|
||
switch (static_cast<Role>(role)) { | ||
case AvailableDevicesModel::Name: | ||
return m_devices.at(index.row()).name(); | ||
case AvailableDevicesModel::TypeName: | ||
return m_devices.at(index.row()).typeName; | ||
case AvailableDevicesModel::ImagePath: | ||
return m_devices.at(index.row()).imagePath; | ||
case AvailableDevicesModel::Index: | ||
return index.row(); | ||
} | ||
return QVariant(); | ||
} | ||
|
||
int AvailableDevicesModel::rowCount(const QModelIndex &parent) const | ||
void AvailableDevicesModel::detectDevices() | ||
{ | ||
if (!parent.isValid()) | ||
return 0; | ||
beginResetModel(); | ||
endResetModel(); | ||
m_devices.clear(); | ||
setScanInProgress(true); | ||
m_discoveryAgent->start(); | ||
} | ||
|
||
// FIXME: Implement me! | ||
void AvailableDevicesModel::deviceDiscovered(const QBluetoothDeviceInfo & info) | ||
{ | ||
if (Shell_RC_Car::isDevice(info)) { | ||
DetectedDevice d; | ||
d.info = info; | ||
d.typeName = tr("Shell Bluetooth RC car"); | ||
d.imagePath = Shell_RC_Car::imagePath(); | ||
d.type = Shell; | ||
beginInsertRows(QModelIndex(), m_devices.count(), m_devices.count() + 1); | ||
m_devices.append(d); | ||
endInsertRows(); | ||
} | ||
} | ||
|
||
int AvailableDevicesModel::columnCount(const QModelIndex &parent) const | ||
void AvailableDevicesModel::deviceScanFinished() | ||
{ | ||
if (!parent.isValid()) | ||
return 0; | ||
setScanInProgress(false); | ||
} | ||
|
||
// FIXME: Implement me! | ||
void AvailableDevicesModel::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error) | ||
{ | ||
setScanInProgress(false); | ||
} | ||
|
||
QVariant AvailableDevicesModel::data(const QModelIndex &index, int role) const | ||
void AvailableDevicesModel::setScanInProgress(bool scanInProgress) | ||
{ | ||
if (!index.isValid()) | ||
return QVariant(); | ||
if (m_scanInProgress != scanInProgress) { | ||
m_scanInProgress = scanInProgress; | ||
emit scanStateChanged(); | ||
} | ||
} | ||
|
||
// FIXME: Implement me! | ||
return QVariant(); | ||
AbstractRC_Car *AvailableDevicesModel::currentDevice() const | ||
{ | ||
return m_currentDevice; | ||
} | ||
|
||
QObject *AvailableDevicesModel::qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine) | ||
{ | ||
Q_UNUSED(engine) | ||
Q_UNUSED(scriptEngine) | ||
|
||
static AvailableDevicesModel instance; | ||
return &instance; | ||
} | ||
|
||
bool AvailableDevicesModel::scanInProgress() const | ||
{ | ||
return m_scanInProgress; | ||
} | ||
|
||
QHash<int, QByteArray> AvailableDevicesModel::roleNames() const | ||
{ | ||
QHash<int, QByteArray> roles; | ||
roles[TypeName] = QByteArrayLiteral("TypeName"); | ||
roles[ImagePath] = QByteArrayLiteral("ImagePath"); | ||
roles[Name] = QByteArrayLiteral("Name"); | ||
roles[Index] = QByteArrayLiteral("Index"); | ||
return roles; | ||
} | ||
|
||
void AvailableDevicesModel::useDevice(int deviceIndex) | ||
{ | ||
if (deviceIndex < m_devices.count()) { | ||
if (m_currentDevice) { | ||
delete m_currentDevice; | ||
} | ||
switch (m_devices.at(deviceIndex).type) { | ||
case Shell: | ||
auto shell = new Shell_RC_Car(this); | ||
shell->connectToDevice(m_devices.at(deviceIndex).info); | ||
m_currentDevice = shell; | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,73 @@ | ||
#ifndef AVAILABLEDEVICESMODEL_H | ||
#define AVAILABLEDEVICESMODEL_H | ||
#pragma once | ||
|
||
#include <QAbstractItemModel> | ||
#include "abstract_rc_car.h" | ||
|
||
class AvailableDevicesModel : public QAbstractItemModel | ||
#include <QAbstractListModel> | ||
#include <QBluetoothDeviceDiscoveryAgent> | ||
#include <QLowEnergyController> | ||
#include <QQmlEngine> | ||
|
||
class AvailableDevicesModel : public QAbstractListModel | ||
{ | ||
Q_OBJECT | ||
|
||
Q_PROPERTY(bool scanInProgress READ scanInProgress NOTIFY scanStateChanged) | ||
public: | ||
explicit AvailableDevicesModel(QObject *parent = nullptr); | ||
enum Role { | ||
Name = Qt::UserRole + 1, | ||
TypeName, | ||
ImagePath, | ||
Index | ||
}; | ||
|
||
// Header: | ||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; | ||
enum DeviceType { | ||
Shell, | ||
}; | ||
|
||
// Basic functionality: | ||
QModelIndex index(int row, int column, | ||
const QModelIndex &parent = QModelIndex()) const override; | ||
QModelIndex parent(const QModelIndex &index) const override; | ||
class DetectedDevice { | ||
public: | ||
DetectedDevice() = default; | ||
QString imagePath, typeName; | ||
QBluetoothDeviceInfo info; | ||
DeviceType type; | ||
QString name() const | ||
{ | ||
return info.name(); | ||
} | ||
}; | ||
|
||
explicit AvailableDevicesModel(QObject *parent = nullptr); | ||
|
||
int rowCount(const QModelIndex &parent = QModelIndex()) const override; | ||
int columnCount(const QModelIndex &parent = QModelIndex()) const override; | ||
|
||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | ||
|
||
Q_INVOKABLE void detectDevices(); | ||
|
||
bool scanInProgress() const; | ||
QHash<int, QByteArray> roleNames() const override; | ||
|
||
Q_INVOKABLE void useDevice(int deviceIndex); | ||
|
||
Q_INVOKABLE AbstractRC_Car *currentDevice() const; | ||
|
||
static QObject *qmlInstance(QQmlEngine *engine, QJSEngine *scriptEngine); | ||
|
||
private slots: | ||
// QBluetoothDeviceDiscoveryAgent related | ||
void deviceDiscovered(const QBluetoothDeviceInfo&); | ||
void deviceScanFinished(); | ||
void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error); | ||
|
||
Q_SIGNALS: | ||
void scanStateChanged(); | ||
|
||
private: | ||
void setScanInProgress(bool scanInProgress); | ||
|
||
QBluetoothDeviceDiscoveryAgent *m_discoveryAgent = nullptr; | ||
QList<DetectedDevice> m_devices; | ||
bool m_scanInProgress = false; | ||
AbstractRC_Car *m_currentDevice = nullptr; | ||
}; | ||
|
||
#endif // AVAILABLEDEVICESMODEL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,41 @@ | ||
import QtQuick 2.0 | ||
import Sailfish.Silica 1.0 | ||
|
||
Item { | ||
ListItem { | ||
id: detectedDevice | ||
property string name : "" | ||
property string typeName : "" | ||
property string image : "" | ||
property int index: -1 | ||
|
||
onClicked: { | ||
AvailableDevicesModel.useDevice(index) | ||
pageStack.push(Qt.resolvedUrl("DrivePage.qml")) | ||
} | ||
|
||
Row { | ||
id: row | ||
Column { | ||
Text { | ||
id: nameText | ||
text: detectedDevice.name | ||
font.pixelSize: Theme.fontSizeLarge | ||
} | ||
|
||
Text { | ||
id: typeNameText | ||
text: detectedDevice.typeName | ||
font.pixelSize: Theme.fontSizeSmall | ||
} | ||
width: (detectedDevice.width - row.height - Theme.paddingLarge) | ||
} | ||
Image { | ||
id: name | ||
source: detectedDevice.image | ||
height: row.height | ||
width: row.height | ||
fillMode: Image.PreserveAspectFit | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.