2017-12-08 15:00:52 +00:00
|
|
|
#include "VisualNavbar.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-10-01 19:09:42 +00:00
|
|
|
#include "MainWindow.h"
|
2017-12-07 23:41:15 +00:00
|
|
|
#include "utils/TempConfig.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-04-13 15:14:02 +00:00
|
|
|
#include <QGraphicsView>
|
2017-03-29 10:18:37 +00:00
|
|
|
#include <QComboBox>
|
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QGraphicsRectItem>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonParseError>
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
VisualNavbar::VisualNavbar(MainWindow *main, QWidget *parent) :
|
2017-10-01 18:08:12 +00:00
|
|
|
QToolBar(main),
|
2017-12-08 15:00:52 +00:00
|
|
|
graphicsView(new QGraphicsView),
|
2017-10-01 18:08:12 +00:00
|
|
|
main(main)
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2017-10-01 18:08:12 +00:00
|
|
|
Q_UNUSED(parent);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
setObjectName("visualNavbar");
|
|
|
|
setWindowTitle(tr("Visual navigation bar"));
|
2017-04-09 19:55:06 +00:00
|
|
|
// setMovable(false);
|
2017-03-29 10:18:37 +00:00
|
|
|
setContentsMargins(0, 0, 0, 0);
|
2017-04-07 15:34:24 +00:00
|
|
|
// 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;}");
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
QComboBox *addsCombo = new QComboBox();
|
|
|
|
addsCombo->addItem("");
|
|
|
|
addsCombo->addItem("Entry points");
|
|
|
|
addsCombo->addItem("Marks");
|
|
|
|
*/
|
2017-12-08 15:00:52 +00:00
|
|
|
addWidget(this->graphicsView);
|
2017-03-29 10:18:37 +00:00
|
|
|
//addWidget(addsCombo);
|
2017-11-19 12:56:10 +00:00
|
|
|
|
2017-12-07 23:41:15 +00:00
|
|
|
connect(Core(), SIGNAL(seekChanged(RVA)), this, SLOT(on_seekChanged(RVA)));
|
|
|
|
connect(Core(), SIGNAL(refreshAll()), this, SLOT(fetchAndPaintData()));
|
2017-12-08 09:55:47 +00:00
|
|
|
|
|
|
|
graphicsScene = new QGraphicsScene(this);
|
2017-12-08 15:00:52 +00:00
|
|
|
|
|
|
|
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");
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::paintEvent(QPaintEvent *event)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-10-01 18:08:12 +00:00
|
|
|
Q_UNUSED(event);
|
2017-04-09 17:09:35 +00:00
|
|
|
|
|
|
|
QPainter painter(this);
|
2017-12-08 15:00:52 +00:00
|
|
|
if(previousWidth != this->width())
|
2017-12-08 09:55:47 +00:00
|
|
|
{
|
|
|
|
this->fillData();
|
2017-12-08 15:00:52 +00:00
|
|
|
previousWidth = this->width();
|
2017-12-08 09:55:47 +00:00
|
|
|
}
|
2017-04-09 17:09:35 +00:00
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::fetchAndPaintData()
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
|
|
|
fetchData();
|
|
|
|
fillData();
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::fetchData()
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
|
|
|
TempConfig tempConfig;
|
|
|
|
tempConfig.set("search.in", QString("io.section"));
|
|
|
|
sections = Core()->getAllSections();
|
|
|
|
|
|
|
|
totalSectionsSize = 0;
|
|
|
|
blockMaps.clear();
|
|
|
|
for(SectionDescription section : sections)
|
|
|
|
{
|
|
|
|
QString command = "p-j @" + RAddressString(section.vaddr);
|
|
|
|
QJsonDocument doc = Core()->cmdj(command);
|
|
|
|
QJsonObject jObject = doc.object();
|
|
|
|
QVariantMap mainMap = jObject.toVariantMap();
|
|
|
|
blockMaps.append(mainMap);
|
|
|
|
totalSectionsSize += section.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::fillData()
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-12-08 15:00:52 +00:00
|
|
|
// qDeleteAll(graphicsScene->items());
|
|
|
|
graphicsScene->clear();
|
2017-12-08 12:21:24 +00:00
|
|
|
cursorGraphicsItem = nullptr;
|
2017-12-07 23:41:15 +00:00
|
|
|
int from = blockMaps.first()["from"].toInt();
|
|
|
|
int to = blockMaps.first()["to"].toInt();
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
// Prepare the graph scene
|
2017-12-08 15:00:52 +00:00
|
|
|
int w = this->graphicsView->width();
|
|
|
|
int h = this->graphicsView->height();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
|
|
|
|
2017-11-19 12:56:10 +00:00
|
|
|
|
2017-12-07 23:41:15 +00:00
|
|
|
RVA current_address = Core()->getOffset();
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-12-07 23:41:15 +00:00
|
|
|
double width_per_byte = (double)w/(double)totalSectionsSize;
|
|
|
|
xToAddress.clear();
|
|
|
|
double x_start = 0.0;
|
2017-11-19 12:56:10 +00:00
|
|
|
|
2017-12-07 23:41:15 +00:00
|
|
|
for(int i=0; i < sections.length(); i++)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-12-07 23:41:15 +00:00
|
|
|
SectionDescription section = sections[i];
|
|
|
|
double width = ((double)section.size * width_per_byte);
|
|
|
|
double x_end = x_start + width;
|
|
|
|
double local_w = x_end - x_start;
|
|
|
|
|
|
|
|
QVariantMap mainMap = blockMaps[i];
|
|
|
|
from = mainMap["from"].toInt();
|
|
|
|
to = mainMap["to"].toInt();
|
|
|
|
int block = mainMap["blocksize"].toInt();
|
|
|
|
int size = (to - from);
|
|
|
|
int num = 1;
|
|
|
|
if (block != 0)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-12-07 23:41:15 +00:00
|
|
|
num = size / block;
|
2017-04-03 08:23:21 +00:00
|
|
|
}
|
2017-12-07 23:41:15 +00:00
|
|
|
|
|
|
|
if (num < 1)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-12-07 23:41:15 +00:00
|
|
|
num = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int graph_block = local_w / num;
|
2017-12-08 09:55:47 +00:00
|
|
|
if(graph_block == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2017-12-07 23:41:15 +00:00
|
|
|
int counter = 0;
|
|
|
|
|
|
|
|
for (auto j : mainMap["blocks"].toList())
|
|
|
|
{
|
|
|
|
QMap<QString, QVariant> map = j.toMap();
|
|
|
|
|
|
|
|
// The X of where this block will start
|
|
|
|
double block_x_start = x_start + (double)(counter * graph_block);
|
|
|
|
|
|
|
|
// Keep X start and end as well as the start & end address in a list.
|
|
|
|
// This is used to convert address to an X position on the bar and vice versa.
|
|
|
|
struct xToAddress x2a;
|
|
|
|
x2a.x_start = block_x_start;
|
|
|
|
x2a.x_end = block_x_start + graph_block;
|
|
|
|
x2a.address_from = map["offset"].toULongLong();
|
|
|
|
x2a.address_to = map["offset"].toULongLong() + map["size"].toULongLong();
|
|
|
|
xToAddress.append(x2a);
|
|
|
|
|
|
|
|
if (map.empty())
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-12-07 23:41:15 +00:00
|
|
|
// Fill as empty
|
|
|
|
QGraphicsRectItem *rect = new QGraphicsRectItem(block_x_start, 0, graph_block, h);
|
|
|
|
rect->setPen(Qt::NoPen);
|
|
|
|
rect->setBrush(QBrush(QColor(252, 249, 190)));
|
|
|
|
rect->setToolTip("Data");
|
2017-12-08 09:55:47 +00:00
|
|
|
graphicsScene->addItem(rect);
|
2017-04-09 19:55:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-07 23:41:15 +00:00
|
|
|
// TODO: Make this compatible with theming
|
|
|
|
QGraphicsRectItem *rect = new QGraphicsRectItem(block_x_start, 0, graph_block, h);
|
|
|
|
rect->setPen(Qt::NoPen);
|
|
|
|
if (map["functions"].toInt() > 0)
|
|
|
|
{
|
|
|
|
rect->setBrush(QBrush(QColor(69, 104, 229)));
|
|
|
|
}
|
|
|
|
else if(map["symbols"].toInt() > 0)
|
|
|
|
{
|
|
|
|
rect->setBrush(QBrush(QColor(229, 150, 69)));
|
|
|
|
}
|
|
|
|
else if(map["strings"].toInt() > 0)
|
|
|
|
{
|
|
|
|
rect->setBrush(QBrush(QColor(104, 229, 69)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rect->setBrush(QBrush(QColor(190, 190, 190)));
|
|
|
|
}
|
|
|
|
rect->setToolTip(generateTooltip(section.name, map));
|
2017-12-08 09:55:47 +00:00
|
|
|
graphicsScene->addItem(rect);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
2017-12-07 23:41:15 +00:00
|
|
|
|
|
|
|
counter += 1;
|
|
|
|
}
|
|
|
|
x_start = x_end;
|
|
|
|
}
|
2017-12-08 15:00:52 +00:00
|
|
|
// Update scene width
|
|
|
|
graphicsScene->setSceneRect(graphicsScene->itemsBoundingRect());
|
2017-12-07 23:41:15 +00:00
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
// Draw cursor
|
2017-12-08 12:21:24 +00:00
|
|
|
drawCursor();
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::drawCursor()
|
2017-12-08 12:21:24 +00:00
|
|
|
{
|
|
|
|
RVA offset = Core()->getOffset();
|
|
|
|
double cursor_x = addressToLocalX(offset);
|
|
|
|
if (cursorGraphicsItem != nullptr)
|
|
|
|
{
|
|
|
|
graphicsScene->removeItem(cursorGraphicsItem);
|
|
|
|
delete cursorGraphicsItem;
|
2017-12-08 15:00:52 +00:00
|
|
|
cursorGraphicsItem = nullptr;
|
2017-12-08 12:21:24 +00:00
|
|
|
}
|
2017-12-08 15:00:52 +00:00
|
|
|
if (isnan(cursor_x))
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
2017-12-08 12:21:24 +00:00
|
|
|
return;
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
2017-12-08 15:00:52 +00:00
|
|
|
int h = this->graphicsView->height();
|
2017-12-08 12:21:24 +00:00
|
|
|
cursorGraphicsItem = new QGraphicsRectItem(cursor_x, 0, 2, h);
|
|
|
|
cursorGraphicsItem->setPen(Qt::NoPen);
|
|
|
|
cursorGraphicsItem->setBrush(QBrush(QColor(255, 0, 0)));
|
|
|
|
graphicsScene->addItem(cursorGraphicsItem);
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
QString VisualNavbar::generateTooltip(QString section_name, QMap<QString, QVariant> map)
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
|
|
|
QString ret = "";
|
|
|
|
ret += "Offset: 0x" + QString::number(map["offset"].toInt(), 16) + "\n";
|
|
|
|
ret += "Section: " + section_name + "\n";
|
|
|
|
ret += "Size: " + QString::number(map["size"].toInt()) + "\n";
|
|
|
|
ret += "Functions: " + QString::number(map["functions"].toInt()) + "\n";
|
|
|
|
ret += "Flags: " + QString::number(map["flags"].toInt()) + "\n";
|
|
|
|
ret += "Comments: " + QString::number(map["comments"].toInt()) + "\n";
|
|
|
|
ret += "Symbols: " + QString::number(map["symbols"].toInt()) + "\n";
|
|
|
|
ret += "Strings: " + QString::number(map["strings"].toInt()) + "\n";
|
|
|
|
ret += "rwx: " + map["rwx"].toString() + "\n";
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::on_seekChanged(RVA addr)
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(addr);
|
2017-12-08 15:00:52 +00:00
|
|
|
// Update cursor
|
2017-12-08 12:21:24 +00:00
|
|
|
this->drawCursor();
|
2017-12-07 23:41:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
void VisualNavbar::mousePressEvent(QMouseEvent *event)
|
2017-12-07 23:41:15 +00:00
|
|
|
{
|
|
|
|
event->accept();
|
|
|
|
// Convert the local X coordinate to an address.
|
|
|
|
qreal x = event->localPos().x();
|
2017-12-08 12:21:24 +00:00
|
|
|
RVA address = localXToAddress(x);
|
2017-12-08 15:00:52 +00:00
|
|
|
if(address != RVA_INVALID)
|
2017-12-08 12:21:24 +00:00
|
|
|
{
|
|
|
|
Core()->seek(address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
RVA VisualNavbar::localXToAddress(double x)
|
2017-12-08 12:21:24 +00:00
|
|
|
{
|
2017-12-07 23:41:15 +00:00
|
|
|
for(auto x2a : xToAddress)
|
|
|
|
{
|
|
|
|
if ((x2a.x_start <= x) && (x <= x2a.x_end))
|
|
|
|
{
|
|
|
|
double offset = (x - x2a.x_start) / (x2a.x_end - x2a.x_start);
|
|
|
|
double size = x2a.address_to - x2a.address_from;
|
2017-12-08 12:21:24 +00:00
|
|
|
return x2a.address_from + (offset * size);
|
|
|
|
}
|
|
|
|
}
|
2017-12-08 15:00:52 +00:00
|
|
|
return RVA_INVALID;
|
2017-12-08 12:21:24 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 15:00:52 +00:00
|
|
|
double VisualNavbar::addressToLocalX(RVA address)
|
2017-12-08 12:21:24 +00:00
|
|
|
{
|
|
|
|
for(auto x2a : xToAddress)
|
|
|
|
{
|
|
|
|
if ((x2a.address_from <= address) && (address < x2a.address_to))
|
|
|
|
{
|
|
|
|
double offset = (double)(address - x2a.address_from) / (double)(x2a.address_to - x2a.address_from);
|
|
|
|
double size = x2a.x_end - x2a.x_start;
|
|
|
|
return x2a.x_start + (offset * size);
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-08 12:21:24 +00:00
|
|
|
return nan("");
|
2017-03-29 10:18:37 +00:00
|
|
|
}
|