VcsOutputWindow: Cleanup and modernize

Change-Id: I3239fb232207fb661bbcdf460ee01fb297720353
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Andre Hartmann
2019-03-09 18:07:50 +01:00
committed by André Hartmann
parent f7625e82e9
commit ea8a1c85c1
2 changed files with 36 additions and 41 deletions

View File

@@ -35,22 +35,21 @@
#include <utils/qtcprocess.h>
#include <utils/theme/theme.h>
#include <QPlainTextEdit>
#include <QTextCharFormat>
#include <QContextMenuEvent>
#include <QTextBlock>
#include <QMenu>
#include <QAction>
#include <QTextBlockUserData>
#include <QPointer>
#include <QTextCodec>
#include <QContextMenuEvent>
#include <QDir>
#include <QFileInfo>
#include <QMenu>
#include <QPlainTextEdit>
#include <QPoint>
#include <QPointer>
#include <QRegExp>
#include <QTextBlock>
#include <QTextBlockUserData>
#include <QTextCharFormat>
#include <QTextCodec>
#include <QTextStream>
#include <QTime>
#include <QPoint>
#include <QFileInfo>
using namespace Utils;
@@ -86,7 +85,7 @@ private:
const QString m_repository;
};
// A plain text edit with a special context menu containing "Clear" and
// A plain text edit with a special context menu containing "Clear"
// and functions to append specially formatted entries.
class OutputWindowPlainTextEdit : public Core::OutputWindow
{
@@ -94,23 +93,19 @@ public:
explicit OutputWindowPlainTextEdit(QWidget *parent = nullptr);
~OutputWindowPlainTextEdit() override;
void appendLines(QString const& s, const QString &repository = QString());
void appendLinesWithStyle(QString const& s, enum VcsOutputWindow::MessageStyle style, const QString &repository = QString());
void appendLines(const QString &s, const QString &repository = QString());
void appendLinesWithStyle(const QString &s, VcsOutputWindow::MessageStyle style,
const QString &repository = QString());
protected:
void contextMenuEvent(QContextMenuEvent *event) override;
private:
void setFormat(enum VcsOutputWindow::MessageStyle style);
void setFormat(VcsOutputWindow::MessageStyle style);
QString identifierUnderCursor(const QPoint &pos, QString *repository = nullptr) const;
Utils::OutputFormat m_format;
const QTextCharFormat m_defaultFormat;
QTextCharFormat m_errorFormat;
QTextCharFormat m_warningFormat;
QTextCharFormat m_commandFormat;
QTextCharFormat m_messageFormat;
OutputFormatter *m_formatter;
OutputFormatter *m_formatter = nullptr;
};
OutputWindowPlainTextEdit::OutputWindowPlainTextEdit(QWidget *parent) :
@@ -163,7 +158,7 @@ QString OutputWindowPlainTextEdit::identifierUnderCursor(const QPoint &widgetPos
// Retrieve repository if desired
if (repository)
if (QTextBlockUserData *data = cursor.block().userData())
*repository = static_cast<const RepositoryUserData*>(data)->repository();
*repository = static_cast<const RepositoryUserData *>(data)->repository();
// Find first non-space character of word and find first non-space character past
const int startPos = firstWordCharacter(block, cursorPos);
int endPos = cursorPos;
@@ -182,7 +177,7 @@ void OutputWindowPlainTextEdit::contextMenuEvent(QContextMenuEvent *event)
// Check for a file, expand via repository if relative
QFileInfo fi(token);
if (!repository.isEmpty() && !fi.isFile() && fi.isRelative())
fi = QFileInfo(repository + QLatin1Char('/') + token);
fi = QFileInfo(repository + '/' + token);
if (fi.isFile()) {
menu->addSeparator();
openAction = menu->addAction(VcsOutputWindow::tr("Open \"%1\"").
@@ -209,16 +204,16 @@ void OutputWindowPlainTextEdit::contextMenuEvent(QContextMenuEvent *event)
delete menu;
}
void OutputWindowPlainTextEdit::appendLines(QString const& s, const QString &repository)
void OutputWindowPlainTextEdit::appendLines(const QString &s, const QString &repository)
{
if (s.isEmpty())
return;
const int previousLineCount = document()->lineCount();
const QChar newLine(QLatin1Char('\n'));
const QChar newLine('\n');
const QChar lastChar = s.at(s.size() - 1);
const bool appendNewline = (lastChar != QLatin1Char('\r') && lastChar != newLine);
const bool appendNewline = (lastChar != '\r' && lastChar != newLine);
m_formatter->appendMessage(appendNewline ? s + newLine : s, m_format);
// Scroll down
@@ -232,20 +227,21 @@ void OutputWindowPlainTextEdit::appendLines(QString const& s, const QString &rep
}
}
void OutputWindowPlainTextEdit::appendLinesWithStyle(QString const& s, enum VcsOutputWindow::MessageStyle style, const QString &repository)
void OutputWindowPlainTextEdit::appendLinesWithStyle(const QString &s,
VcsOutputWindow::MessageStyle style,
const QString &repository)
{
setFormat(style);
if (style == VcsOutputWindow::Command) {
const QString timeStamp = QTime::currentTime().toString(QLatin1String("\nHH:mm "));
const QString timeStamp = QTime::currentTime().toString("\nHH:mm ");
appendLines(timeStamp + s, repository);
}
else {
} else {
appendLines(s, repository);
}
}
void OutputWindowPlainTextEdit::setFormat(enum VcsOutputWindow::MessageStyle style)
void OutputWindowPlainTextEdit::setFormat(VcsOutputWindow::MessageStyle style)
{
switch (style) {
case VcsOutputWindow::Warning:
@@ -284,7 +280,7 @@ static VcsOutputWindowPrivate *d = nullptr;
VcsOutputWindow::VcsOutputWindow()
{
d = new VcsOutputWindowPrivate;
d->passwordRegExp = QRegExp(QLatin1String("://([^@:]+):([^@]+)@"));
d->passwordRegExp = QRegExp("://([^@:]+):([^@]+)@");
Q_ASSERT(d->passwordRegExp.isValid());
m_instance = this;
}
@@ -294,7 +290,7 @@ static QString filterPasswordFromUrls(const QString &input)
int pos = 0;
QString result = input;
while ((pos = d->passwordRegExp.indexIn(result, pos)) >= 0) {
QString tmp = result.left(pos + 3) + d->passwordRegExp.cap(1) + QLatin1String(":***@");
QString tmp = result.left(pos + 3) + d->passwordRegExp.cap(1) + ":***@";
int newStart = tmp.count();
tmp += result.midRef(pos + d->passwordRegExp.matchedLength());
result = tmp;
@@ -395,7 +391,7 @@ void VcsOutputWindow::appendSilently(const QString &text)
append(text, None, true);
}
void VcsOutputWindow::append(const QString &text, enum MessageStyle style, bool silently)
void VcsOutputWindow::append(const QString &text, MessageStyle style, bool silently)
{
d->widget.appendLinesWithStyle(text, style, d->repository);
@@ -413,8 +409,7 @@ void VcsOutputWindow::appendWarning(const QString &text)
append(text, Warning, false);
}
// Helper to format arguments for log windows hiding common password
// options.
// Helper to format arguments for log windows hiding common password options.
static inline QString formatArguments(const QStringList &args)
{
const char passwordOptionC[] = "--password";
@@ -427,12 +422,12 @@ static inline QString formatArguments(const QStringList &args)
const QString arg = filterPasswordFromUrls(args.at(i));
if (i)
str << ' ';
if (arg.startsWith(QString::fromLatin1(passwordOptionC) + QLatin1Char('='))) {
if (arg.startsWith(QString::fromLatin1(passwordOptionC) + '=')) {
str << QtcProcess::quoteArg("--password=********");
continue;
}
str << QtcProcess::quoteArg(arg);
if (arg == QLatin1String(passwordOptionC)) {
if (arg == passwordOptionC) {
str << ' ' << QtcProcess::quoteArg("********");
i++;
}
@@ -447,9 +442,9 @@ QString VcsOutputWindow::msgExecutionLogEntry(const QString &workingDir,
const QString args = formatArguments(arguments);
const QString nativeExecutable = QtcProcess::quoteArg(executable.toUserOutput());
if (workingDir.isEmpty())
return tr("Running: %1 %2").arg(nativeExecutable, args) + QLatin1Char('\n');
return tr("Running: %1 %2").arg(nativeExecutable, args) + '\n';
return tr("Running in %1: %2 %3").
arg(QDir::toNativeSeparators(workingDir), nativeExecutable, args) + QLatin1Char('\n');
arg(QDir::toNativeSeparators(workingDir), nativeExecutable, args) + '\n';
}
void VcsOutputWindow::appendShellCommandLine(const QString &text)

View File

@@ -89,7 +89,7 @@ public slots:
// Append text with a certain style (none by default),
// and maybe pop up (silent by default)
static void append(const QString &text, enum MessageStyle style = None, bool silently = false);
static void append(const QString &text, MessageStyle style = None, bool silently = false);
// Silently append text, do not pop up.
static void appendSilently(const QString &text);