1
0
Fork 0

GUI: RichTextPainter now supports variable-width fonts.

This commit is contained in:
mrexodia 2016-06-04 13:01:48 +02:00
parent 36decf84e3
commit 022fc72579
No known key found for this signature in database
GPG Key ID: D72F9A4FAA0073B4
8 changed files with 16 additions and 14 deletions

View File

@ -424,7 +424,7 @@ QString Disassembly::paintContent(QPainter* painter, dsint rowBase, int rowOffse
curByte.textColor = mBytesColor;
richBytes.push_back(curByte);
}
RichTextPainter::paintRichText(painter, x, y, getColumnWidth(col), getRowHeight(), jumpsize + funcsize, richBytes, getCharWidth());
RichTextPainter::paintRichText(painter, x, y, getColumnWidth(col), getRowHeight(), jumpsize + funcsize, richBytes, font());
}
break;
@ -467,7 +467,7 @@ QString Disassembly::paintContent(QPainter* painter, dsint rowBase, int rowOffse
else
CapstoneTokenizer::TokenToRichText(token, richText, 0);
int xinc = 4;
RichTextPainter::paintRichText(painter, x + loopsize, y, getColumnWidth(col) - loopsize, getRowHeight(), xinc, richText, getCharWidth());
RichTextPainter::paintRichText(painter, x + loopsize, y, getColumnWidth(col) - loopsize, getRowHeight(), xinc, richText, font());
token.x = x + loopsize + xinc;
}
break;
@ -648,7 +648,7 @@ void Disassembly::mousePressEvent(QMouseEvent* event)
if(rowOffset < mInstBuffer.size())
{
CapstoneTokenizer::SingleToken token;
if(CapstoneTokenizer::TokenFromX(mInstBuffer.at(rowOffset).tokens, token, event->x(), getCharWidth()))
if(CapstoneTokenizer::TokenFromX(mInstBuffer.at(rowOffset).tokens, token, event->x(), font()))
{
if(CapstoneTokenizer::IsHighlightableToken(token) && !CapstoneTokenizer::TokenEquals(&token, &mHighlightToken))
mHighlightToken = token;

View File

@ -465,7 +465,7 @@ QString HexDump::paintContent(QPainter* painter, dsint rowBase, int rowOffset, i
RichTextPainter::List richText;
getColumnRichText(col, wRva, richText);
RichTextPainter::paintRichText(painter, x, y, w, h, 4, richText, getCharWidth());
RichTextPainter::paintRichText(painter, x, y, w, h, 4, richText, font());
return "";
}

View File

@ -100,7 +100,7 @@ QString SearchListViewTable::paintContent(QPainter* painter, dsint rowBase, int
}
//paint the rich text
RichTextPainter::paintRichText(painter, x + 1, y, w, h, 4, richText, getCharWidth());
RichTextPainter::paintRichText(painter, x + 1, y, w, h, 4, richText, font());
text = "";
}
return text;

View File

@ -171,15 +171,16 @@ void CapstoneTokenizer::TokenToRichText(const InstructionToken & instr, RichText
}
}
bool CapstoneTokenizer::TokenFromX(const InstructionToken & instr, SingleToken & token, int x, int charwidth)
bool CapstoneTokenizer::TokenFromX(const InstructionToken & instr, SingleToken & token, int x, const QFont & font)
{
if(x < instr.x) //before the first token
return false;
int len = int(instr.tokens.size());
QFontMetrics metrics(font);
for(int i = 0, xStart = instr.x; i < len; i++)
{
const auto & curToken = instr.tokens.at(i);
int curWidth = int(curToken.text.length()) * charwidth;
int curWidth = metrics.width(curToken.text);
int xEnd = xStart + curWidth;
if(x >= xStart && x < xEnd)
{

View File

@ -145,7 +145,7 @@ public:
static void UpdateColors();
static void UpdateStringPool();
static void TokenToRichText(const InstructionToken & instr, RichTextPainter::List & richTextList, const SingleToken* highlightToken);
static bool TokenFromX(const InstructionToken & instr, SingleToken & token, int x, int charwidth);
static bool TokenFromX(const InstructionToken & instr, SingleToken & token, int x, const QFont & font);
static bool IsHighlightableToken(const SingleToken & token);
static bool TokenEquals(const SingleToken* a, const SingleToken* b, bool ignoreSize = true);

View File

@ -260,7 +260,7 @@ QString ScriptView::paintContent(QPainter* painter, dsint rowBase, int rowOffset
}
//paint the rich text
RichTextPainter::paintRichText(painter, x + 1, y, w, h, xadd, richText, charwidth);
RichTextPainter::paintRichText(painter, x + 1, y, w, h, xadd, richText, font());
returnString = "";
}
else //no syntax highlighting

View File

@ -1,16 +1,17 @@
#include "RichTextPainter.h"
//TODO: fix performance
void RichTextPainter::paintRichText(QPainter* painter, int x, int y, int w, int h, int xinc, const List & richText, int charwidth)
void RichTextPainter::paintRichText(QPainter* painter, int x, int y, int w, int h, int xinc, const List & richText, const QFont & font)
{
QPen pen;
QPen highlightPen;
highlightPen.setWidth(2);
QBrush brush(Qt::cyan);
QFontMetrics metrics(font);
for(const auto & curRichText : richText)
{
int curRichTextLength = curRichText.text.length();
int backgroundWidth = charwidth * curRichTextLength;
int textWidth = metrics.width(curRichText.text);
int backgroundWidth = textWidth;
if(backgroundWidth + xinc > w)
backgroundWidth = w - xinc;
if(backgroundWidth <= 0) //stop drawing when going outside the specified width
@ -47,6 +48,6 @@ void RichTextPainter::paintRichText(QPainter* painter, int x, int y, int w, int
painter->setPen(highlightPen);
painter->drawLine(x + xinc + 1, y + h - 1, x + xinc + backgroundWidth - 1, y + h - 1);
}
xinc += charwidth * curRichTextLength;
xinc += textWidth;
}
}

View File

@ -30,7 +30,7 @@ public:
typedef std::vector<CustomRichText_t> List;
//functions
static void paintRichText(QPainter* painter, int x, int y, int w, int h, int xinc, const List & richText, int charwidth);
static void paintRichText(QPainter* painter, int x, int y, int w, int h, int xinc, const List & richText, const QFont & font);
};
#endif // RICHTEXTPAINTER_H