summaryrefslogtreecommitdiff
path: root/examples/vehiclefunctions/climate_widget/mainwindow.cpp
blob: a96fcf833ec3542329799658a9e89e157050a57e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/****************************************************************************
**
** Copyright (C) 2015 Pelagicore AG
** Contact: http://www.qt.io/ or http://www.pelagicore.com/
**
** This file is part of the QtIVI module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3-PELAGICORE$
** Commercial License Usage
** Licensees holding valid commercial Qt IVI 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 Pelagicore. For licensing terms
** and conditions, contact us at http://www.pelagicore.com.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>
#include <QSpinBox>
#include <QButtonGroup>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_radioButtonGroup(new QButtonGroup(this)),
    m_climateControl(0)
{
    ui->setupUi(this);

    m_radioButtonGroup->addButton(ui->rb_BiLevel);
    m_radioButtonGroup->addButton(ui->rb_DefrostFloor);
    m_radioButtonGroup->addButton(ui->rb_FloorDuct);
    m_radioButtonGroup->addButton(ui->rb_FloorPanel);

//![1]
    m_climateControl = new QtIVIClimateControl(QString(), this);
    m_climateControl->startAutoDiscovery();

    if (!m_climateControl->isValid())
        QMessageBox::critical(this, tr("Auto Discovery Failed !"), tr("No Climate Backend available"));
//![1]

//![2]
    //Air Flow Direction
    setupFlowDirectionRadioButtons(m_climateControl->airflowDirection());
    setupFlowDirectionAttribute(m_climateControl->airflowDirectionAttribute());
    connect(m_radioButtonGroup, static_cast<void (QButtonGroup::*)(QAbstractButton *, bool)>(&QButtonGroup::buttonToggled),
            this, &MainWindow::onFlowDirectionButtonToggled);
    connect(m_climateControl, &QtIVIClimateControl::airflowDirectionChanged,
            this, &MainWindow::setupFlowDirectionRadioButtons);
    connect(m_climateControl, &QtIVIClimateControl::airflowDirectionAttributeChanged,
            this, &MainWindow::setupFlowDirectionAttribute);

    //Air Condition
    ui->cb_airCondition->setChecked(m_climateControl->isAirConditioningEnabled());
    ui->cb_airCondition->setEnabled(m_climateControl->airConditioningAttribute().isAvailable());
    connect(m_climateControl, &QtIVIClimateControl::airConditioningEnabledChanged,
            ui->cb_airCondition, &QCheckBox::setChecked);
    connect(m_climateControl, &QtIVIClimateControl::airConditioningAttributeChanged,
            this, &MainWindow::onAirConditioningAttributeChanged);
    connect(ui->cb_airCondition, &QCheckBox::clicked,
            m_climateControl, &QtIVIClimateControl::setAirConditioningEnabled);

    //Air Recirculation
    ui->cb_airRecirculation->setChecked(m_climateControl->isAirRecirculationEnabled());
    ui->cb_airRecirculation->setEnabled(m_climateControl->airRecirculationAttribute().isAvailable());
    connect(m_climateControl, &QtIVIClimateControl::airRecirculationEnabledChanged,
            ui->cb_airRecirculation, &QCheckBox::setChecked);
    connect(m_climateControl, &QtIVIClimateControl::airRecirculationAttributeChanged,
            this, &MainWindow::onAirRecirculationAttributeChanged);
    connect(ui->cb_airRecirculation, &QCheckBox::clicked,
            m_climateControl, &QtIVIClimateControl::setAirRecirculationEnabled);

    //Heater
    ui->cb_heater->setChecked(m_climateControl->isHeaterEnabled());
    ui->cb_heater->setEnabled(m_climateControl->heaterAttribute().isAvailable());
    connect(m_climateControl, &QtIVIClimateControl::heaterEnabledChanged,
            ui->cb_heater, &QCheckBox::setChecked);
    connect(m_climateControl, &QtIVIClimateControl::heaterAttributeChanged,
            this, &MainWindow::onHeaterAttributeChanged);
    connect(ui->cb_heater, &QCheckBox::clicked,
            m_climateControl, &QtIVIClimateControl::setHeaterEnabled);
}
//![2]

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onAirRecirculationAttributeChanged(const QtIVIPropertyAttribute<bool> & attribute)
{
    ui->cb_airRecirculation->setEnabled(attribute.isAvailable());
}

void MainWindow::onHeaterAttributeChanged(const QtIVIPropertyAttribute<bool> & attribute)
{
    ui->cb_heater->setEnabled(attribute.isAvailable());
}

void MainWindow::onAirConditioningAttributeChanged(const QtIVIPropertyAttribute<bool> & attribute)
{
    ui->cb_airCondition->setEnabled(attribute.isAvailable());
}

//![3]
void MainWindow::setupFlowDirectionRadioButtons(QtIVIClimateControl::AirflowDirection direction)
{
    QAbstractButton* button = ui->rb_BiLevel;

    if (direction == QtIVIClimateControl::BiLevel)
        button = ui->rb_BiLevel;
    else if (direction == QtIVIClimateControl::DefrostFloor)
        button = ui->rb_DefrostFloor;
    else if (direction == QtIVIClimateControl::FloorDuct)
        button = ui->rb_FloorDuct;
    else if (direction == QtIVIClimateControl::FloorPanel)
        button = ui->rb_FloorPanel;

    button->setChecked(true);
}

void MainWindow::setupFlowDirectionAttribute(const QtIVIPropertyAttribute<QtIVIClimateControl::AirflowDirection> & attribute)
{
    ui->rb_BiLevel->setEnabled(attribute.availableValues().contains(QtIVIClimateControl::BiLevel));
    ui->rb_DefrostFloor->setEnabled(attribute.availableValues().contains(QtIVIClimateControl::DefrostFloor));
    ui->rb_FloorDuct->setEnabled(attribute.availableValues().contains(QtIVIClimateControl::FloorDuct));
    ui->rb_FloorPanel->setEnabled(attribute.availableValues().contains(QtIVIClimateControl::FloorPanel));
}

void MainWindow::onFlowDirectionButtonToggled(QAbstractButton *button, bool checked)
{
    if (!checked)
        return;

    QtIVIClimateControl::AirflowDirection direction = QtIVIClimateControl::BiLevel;

    if (button == ui->rb_BiLevel)
        direction = QtIVIClimateControl::BiLevel;
    else if (button == ui->rb_DefrostFloor)
        direction = QtIVIClimateControl::DefrostFloor;
    else if (button == ui->rb_FloorDuct)
        direction = QtIVIClimateControl::FloorDuct;
    else if (button == ui->rb_FloorPanel)
        direction = QtIVIClimateControl::FloorPanel;

    m_climateControl->setAirflowDirection(direction);
}
//![3]