aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/outputformatter.h
diff options
context:
space:
mode:
authorChristian Kandeler <christian.kandeler@qt.io>2020-04-09 17:47:01 +0200
committerChristian Kandeler <christian.kandeler@qt.io>2020-04-14 09:46:34 +0000
commit0f163781888a6b3b39c705e5171db5ad0e766f6e (patch)
tree91bfe182c2a929f04f04652fee954bbc315ea06f /src/libs/utils/outputformatter.h
parentdeb0eaf7950e4aa4067af730367e61cbd732d178 (diff)
OutputFormatter: Do all formatting centrally
Instead of working directly on the text edit, the specialized OutputFormatter classes now simply ask the base class to do it for them. In practice, the request currently always is "turn this part of the text into a link", but the interface can be extended to other types of formatting, should that ever be required. This is a win/win situation: Derived classes no longer have to fiddle with QTextCursor & friends (nor do they have to call any base class functions), while the base class can make strong assumptions about what the derived class does to the text edit (i.e.: nothing). Change-Id: Icc4bc52d4001b0359247563e39a206fa274833d7 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/libs/utils/outputformatter.h')
-rw-r--r--src/libs/utils/outputformatter.h37
1 files changed, 27 insertions, 10 deletions
diff --git a/src/libs/utils/outputformatter.h b/src/libs/utils/outputformatter.h
index 0aaf880714..a08ecd6863 100644
--- a/src/libs/utils/outputformatter.h
+++ b/src/libs/utils/outputformatter.h
@@ -26,6 +26,7 @@
#pragma once
#include "utils_global.h"
+#include "optional.h"
#include "outputformat.h"
#include <QObject>
@@ -61,33 +62,49 @@ public:
virtual bool handleLink(const QString &href);
void clear();
void setBoldFontEnabled(bool enabled);
- static QTextCharFormat linkFormat(const QTextCharFormat &inputFormat, const QString &href);
// For unit testing only
void overrideTextCharFormat(const QTextCharFormat &fmt);
protected:
- enum class Status { Done, InProgress, NotHandled };
-
- void appendMessageDefault(const QString &text, OutputFormat format);
- void clearLastLine();
QTextCharFormat charFormat(OutputFormat format) const;
- QList<FormattedText> parseAnsi(const QString &text, const QTextCharFormat &format);
- QTextCursor &cursor() const;
+ static QTextCharFormat linkFormat(const QTextCharFormat &inputFormat, const QString &href);
+
+ enum class Status { Done, InProgress, NotHandled };
+ class LinkSpec {
+ public:
+ LinkSpec() = default;
+ LinkSpec(int sp, int l, const QString &t) : startPos(sp), length(l), target(t) {}
+ int startPos = -1;
+ int length = -1;
+ QString target;
+ };
+ using LinkSpecs = QList<LinkSpec>;
+ class Result {
+ public:
+ Result(Status s, const LinkSpecs &l = {}, const optional<QString> &c = {})
+ : status(s), linkSpecs(l), newContent(c) {}
+ Status status;
+ LinkSpecs linkSpecs;
+ optional<QString> newContent; // Hard content override. Only to be used in extreme cases.
+ };
private:
// text contains at most one line feed character, and if it does occur, it's the last character.
// Either way, the input is to be considered "complete" for formatting purposes.
void doAppendMessage(const QString &text, OutputFormat format);
- virtual Status handleMessage(const QString &text, OutputFormat format);
+ virtual Result handleMessage(const QString &text, OutputFormat format);
virtual void reset() {}
- void doAppendMessage(const QString &text, const QTextCharFormat &format);
void append(const QString &text, const QTextCharFormat &format);
void initFormats();
void flushIncompleteLine();
void dumpIncompleteLine(const QString &line, OutputFormat format);
+ void clearLastLine();
+ QList<FormattedText> parseAnsi(const QString &text, const QTextCharFormat &format);
+ const QList<Utils::FormattedText> linkifiedText(const QList<FormattedText> &text,
+ const LinkSpecs &linkSpecs);
Internal::OutputFormatterPrivate *d;
};
@@ -102,7 +119,7 @@ public:
bool handleLink(const QString &href) override;
private:
- Status handleMessage(const QString &text, OutputFormat format) override;
+ Result handleMessage(const QString &text, OutputFormat format) override;
class Private;
Private * const d;