From 5a5d16bdc6cf88310b58fdcbd9256ef6117c9c9d Mon Sep 17 00:00:00 2001 From: GriffinR Date: Tue, 30 Nov 2021 13:38:06 -0500 Subject: [PATCH] Add overlay visibility toggle --- include/mainwindow.h | 3 +++ include/ui/graphicsview.h | 1 + include/ui/overlay.h | 2 ++ src/mainwindow_scriptapi.cpp | 18 ++++++++++++++++++ src/ui/graphicsview.cpp | 5 +++++ src/ui/overlay.cpp | 9 +++++++-- 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/mainwindow.h b/include/mainwindow.h index f94497f3..66c459d8 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -68,6 +68,9 @@ public: Q_INVOKABLE void setWidth(int width); Q_INVOKABLE void setHeight(int height); Q_INVOKABLE void clearOverlay(int layer = INT_MAX); + Q_INVOKABLE void hideOverlay(int layer = INT_MAX); + Q_INVOKABLE void showOverlay(int layer = INT_MAX); + Q_INVOKABLE void setOverlayVisibility(bool visible, int layer = INT_MAX); Q_INVOKABLE void addText(QString text, int x, int y, QString color = "#000000", int fontSize = 12, int layer = 0); Q_INVOKABLE void addRect(int x, int y, int width, int height, QString color = "#000000", int layer = 0); Q_INVOKABLE void addFilledRect(int x, int y, int width, int height, QString color = "#000000", int layer = 0); diff --git a/include/ui/graphicsview.h b/include/ui/graphicsview.h index 69faef6c..78bfe40d 100644 --- a/include/ui/graphicsview.h +++ b/include/ui/graphicsview.h @@ -14,6 +14,7 @@ public: GraphicsView(QWidget *parent) : QGraphicsView(parent) {} Overlay * getOverlay(int layer); void clearOverlays(); + void setOverlaysHidden(bool hidden); public: // GraphicsView_Object object; diff --git a/include/ui/overlay.h b/include/ui/overlay.h index ec0ab7cb..68277b16 100644 --- a/include/ui/overlay.h +++ b/include/ui/overlay.h @@ -75,6 +75,7 @@ public: ~Overlay() { this->clearItems(); } + void setHidden(bool hidden); void renderItems(QPainter *painter); QList getItems(); void clearItems(); @@ -83,6 +84,7 @@ public: bool addImage(int x, int y, QString filepath, int width = -1, int height = -1, unsigned offset = 0, bool hflip = false, bool vflip = false, bool setTransparency = false); private: QList items; + bool hidden; }; #endif // OVERLAY_H diff --git a/src/mainwindow_scriptapi.cpp b/src/mainwindow_scriptapi.cpp index 52b87e7d..a8dab935 100644 --- a/src/mainwindow_scriptapi.cpp +++ b/src/mainwindow_scriptapi.cpp @@ -238,6 +238,24 @@ void MainWindow::clearOverlay(int layer) { this->ui->graphicsView_Map->scene()->update(); } +void MainWindow::hideOverlay(int layer) { + this->setOverlayVisibility(false, layer); +} + +void MainWindow::showOverlay(int layer) { + this->setOverlayVisibility(true, layer); +} + +void MainWindow::setOverlayVisibility(bool visible, int layer) { + if (!this->ui || !this->ui->graphicsView_Map) + return; + if (layer == INT_MAX) + this->ui->graphicsView_Map->setOverlaysHidden(!visible); + else + this->ui->graphicsView_Map->getOverlay(layer)->setHidden(!visible); + this->ui->graphicsView_Map->scene()->update(); +} + void MainWindow::addText(QString text, int x, int y, QString color, int fontSize, int layer) { if (!this->ui || !this->ui->graphicsView_Map || layer == INT_MAX) return; diff --git a/src/ui/graphicsview.cpp b/src/ui/graphicsview.cpp index 2363b511..c16385c3 100644 --- a/src/ui/graphicsview.cpp +++ b/src/ui/graphicsview.cpp @@ -26,6 +26,11 @@ void GraphicsView::clearOverlays() { overlay->clearItems(); } +void GraphicsView::setOverlaysHidden(bool hidden) { + foreach (Overlay * overlay, this->overlayMap) + overlay->setHidden(hidden); +} + Overlay * GraphicsView::getOverlay(int layer) { Overlay * overlay = this->overlayMap.value(layer, nullptr); if (!overlay) { diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index 955ed626..7c7aeab3 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -23,9 +23,10 @@ void OverlayImage::render(QPainter *painter) { } void Overlay::renderItems(QPainter *painter) { - for (auto item : this->items) { + if (this->hidden) return; + + for (auto item : this->items) item->render(painter); - } } void Overlay::clearItems() { @@ -39,6 +40,10 @@ QList Overlay::getItems() { return this->items; } +void Overlay::setHidden(bool hidden) { + this->hidden = hidden; +} + void Overlay::addText(QString text, int x, int y, QString color, int fontSize) { this->items.append(new OverlayText(text, x, y, QColor(color), fontSize)); }