CFLinker.cpp 8 KB
Newer Older
1
//    Copyright (C) 2017, ETH Zurich, D-ITET, Angel Romero
roangel's avatar
roangel committed
2
//
3
4
5
//    This file is part of D-FaLL-System.
//    
//    D-FaLL-System is free software: you can redistribute it and/or modify
roangel's avatar
roangel committed
6
7
8
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
9
10
//    
//    D-FaLL-System is distributed in the hope that it will be useful,
roangel's avatar
roangel committed
11
12
13
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
14
//    
roangel's avatar
roangel committed
15
//    You should have received a copy of the GNU General Public License
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//    along with D-FaLL-System.  If not, see <http://www.gnu.org/licenses/>.
//    
//
//    ----------------------------------------------------------------------------------
//    DDDD        FFFFF        L     L           SSSS  Y   Y   SSSS  TTTTT  EEEEE  M   M
//    D   D       F      aaa   L     L          S       Y Y   S        T    E      MM MM
//    D   D  ---  FFFF  a   a  L     L     ---   SSS     Y     SSS     T    EEE    M M M
//    D   D       F     a  aa  L     L              S    Y        S    T    E      M   M
//    DDDD        F      aa a  LLLL  LLLL       SSSS     Y    SSSS     T    EEEEE  M   M
//
//
//    DESCRIPTION:
//    Links the crazyflies with the student ids and with the crazyflie zones
//
//    ----------------------------------------------------------------------------------

roangel's avatar
roangel committed
32

33
34
#include "CFLinker.h"

35
#include <QHeaderView>
36

37
CFLinker::CFLinker(Ui::MainGUIWindow* ui, std::vector<crazyFly*> *crazyflies_vector, std::vector<crazyFlyZone*> *crazyfly_zones)
38
    : QObject(0)
39
{
40
    m_ui = ui;
41
42
    m_crazyflies_vector = crazyflies_vector;
    m_crazyfly_zones = crazyfly_zones;
43
44
45
46
47
48
}

CFLinker::~CFLinker()
{
}

49
int CFLinker::getCFZoneIndexFromName(QString name)
50
{
51
52
    return name.split(" ")[1].toInt() - 1;
}
53

54
55
56
57
58
59
60
61
62
63
int CFLinker::getCFIndexFromName(std::string name)
{
    for(int i = 0; (*m_crazyflies_vector).size(); i++)
    {
        if(name == (*m_crazyflies_vector)[i]->getName())
        {
            return i;
        }
    }
}
64

65
66
67
68
69
70
71
72
73
74
75
76
77
78
bool CFLinker::isStudentIDLinked(int student_id)
{
    bool is_linked = false;
    for(int i = 0; i < links.size(); i++)
    {
        if(links[i].student_id == student_id)
        {
            is_linked = true;
        }
    }

    return is_linked;
}

79
80
81
82
83
84
85
86
87
88
89
90
91
bool CFLinker::isRadioAddressLinked(std::string radio_address)
{
    bool is_linked = false;
    for(int i = 0; i < links.size(); i++)
    {
        if(links[i].radio_address == radio_address)
        {
            is_linked = true;
        }
    }
    return is_linked;
}

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
bool CFLinker::isCFZoneLinked(int cf_zone_index)
{
    bool is_linked = false;
    for(int i = 0; i < links.size(); i++)
    {
        if(links[i].cf_zone_index == cf_zone_index)
        {
            is_linked = true;
        }
    }

    return is_linked;
}

bool CFLinker::isCFLinked(std::string cf_name)
{
    bool is_linked = false;
    for(int i = 0; i < links.size(); i++)
    {
        if(links[i].cf_name == cf_name)
        {
            is_linked = true;
        }
    }
    return is_linked;
}


120
void CFLinker::addNewRow(int student_id, std::string crazyfly_name, int cf_zone_index, std::string radio_address)
121
122
{
    m_ui->table_links->insertRow(m_ui->table_links->rowCount());
123
    QString str_id = QString::number(student_id);
124
125
126
127
    QTableWidgetItem *item_id = new QTableWidgetItem(str_id);
    item_id->setFlags(item_id->flags() & ~Qt::ItemIsEditable);
    m_ui->table_links->setItem(m_ui->table_links->rowCount() - 1, 0, item_id);

128
    QString str_cf_name = QString::fromStdString(crazyfly_name);
129
130
131
132
    QTableWidgetItem *item_cf = new QTableWidgetItem(str_cf_name);
    item_cf->setFlags(item_cf->flags() & ~Qt::ItemIsEditable);
    m_ui->table_links->setItem(m_ui->table_links->rowCount() - 1, 1, item_cf);

133
    QString str_cf_zone_index = QString("CrazyFlyZone ").append(QString::number(cf_zone_index + 1));
134
135
136
    QTableWidgetItem *item_cf_zone = new QTableWidgetItem(str_cf_zone_index);
    item_cf_zone->setFlags(item_cf_zone->flags() & ~Qt::ItemIsEditable);
    m_ui->table_links->setItem(m_ui->table_links->rowCount() - 1, 2, item_cf_zone);
137
138
139
140
141

    QString str_radio_address = QString::fromStdString(radio_address);
    QTableWidgetItem *item_radio_address = new QTableWidgetItem(str_radio_address);
    item_cf->setFlags(item_radio_address->flags() & ~Qt::ItemIsEditable);
    m_ui->table_links->setItem(m_ui->table_links->rowCount() - 1, 3, item_radio_address);
142
143
}

