From 46e862a3b87daf674313d3bfbc8957ad91ed5fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Tue, 19 Dec 2017 17:00:42 +0100 Subject: [PATCH] Crop Line Length in Graph --- src/utils/Configuration.h | 4 ++ src/utils/RichTextPainter.cpp | 55 +++++++++++++++++++++++++++ src/utils/RichTextPainter.h | 2 + src/widgets/DisassemblerGraphView.cpp | 2 +- 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/utils/Configuration.h b/src/utils/Configuration.h index 4f79f239..fa9b5639 100644 --- a/src/utils/Configuration.h +++ b/src/utils/Configuration.h @@ -38,6 +38,10 @@ public: void setDarkTheme(bool set); bool getDarkTheme() { return s.value("dark").toBool(); } + // Graph + int getGraphBlockMaxChars() const { return s.value("graph.maxcols", 50).toInt(); } + void setGraphBlockMaxChars(int ch) { s.setValue("graph.maxcols", ch); } + // TODO Imho it's wrong doing it this way. Should find something else. bool getAsmESIL() const { return s.value("asm.esil", false).toBool(); } void setAsmESIL(bool v) { s.setValue("asm.esil", v); } diff --git a/src/utils/RichTextPainter.cpp b/src/utils/RichTextPainter.cpp index f2a4aefa..120b1211 100644 --- a/src/utils/RichTextPainter.cpp +++ b/src/utils/RichTextPainter.cpp @@ -101,3 +101,58 @@ void RichTextPainter::htmlRichText(const List & richText, QString & textHtml, QS textPlain += curRichText.text; } } + +RichTextPainter::List RichTextPainter::cropped(const RichTextPainter::List &richText, int maxCols, const QString &indicator) +{ + List r; + r.reserve(richText.size()); + + int cols = 0; + bool cropped = false; + for(const auto &text : richText) + { + int textLength = text.text.size(); + if(cols + textLength <= maxCols) + { + r.push_back(text); + cols += textLength; + } + else if(cols == maxCols) + { + break; + } + else + { + CustomRichText_t croppedText = text; + croppedText.text.truncate(maxCols - cols); + r.push_back(croppedText); + cropped = true; + break; + } + } + + if(cropped && !indicator.isEmpty()) + { + int indicatorCropLength = indicator.length(); + if(indicatorCropLength > maxCols) + { + indicatorCropLength = maxCols; + } + + while(!r.empty()) + { + auto &text = r.back(); + + if(text.text.length() >= indicatorCropLength) + { + text.text.replace(text.text.length() - indicatorCropLength, indicatorCropLength, indicator); + break; + } + + indicatorCropLength -= text.text.length(); + r.pop_back(); + } + } + + return r; +} diff --git a/src/utils/RichTextPainter.h b/src/utils/RichTextPainter.h index 8c7b2dac..af9797ad 100644 --- a/src/utils/RichTextPainter.h +++ b/src/utils/RichTextPainter.h @@ -38,6 +38,8 @@ public: //functions 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 cropped(const List &richText, int maxCols, const QString &indicator = nullptr); }; #endif // RICHTEXTPAINTER_H diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index fca70233..3c72d1ea 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -165,7 +165,7 @@ void DisassemblerGraphView::loadCurrentGraph() comment.flags = RichTextPainter::FlagColor; richText.insert(richText.end(), comment); } - i.text = Text(richText); + i.text = Text(RichTextPainter::cropped(richText, Config()->getGraphBlockMaxChars(), "...")); db.instrs.push_back(i); } disassembly_blocks[db.entry] = db;