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"
22 class StatusBarModel::Private
25 Private(StatusBarModel *parent);
30 StatusBarServer server;
31 QString iconList[StatusBarServer::SupportedCount];
36 StatusBarModel::Private::Private(StatusBarModel *parent)
39 connect(&server, &StatusBarServer::statusIconChanged, [&](int placeholderIndex, const QString &icon) {
40 if (placeholderIndex < 0 || StatusBarServer::SupportedCount <= placeholderIndex) return;
41 if (iconList[placeholderIndex] == icon) return;
42 iconList[placeholderIndex] = icon;
43 emit q->dataChanged(q->index(placeholderIndex), q->index(placeholderIndex));
45 for (int i = 0; i < StatusBarServer::SupportedCount; i++) {
46 iconList[i] = server.getStatusIcon(i);
50 StatusBarModel::StatusBarModel(QObject *parent)
51 : QAbstractListModel(parent)
52 , d(new Private(this))
56 StatusBarModel::~StatusBarModel()
61 void StatusBarModel::init(QUrl &url, QQmlContext *context)
63 d->network = new Network(url, context);
64 context->setContextProperty("network", d->network);
65 d->wifi_a = static_cast<WifiAdapter*>(d->network->findAdapter("wifi"));
66 Q_CHECK_PTR(d->wifi_a);
68 QObject::connect(d->wifi_a, &WifiAdapter::wifiConnectedChanged,
69 this, &StatusBarModel::onWifiConnectedChanged);
70 QObject::connect(d->wifi_a, &WifiAdapter::wifiEnabledChanged,
71 this, &StatusBarModel::onWifiEnabledChanged);
72 QObject::connect(d->wifi_a, &WifiAdapter::wifiStrengthChanged,
73 this, &StatusBarModel::onWifiStrengthChanged);
75 setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
78 void StatusBarModel::setWifiStatus(bool connected, bool enabled, int strength)
80 if (enabled && connected)
82 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_1Bar-01.png"));
83 else if (strength < 50)
84 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_2Bars-01.png"));
85 else if (strength < 70)
86 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_3Bars-01.png"));
88 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_Full-01.png"));
90 d->server.setStatusIcon(0, QStringLiteral("qrc:/images/Status/HMI_Status_Wifi_NoBars-01.png"));
93 void StatusBarModel::onWifiConnectedChanged(bool connected)
95 setWifiStatus(connected, d->wifi_a->wifiEnabled(), d->wifi_a->wifiStrength());
98 void StatusBarModel::onWifiEnabledChanged(bool enabled)
100 setWifiStatus(d->wifi_a->wifiConnected(), enabled, d->wifi_a->wifiStrength());
103 void StatusBarModel::onWifiStrengthChanged(int strength)
105 qInfo() << "Strength changed: " << strength;
106 setWifiStatus(d->wifi_a->wifiConnected(), d->wifi_a->wifiEnabled(), strength);
109 int StatusBarModel::rowCount(const QModelIndex &parent) const
111 if (parent.isValid())
114 // Delete bluetooth because use agl-service-bluetooth.
115 return StatusBarServer::SupportedCount - 1;
118 QVariant StatusBarModel::data(const QModelIndex &index, int role) const
121 if (!index.isValid())
125 case Qt::DisplayRole:
126 if (index.row() == 0){
127 ret = d->iconList[StatusBarServer::StatusWifi];
128 }else if (index.row() == 1){
129 ret = d->iconList[StatusBarServer::StatusCellular];
139 QHash<int, QByteArray> StatusBarModel::roleNames() const
141 QHash<int, QByteArray> roles;
142 roles[Qt::DisplayRole] = "icon";