Factor LogDialog

This commit is contained in:
Atlante45 2017-01-31 19:03:57 -08:00
parent fcb74bce10
commit 2962dd6dc2
6 changed files with 226 additions and 173 deletions

View file

@ -41,6 +41,7 @@
#include <QtMultimedia/QMediaPlayer>
#include <QFontDatabase>
#include <QProcessEnvironment>
#include <QTemporaryDir>

View file

@ -0,0 +1,139 @@
//
// BaseLogDialog.cpp
// interface/src/ui
//
// Created by Clement Brisset on 1/31/17.
// Copyright 2017 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "BaseLogDialog.h"
#include <QDir>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QSyntaxHighlighter>
#include <PathUtils.h>
const int TOP_BAR_HEIGHT = 46;
const int INITIAL_WIDTH = 720;
const int INITIAL_HEIGHT = 480;
const int MINIMAL_WIDTH = 570;
const int SEARCH_BUTTON_LEFT = 25;
const int SEARCH_BUTTON_WIDTH = 20;
const int SEARCH_TEXT_WIDTH = 240;
const QColor HIGHLIGHT_COLOR = QColor("#3366CC");
class KeywordHighlighter : public QSyntaxHighlighter {
public:
KeywordHighlighter(QTextDocument* parent = nullptr);
QString keyword;
protected:
void highlightBlock(const QString& text) override;
private:
QTextCharFormat keywordFormat;
};
BaseLogDialog::BaseLogDialog(QWidget* parent) : QDialog(parent, Qt::Window) {
setWindowTitle("Base Log");
setAttribute(Qt::WA_DeleteOnClose);
QFile styleSheet(PathUtils::resourcesPath() + "styles/log_dialog.qss");
if (styleSheet.open(QIODevice::ReadOnly)) {
QDir::setCurrent(PathUtils::resourcesPath());
setStyleSheet(styleSheet.readAll());
}
initControls();
resize(INITIAL_WIDTH, INITIAL_HEIGHT);
setMinimumWidth(MINIMAL_WIDTH);
}
BaseLogDialog::~BaseLogDialog() {
deleteLater();
}
void BaseLogDialog::initControls() {
_searchButton = new QPushButton(this);
// set object name for css styling
_searchButton->setObjectName("searchButton");
_leftPad = SEARCH_BUTTON_LEFT;
_searchButton->setGeometry(_leftPad, ELEMENT_MARGIN, SEARCH_BUTTON_WIDTH, ELEMENT_HEIGHT);
_leftPad += SEARCH_BUTTON_WIDTH;
_searchButton->show();
connect(_searchButton, SIGNAL(clicked()), SLOT(handleSearchButton()));
_searchTextBox = new QLineEdit(this);
// disable blue outline in Mac
_searchTextBox->setAttribute(Qt::WA_MacShowFocusRect, false);
_searchTextBox->setGeometry(_leftPad, ELEMENT_MARGIN, SEARCH_TEXT_WIDTH, ELEMENT_HEIGHT);
_leftPad += SEARCH_TEXT_WIDTH + CHECKBOX_MARGIN;
_searchTextBox->show();
connect(_searchTextBox, SIGNAL(textChanged(QString)), SLOT(handleSearchTextChanged(QString)));
_logTextBox = new QPlainTextEdit(this);
_logTextBox->setReadOnly(true);
_logTextBox->show();
_highlighter = new KeywordHighlighter(_logTextBox->document());
}
void BaseLogDialog::showEvent(QShowEvent* event) {
showLogData();
}
void BaseLogDialog::resizeEvent(QResizeEvent* event) {
_logTextBox->setGeometry(0, TOP_BAR_HEIGHT, width(), height() - TOP_BAR_HEIGHT);
}
void BaseLogDialog::appendLogLine(QString logLine) {
if (isVisible()) {
if (logLine.contains(_searchTerm, Qt::CaseInsensitive)) {
_logTextBox->appendPlainText(logLine.trimmed());
}
}
}
void BaseLogDialog::handleSearchButton() {
_searchTextBox->setFocus();
}
void BaseLogDialog::handleSearchTextChanged(QString searchText) {
_searchTerm = searchText;
_highlighter->keyword = searchText;
showLogData();
}
void BaseLogDialog::showLogData() {
_logTextBox->clear();
_logTextBox->insertPlainText(getCurrentLog());
_logTextBox->ensureCursorVisible();
}
KeywordHighlighter::KeywordHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {
keywordFormat.setForeground(HIGHLIGHT_COLOR);
}
void KeywordHighlighter::highlightBlock(const QString& text) {
if (keyword.isNull() || keyword.isEmpty()) {
return;
}
int index = text.indexOf(keyword, 0, Qt::CaseInsensitive);
int length = keyword.length();
while (index >= 0) {
setFormat(index, length, keywordFormat);
index = text.indexOf(keyword, index + length, Qt::CaseInsensitive);
}
}

