From b6ab0ec2ca7ed85e20d0c14001392c078e294499 Mon Sep 17 00:00:00 2001 From: TriKriSta Date: Wed, 8 Aug 2018 21:35:28 +0300 Subject: [PATCH] refactor: add SearchDirection --- src/persistence/history.cpp | 2 +- src/widget/form/chatform.cpp | 8 +++--- src/widget/form/genericchatform.cpp | 16 +++++++---- src/widget/form/genericchatform.h | 6 ++-- src/widget/form/groupchatform.cpp | 8 +++--- src/widget/form/searchsettingsform.ui | 13 +++++++-- src/widget/searchform.cpp | 41 +++++++++++++++++++++------ src/widget/searchform.h | 5 ++-- src/widget/searchtypes.h | 5 ++++ 9 files changed, 73 insertions(+), 31 deletions(-) diff --git a/src/persistence/history.cpp b/src/persistence/history.cpp index 7c52a8f76..114595d7e 100644 --- a/src/persistence/history.cpp +++ b/src/persistence/history.cpp @@ -356,7 +356,7 @@ QDateTime History::getDateWhereFindPhrase(const QString& friendPk, const QDateTi } QDateTime date = from; - if (parameter.period != PeriodSearch::None) { + if (parameter.period == PeriodSearch::AfterDate || parameter.period == PeriodSearch::BeforeDate) { date = QDateTime(parameter.date); } diff --git a/src/widget/form/chatform.cpp b/src/widget/form/chatform.cpp index 3ac986817..86f1850d0 100644 --- a/src/widget/form/chatform.cpp +++ b/src/widget/form/chatform.cpp @@ -550,14 +550,14 @@ void ChatForm::onSearchUp(const QString& phrase, const ParameterSearch& paramete return; } - bool isSearch = searchInText(phrase, parameter, true); + const bool isSearch = searchInText(phrase, parameter, SearchDirection::Up); if (!isSearch) { const QString pk = f->getPublicKey().toString(); const QDateTime newBaseDate = history->getDateWhereFindPhrase(pk, earliestMessage, phrase, parameter); if (!newBaseDate.isValid()) { - emit messageNotFoundShow(true); + emit messageNotFoundShow(SearchDirection::Up); return; } @@ -569,8 +569,8 @@ void ChatForm::onSearchUp(const QString& phrase, const ParameterSearch& paramete void ChatForm::onSearchDown(const QString& phrase, const ParameterSearch& parameter) { - if (!searchInText(phrase, parameter, false)) { - emit messageNotFoundShow(false); + if (!searchInText(phrase, parameter, SearchDirection::Down)) { + emit messageNotFoundShow(SearchDirection::Down); } } diff --git a/src/widget/form/genericchatform.cpp b/src/widget/form/genericchatform.cpp index e9e4e3777..c4d63f4f8 100644 --- a/src/widget/form/genericchatform.cpp +++ b/src/widget/form/genericchatform.cpp @@ -570,7 +570,7 @@ void GenericChatForm::disableSearchText() } } -bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& parameter, bool searchUp) +bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& parameter, SearchDirection direction) { bool isSearch = false; @@ -586,8 +586,11 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& int numLines = lines.size(); - int startLine = numLines - searchPoint.x(); - if (parameter.period == PeriodSearch::WithTheFirst) { + int startLine = -1; + + if (parameter.period == PeriodSearch::WithTheEnd || parameter.period == PeriodSearch::None) { + startLine = numLines - searchPoint.x(); + } else if (parameter.period == PeriodSearch::WithTheFirst) { startLine = 0; } else if (parameter.period == PeriodSearch::AfterDate) { const auto lambda = [=](const ChatLine::Ptr& item) { @@ -627,6 +630,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& return isSearch; } + const bool searchUp = (direction == SearchDirection::Up); for (int i = startLine; searchUp ? i >= 0 : i < numLines; searchUp ? --i : ++i) { ChatLine::Ptr l = lines[i]; @@ -681,7 +685,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& continue; } - auto point = indexForSearchInLine(txt, phrase, parameter, searchUp); + auto point = indexForSearchInLine(txt, phrase, parameter, direction); if ((point.first == -1 && searchPoint.y() > -1)) { text->deselectText(); searchPoint.setY(-1); @@ -705,7 +709,7 @@ bool GenericChatForm::searchInText(const QString& phrase, const ParameterSearch& return isSearch; } -std::pair GenericChatForm::indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, bool searchUp) +std::pair GenericChatForm::indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, SearchDirection direction) { int index = -1; int size = 0; @@ -713,7 +717,7 @@ std::pair GenericChatForm::indexForSearchInLine(const QString& txt, co QRegularExpression exp; auto flagIns = QRegularExpression::CaseInsensitiveOption | QRegularExpression::UseUnicodePropertiesOption; auto flag = QRegularExpression::UseUnicodePropertiesOption; - if (searchUp) { + if (direction == SearchDirection::Up) { int startIndex = -1; if (searchPoint.y() > -1) { startIndex = searchPoint.y() - 1; diff --git a/src/widget/form/genericchatform.h b/src/widget/form/genericchatform.h index c8a8f7216..62b36e575 100644 --- a/src/widget/form/genericchatform.h +++ b/src/widget/form/genericchatform.h @@ -83,7 +83,7 @@ signals: void sendAction(uint32_t, QString); void chatAreaCleared(); void messageInserted(); - void messageNotFoundShow(const bool searchUp); + void messageNotFoundShow(SearchDirection direction); public slots: void focusInput(); @@ -137,8 +137,8 @@ protected: virtual void resizeEvent(QResizeEvent* event) final override; virtual bool eventFilter(QObject* object, QEvent* event) final override; void disableSearchText(); - bool searchInText(const QString& phrase, const ParameterSearch& parameter, bool searchUp); - std::pair indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, bool searchUp); + bool searchInText(const QString& phrase, const ParameterSearch& parameter, SearchDirection direction); + std::pair indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, SearchDirection direction); protected: bool audioInputFlag; diff --git a/src/widget/form/groupchatform.cpp b/src/widget/form/groupchatform.cpp index 619e387a9..2a73d8ca4 100644 --- a/src/widget/form/groupchatform.cpp +++ b/src/widget/form/groupchatform.cpp @@ -214,15 +214,15 @@ void GroupChatForm::searchInBegin(const QString& phrase, const ParameterSearch& void GroupChatForm::onSearchUp(const QString& phrase, const ParameterSearch& parameter) { - if (!searchInText(phrase, parameter, true)) { - emit messageNotFoundShow(true); + if (!searchInText(phrase, parameter, SearchDirection::Up)) { + emit messageNotFoundShow(SearchDirection::Up); } } void GroupChatForm::onSearchDown(const QString& phrase, const ParameterSearch& parameter) { - if (!searchInText(phrase, parameter, false)) { - emit messageNotFoundShow(false); + if (!searchInText(phrase, parameter, SearchDirection::Down)) { + emit messageNotFoundShow(SearchDirection::Down); } } diff --git a/src/widget/form/searchsettingsform.ui b/src/widget/form/searchsettingsform.ui index f7f91a609..33af9b761 100644 --- a/src/widget/form/searchsettingsform.ui +++ b/src/widget/form/searchsettingsform.ui @@ -20,7 +20,16 @@ Form - + + 0 + + + 0 + + + 0 + + 0 @@ -38,7 +47,7 @@ - Start searching: + Start search: diff --git a/src/widget/searchform.cpp b/src/widget/searchform.cpp index fce873d07..7ed28c476 100644 --- a/src/widget/searchform.cpp +++ b/src/widget/searchform.cpp @@ -20,13 +20,16 @@ #include "searchform.h" #include "form/searchsettingsform.h" #include "src/widget/style.h" + #include #include #include #include #include -const QString STATE_NAME[] = { +#include + +static std::array STATE_NAME = { QString{}, QStringLiteral("green"), QStringLiteral("red"), @@ -151,7 +154,7 @@ ParameterSearch SearchForm::getAndCheckParametrSearch() void SearchForm::setStateName(QPushButton *btn, ToolButtonState state) { - const int index = static_cast(state); + const auto index = static_cast(state); btn->setProperty("state", STATE_NAME[index]); btn->setStyleSheet(Style::getStylesheet(QStringLiteral(":/ui/chatForm/buttons.css"))); btn->setEnabled(index != 0); @@ -162,6 +165,7 @@ void SearchForm::useBeginState() setStateName(upButton, ToolButtonState::Common); setStateName(downButton, ToolButtonState::Common); messageLabel->setVisible(false); + isPrevSearch = false; } void SearchForm::changedSearchPhrase(const QString& text) @@ -192,8 +196,13 @@ void SearchForm::changedSearchPhrase(const QString& text) void SearchForm::clickedUp() { - setStateName(downButton, ToolButtonState::Common); - messageLabel->setVisible(false); + if (downButton->isEnabled()) { + isPrevSearch = false; + } else { + isPrevSearch = true; + setStateName(downButton, ToolButtonState::Common); + messageLabel->setVisible(false); + } if (startButton->isHidden()) { isSearchInBegin = false; @@ -205,8 +214,13 @@ void SearchForm::clickedUp() void SearchForm::clickedDown() { - setStateName(upButton, ToolButtonState::Common); - messageLabel->setVisible(false); + if (upButton->isEnabled()) { + isPrevSearch = false; + } else { + isPrevSearch = true; + setStateName(upButton, ToolButtonState::Common); + messageLabel->setVisible(false); + } if (startButton->isHidden()) { isSearchInBegin = false; @@ -243,7 +257,7 @@ void SearchForm::clickedSearch() } } -void SearchForm::changedState(const bool isUpdate) +void SearchForm::changedState(bool isUpdate) { if (isUpdate) { startButton->setHidden(false); @@ -258,12 +272,21 @@ void SearchForm::changedState(const bool isUpdate) useBeginState(); } -void SearchForm::showMessageNotFound(const bool searchUp) +void SearchForm::showMessageNotFound(SearchDirection direction) { if (isSearchInBegin) { + if (parameter.period == PeriodSearch::AfterDate) { + setStateName(downButton, ToolButtonState::Disabled); + } else if (parameter.period == PeriodSearch::BeforeDate) { + setStateName(upButton, ToolButtonState::Disabled); + } else { + setStateName(upButton, ToolButtonState::Disabled); + setStateName(downButton, ToolButtonState::Disabled); + } + } else if (isPrevSearch) { setStateName(upButton, ToolButtonState::Disabled); setStateName(downButton, ToolButtonState::Disabled); - } else if (searchUp) { + } else if (direction == SearchDirection::Up) { setStateName(upButton, ToolButtonState::Disabled); } else { setStateName(downButton, ToolButtonState::Disabled); diff --git a/src/widget/searchform.h b/src/widget/searchform.h index 1f6656120..4adc4103c 100644 --- a/src/widget/searchform.h +++ b/src/widget/searchform.h @@ -71,6 +71,7 @@ private: bool isActiveSettings{false}; bool isChangedPhrase{false}; bool isSearchInBegin{true}; + bool isPrevSearch{false}; private slots: void changedSearchPhrase(const QString& text); @@ -79,10 +80,10 @@ private slots: void clickedHide(); void clickedStart(); void clickedSearch(); - void changedState(const bool isUpdate); + void changedState(bool isUpdate); public slots: - void showMessageNotFound(const bool searchUp); + void showMessageNotFound(SearchDirection direction); signals: void searchInBegin(const QString& phrase, const ParameterSearch& parameter); diff --git a/src/widget/searchtypes.h b/src/widget/searchtypes.h index 319964d6c..ab8224196 100644 --- a/src/widget/searchtypes.h +++ b/src/widget/searchtypes.h @@ -21,6 +21,11 @@ enum class PeriodSearch { BeforeDate }; +enum class SearchDirection { + Up, + Down +}; + struct ParameterSearch { FilterSearch filter{FilterSearch::None}; PeriodSearch period{PeriodSearch::None};