Utils: Dissolve the AggregatingOutputFormatter class

Inheritance is not the right design here (anymore), so we merge the
derived class into the base for now. We will later re-split in a more
sensible manner.

Change-Id: I326e9f02287b3070f47147c771f3fa908d51b7fb
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2020-04-14 12:18:50 +02:00
parent 37b079a19c
commit 70bddbcab4
3 changed files with 45 additions and 84 deletions

View File

@@ -48,6 +48,8 @@ public:
AnsiEscapeCodeHandler escapeCodeHandler;
QPair<QString, OutputFormat> incompleteLine;
optional<QTextCharFormat> formatOverride;
QList<OutputFormatter *> formatters;
OutputFormatter *nextFormatter = nullptr;
bool boldFontEnabled = true;
bool prependCarriageReturn = false;
};
@@ -77,6 +79,14 @@ void OutputFormatter::setPlainTextEdit(QPlainTextEdit *plainText)
initFormats();
}
void OutputFormatter::setFormatters(const QList<OutputFormatter *> &formatters)
{
for (OutputFormatter * const f : formatters)
f->setPlainTextEdit(plainTextEdit());
d->formatters = formatters;
d->nextFormatter = nullptr;
}
void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
{
const QTextCharFormat charFmt = charFormat(format);
@@ -94,8 +104,33 @@ void OutputFormatter::doAppendMessage(const QString &text, OutputFormat format)
OutputFormatter::Result OutputFormatter::handleMessage(const QString &text, OutputFormat format)
{
Q_UNUSED(text);
Q_UNUSED(format);
if (d->nextFormatter) {
const Result res = d->nextFormatter->handleMessage(text, format);
switch (res.status) {
case Status::Done:
d->nextFormatter = nullptr;
return res;
case Status::InProgress:
return res;
case Status::NotHandled:
QTC_CHECK(false); // TODO: This case will be legal after the merge
d->nextFormatter = nullptr;
return res;
}
}
QTC_CHECK(!d->nextFormatter);
for (OutputFormatter * const formatter : qAsConst(d->formatters)) {
const Result res = formatter->handleMessage(text, format);
switch (res.status) {
case Status::Done:
return res;
case Status::InProgress:
d->nextFormatter = formatter;
return res;
case Status::NotHandled:
break;
}
}
return Status::NotHandled;
}
@@ -233,7 +268,10 @@ void OutputFormatter::dumpIncompleteLine(const QString &line, OutputFormat forma
bool OutputFormatter::handleLink(const QString &href)
{
Q_UNUSED(href)
for (OutputFormatter * const f : qAsConst(d->formatters)) {
if (f->handleLink(href))
return true;
}
return false;
}
@@ -310,64 +348,4 @@ void OutputFormatter::appendMessage(const QString &text, OutputFormat format)
}
}
class AggregatingOutputFormatter::Private
{
public:
QList<OutputFormatter *> formatters;
OutputFormatter *nextFormatter = nullptr;
};
AggregatingOutputFormatter::AggregatingOutputFormatter() : d(new Private) {}
AggregatingOutputFormatter::~AggregatingOutputFormatter() { delete d; }
void AggregatingOutputFormatter::setFormatters(const QList<OutputFormatter *> &formatters)
{
for (OutputFormatter * const f : formatters)
f->setPlainTextEdit(plainTextEdit());
d->formatters = formatters;
d->nextFormatter = nullptr;
}
OutputFormatter::Result AggregatingOutputFormatter::handleMessage(const QString &text,
OutputFormat format)
{
if (d->nextFormatter) {
const Result res = d->nextFormatter->handleMessage(text, format);
switch (res.status) {
case Status::Done:
d->nextFormatter = nullptr;
return res;
case Status::InProgress:
return res;
case Status::NotHandled:
QTC_CHECK(false); // TODO: This case will be legal after the merge
d->nextFormatter = nullptr;
return res;
}
}
QTC_CHECK(!d->nextFormatter);
for (OutputFormatter * const formatter : qAsConst(d->formatters)) {
const Result res = formatter->handleMessage(text, format);
switch (res.status) {
case Status::Done:
return res;
case Status::InProgress:
d->nextFormatter = formatter;
return res;
case Status::NotHandled:
break;
}
}
return Status::NotHandled;
}
bool AggregatingOutputFormatter::handleLink(const QString &href)
{
for (OutputFormatter * const f : qAsConst(d->formatters)) {
if (f->handleLink(href))
return true;
}
return false;
}
} // namespace Utils

View File

@@ -43,11 +43,8 @@ class FormattedText;
namespace Internal { class OutputFormatterPrivate; }
class QTCREATOR_UTILS_EXPORT AggregatingOutputFormatter;
class QTCREATOR_UTILS_EXPORT OutputFormatter : public QObject
{
friend class AggregatingOutputFormatter;
public:
OutputFormatter();
~OutputFormatter() override;
@@ -55,6 +52,8 @@ public:
QPlainTextEdit *plainTextEdit() const;
void setPlainTextEdit(QPlainTextEdit *plainText);
void setFormatters(const QList<OutputFormatter *> &formatters);
void flush();
void appendMessage(const QString &text, OutputFormat format);
@@ -109,20 +108,4 @@ private:
Internal::OutputFormatterPrivate *d;
};
class QTCREATOR_UTILS_EXPORT AggregatingOutputFormatter : public OutputFormatter
{
public:
AggregatingOutputFormatter();
~AggregatingOutputFormatter();
void setFormatters(const QList<OutputFormatter *> &formatters);
bool handleLink(const QString &href) override;
private:
Result handleMessage(const QString &text, OutputFormat format) override;
class Private;
Private * const d;
};
} // namespace Utils

View File

@@ -66,7 +66,7 @@ public:
IContext *outputWindowContext = nullptr;
QString settingsKey;
AggregatingOutputFormatter formatter;
OutputFormatter formatter;
bool scrollToBottom = true;
bool linksActive = true;
@@ -568,7 +568,7 @@ void Internal::CorePlugin::testOutputFormatter()
"handled by B\n";
TestFormatterA formatterA;
TestFormatterB formatterB;
AggregatingOutputFormatter formatter;
OutputFormatter formatter;
QPlainTextEdit textEdit;
formatter.setPlainTextEdit(&textEdit);
formatter.setFormatters({&formatterB, &formatterA});