Skip to content
Snippets Groups Projects
Commit 9e7b2a02 authored by roangel's avatar roangel
Browse files

Rework in CFZones. When we remove now, everything OK, we unlink perfectly, we...

Rework in CFZones. When we remove now, everything OK, we unlink perfectly, we dont change the names... Getting some crashes when we close tabs. Need to fix
parent d1b46458
No related branches found
No related tags found
No related merge requests found
......@@ -26,13 +26,16 @@ public:
~CFLinker();
void link();
void unlink();
void unlink_selection();
void unlink_cf_zone(int cf_zone_index);
std::vector<struct link> links;
bool isStudentIDLinked(int student_id);
bool isCFZoneLinked(int cf_zone_index);
bool isCFLinked(std::string cf_name);
int getCFZoneIndexFromName(QString name);
int getCFIndexFromName(std::string name);
signals:
void updateComboBoxes();
......@@ -45,8 +48,6 @@ private:
std::vector<crazyFly*>* m_crazyflies_vector;
std::vector<crazyFlyZone*>* m_crazyfly_zones;
int getCFZoneIndexFromName(QString name);
int getCFIndexFromName(std::string name);
void addNewRow(int student_id, std::string crazyfly_name, int cf_zone_index);
};
......
......@@ -115,6 +115,9 @@ private slots:
void updateComboBoxes();
void setTabIndex(int index);
void doTabClosed(int tab_index);
private:
Ui::MainGUIWindow *ui;
......@@ -133,6 +136,8 @@ private:
void updateComboBoxesCFZones();
int getTabIndexFromName(QString name);
};
......
......@@ -72,7 +72,6 @@ private:
void addCrazyFlyZoneToVector(crazyFlyZone* rect);
void addTablePieceToVector(tablePiece* rect);
int checkSelectedCrazyFlyZone();
void updateIndexesAndLabelsCrazyFlyZones();
void removeTablePiece(int index);
QPen* pen;
......
......@@ -123,7 +123,31 @@ void CFLinker::link()
// ui->comboBox->setItemData(index, 33, Qt::UserRole - 1);
}
void CFLinker::unlink()
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()
{
QModelIndexList selection = m_ui->table_links->selectionModel()->selectedRows();
......
......@@ -41,19 +41,64 @@ MainGUIWindow::~MainGUIWindow()
delete ui;
}
int MainGUIWindow::getTabIndexFromName(QString name)
{
int found_name = -1;
for(int i = 0; i < ui->tabWidget->count(); i++)
{
qDebug("name: %s", name.toStdString().c_str());
qDebug("tabText: %s", ui->tabWidget->tabText(i).toStdString().c_str());
if(name == ui->tabWidget->tabText(i))
{
found_name = i;
}
}
return found_name;
}
void MainGUIWindow::doNumCrazyFlyZonesChanged(int n)
{
// tabs number management, maybe do it in a different way so we dont have to remove and add everything?
ui->tabWidget->clear();
for (int i = 0; i < n; i++)
// first check if size of tabs is greater than size of vector or viceversa. Have we removed or added a zone?
qDebug("tabWidgetCount : %d", ui->tabWidget->count());
if(ui->tabWidget->count() > scene->crazyfly_zones.size())
{
// we removed one crazyfly_zone, n means index of the one we removed. Look for that index tab and remove it
QString qstr = "CrazyFly ";
qstr.append(QString::number(n+1));
if(scene->crazyfly_zones.size() == 0)
{
ui->tabWidget->clear();
}
int found_index = getTabIndexFromName(qstr);
if(found_index != -1)
{
ui->tabWidget->removeTab(found_index);
}
// now unlink it from table also:
if(cf_linker->isCFZoneLinked(n))
{
cf_linker->unlink_cf_zone(n);
}
}
else if(ui->tabWidget->count() < scene->crazyfly_zones.size())
{
// we added one crazyfly_zone, n means index of the new one. New tab will be labeld index + 1
QString qstr = "CrazyFly ";
qstr.append(QString::number(i+1));
crazyFlyZoneTab* widget = new crazyFlyZoneTab(i);
ui->tabWidget->addTab(widget, qstr);
qstr.append(QString::number(n+1));
crazyFlyZoneTab* widget = new crazyFlyZoneTab(n);
ui->tabWidget->insertTab(n, widget, qstr);
connect(widget, SIGNAL(centerButtonClickedSignal(int)), this, SLOT(centerViewIndex(int)));
}
// for (int i = 0; i < n; i++)
// {
// QString qstr = "CrazyFly ";
// qstr.append(QString::number(i+1));
// crazyFlyZoneTab* widget = new crazyFlyZoneTab(i);
// ui->tabWidget->addTab(widget, qstr);
// connect(widget, SIGNAL(centerButtonClickedSignal(int)), this, SLOT(centerViewIndex(int)));
// }
updateComboBoxesCFZones();
}
......@@ -122,10 +167,9 @@ void MainGUIWindow::_init()
cf_linker = new CFLinker(ui, &crazyflies_vector, &scene->crazyfly_zones);
#endif
// connections
QObject::connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), scene, SLOT(removeCrazyFlyZone(int)));
QObject::connect(ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(doTabClosed(int)));
QObject::connect(scene, SIGNAL(numCrazyFlyZonesChanged(int)), this, SLOT(doNumCrazyFlyZonesChanged(int)));
QObject::connect(ui->tabWidget, SIGNAL(currentChanged(int)), scene, SLOT(setSelectedCrazyFlyZone(int)));
QObject::connect(scene, SIGNAL(crazyFlyZoneSelected(int)), ui->tabWidget, SLOT(setCurrentIndex(int)));
QObject::connect(scene, SIGNAL(crazyFlyZoneSelected(int)), this, SLOT(setTabIndex(int)));
QObject::connect(scene, SIGNAL(modeChanged(int)), this, SLOT(transitionToMode(int)));
QObject::connect(scene, SIGNAL(numTablePiecesChanged(int)), this, SLOT(handleTablePiecesNumChanged(int)));
......@@ -139,6 +183,20 @@ void MainGUIWindow::_init()
#endif
}
void MainGUIWindow::doTabClosed(int tab_index)
{
QString name = ui->tabWidget->tabText(tab_index);
int cf_index = cf_linker->getCFZoneIndexFromName(name);
scene->removeCrazyFlyZone(cf_index);
}
void MainGUIWindow::setTabIndex(int index)
{
QString qstr = "CrazyFly ";
qstr.append(QString::number(index + 1));
ui->tabWidget->setCurrentIndex(getTabIndexFromName(qstr));
}
void MainGUIWindow::updateComboBoxes()
{
updateComboBoxesCFs();
......@@ -167,8 +225,9 @@ void MainGUIWindow::updateComboBoxesCFZones()
{
if(!cf_linker->isCFZoneLinked(scene->crazyfly_zones[i]->getIndex()))
{
int cf_zone_index = scene->crazyfly_zones[i]->getIndex();
QString qstr = "CrazyFlyZone ";
qstr.append(QString::number(i+1));
qstr.append(QString::number(cf_zone_index + 1));
ui->comboBoxCFZones->addItem(qstr);
}
}
......@@ -421,10 +480,10 @@ void MainGUIWindow::on_checkBox_crazyfly_zones_toggled(bool checked)
void MainGUIWindow::on_tabWidget_currentChanged(int index)
{
if(index >= 0)
{
scene->setSelectedCrazyFlyZone(index);
}
// this index is tab index. Need to go to cf index
QString name = ui->tabWidget->tabText(index);
int cf_index = cf_linker->getCFZoneIndexFromName(name);
scene->setSelectedCrazyFlyZone(cf_index);
}
void MainGUIWindow::centerViewIndex(int index)
......@@ -621,6 +680,6 @@ void MainGUIWindow::on_link_button_clicked()
void MainGUIWindow::on_unlink_button_clicked()
{
#ifdef CATKIN_MAKE
cf_linker->unlink();
cf_linker->unlink_selection();
#endif
}
......@@ -71,10 +71,13 @@ void myGraphicsScene::setSelectedCrazyFlyZone(int index)
{
for(unsigned int i = 0; i < crazyfly_zones.size(); i++)
{
qDebug("index: %d", index);
qDebug("i: %d", i);
qDebug("getIndex: %d", crazyfly_zones[i]->getIndex());
crazyfly_zones[i]->setSelected(false);
if(index == i)
if(index == crazyfly_zones[i]->getIndex())
{
crazyfly_zones[index]->setSelected(true);
crazyfly_zones[i]->setSelected(true);
}
}
}
......@@ -85,8 +88,8 @@ int myGraphicsScene::checkSelectedCrazyFlyZone()
{
if(crazyfly_zones[i]->isSelected())
{
qDebug("rectangle selected index = %d", i);
return i;
qDebug("rectangle selected index = %d", crazyfly_zones[i]->getIndex());
return crazyfly_zones[i]->getIndex();
}
}
return -1;
......@@ -115,8 +118,38 @@ void myGraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
startedRect = true;
p1 = new QPointF(mouseEvent->scenePos());
tmp_rect = new QRectF(*p1, *p1);
int index = crazyfly_zones.size();
tmp_crazyfly_zone_item = new crazyFlyZone(*tmp_rect, index);
// look for first available index here
bool first_available_value_found = false;
int first_available_value = 0;
while(!first_available_value_found)
{
if(crazyfly_zones.size() == 0)
{
first_available_value_found = true;
break;
}
else // we enter the for loop for sure
{
bool index_is_present = false;
for(int i = 0; i < crazyfly_zones.size(); i++)
{
if(crazyfly_zones[i]->getIndex() == first_available_value)
{
index_is_present = true;
break; // break because we finish searching here
}
}
if(!index_is_present)
{
first_available_value_found = true; // finish loop here, found value
break;
}
}
first_available_value++; //first iteration 0
}
qDebug("---------------------------first available value: %d", first_available_value);
tmp_crazyfly_zone_item = new crazyFlyZone(*tmp_rect, first_available_value);
addItem(tmp_crazyfly_zone_item);
break;
}
......@@ -163,7 +196,7 @@ void myGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
void myGraphicsScene::addCrazyFlyZoneToVector(crazyFlyZone* rect)
{
crazyfly_zones.push_back(rect);
emit numCrazyFlyZonesChanged(crazyfly_zones.size());
emit numCrazyFlyZonesChanged(rect->getIndex());
}
void myGraphicsScene::addTablePieceToVector(tablePiece* rect)
......@@ -172,18 +205,6 @@ void myGraphicsScene::addTablePieceToVector(tablePiece* rect)
emit numTablePiecesChanged(table_pieces.size());
}
void myGraphicsScene::updateIndexesAndLabelsCrazyFlyZones()
{
for(int i = 0; i < crazyfly_zones.size(); i++)
{
crazyfly_zones[i]->setIndex(i);
std::string str = std::to_string(i + 1);
crazyfly_zones[i]->updateLabel(str.c_str());
qDebug("reset Index %d and update label",i);
}
}
void myGraphicsScene::changeModeTo(int next_mode)
{
mode = next_mode;
......@@ -302,11 +323,11 @@ QRectF myGraphicsScene::getRectFCrazyFlyZone(int index)
void myGraphicsScene::removeCrazyFlyZone(int index)
{
int n = crazyfly_zones[index]->getIndex();
this->removeItem(crazyfly_zones[index]);
crazyfly_zones.erase(crazyfly_zones.begin() + index);
qDebug("removed CFzone %d", index);
updateIndexesAndLabelsCrazyFlyZones();
emit numCrazyFlyZonesChanged(crazyfly_zones.size()); // for tab managing
emit numCrazyFlyZonesChanged(n); // for tab managing
}
void myGraphicsScene::removeTable()
......@@ -386,7 +407,8 @@ void myGraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
tmp_crazyfly_zone_item->setRect(tmp_crazyfly_zone_item->rect().normalized());
addCrazyFlyZoneToVector(tmp_crazyfly_zone_item);
std::string str = std::to_string(crazyfly_zones.size());
// TODO: set first available number, not size of vector. Index + 1
std::string str = std::to_string(tmp_crazyfly_zone_item->getIndex() + 1);
tmp_crazyfly_zone_item->setLabel(str.c_str());
setSelectedCrazyFlyZone(crazyfly_zones.size() - 1); //select just created rectangle
tmp_rect = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment