Showing the changes to interface/src/ui/BaseLogDialog.cpp (1 of 6 files). Show all
Unified · Split
| @@ -0,0 +1,139 @@ | ||
| 1 | +// | |
| 2 | +// BaseLogDialog.cpp | |
| 3 | +// interface/src/ui | |
| 4 | +// | |
| 5 | +// Created by Clement Brisset on 1/31/17. | |
| 6 | +// Copyright 2017 High Fidelity, Inc. | |
| 7 | +// | |
| 8 | +// Distributed under the Apache License, Version 2.0. | |
| 9 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 10 | +// | |
| 11 | + | |
| 12 | +#include "BaseLogDialog.h" | |
| 13 | + | |
| 14 | +#include <QDir> | |
| 15 | +#include <QLineEdit> | |
| 16 | +#include <QPlainTextEdit> | |
| 17 | +#include <QPushButton> | |
| 18 | +#include <QSyntaxHighlighter> | |
| 19 | + | |
| 20 | +#include <PathUtils.h> | |
| 21 | + | |
| 22 | +const int TOP_BAR_HEIGHT = 46; | |
| 23 | +const int INITIAL_WIDTH = 720; | |
| 24 | +const int INITIAL_HEIGHT = 480; | |
| 25 | +const int MINIMAL_WIDTH = 570; | |
| 26 | +const int SEARCH_BUTTON_LEFT = 25; | |
| 27 | +const int SEARCH_BUTTON_WIDTH = 20; | |
| 28 | +const int SEARCH_TEXT_WIDTH = 240; | |
| 29 | +const QColor HIGHLIGHT_COLOR = QColor("#3366CC"); | |
| 30 | + | |
| 31 | +class KeywordHighlighter : public QSyntaxHighlighter { | |
| 32 | +public: | |
| 33 | + KeywordHighlighter(QTextDocument* parent = nullptr); | |
| 34 | + QString keyword; | |
| 35 | + | |
| 36 | +protected: | |
| 37 | + void highlightBlock(const QString& text) override; | |
| 38 | + | |
| 39 | +private: | |
| 40 | + QTextCharFormat keywordFormat; | |
| 41 | + | |
| 42 | +}; | |
| 43 | + | |
| 44 | +BaseLogDialog::BaseLogDialog(QWidget* parent) : QDialog(parent, Qt::Window) { | |
| 45 | + setWindowTitle("Base Log"); | |
| 46 | + setAttribute(Qt::WA_DeleteOnClose); | |
| 47 | + | |
| 48 | + QFile styleSheet(PathUtils::resourcesPath() + "styles/log_dialog.qss"); | |
| 49 | + if (styleSheet.open(QIODevice::ReadOnly)) { | |
| 50 | + QDir::setCurrent(PathUtils::resourcesPath()); | |
| 51 | + setStyleSheet(styleSheet.readAll()); | |
| 52 | + } | |
| 53 | + | |
| 54 | + initControls(); | |
| 55 | + | |
| 56 | + resize(INITIAL_WIDTH, INITIAL_HEIGHT); | |
| 57 | + setMinimumWidth(MINIMAL_WIDTH); | |
| 58 | +} | |
| 59 | + | |
| 60 | +BaseLogDialog::~BaseLogDialog() { | |
| 61 | + deleteLater(); | |
| 62 | +} | |
| 63 | + | |
| 64 | +void BaseLogDialog::initControls() { | |
| 65 | + _searchButton = new QPushButton(this); | |
| 66 | + // set object name for css styling | |
| 67 | + _searchButton->setObjectName("searchButton"); | |
| 68 | + _leftPad = SEARCH_BUTTON_LEFT; | |
| 69 | + _searchButton->setGeometry(_leftPad, ELEMENT_MARGIN, SEARCH_BUTTON_WIDTH, ELEMENT_HEIGHT); | |
| 70 | + _leftPad += SEARCH_BUTTON_WIDTH; | |
| 71 | + _searchButton->show(); | |
| 72 | + connect(_searchButton, SIGNAL(clicked()), SLOT(handleSearchButton())); | |
| 73 | + | |
| 74 | + _searchTextBox = new QLineEdit(this); | |
| 75 | + // disable blue outline in Mac | |
| 76 | + _searchTextBox->setAttribute(Qt::WA_MacShowFocusRect, false); | |
| 77 | + _searchTextBox->setGeometry(_leftPad, ELEMENT_MARGIN, SEARCH_TEXT_WIDTH, ELEMENT_HEIGHT); | |
| 78 | + _leftPad += SEARCH_TEXT_WIDTH + CHECKBOX_MARGIN; | |
| 79 | + _searchTextBox->show(); | |
| 80 | + connect(_searchTextBox, SIGNAL(textChanged(QString)), SLOT(handleSearchTextChanged(QString))); | |
| 81 | + | |
| 82 | + _logTextBox = new QPlainTextEdit(this); | |
| 83 | + _logTextBox->setReadOnly(true); | |
| 84 | + _logTextBox->show(); | |
| 85 | + _highlighter = new KeywordHighlighter(_logTextBox->document()); | |
| 86 | + | |
| 87 | +} | |
| 88 | + | |
| 89 | +void BaseLogDialog::showEvent(QShowEvent* event) { | |
| 90 | + showLogData(); | |
| 91 | +} | |
| 92 | + | |
| 93 | +void BaseLogDialog::resizeEvent(QResizeEvent* event) { | |
| 94 | + _logTextBox->setGeometry(0, TOP_BAR_HEIGHT, width(), height() - TOP_BAR_HEIGHT); | |
| 95 | +} | |
| 96 | + | |
| 97 | +void BaseLogDialog::appendLogLine(QString logLine) { | |
| 98 | + if (isVisible()) { | |
| 99 | + if (logLine.contains(_searchTerm, Qt::CaseInsensitive)) { | |
| 100 | + _logTextBox->appendPlainText(logLine.trimmed()); | |
| 101 | + } | |
| 102 | + } | |
| 103 | +} | |
| 104 | + | |
| 105 | +void BaseLogDialog::handleSearchButton() { | |
| 106 | + _searchTextBox->setFocus(); | |
| 107 | +} | |
| 108 | + | |
| 109 | +void BaseLogDialog::handleSearchTextChanged(QString searchText) { | |
| 110 | + _searchTerm = searchText; | |
| 111 | + _highlighter->keyword = searchText; | |
| 112 | + showLogData(); | |
| 113 | +} | |
| 114 | + | |
| 115 | +void BaseLogDialog::showLogData() { | |
| 116 | + _logTextBox->clear(); | |
| 117 | + _logTextBox->insertPlainText(getCurrentLog()); | |
| 118 | + _logTextBox->ensureCursorVisible(); | |
| 119 | +} | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | +KeywordHighlighter::KeywordHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) { | |
| 124 | + keywordFormat.setForeground(HIGHLIGHT_COLOR); | |
| 125 | +} | |
| 126 | + | |
| 127 | +void KeywordHighlighter::highlightBlock(const QString& text) { | |
| 128 | + if (keyword.isNull() || keyword.isEmpty()) { | |
| 129 | + return; | |
| 130 | + } | |
| 131 | + | |
| 132 | + int index = text.indexOf(keyword, 0, Qt::CaseInsensitive); | |
| 133 | + int length = keyword.length(); | |
| 134 | + | |
| 135 | + while (index >= 0) { | |
| 136 | + setFormat(index, length, keywordFormat); | |
| 137 | + index = text.indexOf(keyword, index + length, Qt::CaseInsensitive); | |
| 138 | + } | |
| 139 | +} | |