Implemented toggle prev search button

This commit is contained in:
kunalgosar 2017-02-14 16:05:48 -08:00
parent 2dd2018d7d
commit 5e5d76ead7
2 changed files with 58 additions and 9 deletions

View file

@ -23,10 +23,11 @@
const int TOP_BAR_HEIGHT = 46;
const int INITIAL_WIDTH = 720;
const int INITIAL_HEIGHT = 480;
const int MINIMAL_WIDTH = 570;
const int MINIMAL_WIDTH = 700;
const int SEARCH_BUTTON_LEFT = 25;
const int SEARCH_BUTTON_WIDTH = 20;
const int SEARCH_NEXT_BUTTON_WIDTH = 50;
const int SEARCH_TOGGLE_BUTTON_WIDTH = 50;
const int BUTTON_MARGIN = 15;
const int SEARCH_TEXT_WIDTH = 240;
const QColor HIGHLIGHT_COLOR = QColor("#3366CC");
@ -77,14 +78,24 @@ void BaseLogDialog::initControls() {
// 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;
_leftPad += SEARCH_TEXT_WIDTH + BUTTON_MARGIN;
_searchTextBox->show();
connect(_searchTextBox, SIGNAL(textChanged(QString)), SLOT(handleSearchTextChanged(QString)));
connect(_searchTextBox, SIGNAL(returnPressed()), SLOT(toggleSearchNext()));
_searchPrevButton = new QPushButton(this);
_searchPrevButton->setObjectName("searchPrevButton");
_searchPrevButton->setGeometry(_leftPad, ELEMENT_MARGIN, SEARCH_TOGGLE_BUTTON_WIDTH, ELEMENT_HEIGHT);
_searchPrevButton->setText("Prev");
_leftPad += SEARCH_TOGGLE_BUTTON_WIDTH + BUTTON_MARGIN;
_searchPrevButton->show();
connect(_searchPrevButton, SIGNAL(clicked()), SLOT(toggleSearchPrev()));
_searchNextButton = new QPushButton(this);
_searchNextButton->setObjectName("searchNextButton");
_searchNextButton->setGeometry(_leftPad, ELEMENT_MARGIN, SEARCH_NEXT_BUTTON_WIDTH, ELEMENT_HEIGHT);
_leftPad += SEARCH_NEXT_BUTTON_WIDTH + CHECKBOX_MARGIN;
_searchNextButton->setGeometry(_leftPad, ELEMENT_MARGIN, SEARCH_TOGGLE_BUTTON_WIDTH, ELEMENT_HEIGHT);
_searchNextButton->setText("Next");
_leftPad += SEARCH_TOGGLE_BUTTON_WIDTH + CHECKBOX_MARGIN;
_searchNextButton->show();
connect(_searchNextButton, SIGNAL(clicked()), SLOT(toggleSearchNext()));
@ -92,6 +103,7 @@ void BaseLogDialog::initControls() {
_logTextBox->setReadOnly(true);
_logTextBox->show();
_highlighter = new KeywordHighlighter(_logTextBox->document());
connect(_logTextBox, SIGNAL(selectionChanged()), SLOT(updateSelection()));
}
@ -114,13 +126,25 @@ void BaseLogDialog::handleSearchButton() {
}
void BaseLogDialog::handleSearchTextChanged(QString searchText) {
QTextCursor searchCursor = _logTextBox->textCursor();
searchCursor.setPosition(0, QTextCursor::MoveAnchor);
_logTextBox->setTextCursor(searchCursor);
if (searchText.isEmpty()) {
return;
}
QTextCursor cursor = _logTextBox->textCursor();
if (cursor.hasSelection()) {
QString selectedTerm = cursor.selectedText();
if (selectedTerm == searchText) {
return;
}
}
cursor.setPosition(0, QTextCursor::MoveAnchor);
_logTextBox->setTextCursor(cursor);
bool foundTerm = _logTextBox->find(searchText);
if (!foundTerm) {
searchCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
_logTextBox->setTextCursor(cursor);
}
_searchTerm = searchText;
@ -128,11 +152,23 @@ void BaseLogDialog::handleSearchTextChanged(QString searchText) {
_highlighter->rehighlight();
}
void BaseLogDialog::toggleSearchPrev() {
QTextCursor searchCursor = _logTextBox->textCursor();
if (searchCursor.hasSelection()) {
QString selectedTerm = searchCursor.selectedText();
_logTextBox->find(selectedTerm, QTextDocument::FindBackward);
} else {
handleSearchTextChanged(_searchTextBox.text());
}
}
void BaseLogDialog::toggleSearchNext() {
QTextCursor searchCursor = _logTextBox->textCursor();
if (searchCursor.hasSelection()) {
QString selectedTerm = searchCursor.selectedText();
_logTextBox->find(selectedTerm);
} else {
handleSearchTextChanged(_searchTextBox.text());
}
}
@ -142,6 +178,16 @@ void BaseLogDialog::showLogData() {
_logTextBox->ensureCursorVisible();
}
void BaseLogDialog::updateSelection() {
QTextCursor cursor = _logTextBox->textCursor();
if (cursor.hasSelection()) {
QString selectionTerm = cursor.selectedText();
if (QString::compare(selectionTerm, _searchTextBox->text(), Qt::CaseInsensitive) != 0) {
_searchTextBox->setText(selectionTerm);
}
}
}
KeywordHighlighter::KeywordHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {
keywordFormat.setForeground(HIGHLIGHT_COLOR);
}

View file

@ -35,8 +35,10 @@ public slots:
void appendLogLine(QString logLine);
private slots:
void updateSelection();
void handleSearchButton();
void handleSearchTextChanged(QString text);
void toggleSearchPrev();
void toggleSearchNext();
protected:
@ -50,6 +52,7 @@ private:
QPushButton* _searchButton { nullptr };
QLineEdit* _searchTextBox { nullptr };
QPlainTextEdit* _logTextBox { nullptr };
QPushButton* _searchPrevButton { nullptr };
QPushButton* _searchNextButton { nullptr };
QString _searchTerm;
KeywordHighlighter* _highlighter { nullptr };