Skip to content

Commit

Permalink
revert(chatlog): "feat: save selected search text after scrolling up"
Browse files Browse the repository at this point in the history
This reverts commit dbf8800.
  • Loading branch information
anthonybilinski committed Apr 13, 2020
1 parent 83d5863 commit c1d0624
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/chatlog/chatlog.cpp
Expand Up @@ -688,7 +688,7 @@ void ChatLog::scrollToLine(ChatLine::Ptr line)
workerStb = false;
} else {
updateSceneRect();
verticalScrollBar()->setValue(line->sceneBoundingRect().top());
verticalScrollBar()->setValue(line->sceneBoundingRect().top()); // NOTE: start here
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/model/ichatlog.h
Expand Up @@ -70,7 +70,7 @@ struct SearchPos

struct SearchResult
{
bool found{false};
bool found;
SearchPos pos;
size_t start;
size_t len;
Expand Down
50 changes: 21 additions & 29 deletions src/widget/form/genericchatform.cpp
Expand Up @@ -680,11 +680,7 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time)
}

if (begin != end) {
if (searchResult.found == true) {
renderMessages(begin, searchResult.pos.logIdx, [this]{enableSearchText();});
} else {
renderMessages(begin, end);
}
renderMessages(begin, end);
} else {
chatWidget->setScroll(true);
}
Expand Down Expand Up @@ -729,23 +725,13 @@ void GenericChatForm::removeLastsMessages(const int num)

void GenericChatForm::disableSearchText()
{
auto msgIt = messages.find(searchResult.pos.logIdx);
auto msgIt = messages.find(searchPos.logIdx);
if (msgIt != messages.end()) {
auto text = qobject_cast<Text*>(msgIt->second->getContent(1));
text->deselectText();
}
}

void GenericChatForm::enableSearchText()
{
auto msg = messages.at(searchResult.pos.logIdx);
chatWidget->scrollToLine(msg);

auto text = qobject_cast<Text*>(msg->getContent(1));
text->visibilityChanged(true);
text->selectText(searchResult.exp, std::make_pair(searchResult.start, searchResult.len));
}

void GenericChatForm::clearChatArea()
{
clearChatArea(/* confirm = */ true, /* inform = */ true);
Expand Down Expand Up @@ -941,7 +927,6 @@ void GenericChatForm::onExportChat()
void GenericChatForm::onSearchTriggered()
{
if (searchForm->isHidden()) {
searchResult.found = false;
searchForm->removeSearchPhrase();
}
disableSearchText();
Expand Down Expand Up @@ -971,27 +956,27 @@ void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch
switch (parameter.period) {
case PeriodSearch::WithTheFirst: {
bForwardSearch = true;
searchResult.pos.logIdx = chatLog.getFirstIdx();
searchResult.pos.numMatches = 0;
searchPos.logIdx = chatLog.getFirstIdx();
searchPos.numMatches = 0;
break;
}
case PeriodSearch::WithTheEnd:
case PeriodSearch::None: {
bForwardSearch = false;
searchResult.pos.logIdx = chatLog.getNextIdx();
searchResult.pos.numMatches = 0;
searchPos.logIdx = chatLog.getNextIdx();
searchPos.numMatches = 0;
break;
}
case PeriodSearch::AfterDate: {
bForwardSearch = true;
searchResult.pos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchResult.pos.numMatches = 0;
searchPos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchPos.numMatches = 0;
break;
}
case PeriodSearch::BeforeDate: {
bForwardSearch = false;
searchResult.pos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchResult.pos.numMatches = 0;
searchPos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchPos.numMatches = 0;
break;
}
}
Expand All @@ -1005,13 +990,13 @@ void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch

void GenericChatForm::onSearchUp(const QString& phrase, const ParameterSearch& parameter)
{
auto result = chatLog.searchBackward(searchResult.pos, phrase, parameter);
auto result = chatLog.searchBackward(searchPos, phrase, parameter);
handleSearchResult(result, SearchDirection::Up);
}

void GenericChatForm::onSearchDown(const QString& phrase, const ParameterSearch& parameter)
{
auto result = chatLog.searchForward(searchResult.pos, phrase, parameter);
auto result = chatLog.searchForward(searchPos, phrase, parameter);

if (result.found && result.pos.logIdx.get() > messages.end()->first.get()) {
const auto dt = chatLog.at(result.pos.logIdx).getTimestamp();
Expand All @@ -1030,11 +1015,18 @@ void GenericChatForm::handleSearchResult(SearchResult result, SearchDirection di

disableSearchText();

searchResult = result;
searchPos = result.pos;

auto const firstRenderedIdx = (messages.empty()) ? chatLog.getNextIdx() : messages.begin()->first;

renderMessages(searchResult.pos.logIdx, firstRenderedIdx, [this]{enableSearchText();});
renderMessages(searchPos.logIdx, firstRenderedIdx, [this, result] {
auto msg = messages.at(searchPos.logIdx);
chatWidget->scrollToLine(msg);

auto text = qobject_cast<Text*>(msg->getContent(1));
text->visibilityChanged(true);
text->selectText(result.exp, std::make_pair(result.start, result.len));
});
}

void GenericChatForm::renderMessage(ChatLogIdx idx)
Expand Down
3 changes: 1 addition & 2 deletions src/widget/form/genericchatform.h
Expand Up @@ -156,7 +156,6 @@ protected slots:
virtual void resizeEvent(QResizeEvent* event) final override;
virtual bool eventFilter(QObject* object, QEvent* event) final override;
void disableSearchText();
void enableSearchText();
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);

Expand Down Expand Up @@ -197,7 +196,7 @@ protected slots:

IChatLog& chatLog;
IMessageDispatcher& messageDispatcher;
SearchResult searchResult;
SearchPos searchPos;
std::map<ChatLogIdx, ChatMessage::Ptr> messages;
bool colorizeNames = false;
};
Expand Down

0 comments on commit c1d0624

Please sign in to comment.