From f21cdfa5c0854f72fcfc8456e01c4792052ec369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Thu, 1 Feb 2018 15:51:03 +0100 Subject: [PATCH] Use r2 colors in graph --- src/utils/RichTextPainter.cpp | 45 +++++++++++++++++++++++++++ src/utils/RichTextPainter.h | 3 ++ src/widgets/DisassemblerGraphView.cpp | 9 +++--- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/src/utils/RichTextPainter.cpp b/src/utils/RichTextPainter.cpp index b5f152c1..75c78db6 100644 --- a/src/utils/RichTextPainter.cpp +++ b/src/utils/RichTextPainter.cpp @@ -2,6 +2,8 @@ #include "RichTextPainter.h" #include "CachedFontMetrics.h" #include +#include +#include //TODO: fix performance (possibly use QTextLayout?) void RichTextPainter::paintRichText(QPainter* painter, int x, int y, int w, int h, int xinc, const List & richText, CachedFontMetrics* fontMetrics) @@ -102,6 +104,49 @@ void RichTextPainter::htmlRichText(const List & richText, QString & textHtml, QS } } +RichTextPainter::List RichTextPainter::fromTextDocument(const QTextDocument &doc) +{ + List r; + + for (QTextBlock block = doc.begin(); block != doc.end(); block = block.next()) + { + for (QTextBlock::iterator it = block.begin(); it != block.end(); it++) + { + QTextFragment fragment = it.fragment(); + QTextCharFormat format = fragment.charFormat(); + + CustomRichText_t text; + text.text = fragment.text(); + text.textColor = format.foreground().color(); + text.textBackground = format.background().color(); + + bool hasForeground = format.hasProperty(QTextFormat::ForegroundBrush); + bool hasBackground = format.hasProperty(QTextFormat::BackgroundBrush); + + if (hasForeground && !hasBackground) + { + text.flags = FlagColor; + } + else if (!hasForeground && hasBackground) + { + text.flags = FlagBackground; + } + else if (hasForeground && hasBackground) + { + text.flags = FlagAll; + } + else + { + text.flags = FlagNone; + } + + r.push_back(text); + } + } + + return r; +} + RichTextPainter::List RichTextPainter::cropped(const RichTextPainter::List &richText, int maxCols, const QString &indicator, bool *croppedOut) { List r; diff --git a/src/utils/RichTextPainter.h b/src/utils/RichTextPainter.h index fc5c2480..dc55e6f6 100644 --- a/src/utils/RichTextPainter.h +++ b/src/utils/RichTextPainter.h @@ -3,6 +3,7 @@ #define RICHTEXTPAINTER_H #include +#include #include #include @@ -39,6 +40,8 @@ public: static void paintRichText(QPainter* painter, int x, int y, int w, int h, int xinc, const List & richText, CachedFontMetrics* fontMetrics); static void htmlRichText(const List & richText, QString & textHtml, QString & textPlain); + static List fromTextDocument(const QTextDocument &doc); + static List cropped(const List &richText, int maxCols, const QString &indicator = nullptr, bool *croppedOut = nullptr); }; diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index 2863ba1a..392024dd 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -112,8 +112,9 @@ void DisassemblerGraphView::refreshView() void DisassemblerGraphView::loadCurrentGraph() { TempConfig tempConfig; - tempConfig.set("scr.html", true); - QJsonDocument functionsDoc = Core()->cmdj("agJ@e:scr.html=true"); + tempConfig.set("scr.html", true) + .set("scr.color", true); + QJsonDocument functionsDoc = Core()->cmdj("agJ"); QJsonArray functions = functionsDoc.array(); disassembly_blocks.clear(); @@ -187,14 +188,14 @@ void DisassemblerGraphView::loadCurrentGraph() // Skip last byte, otherwise it will overlap with next instruction i.size -= 1; - RichTextPainter::List richText; QString disas; disas = op["text"].toString(); QTextDocument textDoc; textDoc.setHtml(disas); - Colors::colorizeAssembly(richText, textDoc.toPlainText(), 0); + RichTextPainter::List richText = RichTextPainter::fromTextDocument(textDoc); + //Colors::colorizeAssembly(richText, textDoc.toPlainText(), 0); bool cropped; int blockLength = Config()->getGraphBlockMaxChars() + Core()->getConfigb("asm.bytes") * 24 + Core()->getConfigb("asm.emu") * 10;