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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
#include "CodeGraphic.h"
#include "VisualNavbar.h"
#include "MainWindow.h"
#include "utils/TempConfig.h"
@ -12,60 +12,68 @@
#include <QJsonArray>
#include <QJsonParseError>
GraphicsBar::GraphicsBar(MainWindow *main, QWidget *parent) :
VisualNavbar::VisualNavbar(MainWindow *main, QWidget *parent) :
QToolBar(main),
codeGraphic(new QGraphicsView),
graphicsView(new QGraphicsView),
main(main)
{
Q_UNUSED(parent);
setObjectName("codeGraphics");
setWindowTitle(tr("Code bar"));
setObjectName("visualNavbar");
setWindowTitle(tr("Visual navigation bar"));
// setMovable(false);
setContentsMargins(0, 0, 0, 0);
// 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 :/
//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();
addsCombo->addItem("");
addsCombo->addItem("Entry points");
addsCombo->addItem("Marks");
*/
addWidget(this->codeGraphic);
addWidget(this->graphicsView);
//addWidget(addsCombo);
connect(Core(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA)));
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fetchAndPaintData()));
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);
QPainter painter(this);
if(previousWidth != this->codeGraphic->width())
if(previousWidth != this->width())
{
this->fillData();
previousWidth = this->codeGraphic->width();
previousWidth = this->width();
}
}
void GraphicsBar::fetchAndPaintData()
void VisualNavbar::fetchAndPaintData()
{
fetchData();
fillData();
}
void GraphicsBar::fetchData()
void VisualNavbar::fetchData()
{
TempConfig tempConfig;
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;
int from = blockMaps.first()["from"].toInt();
int to = blockMaps.first()["to"].toInt();
// Prepare the graph scene
int w = this->codeGraphic->width();
int h = this->codeGraphic->height();
int w = this->graphicsView->width();
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();
@ -195,11 +196,14 @@ void GraphicsBar::fillData()
}
x_start = x_end;
}
// Update scene width
graphicsScene->setSceneRect(graphicsScene->itemsBoundingRect());
// Draw cursor
drawCursor();
}
void GraphicsBar::drawCursor()
void VisualNavbar::drawCursor()
{
RVA offset = Core()->getOffset();
double cursor_x = addressToLocalX(offset);
@ -207,19 +211,20 @@ void GraphicsBar::drawCursor()
{
graphicsScene->removeItem(cursorGraphicsItem);
delete cursorGraphicsItem;
cursorGraphicsItem = nullptr;
}
if (cursor_x == nan(""))
if (isnan(cursor_x))
{
return;
}
int h = this->codeGraphic->height();
int h = this->graphicsView->height();
cursorGraphicsItem = new QGraphicsRectItem(cursor_x, 0, 2, h);
cursorGraphicsItem->setPen(Qt::NoPen);
cursorGraphicsItem->setBrush(QBrush(QColor(255, 0, 0)));
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 = "";
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;
}
void GraphicsBar::on_seekChanged(RVA addr)
void VisualNavbar::on_seekChanged(RVA addr)
{
Q_UNUSED(addr);
// Re-paint, which will also update the cursor.
// Update cursor
this->drawCursor();
}
void GraphicsBar::mousePressEvent(QMouseEvent *event)
void VisualNavbar::mousePressEvent(QMouseEvent *event)
{
event->accept();
// Convert the local X coordinate to an address.
qreal x = event->localPos().x();
RVA address = localXToAddress(x);
if(address != 0)
if(address != RVA_INVALID)
{
Core()->seek(address);
}
}
RVA GraphicsBar::localXToAddress(double x)
RVA VisualNavbar::localXToAddress(double x)
{
for(auto x2a : xToAddress)
{
@ -265,10 +270,10 @@ RVA GraphicsBar::localXToAddress(double x)
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)
{

View File

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