CFLinker.cpp 6.5 KB
Newer Older
1
2
#include "CFLinker.h"

3
#include <QHeaderView>
4

5
CFLinker::CFLinker(Ui::MainGUIWindow* ui, std::vector<crazyFly*> *crazyflies_vector, std::vector<crazyFlyZone*> *crazyfly_zones)
6
    : QObject(0)
7
{
8
    m_ui = ui;
9
10
    m_crazyflies_vector = crazyflies_vector;
    m_crazyfly_zones = crazyfly_zones;
11
12
13
14
15
16
}

CFLinker::~CFLinker()
{
}

17
int CFLinker::getCFZoneIndexFromName(QString name)
18
{
19
20
    return name.split(" ")[1].toInt() - 1;
}
21

22
23
24
25
26
27
28
29
30
31
int CFLinker::getCFIndexFromName(std::string name)
{
    for(int i = 0; (*m_crazyflies_vector).size(); i++)
    {
        if(name == (*m_crazyflies_vector)[i]->getName())
        {
            return i;
        }
    }
}
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
}

47
48
49
50
51
52
53
54
55
56
57
58
59
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;
}

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
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;
}


88
void CFLinker::addNewRow(int student_id, std::string crazyfly_name, int cf_zone_index, std::string radio_address)
89
90
{
    m_ui->table_links->insertRow(m_ui->table_links->rowCount());
91
    QString str_id = QString::number(student_id);
92
93
94
95
    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);

96
    QString str_cf_name = QString::fromStdString(crazyfly_name);
97
98
99
100
    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);

101
    QString str_cf_zone_index = QString("CrazyFlyZone ").append(QString::number(cf_zone_index + 1));
102
103
104
    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);
105
106
107
108
109

    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);
110
111
}

112
void CFLinker::link(int student_id, int cf_zone_index, std::string cf_name, std::string radio_address)
113
114
115
116
{
    m_ui->table_links->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    struct link tmp_link;

117
118
119
    tmp_link.student_id = student_id;
    tmp_link.cf_zone_index = cf_zone_index;
    tmp_link.cf_name = cf_name;
120
    tmp_link.radio_address = radio_address;
121

122
    ROS_INFO("tmp_link.student_id %d", tmp_link.student_id);
123
124
    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());
125
    ROS_INFO("tmp_link.radio_address %s", tmp_link.radio_address.c_str());
126

127
128
    // (*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);
129

130
    addNewRow(tmp_link.student_id, tmp_link.cf_name, tmp_link.cf_zone_index, tmp_link.radio_address);
131

132
    links.push_back(tmp_link);
133
    // TODO: remove options linked from available ones
134
135
136
137
138
139
140
141
142
143
144

    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);
145
146
}

147
148
149
150
151
152
153
void CFLinker::clear_all_links()
{
    links.clear();
    m_ui->table_links->setRowCount(0);
    emit updateComboBoxes();
}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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()
179
{
180
181
182
183
184
185
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
211
212
    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]);
    }
213
214
215

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