Commit a1c96543 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by hjk
Browse files

Utils: Pimpl OutputFormatter



Change-Id: I42682a9793e895fb99b154cf9fac37fb189fd9c8
Reviewed-by: default avatarhjk <hjk@theqtcompany.com>
parent 7b8c58b9
......@@ -34,43 +34,65 @@
#include <QPlainTextEdit>
using namespace Utils;
namespace Utils {
OutputFormatter::OutputFormatter()
: QObject()
, m_plainTextEdit(0)
, m_formats(0)
, m_escapeCodeHandler(new AnsiEscapeCodeHandler)
, m_overwriteOutput(false)
namespace Internal {
class OutputFormatterPrivate
{
public:
OutputFormatterPrivate()
: plainTextEdit(0)
, formats(0)
, escapeCodeHandler(new AnsiEscapeCodeHandler)
, overwriteOutput(false)
{
}
~OutputFormatterPrivate()
{
delete[] formats;
delete escapeCodeHandler;
}
QPlainTextEdit *plainTextEdit;
QTextCharFormat *formats;
QFont font;
AnsiEscapeCodeHandler *escapeCodeHandler;
bool overwriteOutput;
};
} // namespace Internal
OutputFormatter::OutputFormatter()
: d(new Internal::OutputFormatterPrivate)
{
}
OutputFormatter::~OutputFormatter()
{
delete[] m_formats;
delete m_escapeCodeHandler;
delete d;
}
QPlainTextEdit *OutputFormatter::plainTextEdit() const
{
return m_plainTextEdit;
return d->plainTextEdit;
}
void OutputFormatter::setPlainTextEdit(QPlainTextEdit *plainText)
{
m_plainTextEdit = plainText;
d->plainTextEdit = plainText;
initFormats();
}
void OutputFormatter::appendMessage(const QString &text, OutputFormat format)
{
appendMessage(text, m_formats[format]);
appendMessage(text, d->formats[format]);
}
void OutputFormatter::appendMessage(const QString &text, const QTextCharFormat &format)
{
QTextCursor cursor(m_plainTextEdit->document());
QTextCursor cursor(d->plainTextEdit->document());
cursor.movePosition(QTextCursor::End);
foreach (const FormattedText &output, parseAnsi(text, format)) {
......@@ -79,7 +101,7 @@ void OutputFormatter::appendMessage(const QString &text, const QTextCharFormat &
while ((crPos = output.text.indexOf(QLatin1Char('\r'), startPos)) >= 0) {
append(cursor, output.text.mid(startPos, crPos - startPos), output.format);
startPos = crPos + 1;
m_overwriteOutput = true;
d->overwriteOutput = true;
}
if (startPos < output.text.count())
append(cursor, output.text.mid(startPos), output.format);
......@@ -88,28 +110,28 @@ void OutputFormatter::appendMessage(const QString &text, const QTextCharFormat &
QTextCharFormat OutputFormatter::charFormat(OutputFormat format) const
{
return m_formats[format];
return d->formats[format];
}
QList<FormattedText> OutputFormatter::parseAnsi(const QString &text, const QTextCharFormat &format)
{
return m_escapeCodeHandler->parseText(FormattedText(text, format));
return d->escapeCodeHandler->parseText(FormattedText(text, format));
}
void OutputFormatter::append(QTextCursor &cursor, const QString &text,
const QTextCharFormat &format)
{
if (m_overwriteOutput) {
if (d->overwriteOutput) {
cursor.clearSelection();
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
m_overwriteOutput = false;
d->overwriteOutput = false;
}
cursor.insertText(text, format);
}
void OutputFormatter::clearLastLine()
{
QTextCursor cursor(m_plainTextEdit->document());
QTextCursor cursor(d->plainTextEdit->document());
cursor.movePosition(QTextCursor::End);
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
......@@ -120,33 +142,33 @@ void OutputFormatter::initFormats()
if (!plainTextEdit())
return;
QFont boldFont = m_font;
QFont boldFont = d->font;
boldFont.setBold(true);
m_formats = new QTextCharFormat[NumberOfFormats];
d->formats = new QTextCharFormat[NumberOfFormats];
Theme *theme = creatorTheme();
// NormalMessageFormat
m_formats[NormalMessageFormat].setFont(boldFont);
m_formats[NormalMessageFormat].setForeground(theme->color(Theme::OutputFormatter_NormalMessageTextColor));
d->formats[NormalMessageFormat].setFont(boldFont);
d->formats[NormalMessageFormat].setForeground(theme->color(Theme::OutputFormatter_NormalMessageTextColor));
// ErrorMessageFormat
m_formats[ErrorMessageFormat].setFont(boldFont);
m_formats[ErrorMessageFormat].setForeground(theme->color(Theme::OutputFormatter_ErrorMessageTextColor));
d->formats[ErrorMessageFormat].setFont(boldFont);
d->formats[ErrorMessageFormat].setForeground(theme->color(Theme::OutputFormatter_ErrorMessageTextColor));
// StdOutFormat
m_formats[StdOutFormat].setFont(m_font);
m_formats[StdOutFormat].setForeground(theme->color(Theme::OutputFormatter_StdOutTextColor));
m_formats[StdOutFormatSameLine] = m_formats[StdOutFormat];
d->formats[StdOutFormat].setFont(d->font);
d->formats[StdOutFormat].setForeground(theme->color(Theme::OutputFormatter_StdOutTextColor));
d->formats[StdOutFormatSameLine] = d->formats[StdOutFormat];
// StdErrFormat
m_formats[StdErrFormat].setFont(m_font);
m_formats[StdErrFormat].setForeground(theme->color(Theme::OutputFormatter_StdErrTextColor));
m_formats[StdErrFormatSameLine] = m_formats[StdErrFormat];
d->formats[StdErrFormat].setFont(d->font);
d->formats[StdErrFormat].setForeground(theme->color(Theme::OutputFormatter_StdErrTextColor));
d->formats[StdErrFormatSameLine] = d->formats[StdErrFormat];
m_formats[DebugFormat].setFont(m_font);
m_formats[DebugFormat].setForeground(theme->color(Theme::OutputFormatter_DebugTextColor));
d->formats[DebugFormat].setFont(d->font);
d->formats[DebugFormat].setForeground(theme->color(Theme::OutputFormatter_DebugTextColor));
}
void OutputFormatter::handleLink(const QString &href)
......@@ -156,16 +178,18 @@ void OutputFormatter::handleLink(const QString &href)
QFont OutputFormatter::font() const
{
return m_font;
return d->font;
}
void OutputFormatter::setFont(const QFont &font)
{
m_font = font;
d->font = font;
initFormats();
}
void OutputFormatter::flush()
{
m_escapeCodeHandler->endFormatScope();
d->escapeCodeHandler->endFormatScope();
}
} // namespace Utils
......@@ -38,7 +38,6 @@
#include <QFont>
QT_BEGIN_NAMESPACE
class QColor;
class QPlainTextEdit;
class QTextCharFormat;
class QTextCursor;
......@@ -46,9 +45,10 @@ QT_END_NAMESPACE
namespace Utils {
class AnsiEscapeCodeHandler;
class FormattedText;
namespace Internal { class OutputFormatterPrivate; }
class QTCREATOR_UTILS_EXPORT OutputFormatter : public QObject
{
Q_OBJECT
......@@ -76,11 +76,7 @@ protected:
void append(QTextCursor &cursor, const QString &text, const QTextCharFormat &format);
private:
QPlainTextEdit *m_plainTextEdit;
QTextCharFormat *m_formats;
QFont m_font;
AnsiEscapeCodeHandler *m_escapeCodeHandler;
bool m_overwriteOutput;
Internal::OutputFormatterPrivate *d;
};
} // namespace Utils
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment