From d1fcfe58025603ae28e8fda31d96519a92cdb7dd Mon Sep 17 00:00:00 2001 From: Sami Nurmenniemi Date: Tue, 31 Oct 2017 13:46:40 +0200 Subject: Add Wi-Fi enable button to network settings Task-number: QTBUG-64230 Change-Id: I2aad07fb98a45997b116fc9a06924ba8be3dff2a Reviewed-by: Kari Oikarinen Reviewed-by: Teemu Holappa --- src/imports/networksettings/plugin.cpp | 10 ++ src/imports/networksettings/plugins.qmltypes | 20 +++- src/networksettings/qnetworksettingsmanager.cpp | 21 ++++- src/networksettings/qnetworksettingsmanager.h | 4 +- src/settingsui/bluetooth/Bluetooth.qml | 1 + src/settingsui/bluetooth/CustomSwitch.qml | 120 ------------------------ src/settingsui/common/CustomSwitch.qml | 120 ++++++++++++++++++++++++ src/settingsui/network/NetworkSettings.qml | 78 +++++++++++++-- src/settingsui/settingsuiapp.qrc | 2 +- src/settingsui/settingsuiplugin/plugin.qrc | 2 +- 10 files changed, 246 insertions(+), 132 deletions(-) delete mode 100644 src/settingsui/bluetooth/CustomSwitch.qml create mode 100644 src/settingsui/common/CustomSwitch.qml diff --git a/src/imports/networksettings/plugin.cpp b/src/imports/networksettings/plugin.cpp index 665cb6a..431a099 100644 --- a/src/imports/networksettings/plugin.cpp +++ b/src/imports/networksettings/plugin.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -121,6 +122,14 @@ \sa services */ +/*! + \qmlmethod NetworkInterface NetworkSettingsManager::interface(int type, int instance) + + Returns the interface with type \a type and order number of \a instance + + \sa NetworkInterface +*/ + /*! \qmlmethod NetworkSettingsManager::userAgent.setPassphrase(string passphrase) @@ -162,6 +171,7 @@ void NetworksettingspluginPlugin::registerTypes(const char *uri) { Q_ASSERT(QLatin1String(uri) == QLatin1String("QtDeviceUtilities.NetworkSettings")); qmlRegisterUncreatableType(uri, 1, 0, "NetworkService", "Cannot be instantiated directly."); + qmlRegisterUncreatableType(uri, 1, 0, "NetworkInterface", "Cannot be instantiated directly."); qmlRegisterUncreatableType(uri, 1, 0, "NetworkSettingsIPv4", "Cannot be instantiated directly."); qmlRegisterUncreatableType(uri, 1, 0, "NetworkSettingsIPv6", "Cannot be instantiated directly."); qmlRegisterUncreatableType(uri, 1, 0, "NetworkSettingsProxy", "Cannot be instantiated directly."); diff --git a/src/imports/networksettings/plugins.qmltypes b/src/imports/networksettings/plugins.qmltypes index 5022d6f..ac0d3fc 100644 --- a/src/imports/networksettings/plugins.qmltypes +++ b/src/imports/networksettings/plugins.qmltypes @@ -7,7 +7,7 @@ import QtQuick.tooling 1.2 // 'qmlplugindump -nonrelocatable QtDeviceUtilities.NetworkSettings 1.0' Module { - dependencies: [] + dependencies: ["QtQuick 2.8"] Component { name: "QNetworkSettingsIPv4" prototype: "QObject" @@ -55,6 +55,18 @@ Module { Property { name: "privacy"; type: "PrivacyType" } Property { name: "prefixLength"; type: "int" } } + Component { + name: "QNetworkSettingsInterface" + prototype: "QObject" + exports: ["QtDeviceUtilities.NetworkSettings/NetworkInterface 1.0"] + isCreatable: false + exportMetaObjectRevisions: [0] + Property { name: "name"; type: "string"; isReadonly: true } + Property { name: "state"; type: "QNetworkSettingsState::States"; isReadonly: true } + Property { name: "type"; type: "QNetworkSettingsType::Types"; isReadonly: true } + Property { name: "powered"; type: "bool" } + Method { name: "scanServices" } + } Component { name: "QNetworkSettingsManager" prototype: "QObject" @@ -88,6 +100,12 @@ Module { Parameter { name: "name"; type: "string" } Parameter { name: "type"; type: "int" } } + Method { + name: "interface" + type: "QNetworkSettingsInterface*" + Parameter { name: "type"; type: "int" } + Parameter { name: "instance"; type: "int" } + } } Component { name: "QNetworkSettingsProxy" diff --git a/src/networksettings/qnetworksettingsmanager.cpp b/src/networksettings/qnetworksettingsmanager.cpp index caf6057..eb687d1 100644 --- a/src/networksettings/qnetworksettingsmanager.cpp +++ b/src/networksettings/qnetworksettingsmanager.cpp @@ -29,6 +29,7 @@ #include "qnetworksettingsmanager.h" #include "qnetworksettingsservice.h" #include "qnetworksettingsservicemodel.h" +#include "qnetworksettingsinterface.h" #include "qnetworksettingsinterfacemodel.h" #include "qnetworksettingsmanager_p.h" #include @@ -51,7 +52,7 @@ QNetworkSettingsInterfaceModel *QNetworkSettingsManager::interfaces() return &d->m_interfaceModel; } -QNetworkSettingsService* QNetworkSettingsManager::service(const QString& name, const int type) +QNetworkSettingsService* QNetworkSettingsManager::service(const QString& name, int type) { Q_D(QNetworkSettingsManager); @@ -60,7 +61,23 @@ QNetworkSettingsService* QNetworkSettingsManager::service(const QString& name, c return service; } } - return NULL; + return nullptr; +} + +QNetworkSettingsInterface* QNetworkSettingsManager::interface(int type, int instance) +{ + Q_D(QNetworkSettingsManager); + int matchingInstance = 0; + + foreach (QNetworkSettingsInterface* interface, d->m_interfaceModel.getModel()) { + if (interface->type() == type) { + if (matchingInstance == instance) { + return interface; + } + matchingInstance++; + } + } + return nullptr; } void QNetworkSettingsManager::setUserAgent(QNetworkSettingsUserAgent *agent) diff --git a/src/networksettings/qnetworksettingsmanager.h b/src/networksettings/qnetworksettingsmanager.h index 7e7cfad..3ce5095 100644 --- a/src/networksettings/qnetworksettingsmanager.h +++ b/src/networksettings/qnetworksettingsmanager.h @@ -38,6 +38,7 @@ QT_FORWARD_DECLARE_CLASS(QNetworkSettingsService) QT_FORWARD_DECLARE_CLASS(QNetworkSettingsServiceModel) QT_FORWARD_DECLARE_CLASS(QNetworkSettingsUserAgent) QT_FORWARD_DECLARE_CLASS(QNetworkSettingsServiceFilter) +QT_FORWARD_DECLARE_CLASS(QNetworkSettingsInterface) QT_FORWARD_DECLARE_CLASS(QNetworkSettingsInterfaceModel) QT_BEGIN_NAMESPACE @@ -57,7 +58,8 @@ public: void setUserAgent(QNetworkSettingsUserAgent *agent); QNetworkSettingsUserAgent* userAgent(); - Q_INVOKABLE QNetworkSettingsService* service(const QString& name, const int type); + Q_INVOKABLE QNetworkSettingsService* service(const QString& name, int type); + Q_INVOKABLE QNetworkSettingsInterface* interface(int type, int instance); Q_SIGNALS: void servicesChanged(); diff --git a/src/settingsui/bluetooth/Bluetooth.qml b/src/settingsui/bluetooth/Bluetooth.qml index 13572a8..e89476f 100644 --- a/src/settingsui/bluetooth/Bluetooth.qml +++ b/src/settingsui/bluetooth/Bluetooth.qml @@ -30,6 +30,7 @@ import QtQuick 2.6 import QtQuick.Layouts 1.3 import QtQuick.Controls 2.0 import QtDeviceUtilities.BluetoothSettings 1.0 +import "../common" Item { id: root diff --git a/src/settingsui/bluetooth/CustomSwitch.qml b/src/settingsui/bluetooth/CustomSwitch.qml deleted file mode 100644 index fa9f9aa..0000000 --- a/src/settingsui/bluetooth/CustomSwitch.qml +++ /dev/null @@ -1,120 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Device Utilities module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -import QtQuick 2.0 -import QtDeviceUtilities.QtButtonImageProvider 1.0 -import QtQuick.Controls 2.1 - -Switch { - id: control - - property alias indicatorWidth: indicatorImg.width - property alias indicatorHeight: indicatorImg.height - - indicator: Image { - id: indicatorImg - width: 200 - height: 75 - sourceSize: Qt.size(width, height) - anchors.horizontalCenter: control.horizontalCenter - y: parent.height / 2 - height / 2 - source: "image://QtButton/10/#848895/transparent" - Text { - id: offText - anchors.left: parent.left - anchors.leftMargin: parent.width * 0.075 - anchors.verticalCenter: parent.verticalCenter - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - fontSizeMode: Text.Fit - minimumPixelSize: 1 - font.pixelSize: parent.height * 0.55 - color: "#3b4155" - text: "OFF" - font.family: appFont - } - Text { - id: onText - anchors.right: parent.right - anchors.rightMargin: parent.width * 0.1 - anchors.verticalCenter: parent.verticalCenter - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - fontSizeMode: Text.Fit - minimumPixelSize: 1 - font.pixelSize: parent.height * 0.55 - color: "#3b4155" - text: "ON" - font.family: appFont - } - - Binding { - target: qtHandle - property: "x" - value: control.checked ? indicatorImg.width - qtHandle.width - indicatorImg.width * 0.025 : indicatorImg.width * 0.025 - when: !mousearea.drag.active - } - - MouseArea { - anchors.fill: parent - onClicked: control.checked = !control.checked - } - - QtButton { - id: qtHandle - anchors.verticalCenter: parent.verticalCenter - width: parent.width * 0.475 - height: parent.height * 0.9 - fillColor: control.checked ? viewSettings.buttonGreenColor : viewSettings.buttonGrayColor - text: control.checked ? "ON" : "OFF" - borderColor: "transparent" - Behavior on x { - NumberAnimation { duration: 50 } - } - - MouseArea { - id: mousearea - anchors.fill: parent - drag.target: qtHandle - drag.axis: Drag.XAxis - drag.minimumX: indicatorImg.width * 0.005 - drag.maximumX: indicatorImg.width - width - indicatorImg.width * 0.005 - - onClicked: control.checked = !control.checked - - onReleased: { - if (qtHandle.x > indicatorImg.width / 5 ) { - control.checked = true - } else { - control.checked = false - } - } - } - } - } -} diff --git a/src/settingsui/common/CustomSwitch.qml b/src/settingsui/common/CustomSwitch.qml new file mode 100644 index 0000000..fa9f9aa --- /dev/null +++ b/src/settingsui/common/CustomSwitch.qml @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2017 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQuick 2.0 +import QtDeviceUtilities.QtButtonImageProvider 1.0 +import QtQuick.Controls 2.1 + +Switch { + id: control + + property alias indicatorWidth: indicatorImg.width + property alias indicatorHeight: indicatorImg.height + + indicator: Image { + id: indicatorImg + width: 200 + height: 75 + sourceSize: Qt.size(width, height) + anchors.horizontalCenter: control.horizontalCenter + y: parent.height / 2 - height / 2 + source: "image://QtButton/10/#848895/transparent" + Text { + id: offText + anchors.left: parent.left + anchors.leftMargin: parent.width * 0.075 + anchors.verticalCenter: parent.verticalCenter + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + fontSizeMode: Text.Fit + minimumPixelSize: 1 + font.pixelSize: parent.height * 0.55 + color: "#3b4155" + text: "OFF" + font.family: appFont + } + Text { + id: onText + anchors.right: parent.right + anchors.rightMargin: parent.width * 0.1 + anchors.verticalCenter: parent.verticalCenter + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + fontSizeMode: Text.Fit + minimumPixelSize: 1 + font.pixelSize: parent.height * 0.55 + color: "#3b4155" + text: "ON" + font.family: appFont + } + + Binding { + target: qtHandle + property: "x" + value: control.checked ? indicatorImg.width - qtHandle.width - indicatorImg.width * 0.025 : indicatorImg.width * 0.025 + when: !mousearea.drag.active + } + + MouseArea { + anchors.fill: parent + onClicked: control.checked = !control.checked + } + + QtButton { + id: qtHandle + anchors.verticalCenter: parent.verticalCenter + width: parent.width * 0.475 + height: parent.height * 0.9 + fillColor: control.checked ? viewSettings.buttonGreenColor : viewSettings.buttonGrayColor + text: control.checked ? "ON" : "OFF" + borderColor: "transparent" + Behavior on x { + NumberAnimation { duration: 50 } + } + + MouseArea { + id: mousearea + anchors.fill: parent + drag.target: qtHandle + drag.axis: Drag.XAxis + drag.minimumX: indicatorImg.width * 0.005 + drag.maximumX: indicatorImg.width - width - indicatorImg.width * 0.005 + + onClicked: control.checked = !control.checked + + onReleased: { + if (qtHandle.x > indicatorImg.width / 5 ) { + control.checked = true + } else { + control.checked = false + } + } + } + } + } +} diff --git a/src/settingsui/network/NetworkSettings.qml b/src/settingsui/network/NetworkSettings.qml index b3a6350..719ce77 100644 --- a/src/settingsui/network/NetworkSettings.qml +++ b/src/settingsui/network/NetworkSettings.qml @@ -30,13 +30,79 @@ import QtQuick 2.6 import QtQuick.Layouts 1.3 import QtQuick.Controls 2.2 import QtDeviceUtilities.NetworkSettings 1.0 +import QtDeviceUtilities.BluetoothSettings 1.0 +import "../common" Item { - NetworkListView { - id: networkList - anchors.top: parent.top - anchors.left: parent.left - anchors.right: parent.right - anchors.bottom: parent.bottom + id: root + property string title: qsTr("Network Settings") + + Column { + id: networkSettingsColumn + spacing: pluginMain.spacing + anchors.margins: viewSettings.pageMargin + + Row { + spacing: root.width * 0.025 + leftPadding: pluginMain.margin + Text { + text: qsTr("Wi-Fi") + anchors.verticalCenter: parent.verticalCenter + font.pixelSize: pluginMain.subTitleFontSize + font.family: appFont + color: "white" + } + + CustomSwitch { + id: wifiSwitch + indicatorWidth: root.width * 0.15 + indicatorHeight: root.height * 0.06 + property bool wiFiAvailable: NetworkSettingsManager.interface(NetworkSettingsType.Wifi, 0) !== null + enabled: wiFiAvailable && !wifiSwitchTimer.running + onCheckedChanged: { + // Power on/off all WiFi interfaces + for (var i = 0; NetworkSettingsManager.interface(NetworkSettingsType.Wifi, i) !== null; i++) { + NetworkSettingsManager.interface(NetworkSettingsType.Wifi, i).powered = checked + wifiSwitchTimer.start() + } + } + Component.onCompleted: { + // If any of the WiFi interfaces is powered on, switch is checked + var checkedStatus = false; + for (var i = 0; NetworkSettingsManager.interface(NetworkSettingsType.Wifi, i) !== null; i++) { + if (NetworkSettingsManager.interface(NetworkSettingsType.Wifi, i).powered) { + checkedStatus = true; + break; + } + } + checked = checkedStatus; + } + + // At least 1s between switching on/off + Timer { + id: wifiSwitchTimer + interval: 1000 + running: false + } + } + } + + Text { + text: qsTr("Network list:") + font.pixelSize: pluginMain.subTitleFontSize + font.family: appFont + font.styleName: "SemiBold" + color: "white" + } + + Row { + id: listViewRow + leftPadding: pluginMain.margin + NetworkListView { + id: networkList + width: root.width - listViewRow.leftPadding + height: root.height - listViewRow.y - networkSettingsColumn.anchors.margins + } + } } } diff --git a/src/settingsui/settingsuiapp.qrc b/src/settingsui/settingsuiapp.qrc index 618d761..a3e7ca5 100644 --- a/src/settingsui/settingsuiapp.qrc +++ b/src/settingsui/settingsuiapp.qrc @@ -6,7 +6,7 @@ power/Power.qml timedate/ManualTime.qml timedate/CustomComboBox.qml - bluetooth/CustomSwitch.qml + common/CustomSwitch.qml network/PassphraseEnter.qml locale/TableKey.qml locale/TableValue.qml diff --git a/src/settingsui/settingsuiplugin/plugin.qrc b/src/settingsui/settingsuiplugin/plugin.qrc index 733c164..e6d02ea 100644 --- a/src/settingsui/settingsuiplugin/plugin.qrc +++ b/src/settingsui/settingsuiplugin/plugin.qrc @@ -27,7 +27,7 @@ ../power/Power.qml ../timedate/ManualTime.qml ../timedate/CustomComboBox.qml - ../bluetooth/CustomSwitch.qml + ../common/CustomSwitch.qml ../network/NetworkListView.qml ../network/PassphraseEnter.qml ../locale/TableKey.qml -- cgit v1.2.3