2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2017, 2018 TOYOTA MOTOR CORPORATION
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "statusbarmodel.h"
19 #include "statusbarserver.h"
21 #include <wifiadapter.h>
23 class StatusBarModel::Private
26 Private(StatusBarModel *parent);
31 StatusBarServer server;
32 QString iconList[StatusBarServer::SupportedCount];
37 StatusBarModel::Private::Private(StatusBarModel *parent)
40 connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
41 if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
42 if (iconList[placeholderIndex] == icon) return;
43 iconList[placeholderIndex] = icon;
44 emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
46 for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
47 iconList[i] = server.getStatusIcon(i);
51 StatusBarModel::StatusBarModel(QObject *parent)
52 : QAbstractListModel(parent)
53 , d(new Private(this))
57 StatusBarModel::~StatusBarModel()
62 void StatusBarModel::init(QQmlContext *context)
64 d->network = new Network(false, context);
65 context->setContextProperty("network", d->network);
66 d->wifi_a = static_cast<WifiAdapter*>(d->network->findAdapter("wifi"));
67 Q_CHECK_PTR(d->wifi_a);
69 QObject::connect(d->wifi_a, &WifiAdapter::wifiConnectedChanged,
70 this, &StatusBarModel::onWifiConnectedChanged);
71 QObject::connect(d->wifi_a, &WifiAdapter::wifiEnabledChanged,
72 this, &StatusBarModel::onWifiEnabledChanged);
73 QObject::connect(d->wifi_a, &WifiAdapter::wifiStrengthChanged,
74 this, &StatusBarModel::onWifiStrengthChanged);
76 setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
79 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
81 if (enabled && connected)
83 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
84 else if (strength < 50)
85 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
86 else if (strength < 70)
87 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
89 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
91 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
94 void StatusBarModel::onWifiConnectedChanged(bool connected)
96 setWifiStatus(connected, d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
99 void StatusBarModel::onWifiEnabledChanged(bool enabled)
101 setWifiStatus(d->wifi_a->wifiConnected(), enabled, d->wifi_a->wifiStrength());
104 void StatusBarModel::onWifiStrengthChanged(int strength)
106 qInfo() << "Strength changed: " << strength;
107 setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), strength);
110 int StatusBarModel::rowCount(const QModelIndex &parent) const
112 if (parent.isValid())
115 // Delete bluetooth because use agl-service-bluetooth.
116 return StatusBarServer::SupportedCount - 1;
119 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
122 if (!index.isValid())
126 case Qt::DisplayRole:
127 if (index.row() == 0){
128 ret = d->iconList[StatusBarServer::StatusWifi];
129 }else if (index.row() == 1){
130 ret = d->iconList[StatusBarServer::StatusCellular];
140 QHash<int, QByteArray> StatusBarModel::roleNames() const
142 QHash<int, QByteArray> roles;
143 roles[Qt::DisplayRole] = "icon";