mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
refactor: replace TextFormatter class with function
Brief list of changes: - removed TextFormatter class and its unnecessary instantiation; - added single method for applying markdown.
This commit is contained in:
parent
e9209b06f9
commit
1ac21c07db
|
@ -61,8 +61,7 @@ ChatMessage::Ptr ChatMessage::createChatMessage(const QString& sender, const QSt
|
||||||
// text styling
|
// text styling
|
||||||
Settings::StyleType styleType = Settings::getInstance().getStylePreference();
|
Settings::StyleType styleType = Settings::getInstance().getStylePreference();
|
||||||
if (styleType != Settings::StyleType::NONE) {
|
if (styleType != Settings::StyleType::NONE) {
|
||||||
TextFormatter tf = TextFormatter(text);
|
text = applyMarkdown(text, styleType == Settings::StyleType::WITH_CHARS);
|
||||||
text = tf.applyStyling(styleType == Settings::StyleType::WITH_CHARS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -120,17 +120,6 @@ QString highlightURL(const QString& message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// clang-format on
|
// clang-format on
|
||||||
/**
|
|
||||||
* @class TextFormatter
|
|
||||||
*
|
|
||||||
* @brief This class applies formatting to the text messages, e.g. font styling and URL highlighting
|
|
||||||
*/
|
|
||||||
|
|
||||||
TextFormatter::TextFormatter(const QString& str)
|
|
||||||
: message(str)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Counts equal symbols at the beginning of the string
|
* @brief Counts equal symbols at the beginning of the string
|
||||||
* @param str Source string
|
* @param str Source string
|
||||||
|
@ -168,13 +157,16 @@ static bool isTagIntersection(const QString& str)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Applies styles to the font of text that was passed to the constructor
|
* @brief Applies styles to the font of text that was passed to the constructor
|
||||||
|
* @param message Formatting string
|
||||||
* @param showFormattingSymbols True, if it is supposed to include formatting symbols into resulting
|
* @param showFormattingSymbols True, if it is supposed to include formatting symbols into resulting
|
||||||
* string
|
* string
|
||||||
|
* @return Copy of message with markdown applied
|
||||||
*/
|
*/
|
||||||
void TextFormatter::applyHtmlFontStyling(bool showFormattingSymbols)
|
QString applyMarkdown(const QString& message, bool showFormattingSymbols)
|
||||||
{
|
{
|
||||||
|
QString result = message;
|
||||||
for (QPair<QRegularExpression, QString> pair : textPatternStyle) {
|
for (QPair<QRegularExpression, QString> pair : textPatternStyle) {
|
||||||
QRegularExpressionMatchIterator matchesIterator = pair.first.globalMatch(message);
|
QRegularExpressionMatchIterator matchesIterator = pair.first.globalMatch(result);
|
||||||
int insertedTagSymbolsCount = 0;
|
int insertedTagSymbolsCount = 0;
|
||||||
|
|
||||||
while (matchesIterator.hasNext()) {
|
while (matchesIterator.hasNext()) {
|
||||||
|
@ -186,28 +178,17 @@ void TextFormatter::applyHtmlFontStyling(bool showFormattingSymbols)
|
||||||
int capturedStart = match.capturedStart() + insertedTagSymbolsCount;
|
int capturedStart = match.capturedStart() + insertedTagSymbolsCount;
|
||||||
int capturedLength = match.capturedLength();
|
int capturedLength = match.capturedLength();
|
||||||
|
|
||||||
QString stylingText = message.mid(capturedStart, capturedLength);
|
QString stylingText = result.mid(capturedStart, capturedLength);
|
||||||
int choppingSignsCount = showFormattingSymbols ? 0 : patternSignsCount(stylingText);
|
int choppingSignsCount = showFormattingSymbols ? 0 : patternSignsCount(stylingText);
|
||||||
int textStart = capturedStart + choppingSignsCount;
|
int textStart = capturedStart + choppingSignsCount;
|
||||||
int textLength = capturedLength - 2 * choppingSignsCount;
|
int textLength = capturedLength - 2 * choppingSignsCount;
|
||||||
|
|
||||||
QString styledText = pair.second.arg(message.mid(textStart, textLength));
|
QString styledText = pair.second.arg(result.mid(textStart, textLength));
|
||||||
|
|
||||||
message.replace(capturedStart, capturedLength, styledText);
|
result.replace(capturedStart, capturedLength, styledText);
|
||||||
// Subtracting length of "%1"
|
// Subtracting length of "%1"
|
||||||
insertedTagSymbolsCount += pair.second.length() - 2 - 2 * choppingSignsCount;
|
insertedTagSymbolsCount += pair.second.length() - 2 - 2 * choppingSignsCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return result;
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Applies all styling for the text
|
|
||||||
* @param showFormattingSymbols True, if it is supposed to include formatting symbols into resulting
|
|
||||||
* string
|
|
||||||
* @return Styled string
|
|
||||||
*/
|
|
||||||
QString TextFormatter::applyStyling(bool showFormattingSymbols)
|
|
||||||
{
|
|
||||||
applyHtmlFontStyling(showFormattingSymbols);
|
|
||||||
return message;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,19 +24,6 @@
|
||||||
|
|
||||||
QString highlightURL(const QString& message);
|
QString highlightURL(const QString& message);
|
||||||
|
|
||||||
class TextFormatter
|
QString applyMarkdown(const QString& message, bool showFormattingSymbols);
|
||||||
{
|
|
||||||
private:
|
|
||||||
QString message;
|
|
||||||
|
|
||||||
void wrapUrl();
|
|
||||||
|
|
||||||
void applyHtmlFontStyling(bool showFormattingSymbols);
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit TextFormatter(const QString& str);
|
|
||||||
|
|
||||||
QString applyStyling(bool showFormattingSymbols);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // TEXTFORMATTER_H
|
#endif // TEXTFORMATTER_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user