mirror of
https://github.com/overte-org/overte.git
synced 2025-07-23 14:04:23 +02:00
Search keyword highlighter
This commit is contained in:
parent
840d1a33ef
commit
e0255721da
5 changed files with 57 additions and 29 deletions
|
@ -21,7 +21,7 @@ public:
|
||||||
inline void setExtraDebugging(bool debugging) { _extraDebugging = debugging; };
|
inline void setExtraDebugging(bool debugging) { _extraDebugging = debugging; };
|
||||||
|
|
||||||
virtual void addMessage(QString) = 0;
|
virtual void addMessage(QString) = 0;
|
||||||
virtual QStringList getLogData(QString) = 0;
|
virtual QStringList getLogData() = 0;
|
||||||
virtual void locateLog() = 0;
|
virtual void locateLog() = 0;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
|
@ -14,41 +14,22 @@
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QDesktopServices>
|
#include <QDesktopServices>
|
||||||
|
|
||||||
FileLogger::FileLogger() : _lines(NULL) {
|
FileLogger::FileLogger() : _logData(NULL) {
|
||||||
QHostAddress clientAddress = QHostAddress(getHostOrderLocalAddress());
|
|
||||||
QDateTime now = QDateTime::currentDateTime();
|
|
||||||
|
|
||||||
_fileName = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
|
_fileName = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
|
||||||
QDir logDir(_fileName);
|
QDir logDir(_fileName);
|
||||||
if (!logDir.exists(_fileName)) {
|
if (!logDir.exists(_fileName)) {
|
||||||
logDir.mkdir(_fileName);
|
logDir.mkdir(_fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QHostAddress clientAddress = QHostAddress(getHostOrderLocalAddress());
|
||||||
|
QDateTime now = QDateTime::currentDateTime();
|
||||||
_fileName.append(QString("/hifi-log_%1_%2.txt").arg(clientAddress.toString(), now.toString("yyyy-MM-dd_hh.mm.ss")));
|
_fileName.append(QString("/hifi-log_%1_%2.txt").arg(clientAddress.toString(), now.toString("yyyy-MM-dd_hh.mm.ss")));
|
||||||
setExtraDebugging(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList FileLogger::getLogData(QString searchText) {
|
|
||||||
if (searchText.isEmpty()) {
|
|
||||||
return _lines;
|
|
||||||
}
|
|
||||||
|
|
||||||
// wait for adding new log data while iterating over _lines
|
|
||||||
_mutex.lock();
|
|
||||||
QStringList filteredList;
|
|
||||||
for (int i = 0; i < _lines.size(); ++i) {
|
|
||||||
if (_lines[i].contains(searchText, Qt::CaseInsensitive)) {
|
|
||||||
filteredList.append(_lines[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_mutex.unlock();
|
|
||||||
return filteredList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileLogger::addMessage(QString message) {
|
void FileLogger::addMessage(QString message) {
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
emit logReceived(message);
|
emit logReceived(message);
|
||||||
_lines.append(message);
|
_logData.append(message);
|
||||||
|
|
||||||
QFile file(_fileName);
|
QFile file(_fileName);
|
||||||
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
|
if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) {
|
||||||
|
|
|
@ -18,11 +18,11 @@ public:
|
||||||
FileLogger();
|
FileLogger();
|
||||||
|
|
||||||
virtual void addMessage(QString);
|
virtual void addMessage(QString);
|
||||||
virtual QStringList getLogData(QString);
|
virtual QStringList getLogData() { return _logData; };
|
||||||
virtual void locateLog();
|
virtual void locateLog();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QStringList _lines;
|
QStringList _logData;
|
||||||
QString _fileName;
|
QString _fileName;
|
||||||
QMutex _mutex;
|
QMutex _mutex;
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ const int CHECKBOX_MARGIN = 12;
|
||||||
const int CHECKBOX_WIDTH = 140;
|
const int CHECKBOX_WIDTH = 140;
|
||||||
const int REVEAL_BUTTON_WIDTH = 122;
|
const int REVEAL_BUTTON_WIDTH = 122;
|
||||||
const float INITIAL_HEIGHT_RATIO = 0.6f;
|
const float INITIAL_HEIGHT_RATIO = 0.6f;
|
||||||
|
const QString HIGHLIGHT_COLOR = "#3366CC";
|
||||||
|
|
||||||
int qTextCursorMeta = qRegisterMetaType<QTextCursor>("QTextCursor");
|
int qTextCursorMeta = qRegisterMetaType<QTextCursor>("QTextCursor");
|
||||||
int qTextBlockMeta = qRegisterMetaType<QTextBlock>("QTextBlock");
|
int qTextBlockMeta = qRegisterMetaType<QTextBlock>("QTextBlock");
|
||||||
|
@ -91,12 +92,13 @@ void LogDialog::initControls() {
|
||||||
_logTextBox = new QPlainTextEdit(this);
|
_logTextBox = new QPlainTextEdit(this);
|
||||||
_logTextBox->setReadOnly(true);
|
_logTextBox->setReadOnly(true);
|
||||||
_logTextBox->show();
|
_logTextBox->show();
|
||||||
|
_highlighter = new KeywordHighlighter(_logTextBox->document());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogDialog::showEvent(QShowEvent*) {
|
void LogDialog::showEvent(QShowEvent*) {
|
||||||
connect(_logger, SIGNAL(logReceived(QString)), this, SLOT(appendLogLine(QString)));
|
connect(_logger, SIGNAL(logReceived(QString)), this, SLOT(appendLogLine(QString)));
|
||||||
handleSearchTextChanged("");
|
showLogData();
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogDialog::resizeEvent(QResizeEvent*) {
|
void LogDialog::resizeEvent(QResizeEvent*) {
|
||||||
|
@ -110,7 +112,9 @@ void LogDialog::resizeEvent(QResizeEvent*) {
|
||||||
void LogDialog::appendLogLine(QString logLine) {
|
void LogDialog::appendLogLine(QString logLine) {
|
||||||
if (isVisible()) {
|
if (isVisible()) {
|
||||||
pthread_mutex_lock(& _mutex);
|
pthread_mutex_lock(& _mutex);
|
||||||
_logTextBox->appendHtml(logLine.simplified());
|
if (logLine.contains(_searchTerm, Qt::CaseInsensitive)) {
|
||||||
|
_logTextBox->appendPlainText(logLine.simplified());
|
||||||
|
}
|
||||||
pthread_mutex_unlock(& _mutex);
|
pthread_mutex_unlock(& _mutex);
|
||||||
_logTextBox->ensureCursorVisible();
|
_logTextBox->ensureCursorVisible();
|
||||||
}
|
}
|
||||||
|
@ -129,13 +133,37 @@ void LogDialog::handleExtraDebuggingCheckbox(const int state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogDialog::handleSearchTextChanged(const QString searchText) {
|
void LogDialog::handleSearchTextChanged(const QString searchText) {
|
||||||
|
_searchTerm = searchText;
|
||||||
|
_highlighter->keyword = searchText;
|
||||||
|
showLogData();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogDialog::showLogData() {
|
||||||
_logTextBox->clear();
|
_logTextBox->clear();
|
||||||
pthread_mutex_lock(& _mutex);
|
pthread_mutex_lock(& _mutex);
|
||||||
QStringList _logData = _logger->getLogData(searchText);
|
QStringList _logData = _logger->getLogData();
|
||||||
for (int i = 0; i < _logData.size(); ++i) {
|
for (int i = 0; i < _logData.size(); ++i) {
|
||||||
appendLogLine(_logData[i]);
|
appendLogLine(_logData[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
pthread_mutex_unlock(& _mutex);
|
pthread_mutex_unlock(& _mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -14,9 +14,25 @@
|
||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QCheckBox>
|
#include <QCheckBox>
|
||||||
|
#include <QSyntaxHighlighter>
|
||||||
|
|
||||||
#include "AbstractLoggerInterface.h"
|
#include "AbstractLoggerInterface.h"
|
||||||
|
|
||||||
|
class KeywordHighlighter : public QSyntaxHighlighter {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
KeywordHighlighter(QTextDocument *parent = 0);
|
||||||
|
QString keyword;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void highlightBlock(const QString &text);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QTextCharFormat keywordFormat;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
class LogDialog : public QDialog {
|
class LogDialog : public QDialog {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
@ -44,10 +60,13 @@ private:
|
||||||
QPushButton* _revealLogButton;
|
QPushButton* _revealLogButton;
|
||||||
QPlainTextEdit* _logTextBox;
|
QPlainTextEdit* _logTextBox;
|
||||||
pthread_mutex_t _mutex;
|
pthread_mutex_t _mutex;
|
||||||
|
QString _searchTerm;
|
||||||
|
KeywordHighlighter* _highlighter;
|
||||||
|
|
||||||
AbstractLoggerInterface* _logger;
|
AbstractLoggerInterface* _logger;
|
||||||
|
|
||||||
void initControls();
|
void initControls();
|
||||||
|
void showLogData();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue