1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: add SearchDirection

This commit is contained in:
TriKriSta 2018-08-08 21:35:28 +03:00
parent 4253301c56
commit b6ab0ec2ca
9 changed files with 73 additions and 31 deletions

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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<int, int> GenericChatForm::indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, bool searchUp)
std::pair<int, int> 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<int, int> 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;

View File

@ -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<int, int> indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, bool searchUp);
bool searchInText(const QString& phrase, const ParameterSearch& parameter, SearchDirection direction);
std::pair<int, int> indexForSearchInLine(const QString& txt, const QString& phrase, const ParameterSearch& parameter, SearchDirection direction);
protected:
bool audioInputFlag;

View File

@ -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);
}
}

View File

@ -20,7 +20,16 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="margin">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
@ -38,7 +47,7 @@
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Start searching:</string>
<string>Start search:</string>
</property>
</widget>
</item>

View File

@ -20,13 +20,16 @@
#include "searchform.h"
#include "form/searchsettingsform.h"
#include "src/widget/style.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QKeyEvent>
const QString STATE_NAME[] = {
#include <array>
static std::array<QString, 3> 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<int>(state);
const auto index = static_cast<unsigned long>(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);

View File

@ -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);

View File

@ -21,6 +21,11 @@ enum class PeriodSearch {
BeforeDate
};
enum class SearchDirection {
Up,
Down
};
struct ParameterSearch {
FilterSearch filter{FilterSearch::None};
PeriodSearch period{PeriodSearch::None};