diff --git a/src/Cutter.cpp b/src/Cutter.cpp index ee72a738..8ecb734b 100644 --- a/src/Cutter.cpp +++ b/src/Cutter.cpp @@ -1537,6 +1537,11 @@ QList CutterCore::getAllSearch(QString search_for, QString sp BlockStatistics CutterCore::getBlockStatistics(unsigned int blocksCount) { + if (blocksCount == 0) { + BlockStatistics ret; + ret.from = ret.to = ret.blocksize = 0; + return ret; + } QJsonObject statsObj = cmdj("p-j " + QString::number(blocksCount)).object(); BlockStatistics ret; diff --git a/src/widgets/VisualNavbar.cpp b/src/widgets/VisualNavbar.cpp index 4f80bfa9..9f3bd0e5 100644 --- a/src/widgets/VisualNavbar.cpp +++ b/src/widgets/VisualNavbar.cpp @@ -65,32 +65,54 @@ VisualNavbar::VisualNavbar(MainWindow *main, QWidget *parent) : setMouseTracking(true); } +unsigned int nextPow2(unsigned int n) +{ + unsigned int b = 0; + while (n) { + n >>= 1; + b++; + } + return (1u << b); +} + void VisualNavbar::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); - if (previousWidth != this->width()) { + + auto w = static_cast(width()); + bool fetch = false; + if (statsWidth < w) { + statsWidth = nextPow2(w); + fetch = true; + } else if (statsWidth > w*4) { + statsWidth = statsWidth > 0 ? statsWidth / 2 : 0; + fetch = true; + } + + if (fetch) { fetchAndPaintData(); - previousWidth = this->width(); + } else if (previousWidth != w) { + this->previousWidth = w; + updateGraphicsScene(); } } void VisualNavbar::fetchAndPaintData() { - fetchData(); - fillData(); + fetchStats(); + updateGraphicsScene(); } -void VisualNavbar::fetchData() +void VisualNavbar::fetchStats() { - statsWidth = graphicsView->width(); - stats = Core()->getBlockStatistics((unsigned int)statsWidth); + stats = Core()->getBlockStatistics(statsWidth); } enum class DataType: int { Empty, Code, String, Symbol, Count }; -void VisualNavbar::fillData() +void VisualNavbar::updateGraphicsScene() { graphicsScene->clear(); xToAddress.clear(); diff --git a/src/widgets/VisualNavbar.h b/src/widgets/VisualNavbar.h index d7dfcf6f..41472a25 100644 --- a/src/widgets/VisualNavbar.h +++ b/src/widgets/VisualNavbar.h @@ -28,8 +28,8 @@ public slots: private slots: void fetchAndPaintData(); - void fetchData(); - void fillData(); + void fetchStats(); + void updateGraphicsScene(); void drawCursor(); void on_seekChanged(RVA addr); @@ -40,13 +40,11 @@ private: MainWindow *main; BlockStatistics stats; - int statsWidth; + unsigned int statsWidth = 0; + unsigned int previousWidth = 0; QList xToAddress; - // Used to check whether the width changed. If yes we need to re-initialize the scene (slow) - int previousWidth = -1; - RVA localXToAddress(double x); double addressToLocalX(RVA address); QList sectionsForAddress(RVA address);