From 9abe59c917c37f32ec9fb2bde9d12df6a6510429 Mon Sep 17 00:00:00 2001 From: Maxime Gimeno Date: Wed, 25 May 2016 11:12:43 +0200 Subject: [PATCH] Fix the bug of single textItems on multiple scene_items. --- Polyhedron/demo/Polyhedron/MainWindow.cpp | 10 +-- Polyhedron/demo/Polyhedron/MainWindow.ui | 8 +-- Polyhedron/demo/Polyhedron/Scene.cpp | 5 +- .../demo/Polyhedron/Scene_polyhedron_item.cpp | 63 ++++++++++++------- .../demo/Polyhedron/Scene_polyhedron_item.h | 5 +- Polyhedron/demo/Polyhedron/TextRenderer.cpp | 34 +++++----- Polyhedron/demo/Polyhedron/Viewer.cpp | 9 +-- Polyhedron/demo/Polyhedron/Viewer.h | 2 - Three/include/CGAL/Three/TextRenderer.h | 2 + Three/include/CGAL/Three/Viewer_interface.h | 3 - 10 files changed, 76 insertions(+), 65 deletions(-) diff --git a/Polyhedron/demo/Polyhedron/MainWindow.cpp b/Polyhedron/demo/Polyhedron/MainWindow.cpp index 23d134a1bc7..a83dacaf990 100644 --- a/Polyhedron/demo/Polyhedron/MainWindow.cpp +++ b/Polyhedron/demo/Polyhedron/MainWindow.cpp @@ -137,7 +137,7 @@ MainWindow::MainWindow(QWidget* parent) scene = new Scene(this); viewer->textRenderer->setScene(scene); viewer->setScene(scene); - ui->actionMax_text_items_displayed->setText(QString("Max text items displayed %1").arg(viewer->textRenderer->getMax_textItems())); + ui->actionMax_text_items_displayed->setText(QString("Set Maximum Text Items Displayed : %1").arg(viewer->textRenderer->getMax_textItems())); { QShortcut* shortcut = new QShortcut(QKeySequence(Qt::ALT+Qt::Key_Q), this); connect(shortcut, SIGNAL(activated()), @@ -1847,12 +1847,12 @@ void MainWindow::on_actionMaxItemsDisplayed_triggered() { bool ok; bool valid; - QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"), - tr("Maximum text items diplayed:"), QLineEdit::Normal, - "30000", &ok); + QString text = QInputDialog::getText(this, tr("Maximum Number of Text Items"), + tr("Maximum Text Items Diplayed:"), QLineEdit::Normal, + QString("%1").arg(viewer->textRenderer->getMax_textItems()), &ok); text.toInt(&valid); if (ok && valid){ viewer->textRenderer->setMax(text.toInt()); - ui->actionMax_text_items_displayed->setText(QString("Max text items displayed %1").arg(text.toInt())); + ui->actionMax_text_items_displayed->setText(QString("Set Maximum Text Items Displayed : %1").arg(text.toInt())); } } diff --git a/Polyhedron/demo/Polyhedron/MainWindow.ui b/Polyhedron/demo/Polyhedron/MainWindow.ui index acb3b521ba4..b1aacdfdc76 100644 --- a/Polyhedron/demo/Polyhedron/MainWindow.ui +++ b/Polyhedron/demo/Polyhedron/MainWindow.ui @@ -37,7 +37,7 @@ 0 0 978 - 26 + 20 @@ -299,8 +299,8 @@ 0 0 - 534 - 174 + 541 + 175 @@ -679,7 +679,7 @@ - Max text items displayed + Set Maximum Text Items Displayed : diff --git a/Polyhedron/demo/Polyhedron/Scene.cpp b/Polyhedron/demo/Polyhedron/Scene.cpp index 1d59b438df7..5a647c5995b 100644 --- a/Polyhedron/demo/Polyhedron/Scene.cpp +++ b/Polyhedron/demo/Polyhedron/Scene.cpp @@ -1170,9 +1170,8 @@ bool Scene::testDisplayId(double x, double y, double z, CGAL::Three::Viewer_inte bool res = i->testDisplayId(x,y,z, viewer); return res; } - else return false; - - + else + return false; } #include "Scene_find_items.h" diff --git a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp index 6d61d4eadbc..96067edeb03 100644 --- a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp +++ b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.cpp @@ -591,6 +591,7 @@ Scene_polyhedron_item::Scene_polyhedron_item() textItems = new TextListItem(this); init(); targeted_id = NULL; + all_ids_displayed = false; } Scene_polyhedron_item::Scene_polyhedron_item(Polyhedron* const p) @@ -611,6 +612,7 @@ Scene_polyhedron_item::Scene_polyhedron_item(Polyhedron* const p) init(); invalidateOpenGLBuffers(); targeted_id = NULL; + all_ids_displayed = false; } Scene_polyhedron_item::Scene_polyhedron_item(const Polyhedron& p) @@ -631,6 +633,7 @@ Scene_polyhedron_item::Scene_polyhedron_item(const Polyhedron& p) nb_f_lines = 0; invalidateOpenGLBuffers(); targeted_id = NULL; + all_ids_displayed = false; } Scene_polyhedron_item::~Scene_polyhedron_item() @@ -1322,6 +1325,8 @@ void Scene_polyhedron_item::printPrimitiveId(QPoint point, CGAL::Three::Viewer_i { TextRenderer *renderer = viewer->textRenderer; renderer->getLocalTextItems().removeAll(targeted_id); + renderer->removeTextList(textItems); + textItems->clear(); QFont font; font.setBold(true); @@ -1415,7 +1420,8 @@ void Scene_polyhedron_item::printPrimitiveId(QPoint point, CGAL::Three::Viewer_i if (targeted_id == NULL || targeted_id->position() != text_item.position() ) { targeted_id = new TextItem(text_item); - renderer->addText(targeted_id); + textItems->append(targeted_id); + renderer->addTextList(textItems); } else targeted_id=NULL; @@ -1427,45 +1433,60 @@ void Scene_polyhedron_item::printPrimitiveId(QPoint point, CGAL::Three::Viewer_i void Scene_polyhedron_item::printPrimitiveIds(CGAL::Three::Viewer_interface *viewer) const { - TextRenderer *renderer = viewer->textRenderer; - //clears textitems - renderer->removeTextList(textItems); - textItems->clear(); - QFont font; - font.setBold(true); - //fills textItems - Q_FOREACH(Polyhedron::Vertex_const_handle vh, vertices(*poly)) + + if(!all_ids_displayed) { + QFont font; + font.setBold(true); + + //fills textItems + Q_FOREACH(Polyhedron::Vertex_const_handle vh, vertices(*poly)) + { const Point& p = vh->point(); textItems->append(new TextItem((float)p.x(), (float)p.y(), (float)p.z(), QString("%1").arg(vh->id()), true, font, Qt::red)); - } + } - Q_FOREACH(boost::graph_traits::edge_descriptor e, edges(*poly)) - { + Q_FOREACH(boost::graph_traits::edge_descriptor e, edges(*poly)) + { const Point& p1 = source(e, *poly)->point(); const Point& p2 = target(e, *poly)->point(); textItems->append(new TextItem((float)(p1.x() + p2.x()) / 2, (float)(p1.y() + p2.y()) / 2, (float)(p1.z() + p2.z()) / 2, QString("%1").arg(e.halfedge()->id() / 2), true, font, Qt::green)); - } + } - Q_FOREACH(Polyhedron::Facet_handle fh, faces(*poly)) - { + Q_FOREACH(Polyhedron::Facet_handle fh, faces(*poly)) + { double x(0), y(0), z(0); int total(0); Q_FOREACH(Polyhedron::Vertex_handle vh, vertices_around_face(fh->halfedge(), *poly)) { - x += vh->point().x(); - y += vh->point().y(); - z += vh->point().z(); - ++total; + x += vh->point().x(); + y += vh->point().y(); + z += vh->point().z(); + ++total; } textItems->append(new TextItem((float)x / total, (float)y / total, (float)z / total, QString("%1").arg(fh->id()), true, font, Qt::blue)); + } + //add the QList to the render's pool + renderer->addTextList(textItems); + if(textItems->size() > renderer->getMax_textItems()) + all_ids_displayed = !all_ids_displayed; } - //add the QList to the render's pool - renderer->addTextList(textItems); + if(all_ids_displayed) + { + //clears TextItems + textItems->clear(); + renderer->removeTextList(textItems); + if(targeted_id) + { + textItems->append(targeted_id); + renderer->addTextList(textItems); + } + } + all_ids_displayed = !all_ids_displayed; } bool Scene_polyhedron_item::testDisplayId(double x, double y, double z, CGAL::Three::Viewer_interface* viewer) diff --git a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.h b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.h index 93108687372..85fd7559b65 100644 --- a/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.h +++ b/Polyhedron/demo/Polyhedron/Scene_polyhedron_item.h @@ -149,7 +149,7 @@ private: Edges, Feature_edges, Gouraud_Facets, - NbOfVaos = Gouraud_Facets+1 + NbOfVaos }; enum VBOs { Facets_vertices = 0, @@ -159,9 +159,10 @@ private: Feature_edges_vertices, Edges_color, Facets_normals_gouraud, - NbOfVbos = Facets_normals_gouraud+1 + NbOfVbos }; + mutable bool all_ids_displayed; mutable std::vector positions_lines; mutable std::vector positions_feature_lines; mutable std::vector positions_facets; diff --git a/Polyhedron/demo/Polyhedron/TextRenderer.cpp b/Polyhedron/demo/Polyhedron/TextRenderer.cpp index e5293004f2b..8dfe9a7e0db 100644 --- a/Polyhedron/demo/Polyhedron/TextRenderer.cpp +++ b/Polyhedron/demo/Polyhedron/TextRenderer.cpp @@ -7,32 +7,32 @@ void TextRenderer::draw(CGAL::Three::Viewer_interface *viewer) painter->begin(viewer); QRect rect; qglviewer::Camera* camera = viewer->camera(); - if(viewer->textDisplayed()) - { - Q_FOREACH(TextListItem* list, textItems) - if(list->item() == scene->item(scene->mainSelectionIndex())) - Q_FOREACH(TextItem* item, list->textList()) + //Display the items textItems + Q_FOREACH(TextListItem* list, textItems) + if(list->item() == scene->item(scene->mainSelectionIndex())) + Q_FOREACH(TextItem* item, list->textList()) + { + qglviewer::Vec src(item->position().x(), item->position().y(),item->position().z()); + if(viewer->testDisplayId(src.x, src.y, src.z)) { - qglviewer::Vec src(item->position().x(), item->position().y(),item->position().z()); - if(viewer->testDisplayId(src.x, src.y, src.z)) - { - if(item->is_3D()) - rect = QRect(camera->projectedCoordinatesOf(src).x-item->width()/2, + if(item->is_3D()) + rect = QRect(camera->projectedCoordinatesOf(src).x-item->width()/2, camera->projectedCoordinatesOf(src).y-item->height()/2, item->width(), item->height()); - else - rect = QRect(src.x-item->width()/2, + else + rect = QRect(src.x-item->width()/2, src.y-item->height()/2, item->width(), item->height()); - painter->setFont(item->font()); - painter->setPen(QPen(item->color())); - painter->drawText(rect, item->text()); - } + painter->setFont(item->font()); + painter->setPen(QPen(item->color())); + painter->drawText(rect, item->text()); } - } + } + + //Display the local TextItems Q_FOREACH(TextItem* item, local_textItems) { qglviewer::Vec src(item->position().x(), item->position().y(),item->position().z()); diff --git a/Polyhedron/demo/Polyhedron/Viewer.cpp b/Polyhedron/demo/Polyhedron/Viewer.cpp index fdf2fc462d2..fe169fe7920 100644 --- a/Polyhedron/demo/Polyhedron/Viewer.cpp +++ b/Polyhedron/demo/Polyhedron/Viewer.cpp @@ -313,9 +313,7 @@ void Viewer::keyPressEvent(QKeyEvent* e) } } else if(e->key() == Qt::Key_I && e->modifiers() & Qt::ControlModifier){ - has_text = !has_text; - if (has_text) - d->scene->printPrimitiveIds(this); + d->scene->printPrimitiveIds(this); update(); } //forward the event to the scene (item handling of the event) @@ -1206,11 +1204,6 @@ bool Viewer::testDisplayId(double x, double y, double z) return d->scene->testDisplayId(x,y,z,this); } -bool Viewer::textDisplayed() const -{ - return has_text; -} - QPainter* Viewer::getPainter(){return d->painter;} void Viewer::paintEvent(QPaintEvent *) diff --git a/Polyhedron/demo/Polyhedron/Viewer.h b/Polyhedron/demo/Polyhedron/Viewer.h index 14e63f42b74..41a3eb273ab 100644 --- a/Polyhedron/demo/Polyhedron/Viewer.h +++ b/Polyhedron/demo/Polyhedron/Viewer.h @@ -36,7 +36,6 @@ public: Viewer(QWidget * parent, bool antialiasing = false); ~Viewer(); bool testDisplayId(double, double, double); - bool textDisplayed() const; // overload several QGLViewer virtual functions //! Deprecated and does nothing. void draw(); @@ -62,7 +61,6 @@ public: void setScene(CGAL::Three::Scene_draw_interface* scene); //! @returns the antialiasing state. bool antiAliasing() const; - bool hasText() const{ return has_text; } //! @returns the fastDrawing state. bool inFastDrawing() const; //! Implementation of `Viewer_interface::inDrawWithNames()` diff --git a/Three/include/CGAL/Three/TextRenderer.h b/Three/include/CGAL/Three/TextRenderer.h index d9107108ef7..c031a43ef43 100644 --- a/Three/include/CGAL/Three/TextRenderer.h +++ b/Three/include/CGAL/Three/TextRenderer.h @@ -73,6 +73,8 @@ public: QList textList()const {return _list;} void append(TextItem* ti) {_list.append(ti);} void clear(){_list.clear();} + bool isEmpty()const {return _list.empty();} + std::size_t size()const{return _list.size();} private: CGAL::Three::Scene_item* _item; QList _list; diff --git a/Three/include/CGAL/Three/Viewer_interface.h b/Three/include/CGAL/Three/Viewer_interface.h index 327dc24f4c5..a5748937682 100644 --- a/Three/include/CGAL/Three/Viewer_interface.h +++ b/Three/include/CGAL/Three/Viewer_interface.h @@ -102,9 +102,6 @@ public: virtual bool inFastDrawing() const = 0; //! @returns if the viewer is in `drawWithNames()` virtual bool inDrawWithNames() const = 0; - //!Specifies if the Viewer prints the text or not. It is used, for example, in the items to - //! decide if the TextItems for the primitives IDs should be computed or not. - virtual bool textDisplayed() const =0; /*! Passes all the uniform data to the shaders. * According to program_name, this data may change.