From 9c9281bb05f785451d276f0d3145029ab6fb8a2f Mon Sep 17 00:00:00 2001 From: Sami Nurmenniemi Date: Mon, 13 Nov 2017 09:28:21 +0200 Subject: Fix bluetooth settings - Hide "Scanning" text if bluetooth is powered off during scanning - Don't create duplicate entries of devices when scanning again - List devices in alphabetically sorted order - Don't initiate new scan if it's already going on Task-number: QTBUG-60084 Change-Id: I009d66694e3bcfe7419a5f5e01bfd253d7aa4cc8 Reviewed-by: Kari Oikarinen Reviewed-by: Teemu Holappa --- src/bluetoothsettings/bluez/bluetoothdevice_p.cpp | 8 ++++--- src/bluetoothsettings/discoverymodel.cpp | 27 +++++++++++++++++++---- src/bluetoothsettings/discoverymodel.h | 2 ++ src/settingsui/bluetooth/Bluetooth.qml | 2 +- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/bluetoothsettings/bluez/bluetoothdevice_p.cpp b/src/bluetoothsettings/bluez/bluetoothdevice_p.cpp index 4440277..48e6695 100644 --- a/src/bluetoothsettings/bluez/bluetoothdevice_p.cpp +++ b/src/bluetoothsettings/bluez/bluetoothdevice_p.cpp @@ -227,9 +227,11 @@ void BluetoothDevicePrivate::getManagedObjectsFinished(QDBusPendingCallWatcher * if (m_powered) { emit q->poweredChanged(); - m_deviceModel->scanDevices(); - m_scanning = true; - emit q->scanningChanged(); + if (!m_scanning) { + m_deviceModel->scanDevices(); + m_scanning = true; + emit q->scanningChanged(); + } } emit q->availabilityChanged(); diff --git a/src/bluetoothsettings/discoverymodel.cpp b/src/bluetoothsettings/discoverymodel.cpp index 3cae1df..ad174c7 100644 --- a/src/bluetoothsettings/discoverymodel.cpp +++ b/src/bluetoothsettings/discoverymodel.cpp @@ -161,10 +161,19 @@ DiscoveryModel::DiscoveryModel(QObject *parent) void DiscoveryModel::deviceDiscovered(const QBluetoothDeviceInfo &device) { - beginInsertRows(QModelIndex(), m_items.count(), m_items.count()); - BtDeviceItem *item = new BtDeviceItem(device); - m_items.append(item); - endInsertRows(); + int index; + + // Insert the device to alphabetically sorted location + for (index = 0; index < m_items.count(); index++) + { + if (device.name() < m_items.at(index)->name()) + break; + } + + beginInsertRows(QModelIndex(), index, index); + BtDeviceItem *item = new BtDeviceItem(device); + m_items.insert(index, item); + endInsertRows(); } DiscoveryModel::~DiscoveryModel() @@ -183,6 +192,9 @@ void DiscoveryModel::scanDevices() this, SIGNAL(scanFinished())); } + // Reset previous list, devices are already cached by bluez + clearDeviceList(); + m_discoveryAgent->start(); } @@ -245,3 +257,10 @@ void DiscoveryModel::setConnected(const QString &aAddress, bool connected) if (found) emit dataChanged(index(i, 0), index(i, 0), role); } + +void DiscoveryModel::clearDeviceList() +{ + beginResetModel(); + m_items.clear(); + endResetModel(); +} diff --git a/src/bluetoothsettings/discoverymodel.h b/src/bluetoothsettings/discoverymodel.h index ba6e1dd..56f8299 100644 --- a/src/bluetoothsettings/discoverymodel.h +++ b/src/bluetoothsettings/discoverymodel.h @@ -115,6 +115,8 @@ Q_SIGNALS: private Q_SLOTS: void deviceDiscovered(const QBluetoothDeviceInfo &device); private: + void clearDeviceList(); + QList m_items; QHash m_roleNames; QBluetoothDeviceDiscoveryAgent *m_discoveryAgent; diff --git a/src/settingsui/bluetooth/Bluetooth.qml b/src/settingsui/bluetooth/Bluetooth.qml index e89476f..4c82904 100644 --- a/src/settingsui/bluetooth/Bluetooth.qml +++ b/src/settingsui/bluetooth/Bluetooth.qml @@ -68,7 +68,7 @@ Item { color: "white" text: "Scanning" font.family: appFont - opacity: BtDevice.scanning ? 1.0 : 0.0 + opacity: (BtDevice.scanning && BtDevice.powered) ? 1.0 : 0.0 visible: opacity > 0 Behavior on opacity { NumberAnimation {duration: 150} -- cgit v1.2.3