144
void CFLinker::link(int student_id, int cf_zone_index, std::string cf_name, std::string radio_address)
145
146
147
148
{
    m_ui->table_links->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    struct link tmp_link;

149
150
151
    tmp_link.student_id = student_id;
    tmp_link.cf_zone_index = cf_zone_index;
    tmp_link.cf_name = cf_name;
152
    tmp_link.radio_address = radio_address;
153

154
    ROS_INFO("tmp_link.student_id %d", tmp_link.student_id);
155
156
    ROS_INFO("tmp_link.cf_zone_index %d", tmp_link.cf_zone_index);
    ROS_INFO("tmp_link.cf_name %s", tmp_link.cf_name.c_str());
157
    ROS_INFO("tmp_link.radio_address %s", tmp_link.radio_address.c_str());
158

159
160
    // (*m_crazyfly_zones)[tmp_link.cf_zone_index]->linkCF(tmp_link.cf_name);
    // (*m_crazyflies_vector)[getCFIndexFromName(tmp_link.cf_name)]->assignCFZone(tmp_link.cf_zone_index);
161

162
    addNewRow(tmp_link.student_id, tmp_link.cf_name, tmp_link.cf_zone_index, tmp_link.radio_address);
163

164
    links.push_back(tmp_link);
165
    // TODO: remove options linked from available ones
166
167
168
169
170
171
172
173
174
175
176

    int index = m_ui->comboBoxCFs->currentIndex();
    // remove items
    m_ui->comboBoxCFs->removeItem(index);
    index = m_ui->comboBoxCFZones->currentIndex();
    m_ui->comboBoxCFZones->removeItem(index);

    // disable item
    // m_ui->comboBoxCFs->setItemData(index, 0, Qt::UserRole - 1);
    // enable item
    // ui->comboBox->setItemData(index, 33, Qt::UserRole - 1);
177
178
}

179
180
181
182
183
184
185
void CFLinker::clear_all_links()
{
    links.clear();
    m_ui->table_links->setRowCount(0);
    emit updateComboBoxes();
}

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
void CFLinker::unlink_cf_zone(int cf_zone_index)
{
    for(int i = 0; i < links.size(); i++)
    {
        if(links[i].cf_zone_index == cf_zone_index)
        {
            links.erase(links.begin() + i);
            break;
        }
    }
    // remove them graphically
    for(int i = 0; i < m_ui->table_links->rowCount(); i++)
    {
        QString name = m_ui->table_links->item(i, 2)->text(); //2 because cf zone
        if(getCFZoneIndexFromName(name) == cf_zone_index)
        {
            m_ui->table_links->removeRow(i);
            break;
        }
    }
    // update combo boxes
    emit updateComboBoxes();
}

void CFLinker::unlink_selection()
211
{
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
    QModelIndexList selection = m_ui->table_links->selectionModel()->selectedRows();


    // first, get an ordered from greater to lesser vector of indexes of selected rows
    std::vector<int> ordered_row_indexes;

    for(int i = selection.count() - 1; i >= 0; i--) // fill vector first
    {
        QModelIndex index = selection.at(i);
        ordered_row_indexes.push_back(index.row());
    }

    // sort using a standard library compare function object, greater to lesser
    std::sort(ordered_row_indexes.begin(), ordered_row_indexes.end(), std::greater<int>());

    // now, unlink them in data structure and graphically removing rows from the table

    for(int i = 0; i < ordered_row_indexes.size(); i++)
    {
        // TODO: unset linked status in its corresponding objects
        // remove them from links vector. We will look for student as unique key.. Maybe in a future the CF is the unique key?
        int student_id = m_ui->table_links->item(ordered_row_indexes[i], 0)->text().toInt(); // 0: student ID
        for(int i = 0; i < links.size(); i++)
        {
            if(links[i].student_id == student_id)
            {
                links.erase(links.begin() + i);
                break;
            }
        }
        // remove them graphically
        m_ui->table_links->removeRow(ordered_row_indexes[i]);
    }
245
246
247

    // update combo boxes
    emit updateComboBoxes();
248
}