diff options
author | Claudiu Olteanu <olteanu.claudiu@ymail.com> | 2015-07-18 17:50:18 +0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-07-20 05:39:43 -0700 |
commit | b5ac3a3fa8168bd6bf38ecb1985f37c7d683e411 (patch) | |
tree | 1575364d8bc66f25e7df8557dc80c14576a1058b | |
parent | 000c202d7d41b0240d2483911af00ccb5bc30717 (diff) | |
download | subsurface-b5ac3a3fa8168bd6bf38ecb1985f37c7d683e411.tar.gz |
Add combobox for Bluetooth local device selection
Add a combobox which can be used to list/select the available
local Bluetooth devices. In this way, if a user has more
than one local Bluetooth devices (integrated, dongles, etc.)
he can choose which one he wants to use.
Before this patch, only the default local Bluetooth device
could be used.
Signed-off-by: Claudiu Olteanu <olteanu.claudiu@ymail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | qt-ui/btdeviceselectiondialog.cpp | 79 | ||||
-rw-r--r-- | qt-ui/btdeviceselectiondialog.h | 3 | ||||
-rw-r--r-- | qt-ui/btdeviceselectiondialog.ui | 22 |
3 files changed, 81 insertions, 23 deletions
diff --git a/qt-ui/btdeviceselectiondialog.cpp b/qt-ui/btdeviceselectiondialog.cpp index 1ea42d7d8..dfcd64e26 100644 --- a/qt-ui/btdeviceselectiondialog.cpp +++ b/qt-ui/btdeviceselectiondialog.cpp @@ -32,15 +32,27 @@ BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) : connect(ui->discoveredDevicesList, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked(QListWidgetItem*))); - // Set UI information about the local device - ui->deviceAddress->setText(localDevice->address().toString()); - ui->deviceName->setText(localDevice->name()); + // Populate the list with local bluetooth devices + QList<QBluetoothHostInfo> localAvailableDevices = localDevice->allDevices(); + int defaultDeviceIndex = -1; + int availableDevicesSize = localAvailableDevices.size(); + + for (int it = 0; it < availableDevicesSize; it++) { + QBluetoothHostInfo localAvailableDevice = localAvailableDevices.at(it); + ui->localSelectedDevice->addItem(localAvailableDevice.name(), + QVariant::fromValue(localAvailableDevice.address())); + + if (localDevice->address() == localAvailableDevice.address()) + defaultDeviceIndex = it; + } - connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), - this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); + // Positionate the current index to the default device and register to index changes events + ui->localSelectedDevice->setCurrentIndex(defaultDeviceIndex); + connect(ui->localSelectedDevice, SIGNAL(currentIndexChanged(int)), + this, SLOT(localDeviceChanged(int))); - // Initialize the state of the local device and activate/deactive the scan button - hostModeStateChanged(localDevice->hostMode()); + // Update the UI information about the local device + updateLocalDeviceInformation(); // Intialize the discovery agent remoteDeviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(); @@ -49,16 +61,6 @@ BtDeviceSelectionDialog::BtDeviceSelectionDialog(QWidget *parent) : this, SLOT(addRemoteDevice(QBluetoothDeviceInfo))); connect(remoteDeviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(remoteDeviceScanFinished())); - - // Add context menu for devices to be able to pair them - ui->discoveredDevicesList->setContextMenuPolicy(Qt::CustomContextMenu); - connect(ui->discoveredDevicesList, SIGNAL(customContextMenuRequested(QPoint)), - this, SLOT(displayPairingMenu(QPoint))); - connect(localDevice, SIGNAL(pairingFinished(QBluetoothAddress, QBluetoothLocalDevice::Pairing)), - this, SLOT(pairingFinished(QBluetoothAddress, QBluetoothLocalDevice::Pairing))); - - connect(localDevice, SIGNAL(error(QBluetoothLocalDevice::Error)), - this, SLOT(error(QBluetoothLocalDevice::Error))); } BtDeviceSelectionDialog::~BtDeviceSelectionDialog() @@ -174,6 +176,26 @@ void BtDeviceSelectionDialog::itemClicked(QListWidgetItem *item) } } +void BtDeviceSelectionDialog::localDeviceChanged(int index) +{ + QBluetoothAddress localDeviceSelectedAddress = ui->localSelectedDevice->itemData(index, Qt::UserRole).value<QBluetoothAddress>(); + + // Delete the old localDevice + if (localDevice) + delete localDevice; + + // Create a new local device using the selected address + localDevice = new QBluetoothLocalDevice(localDeviceSelectedAddress); + + // Clear the discovered devices list + on_clear_clicked(); + + // Update the UI information about the local device + updateLocalDeviceInformation(); + + ui->dialogStatus->setText(QString("The local device was changed.")); +} + void BtDeviceSelectionDialog::displayPairingMenu(const QPoint &pos) { QMenu menu(this); @@ -271,3 +293,26 @@ QString BtDeviceSelectionDialog::getSelectedDeviceName() return QString(); } + +void BtDeviceSelectionDialog::updateLocalDeviceInformation() +{ + // Set UI information about the local device + ui->deviceAddress->setText(localDevice->address().toString()); + ui->deviceName->setText(localDevice->name()); + + connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)), + this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode))); + + // Initialize the state of the local device and activate/deactive the scan button + hostModeStateChanged(localDevice->hostMode()); + + // Add context menu for devices to be able to pair them + ui->discoveredDevicesList->setContextMenuPolicy(Qt::CustomContextMenu); + connect(ui->discoveredDevicesList, SIGNAL(customContextMenuRequested(QPoint)), + this, SLOT(displayPairingMenu(QPoint))); + connect(localDevice, SIGNAL(pairingFinished(QBluetoothAddress, QBluetoothLocalDevice::Pairing)), + this, SLOT(pairingFinished(QBluetoothAddress, QBluetoothLocalDevice::Pairing))); + + connect(localDevice, SIGNAL(error(QBluetoothLocalDevice::Error)), + this, SLOT(error(QBluetoothLocalDevice::Error))); +} diff --git a/qt-ui/btdeviceselectiondialog.h b/qt-ui/btdeviceselectiondialog.h index e6086cb82..de64c937d 100644 --- a/qt-ui/btdeviceselectiondialog.h +++ b/qt-ui/btdeviceselectiondialog.h @@ -37,12 +37,15 @@ private slots: void displayPairingMenu(const QPoint &pos); void pairingFinished(const QBluetoothAddress &address,QBluetoothLocalDevice::Pairing pairing); void error(QBluetoothLocalDevice::Error error); + void localDeviceChanged(int); private: Ui::BtDeviceSelectionDialog *ui; QBluetoothLocalDevice *localDevice; QBluetoothDeviceDiscoveryAgent *remoteDeviceDiscoveryAgent; QSharedPointer<QBluetoothDeviceInfo> selectedRemoteDeviceInfo; + + void updateLocalDeviceInformation(); }; #endif // BTDEVICESELECTIONDIALOG_H diff --git a/qt-ui/btdeviceselectiondialog.ui b/qt-ui/btdeviceselectiondialog.ui index c28bdcbe8..6f81c9a21 100644 --- a/qt-ui/btdeviceselectiondialog.ui +++ b/qt-ui/btdeviceselectiondialog.ui @@ -122,35 +122,35 @@ <bool>false</bool> </property> <layout class="QFormLayout" name="formLayout_2"> - <item row="0" column="0"> + <item row="2" column="0"> <widget class="QLabel" name="deviceNameLable"> <property name="text"> <string>Name: </string> </property> </widget> </item> - <item row="0" column="1"> + <item row="2" column="1"> <widget class="QLineEdit" name="deviceName"> <property name="readOnly"> <bool>true</bool> </property> </widget> </item> - <item row="1" column="0"> + <item row="3" column="0"> <widget class="QLabel" name="deviceAddressLable"> <property name="text"> <string>Address:</string> </property> </widget> </item> - <item row="1" column="1"> + <item row="3" column="1"> <widget class="QLineEdit" name="deviceAddress"> <property name="readOnly"> <bool>true</bool> </property> </widget> </item> - <item row="2" column="1"> + <item row="4" column="1"> <widget class="QCheckBox" name="deviceState"> <property name="enabled"> <bool>false</bool> @@ -175,7 +175,7 @@ </property> </widget> </item> - <item row="3" column="1"> + <item row="5" column="1"> <widget class="QPushButton" name="changeDeviceState"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Minimum"> @@ -194,6 +194,16 @@ </property> </widget> </item> + <item row="1" column="1"> + <widget class="QComboBox" name="localSelectedDevice"/> + </item> + <item row="1" column="0"> + <widget class="QLabel" name="selectDeviceLable"> + <property name="text"> + <string>Select device:</string> + </property> + </widget> + </item> </layout> </widget> </item> |