View file

@ -0,0 +1,60 @@
//
// BaseLogDialog.h
// interface/src/ui
//
// Created by Clement Brisset on 1/31/17.
// Copyright 2017 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_BaseLogDialog_h
#define hifi_BaseLogDialog_h
#include <QDialog>
const int ELEMENT_MARGIN = 7;
const int ELEMENT_HEIGHT = 32;
const int CHECKBOX_MARGIN = 12;
const int CHECKBOX_WIDTH = 140;
class QPushButton;
class QLineEdit;
class QPlainTextEdit;
class KeywordHighlighter;
class BaseLogDialog : public QDialog {
Q_OBJECT
public:
BaseLogDialog(QWidget* parent);
~BaseLogDialog();
public slots:
void appendLogLine(QString logLine);
private slots:
void handleSearchButton();
void handleSearchTextChanged(QString text);
protected:
int _leftPad { 0 };
void resizeEvent(QResizeEvent* event) override;
void showEvent(QShowEvent* event) override;
virtual QString getCurrentLog() = 0;
private:
QPushButton* _searchButton { nullptr };
QLineEdit* _searchTextBox { nullptr };
QPlainTextEdit* _logTextBox { nullptr };
QString _searchTerm;
KeywordHighlighter* _highlighter { nullptr };
void initControls();
void showLogData();
};
#endif // hifi_BaseLogDialog_h

View file

