Skip to content

Commit

Permalink
Add data visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
spuriousdata committed Aug 27, 2016
1 parent a9aebcb commit 63ba753
Show file tree
Hide file tree
Showing 19 changed files with 430 additions and 79 deletions.
11 changes: 8 additions & 3 deletions src/BinGrok.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ SOURCES += \
txtdisplaywidget.cc \
structeditor.cc \
syntaxhighlighter.cc \
datahighlighter.cc \
structtypes.cc \
rd_parser.cc
rd_parser.cc \
datavisualizer.cc

HEADERS += \
bingrokwindow.h \
Expand All @@ -31,10 +33,13 @@ HEADERS += \
txtdisplaywidget.h \
structeditor.h \
syntaxhighlighter.h \
datahighlighter.h \
structtypes.h \
rd_parser.h
rd_parser.h \
datavisualizer.h

FORMS += \
bingrokwindow.ui \
preferences.ui \
structeditor.ui
structeditor.ui \
datavisualizer.ui
25 changes: 17 additions & 8 deletions src/bingrokwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void BinGrokWindow::read_settings()
s.beginGroup("window");
resize(s.value("size", QSize(400,400)).toSize());
move(s.value("position", QPoint(200,200)).toPoint());
max_recently_open = s.value("max_recently_open", 8).toInt();
max_recently_open = static_cast<quint8>(s.value("max_recently_open", 8).toUInt());

recently_open = s.value("recently_open").toStringList();

Expand All @@ -88,19 +88,20 @@ void BinGrokWindow::read_settings()
void BinGrokWindow::open()
{
QString filename = QFileDialog::getOpenFileName(this);
if (!filename.isEmpty())
hexwidget->open(filename);
if (!filename.isEmpty())
open_file(filename);
}

void BinGrokWindow::open_file(const QString &f)
{
has_open_file = true;
hexwidget->open(f);
}

void BinGrokWindow::open_recent()
{
QAction *a = qobject_cast<QAction *>(sender());
if (a) hexwidget->open(a->data().toString());
if (a) open_file(a->data().toString());
}

void BinGrokWindow::new_file()
Expand All @@ -115,7 +116,7 @@ void BinGrokWindow::save()
qDebug("save() called");
#endif
/*
if bghexwidget contents is empty:
if bghexwidget contents is empty:test
return saveAs();
else
return save_file(current file);
Expand Down Expand Up @@ -167,10 +168,18 @@ void BinGrokWindow::show_preferences()

void BinGrokWindow::show_struct_editor()
{
if (structeditor_ui == NULL) {
structeditor_ui = new StructEditor(this);
if (!has_open_file) {
if (error_window == nullptr)
error_window = new QMessageBox();
error_window->setWindowTitle("Error");
error_window->setText("Can't edit datastructure when there is no file open!");
error_window->show();
} else {
if (structeditor_ui == NULL) {
structeditor_ui = new StructEditor(this);
}
structeditor_ui->show();
}
structeditor_ui->show();
}

void BinGrokWindow::save_preferences()
Expand Down
3 changes: 3 additions & 0 deletions src/bingrokwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <QMainWindow>
#include <QStringList>
#include <QMessageBox>
#include "hexwidget.h"

class QString;
Expand Down Expand Up @@ -39,6 +40,8 @@ class BinGrokWindow : public QMainWindow
QHBoxLayout *layout;
quint8 max_recently_open;
QStringList recently_open;
bool has_open_file = false;
QMessageBox *error_window = nullptr;

void read_settings();
bool save_file(const QString & filename);
Expand Down
53 changes: 53 additions & 0 deletions src/datahighlighter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "datahighlighter.h"

#include <QSyntaxHighlighter>
#include <QFont>
#include <QStringList>
#include <QString>

#define BOUNDARY(s) ("\\b" s "\\b")

DataHighlighter::DataHighlighter(QTextDocument *parent) :
QSyntaxHighlighter (parent)
{

QTextCharFormat string_format, ident_format, integer_format;

string_format.setForeground(Qt::darkBlue);

highlighting_rules.append({
QRegExp("\".*\";"),
string_format
});

ident_format.setForeground(Qt::blue);
ident_format.setFontItalic(true);

highlighting_rules.append({
QRegExp(".+(?=:)"),
ident_format
});

integer_format.setForeground(Qt::black);
integer_format.setFontWeight(QFont::Bold);
highlighting_rules.append({
QRegExp("\\d+"),
integer_format
});

}

void DataHighlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlighting_rules) {
QRegExp expression(rule.pattern);
int index = expression.indexIn(text);
while (index >= 0) {
int length = expression.matchedLength();
setFormat(index, length, rule.format);
index = expression.indexIn(text, index + length);
}
}

setCurrentBlockState(0);
}
32 changes: 32 additions & 0 deletions src/datahighlighter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef DATAHIGHLIGHTER_H
#define DATAHIGHLIGHTER_H

#include <QSyntaxHighlighter>
#include <QVector>
#include <QRegExp>
#include <QTextCharFormat>
#include <QString>
#include <QTextDocument>


class DataHighlighter : public QSyntaxHighlighter
{
Q_OBJECT

public:
DataHighlighter(QTextDocument *parent=0);

protected:
void highlightBlock(const QString &text) Q_DECL_OVERRIDE;

private:
struct HighlightingRule
{
QRegExp pattern;
QTextCharFormat format;
};

QVector<HighlightingRule> highlighting_rules;
};

#endif // DATAHIGHLIGHTER_H
25 changes: 25 additions & 0 deletions src/datavisualizer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "datavisualizer.h"
#include "ui_datavisualizer.h"

DataVisualizer::DataVisualizer(QWidget *parent) :
QDialog(parent),
ui(new Ui::DataVisualizer)
{
ui->setupUi(this);
ui->status_panel->setPlainText("test");
highlighter = new DataHighlighter(ui->text_display->document());
connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(close()));
}

DataVisualizer::~DataVisualizer()
{
delete ui;
}

void DataVisualizer::visualize(Struct *s)
{
if (current_data != nullptr)
delete current_data;
current_data = s;
ui->text_display->setText(s->to_string());
}
27 changes: 27 additions & 0 deletions src/datavisualizer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef DATAVISUALIZER_H
#define DATAVISUALIZER_H

#include <QDialog>
#include "structtypes.h"
#include "datahighlighter.h"

namespace Ui {
class DataVisualizer;
}

class DataVisualizer : public QDialog
{
Q_OBJECT

public:
explicit DataVisualizer(QWidget *parent = 0);
~DataVisualizer();
void visualize(Struct *s);

private:
Ui::DataVisualizer *ui;
Struct *current_data = nullptr;
DataHighlighter *highlighter;
};

#endif // DATAVISUALIZER_H
84 changes: 84 additions & 0 deletions src/datavisualizer.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DataVisualizer</class>
<widget class="QDialog" name="DataVisualizer">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>584</width>
<height>372</height>
</rect>
</property>
<property name="windowTitle">
<string>BinGrok :: Data Visualizer</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTextEdit" name="text_display">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>3</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="right_panel" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item alignment="Qt::AlignRight">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="status_panel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="focusPolicy">
<enum>Qt::NoFocus</enum>
</property>
<property name="contextMenuPolicy">
<enum>Qt::NoContextMenu</enum>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Loading

0 comments on commit 63ba753

Please sign in to comment.