Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
feat: add search in text in group chats
Browse files Browse the repository at this point in the history
  • Loading branch information
TriKriSta committed Feb 11, 2018
1 parent 8bb80c7 commit 7718734
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 179 deletions.
6 changes: 3 additions & 3 deletions res.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,15 @@
<file>ui/chatArea/scrollBarRightArrow.svg</file>
<file>ui/chatForm/buttons.css</file>
<file>ui/chatForm/callButton.svg</file>
<file>ui/chatForm/hideButton.svg</file>
<file>ui/chatForm/micButton.svg</file>
<file>ui/chatForm/videoButton.svg</file>
<file>ui/chatForm/volButton.svg</file>
<file>ui/chatForm/emoteButton.svg</file>
<file>ui/chatForm/fileButton.svg</file>
<file>ui/chatForm/screenshotButton.svg</file>
<file>ui/chatForm/searchDownButton.svg</file>
<file>ui/chatForm/searchUpButton.svg</file>
<file>ui/chatForm/sendButton.svg</file>
<file>ui/emoticonWidget/dot_page.svg</file>
<file>ui/emoticonWidget/dot_page_current.svg</file>
Expand Down Expand Up @@ -95,8 +98,5 @@
<file>img/caps_lock.svg</file>
<file>ui/contentDialog/contentDialog.css</file>
<file>ui/tooliconsZone/tooliconsZone.css</file>
<file>ui/chatForm/searchDownButton.svg</file>
<file>ui/chatForm/searchUpButton.svg</file>
<file>ui/chatForm/hideButton.svg</file>
</qresource>
</RCC>
7 changes: 4 additions & 3 deletions src/chatlog/content/text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ void Text::selectText(const QString &txt, const int index)
{
regenerate();

if (!doc)
if (!doc) {
return;
}

QTextCursor cursor = doc->find(txt, index);
auto cursor = doc->find(txt, index);

if (cursor != QTextCursor()) {
if (!cursor.isNull()) {
cursor.beginEditBlock();
cursor.setPosition(index);
cursor.setPosition(index + txt.size(), QTextCursor::KeepAnchor);
Expand Down
155 changes: 17 additions & 138 deletions src/widget/form/chatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,6 @@ ChatForm::ChatForm(Friend* chatFriend, History* history)
connect(headWidget, &ChatFormHeader::micMuteToggle, this, &ChatForm::onMicMuteToggle);
connect(headWidget, &ChatFormHeader::volMuteToggle, this, &ChatForm::onVolMuteToggle);

connect(searchForm, &SearchForm::searchInBegin, this, &ChatForm::searchInBegin);
connect(searchForm, &SearchForm::searchUp, this, &ChatForm::onSearchUp);
connect(searchForm, &SearchForm::searchDown, this, &ChatForm::onSearchDown);
connect(searchForm, &SearchForm::visibleChanged, this, &ChatForm::onSearchTrigered);

connect(msgEdit, &ChatTextEdit::enterPressed, this, &ChatForm::onSendTriggered);
connect(msgEdit, &ChatTextEdit::textChanged, this, &ChatForm::onTextEditChanged);
connect(msgEdit, &ChatTextEdit::pasteImage, this, &ChatForm::sendImage);
Expand All @@ -208,8 +203,6 @@ ChatForm::ChatForm(Friend* chatFriend, History* history)
});
connect(headWidget, &ChatFormHeader::callRejected, this, &ChatForm::onRejectCallTriggered);

connect(chatWidget, &ChatLog::workerTimeoutFinished, this, &ChatForm::onContinueSearch);

updateCallButtons();
if (Nexus::getProfile()->isHistoryEnabled()) {
loadHistory(QDateTime::currentDateTime().addDays(-7), true);
Expand Down Expand Up @@ -496,27 +489,7 @@ void ChatForm::onVolMuteToggle()
updateMuteVolButton();
}

void ChatForm::onSearchTrigered()
{
if (searchForm->isHidden()) {
searchForm->removeSearchPhrase();

desibleSearchText();
} else {
searchPoint = QPoint(1, -1);
searchAfterLoadHistory = false;
}
}

void ChatForm::searchInBegin(const QString &phrase)
{
desibleSearchText();

searchPoint = QPoint(1, -1);
onSearchUp(phrase);
}

void ChatForm::onSearchUp(const QString &phrase)
void ChatForm::onSearchUp(const QString& phrase)
{
if (phrase.isEmpty()) {
desibleSearchText();
Expand All @@ -531,7 +504,7 @@ void ChatForm::onSearchUp(const QString &phrase)
QString pk = f->getPublicKey().toString();
QDateTime startDate = history->getStartDateChatHistory(pk);

if (startDate == earliestMessage) {
if (!startDate.isValid() || startDate == earliestMessage) {
return;
}

Expand All @@ -547,109 +520,30 @@ void ChatForm::onSearchUp(const QString &phrase)
return;
}

for (int i = startLine; i >= 0; --i) {
ChatLine::Ptr l = lines[i];

if (l->getColumnCount() >= 2) {
ChatLineContent* content = l->getContent(1);
Text* text = static_cast<Text*>(content);

if (searchPoint.y() == 0) {
text->deselectText();
searchPoint.setY(-1);
} else {
QString txt = content->getText();

if (txt.contains(phrase, Qt::CaseInsensitive)) {
int startIndex = -1;
if (searchPoint.y() > -1) {
startIndex = searchPoint.y() - 1;
}

int index = txt.lastIndexOf(phrase, startIndex, Qt::CaseInsensitive);
if ((index == -1 && searchPoint.y() > -1)) {
text->deselectText();
searchPoint.setY(-1);
} else {
chatWidget->scrollToLine(l);
text->deselectText();
text->selectText(phrase, index);
searchPoint = QPoint(numLines - i, index);

break;
}
}
}
bool isSearch = searchInText(phrase, true);

if (i == 0) {
QString pk = f->getPublicKey().toString();
QDateTime startDate = history->getStartDateChatHistory(pk);
QDateTime newBaseData = earliestMessage.addDays(-1);

if (startDate > newBaseData) {
newBaseData = startDate;
}
if (!isSearch) {
QString pk = f->getPublicKey().toString();
QDateTime startDate = history->getStartDateChatHistory(pk);
QDateTime newBaseData = earliestMessage.addDays(-1);

searchPoint.setX(numLines);
searchAfterLoadHistory = true;
loadHistory(newBaseData);
}
if (!startDate.isValid()) {
return;
}
}
}

void ChatForm::onSearchDown(const QString &phrase)
{
if (phrase.isEmpty()) {
desibleSearchText();
}

QVector<ChatLine::Ptr> lines = chatWidget->getLines();

if (lines.isEmpty()) {
return;
}

int numLines = lines.size();
int startLine = numLines - searchPoint.x();

for (int i = startLine; i < numLines; ++i) {
ChatLine::Ptr l = lines[i];
if (l->getColumnCount() >= 2) {
ChatLineContent* content = l->getContent(1);
Text* text = static_cast<Text*>(content);
QString txt = content->getText();

if (txt.contains(phrase, Qt::CaseInsensitive)) {
int startIndex = 0;
if (searchPoint.y() > -1) {
startIndex = searchPoint.y() + 1;
}

int index = txt.indexOf(phrase, startIndex, Qt::CaseInsensitive);
if (index == -1 && searchPoint.y() > -1) {
text->deselectText();
searchPoint.setY(-1);
} else {
chatWidget->scrollToLine(l);
text->deselectText();
text->selectText(phrase, index);
searchPoint = QPoint(numLines - i, index);

break;
}
}
if (startDate > newBaseData) {
newBaseData = startDate;
}

searchPoint.setX(numLines);
searchAfterLoadHistory = true;
loadHistory(newBaseData);
}
}

void ChatForm::onContinueSearch()
void ChatForm::onSearchDown(const QString& phrase)
{
QString phrase = searchForm->getSearchPhrase();
if (!phrase.isEmpty() && searchAfterLoadHistory) {
searchAfterLoadHistory = false;
onSearchUp(phrase);
}
searchInText(phrase, false);
}

void ChatForm::onFileSendFailed(uint32_t friendId, const QString& fname)
Expand Down Expand Up @@ -1124,21 +1018,6 @@ void ChatForm::SendMessageStr(QString msg)
}
}

void ChatForm::desibleSearchText()
{
if (searchPoint != QPoint(1, -1)) {
QVector<ChatLine::Ptr> lines = chatWidget->getLines();
int numLines = lines.size();
int index = numLines - searchPoint.x();
if (numLines > index) {
ChatLine::Ptr l = lines[index];
ChatLineContent* content = l->getContent(1);
Text* text = static_cast<Text*>(content);
text->deselectText();
}
}
}

void ChatForm::retranslateUi()
{
loadHistoryAction->setText(tr("Load chat history..."));
Expand Down
13 changes: 4 additions & 9 deletions src/widget/form/chatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public slots:
void onAvatarChange(uint32_t friendId, const QPixmap& pic);
void onAvatarRemoved(uint32_t friendId);

protected slots:
void onSearchUp(const QString& phrase) override;
void onSearchDown(const QString& phrase) override;

private slots:
void clearChatArea(bool notInForm) override final;
void onSendTriggered() override;
Expand All @@ -87,12 +91,7 @@ private slots:
void onRejectCallTriggered();
void onMicMuteToggle();
void onVolMuteToggle();
void onSearchTrigered();

void searchInBegin(const QString& phrase);
void onSearchUp(const QString& phrase);
void onSearchDown(const QString& phrase);
void onContinueSearch();
void onFileSendFailed(uint32_t friendId, const QString& fname);
void onFriendStatusChanged(quint32 friendId, Status status);
void onFriendTypingChanged(quint32 friendId, bool isTyping);
Expand All @@ -117,8 +116,6 @@ private slots:
void updateCallButtons();
void SendMessageStr(QString msg);

void desibleSearchText();

protected:
GenericNetCamView* createNetcam() final override;
void insertChatMessage(ChatMessage::Ptr msg) final override;
Expand All @@ -144,8 +141,6 @@ private slots:
QHash<uint, FileTransferInstance*> ftransWidgets;
bool isTyping;
bool lastCallIsVideo;
QPoint searchPoint;
bool searchAfterLoadHistory;
};

#endif // CHATFORM_H
Loading

0 comments on commit 7718734

Please sign in to comment.