From 2962dd6dc27bf7b817aace1cb36ab3a745077670 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Tue, 31 Jan 2017 19:03:57 -0800 Subject: [PATCH] Factor LogDialog --- interface/src/Application.cpp | 1 + interface/src/ui/BaseLogDialog.cpp | 139 +++++++++++++++++++++++++++ interface/src/ui/BaseLogDialog.h | 60 ++++++++++++ interface/src/ui/LogDialog.cpp | 143 +++------------------------- interface/src/ui/LogDialog.h | 54 +++-------- libraries/shared/src/LogHandler.cpp | 2 +- 6 files changed, 226 insertions(+), 173 deletions(-) create mode 100644 interface/src/ui/BaseLogDialog.cpp create mode 100644 interface/src/ui/BaseLogDialog.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index d39ad337a8..964549d283 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -41,6 +41,7 @@ #include +#include #include #include diff --git a/interface/src/ui/BaseLogDialog.cpp b/interface/src/ui/BaseLogDialog.cpp new file mode 100644 index 0000000000..cb8a510c26 --- /dev/null +++ b/interface/src/ui/BaseLogDialog.cpp @@ -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 +#include +#include +#include +#include + +#include + +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); + } +} diff --git a/interface/src/ui/BaseLogDialog.h b/interface/src/ui/BaseLogDialog.h new file mode 100644 index 0000000000..72fe83cd82 --- /dev/null +++ b/interface/src/ui/BaseLogDialog.h @@ -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 + +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 diff --git a/interface/src/ui/LogDialog.cpp b/interface/src/ui/LogDialog.cpp index 1ba8fb5ff8..809fbd3772 100644 --- a/interface/src/ui/LogDialog.cpp +++ b/interface/src/ui/LogDialog.cpp @@ -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 -#include +#include +#include -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif +#include -#include -#include - -#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"); -int qTextBlockMeta = qRegisterMetaType("QTextBlock"); - -LogDialog::LogDialog(QWidget* parent, AbstractLoggerInterface* logger) : QDialog(parent, Qt::Window) { - - auto& packetReceiver = DependencyManager::get()->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(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()->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(); @@ -185,21 +86,3 @@ void LogDialog::handleEntityServerScriptLogPacket(QSharedPointer= 0) { - setFormat(index, length, keywordFormat); - index = text.indexOf(keyword, index + length, Qt::CaseInsensitive); - } -} diff --git a/interface/src/ui/LogDialog.h b/interface/src/ui/LogDialog.h index 4f366257ec..406d5ce74e 100644 --- a/interface/src/ui/LogDialog.h +++ b/interface/src/ui/LogDialog.h @@ -12,67 +12,37 @@ #ifndef hifi_LogDialog_h #define hifi_LogDialog_h -#include -#include -#include -#include -#include -#include -#include +#include "BaseLogDialog.h" -#include +#include -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 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 diff --git a/libraries/shared/src/LogHandler.cpp b/libraries/shared/src/LogHandler.cpp index 8e2c372bb6..e40ef814ea 100644 --- a/libraries/shared/src/LogHandler.cpp +++ b/libraries/shared/src/LogHandler.cpp @@ -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; }