Rename Codegraphic to VisualNav and fix some bugs (#192)

* CodeGraphic: Use RVA_INVALID instead of 0 to indicate an invalid address.
* Rename CodeGraphic/GraphicsBar to VisualNavbar
* VisualNavbar: Fix a crash, do some cleanup, fix a resizing bug.
This commit is contained in:
Thomas (nezza-_-) Roth 2017-12-08 16:00:52 +01:00 committed by xarkes
parent 3f357fbbc3
commit f36e9277fc
5 changed files with 62 additions and 57 deletions

View File

@ -50,7 +50,7 @@
#include "widgets/SectionsDock.h" #include "widgets/SectionsDock.h"
#include "widgets/RelocsWidget.h" #include "widgets/RelocsWidget.h"
#include "widgets/FlagsWidget.h" #include "widgets/FlagsWidget.h"
#include "widgets/CodeGraphic.h" #include "widgets/VisualNavbar.h"
#include "widgets/Dashboard.h" #include "widgets/Dashboard.h"
#include "widgets/Notepad.h" #include "widgets/Notepad.h"
#include "widgets/Sidebar.h" #include "widgets/Sidebar.h"
@ -90,7 +90,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui(new Ui::MainWindow), ui(new Ui::MainWindow),
highlighter(nullptr), highlighter(nullptr),
hex_highlighter(nullptr), hex_highlighter(nullptr),
graphicsBar(nullptr), visualNavbar(nullptr),
entrypointDock(nullptr), entrypointDock(nullptr),
functionsDock(nullptr), functionsDock(nullptr),
importsDock(nullptr), importsDock(nullptr),
@ -171,11 +171,11 @@ void MainWindow::initUI()
spacer->setMinimumSize(20, 20); spacer->setMinimumSize(20, 20);
ui->mainToolBar->addWidget(spacer); ui->mainToolBar->addWidget(spacer);
// codeGraphics tool bar // Visual navigation tool bar
this->graphicsBar = new GraphicsBar(this); this->visualNavbar = new VisualNavbar(this);
this->graphicsBar->setMovable(false); this->visualNavbar->setMovable(false);
addToolBarBreak(Qt::TopToolBarArea); addToolBarBreak(Qt::TopToolBarArea);
addToolBar(graphicsBar); addToolBar(visualNavbar);
/* /*
* Dock Widgets * Dock Widgets

View File

@ -20,7 +20,7 @@ class PreviewWidget;
class Notepad; class Notepad;
class Highlighter; class Highlighter;
class AsciiHighlighter; class AsciiHighlighter;
class GraphicsBar; class VisualNavbar;
class FunctionsWidget; class FunctionsWidget;
class ImportsWidget; class ImportsWidget;
class ExportsWidget; class ExportsWidget;
@ -187,7 +187,7 @@ private:
std::unique_ptr<Ui::MainWindow> ui; std::unique_ptr<Ui::MainWindow> ui;
Highlighter *highlighter; Highlighter *highlighter;
AsciiHighlighter *hex_highlighter; AsciiHighlighter *hex_highlighter;
GraphicsBar *graphicsBar; VisualNavbar *visualNavbar;
EntrypointWidget *entrypointDock; EntrypointWidget *entrypointDock;
FunctionsWidget *functionsDock; FunctionsWidget *functionsDock;
ImportsWidget *importsDock; ImportsWidget *importsDock;

View File

@ -52,7 +52,6 @@ SOURCES += \
dialogs/CreateNewDialog.cpp \ dialogs/CreateNewDialog.cpp \
dialogs/NewFileDialog.cpp \ dialogs/NewFileDialog.cpp \
AnalThread.cpp \ AnalThread.cpp \
widgets/CodeGraphic.cpp \
widgets/CommentsWidget.cpp \ widgets/CommentsWidget.cpp \
widgets/ConsoleWidget.cpp \ widgets/ConsoleWidget.cpp \
widgets/Dashboard.cpp \ widgets/Dashboard.cpp \
@ -80,7 +79,8 @@ SOURCES += \
dialogs/SaveProjectDialog.cpp \ dialogs/SaveProjectDialog.cpp \
utils/TempConfig.cpp \ utils/TempConfig.cpp \
utils/SvgIconEngine.cpp \ utils/SvgIconEngine.cpp \
widgets/PseudocodeWidget.cpp widgets/PseudocodeWidget.cpp \
widgets/VisualNavbar.cpp
HEADERS += \ HEADERS += \
cutter.h \ cutter.h \
@ -103,7 +103,6 @@ HEADERS += \
dialogs/CreateNewDialog.h \ dialogs/CreateNewDialog.h \
dialogs/NewFileDialog.h \ dialogs/NewFileDialog.h \
AnalThread.h \ AnalThread.h \
widgets/CodeGraphic.h \
widgets/CommentsWidget.h \ widgets/CommentsWidget.h \
widgets/ConsoleWidget.h \ widgets/ConsoleWidget.h \
widgets/Dashboard.h \ widgets/Dashboard.h \
@ -131,7 +130,8 @@ HEADERS += \
dialogs/SaveProjectDialog.h \ dialogs/SaveProjectDialog.h \
utils/TempConfig.h \ utils/TempConfig.h \
utils/SvgIconEngine.h \ utils/SvgIconEngine.h \
widgets/PseudocodeWidget.h widgets/PseudocodeWidget.h \
widgets/VisualNavbar.h
FORMS += \ FORMS += \
dialogs/AboutDialog.ui \ dialogs/AboutDialog.ui \

View File

@ -1,4 +1,4 @@
#include "CodeGraphic.h" #include "VisualNavbar.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "utils/TempConfig.h" #include "utils/TempConfig.h"
@ -12,60 +12,68 @@
#include <QJsonArray> #include <QJsonArray>
#include <QJsonParseError> #include <QJsonParseError>
GraphicsBar::GraphicsBar(MainWindow *main, QWidget *parent) : VisualNavbar::VisualNavbar(MainWindow *main, QWidget *parent) :
QToolBar(main), QToolBar(main),
codeGraphic(new QGraphicsView), graphicsView(new QGraphicsView),
main(main) main(main)
{ {
Q_UNUSED(parent); Q_UNUSED(parent);
setObjectName("codeGraphics"); setObjectName("visualNavbar");
setWindowTitle(tr("Code bar")); setWindowTitle(tr("Visual navigation bar"));
// setMovable(false); // setMovable(false);
setContentsMargins(0, 0, 0, 0); setContentsMargins(0, 0, 0, 0);
// If line below is used, with the dark theme the paintEvent is not called // If line below is used, with the dark theme the paintEvent is not called
// and the result is wrong. Something to do with overwriting the style sheet :/ // and the result is wrong. Something to do with overwriting the style sheet :/
//setStyleSheet("QToolBar { border: 0px; border-bottom: 0px; border-top: 0px; border-width: 0px;}"); //setStyleSheet("QToolBar { border: 0px; border-bottom: 0px; border-top: 0px; border-width: 0px;}");
this->codeGraphic->setAlignment(Qt::AlignLeft);
this->codeGraphic->setMinimumHeight(20);
this->codeGraphic->setMaximumHeight(20);
this->codeGraphic->setFrameShape(QFrame::NoFrame);
/* /*
QComboBox *addsCombo = new QComboBox(); QComboBox *addsCombo = new QComboBox();
addsCombo->addItem(""); addsCombo->addItem("");
addsCombo->addItem("Entry points"); addsCombo->addItem("Entry points");
addsCombo->addItem("Marks"); addsCombo->addItem("Marks");
*/ */
addWidget(this->codeGraphic); addWidget(this->graphicsView);
//addWidget(addsCombo); //addWidget(addsCombo);
connect(Core(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA))); connect(Core(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA)));
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fetchAndPaintData())); connect(Core(), SIGNAL(refreshAll()), this, SLOT(fetchAndPaintData()));
graphicsScene = new QGraphicsScene(this); graphicsScene = new QGraphicsScene(this);
const QBrush bg = QBrush(QColor(74, 74, 74));
graphicsScene->setBackgroundBrush(bg);
this->graphicsView->setAlignment(Qt::AlignLeft);
this->graphicsView->setMinimumHeight(20);
this->graphicsView->setMaximumHeight(20);
this->graphicsView->setFrameShape(QFrame::NoFrame);
this->graphicsView->setRenderHints(0);
this->graphicsView->setScene(graphicsScene);
this->graphicsView->setRenderHints(QPainter::Antialiasing);
this->graphicsView->setToolTip("gap");
} }
void GraphicsBar::paintEvent(QPaintEvent *event) void VisualNavbar::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);
QPainter painter(this); QPainter painter(this);
if(previousWidth != this->codeGraphic->width()) if(previousWidth != this->width())
{ {
this->fillData(); this->fillData();
previousWidth = this->codeGraphic->width(); previousWidth = this->width();
} }
} }
void GraphicsBar::fetchAndPaintData() void VisualNavbar::fetchAndPaintData()
{ {
fetchData(); fetchData();
fillData(); fillData();
} }
void GraphicsBar::fetchData() void VisualNavbar::fetchData()
{ {
TempConfig tempConfig; TempConfig tempConfig;
tempConfig.set("search.in", QString("io.section")); tempConfig.set("search.in", QString("io.section"));
@ -84,26 +92,19 @@ void GraphicsBar::fetchData()
} }
} }
void GraphicsBar::fillData() void VisualNavbar::fillData()
{ {
qDeleteAll(graphicsScene->items()); // qDeleteAll(graphicsScene->items());
graphicsScene->clear();
cursorGraphicsItem = nullptr; cursorGraphicsItem = nullptr;
int from = blockMaps.first()["from"].toInt(); int from = blockMaps.first()["from"].toInt();
int to = blockMaps.first()["to"].toInt(); int to = blockMaps.first()["to"].toInt();
// Prepare the graph scene // Prepare the graph scene
int w = this->codeGraphic->width(); int w = this->graphicsView->width();
int h = this->codeGraphic->height(); int h = this->graphicsView->height();
const QBrush bg = QBrush(QColor(74, 74, 74));
graphicsScene->setBackgroundBrush(bg);
this->codeGraphic->setRenderHints(0);
this->codeGraphic->setScene(graphicsScene);
this->codeGraphic->setRenderHints(QPainter::Antialiasing);
this->codeGraphic->setToolTip("gap");
RVA current_address = Core()->getOffset(); RVA current_address = Core()->getOffset();
@ -195,11 +196,14 @@ void GraphicsBar::fillData()
} }
x_start = x_end; x_start = x_end;
} }
// Update scene width
graphicsScene->setSceneRect(graphicsScene->itemsBoundingRect());
// Draw cursor
drawCursor(); drawCursor();
} }
void GraphicsBar::drawCursor() void VisualNavbar::drawCursor()
{ {
RVA offset = Core()->getOffset(); RVA offset = Core()->getOffset();
double cursor_x = addressToLocalX(offset); double cursor_x = addressToLocalX(offset);
@ -207,19 +211,20 @@ void GraphicsBar::drawCursor()
{ {
graphicsScene->removeItem(cursorGraphicsItem); graphicsScene->removeItem(cursorGraphicsItem);
delete cursorGraphicsItem; delete cursorGraphicsItem;
cursorGraphicsItem = nullptr;
} }
if (cursor_x == nan("")) if (isnan(cursor_x))
{ {
return; return;
} }
int h = this->codeGraphic->height(); int h = this->graphicsView->height();
cursorGraphicsItem = new QGraphicsRectItem(cursor_x, 0, 2, h); cursorGraphicsItem = new QGraphicsRectItem(cursor_x, 0, 2, h);
cursorGraphicsItem->setPen(Qt::NoPen); cursorGraphicsItem->setPen(Qt::NoPen);
cursorGraphicsItem->setBrush(QBrush(QColor(255, 0, 0))); cursorGraphicsItem->setBrush(QBrush(QColor(255, 0, 0)));
graphicsScene->addItem(cursorGraphicsItem); graphicsScene->addItem(cursorGraphicsItem);
} }
QString GraphicsBar::generateTooltip(QString section_name, QMap<QString, QVariant> map) QString VisualNavbar::generateTooltip(QString section_name, QMap<QString, QVariant> map)
{ {
QString ret = ""; QString ret = "";
ret += "Offset: 0x" + QString::number(map["offset"].toInt(), 16) + "\n"; ret += "Offset: 0x" + QString::number(map["offset"].toInt(), 16) + "\n";
@ -235,26 +240,26 @@ QString GraphicsBar::generateTooltip(QString section_name, QMap<QString, QVarian
return ret; return ret;
} }
void GraphicsBar::on_seekChanged(RVA addr) void VisualNavbar::on_seekChanged(RVA addr)
{ {
Q_UNUSED(addr); Q_UNUSED(addr);
// Re-paint, which will also update the cursor. // Update cursor
this->drawCursor(); this->drawCursor();
} }
void GraphicsBar::mousePressEvent(QMouseEvent *event) void VisualNavbar::mousePressEvent(QMouseEvent *event)
{ {
event->accept(); event->accept();
// Convert the local X coordinate to an address. // Convert the local X coordinate to an address.
qreal x = event->localPos().x(); qreal x = event->localPos().x();
RVA address = localXToAddress(x); RVA address = localXToAddress(x);
if(address != 0) if(address != RVA_INVALID)
{ {
Core()->seek(address); Core()->seek(address);
} }
} }
RVA GraphicsBar::localXToAddress(double x) RVA VisualNavbar::localXToAddress(double x)
{ {
for(auto x2a : xToAddress) for(auto x2a : xToAddress)
{ {
@ -265,10 +270,10 @@ RVA GraphicsBar::localXToAddress(double x)
return x2a.address_from + (offset * size); return x2a.address_from + (offset * size);
} }
} }
return 0; return RVA_INVALID;
} }
double GraphicsBar::addressToLocalX(RVA address) double VisualNavbar::addressToLocalX(RVA address)
{ {
for(auto x2a : xToAddress) for(auto x2a : xToAddress)
{ {

View File

@ -1,5 +1,5 @@
#ifndef GRAPHICSBAR_H #ifndef VISUALNAVBAR_H
#define GRAPHICSBAR_H #define VISUALNAVBAR_H
#include <QToolBar> #include <QToolBar>
#include <QGraphicsScene> #include <QGraphicsScene>
@ -9,7 +9,7 @@
class MainWindow; class MainWindow;
class QGraphicsView; class QGraphicsView;
class GraphicsBar : public QToolBar class VisualNavbar : public QToolBar
{ {
Q_OBJECT Q_OBJECT
@ -21,7 +21,7 @@ class GraphicsBar : public QToolBar
}; };
public: public:
explicit GraphicsBar(MainWindow *main, QWidget *parent = 0); explicit VisualNavbar(MainWindow *main, QWidget *parent = 0);
public slots: public slots:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
@ -34,7 +34,7 @@ private slots:
void on_seekChanged(RVA addr); void on_seekChanged(RVA addr);
private: private:
QGraphicsView *codeGraphic; QGraphicsView *graphicsView;
QGraphicsScene *graphicsScene; QGraphicsScene *graphicsScene;
QGraphicsRectItem *cursorGraphicsItem; QGraphicsRectItem *cursorGraphicsItem;
MainWindow *main; MainWindow *main;
@ -54,4 +54,4 @@ private:
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
}; };
#endif // GRAPHICSBAR_H #endif // VISUALNAVBAR_H