@ -9,93 +9,21 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdouble-promotion"
#endif
#include "LogDialog.h"
#include <QDesktopWidget>
#include <QTextBlock>
#include <QCheckBox>
#include <QPushButton>
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#include <shared/AbstractLoggerInterface.h>
#include <PathUtils.h>
#include <SharedUtil.h>
#include "Application.h"
#include "ui/LogDialog.h"
const int TOP_BAR_HEIGHT = 46;
const int INITIAL_WIDTH = 720;
const int MINIMAL_WIDTH = 570;
const int ELEMENT_MARGIN = 7;
const int ELEMENT_HEIGHT = 32;
const int SEARCH_BUTTON_LEFT = 25;
const int SEARCH_BUTTON_WIDTH = 20;
const int SEARCH_TEXT_WIDTH = 240;
const int CHECKBOX_MARGIN = 12;
const int CHECKBOX_WIDTH = 140;
const int REVEAL_BUTTON_WIDTH = 122;
const float INITIAL_HEIGHT_RATIO = 0.6f;
const QString HIGHLIGHT_COLOR = "#3366CC";
int qTextCursorMeta = qRegisterMetaType<QTextCursor>("QTextCursor");
int qTextBlockMeta = qRegisterMetaType<QTextBlock>("QTextBlock");
LogDialog::LogDialog(QWidget* parent, AbstractLoggerInterface* logger) : QDialog(parent, Qt::Window) {
auto& packetReceiver = DependencyManager::get<NodeList>()->getPacketReceiver();
packetReceiver.registerListener(PacketType::EntityServerScriptLog, this, "handleEntityServerScriptLogPacket");
LogDialog::LogDialog(QWidget* parent, AbstractLoggerInterface* logger) : BaseLogDialog(parent) {
_logger = logger;
setWindowTitle("Log");
setAttribute(Qt::WA_DeleteOnClose);
QFile styleSheet(PathUtils::resourcesPath() + "styles/log_dialog.qss");
if (styleSheet.open(QIODevice::ReadOnly)) {
QDir::setCurrent(PathUtils::resourcesPath());
setStyleSheet(styleSheet.readAll());
}
initControls();
QDesktopWidget desktop;
QRect screen = desktop.screenGeometry();
resize(INITIAL_WIDTH, static_cast<int>(screen.height() * INITIAL_HEIGHT_RATIO));
move(screen.center() - rect().center());
setMinimumWidth(MINIMAL_WIDTH);
//connect(_logger, SIGNAL(logReceived(QString)), this, SLOT(appendLogLine(QString)), Qt::QueuedConnection);
}
LogDialog::~LogDialog() {
deleteLater();
}
void LogDialog::initControls() {
int left;
_searchButton = new QPushButton(this);
// set object name for css styling
_searchButton->setObjectName("searchButton");
left = SEARCH_BUTTON_LEFT;
_searchButton->setGeometry(left, ELEMENT_MARGIN, SEARCH_BUTTON_WIDTH, ELEMENT_HEIGHT);
left += SEARCH_BUTTON_WIDTH;
_searchButton->show();
connect(_searchButton, SIGNAL(clicked()), SLOT(handleSearchButton()));
_searchTextBox = new QLineEdit(this);
// disable blue outline in Mac
_searchTextBox->setAttribute(Qt::WA_MacShowFocusRect, false);
_searchTextBox->setGeometry(left, ELEMENT_MARGIN, SEARCH_TEXT_WIDTH, ELEMENT_HEIGHT);
left += SEARCH_TEXT_WIDTH + CHECKBOX_MARGIN;
_searchTextBox->show();
connect(_searchTextBox, SIGNAL(textChanged(QString)), SLOT(handleSearchTextChanged(QString)));
_extraDebuggingBox = new QCheckBox("Extra debugging", this);
_extraDebuggingBox->setGeometry(left, ELEMENT_MARGIN, CHECKBOX_WIDTH, ELEMENT_HEIGHT);
_extraDebuggingBox->setGeometry(_leftPad, ELEMENT_MARGIN, CHECKBOX_WIDTH, ELEMENT_HEIGHT);
if (_logger->extraDebugging()) {
_extraDebuggingBox->setCheckState(Qt::Checked);
}
@ -109,37 +37,19 @@ void LogDialog::initControls() {
_revealLogButton->show();
connect(_revealLogButton, SIGNAL(clicked()), SLOT(handleRevealButton()));
_logTextBox = new QPlainTextEdit(this);
_logTextBox->setReadOnly(true);
_logTextBox->show();
_highlighter = new KeywordHighlighter(_logTextBox->document());
//connect(_logger, SIGNAL(logReceived(QString)), this, SLOT(appendLogLine(QString)), Qt::QueuedConnection);
auto& packetReceiver = DependencyManager::get<NodeList>()->getPacketReceiver();
packetReceiver.registerListener(PacketType::EntityServerScriptLog, this, "handleEntityServerScriptLogPacket");
}
void LogDialog::showEvent(QShowEvent*) {
showLogData();
}
void LogDialog::resizeEvent(QResizeEvent*) {
_logTextBox->setGeometry(0, TOP_BAR_HEIGHT, width(), height() - TOP_BAR_HEIGHT);
void LogDialog::resizeEvent(QResizeEvent* event) {
BaseLogDialog::resizeEvent(event);
_revealLogButton->setGeometry(width() - ELEMENT_MARGIN - REVEAL_BUTTON_WIDTH,
ELEMENT_MARGIN,
REVEAL_BUTTON_WIDTH,
ELEMENT_HEIGHT);
}
void LogDialog::appendLogLine(QString logLine) {
if (isVisible()) {
if (logLine.contains(_searchTerm, Qt::CaseInsensitive)) {
_logTextBox->appendPlainText(logLine.trimmed());
}
}
}
void LogDialog::handleSearchButton() {
_searchTextBox->setFocus();
}
void LogDialog::handleRevealButton() {
_logger->locateLog();
}
@ -148,19 +58,10 @@ void LogDialog::handleExtraDebuggingCheckbox(const int state) {
_logger->setExtraDebugging(state != 0);
}
void LogDialog::handleSearchTextChanged(const QString searchText) {
_searchTerm = searchText;
_highlighter->keyword = searchText;
showLogData();
QString LogDialog::getCurrentLog() {
return _logger->getLogData();
}
void LogDialog::showLogData() {
_logTextBox->clear();
_logTextBox->insertPlainText(_logger->getLogData());
_logTextBox->ensureCursorVisible();
}
void LogDialog::enableToEntityServerScriptLog(bool enable) {
qDebug() << Q_FUNC_INFO << enable;
auto nodeList = DependencyManager::get<NodeList>();
@ -185,21 +86,3 @@ void LogDialog::handleEntityServerScriptLogPacket(QSharedPointer<ReceivedMessage
QMetaObject::invokeMethod(this, "appendLogLine", Q_ARG(QString, lines));
}
KeywordHighlighter::KeywordHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent), keywordFormat() {
keywordFormat.setForeground(QColor(HIGHLIGHT_COLOR));
}
void KeywordHighlighter::highlightBlock(const QString &text) {
if (keyword.isNull() || keyword.isEmpty()) {
return;
}
int index = text.indexOf(keyword, 0, Qt::CaseInsensitive);
int length = keyword.length();
while (index >= 0) {
setFormat(index, length, keywordFormat);
index = text.indexOf(keyword, index + length, Qt::CaseInsensitive);
}
}

View file

@ -12,67 +12,37 @@
#ifndef hifi_LogDialog_h
#define hifi_LogDialog_h
#include <QDialog>
#include <QMutex>
#include <QPlainTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include <QSyntaxHighlighter>
#include "BaseLogDialog.h"
#include <shared/AbstractLoggerInterface.h>
#include <NodeList.h>
class KeywordHighlighter : public QSyntaxHighlighter {
class QCheckBox;
class QPushButton;
class QResizeEvent;
class AbstractLoggerInterface;
class LogDialog : public BaseLogDialog {
Q_OBJECT
public:
KeywordHighlighter(QTextDocument *parent = 0);
QString keyword;
protected:
void highlightBlock(const QString &text) override;
private:
QTextCharFormat keywordFormat;
};
class LogDialog : public QDialog {
Q_OBJECT
public:
LogDialog(QWidget*, AbstractLoggerInterface*);
~LogDialog();
public slots:
void appendLogLine(QString logLine);
LogDialog(QWidget* parent, AbstractLoggerInterface* logger);
private slots:
void handleSearchButton();
void handleRevealButton();
void handleExtraDebuggingCheckbox(const int);
void handleSearchTextChanged(const QString);
void enableToEntityServerScriptLog(bool enable);
void handleEntityServerScriptLogPacket(QSharedPointer<ReceivedMessage> message, SharedNodePointer senderNode);
protected:
void resizeEvent(QResizeEvent*) override;
void showEvent(QShowEvent*) override;
void resizeEvent(QResizeEvent* event) override;
QString getCurrentLog() override;
private:
QPushButton* _searchButton;
QLineEdit* _searchTextBox;
QCheckBox* _extraDebuggingBox;
QPushButton* _revealLogButton;
QPlainTextEdit* _logTextBox;
QString _searchTerm;
KeywordHighlighter* _highlighter;
AbstractLoggerInterface* _logger;
void initControls();
void showLogData();
};
#endif // hifi_LogDialog_h

View file

@ -177,7 +177,7 @@ QString LogHandler::printMessage(LogMsgType type, const QMessageLogContext& cont
prefixString.append(QString(" [%1]").arg(_targetName));
}
QString logMessage = QString("%1 %2").arg(prefixString, message.split("\n").join("\n" + prefixString + " "));
QString logMessage = QString("%1 %2").arg(prefixString, message.split('\n').join('\n' + prefixString + " "));
fprintf(stdout, "%s\n", qPrintable(logMessage));
return logMessage;
}