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

Merge pull request #5793

TriKriSta (5):
      fix: data validation during the search (fix: #5791, #5723)
      feat: check chat status before start a search
      feat: save selected search text after scrolling up
      refactor: optimize load messages during the search
      fix: update workerStb
This commit is contained in:
sudden6 2019-08-28 21:13:38 +02:00
commit 834baff556
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
5 changed files with 105 additions and 36 deletions

View File

@ -683,7 +683,7 @@ void ChatLog::scrollToLine(ChatLine::Ptr line)
workerStb = false;
} else {
updateSceneRect();
verticalScrollBar()->setValue(line->sceneBoundingRect().top()); // NOTE: start here
verticalScrollBar()->setValue(line->sceneBoundingRect().top());
}
}
@ -936,10 +936,12 @@ void ChatLog::onWorkerTimeout()
updateMultiSelectionRect();
// scroll
if (workerStb)
if (workerStb) {
scrollToBottom();
else
workerStb = false;
} else {
scrollToLine(workerAnchorLine);
}
// don't keep a Ptr to the anchor line
workerAnchorLine = ChatLine::Ptr();

View File

@ -174,13 +174,19 @@ SearchResult ChatHistory::searchBackward(SearchPos startIdx, const QString& phra
history->getDateWhereFindPhrase(f.getPublicKey().toString(), earliestMessageDate, phrase,
parameter);
auto loadIdx = history->getNumMessagesForFriendBeforeDate(f.getPublicKey(), dateWherePhraseFound);
loadHistoryIntoSessionChatLog(ChatLogIdx(loadIdx));
if (dateWherePhraseFound.isValid()) {
auto loadIdx = history->getNumMessagesForFriendBeforeDate(f.getPublicKey(), dateWherePhraseFound);
loadHistoryIntoSessionChatLog(ChatLogIdx(loadIdx));
// Reset search pos to the message we just loaded to avoid a double search
startIdx.logIdx = ChatLogIdx(loadIdx);
startIdx.numMatches = 0;
return sessionChatLog.searchBackward(startIdx, phrase, parameter);
// Reset search pos to the message we just loaded to avoid a double search
startIdx.logIdx = ChatLogIdx(loadIdx);
startIdx.numMatches = 0;
return sessionChatLog.searchBackward(startIdx, phrase, parameter);
}
SearchResult ret;
ret.found = false;
return ret;
}
ChatLogIdx ChatHistory::getFirstIdx() const

View File

@ -70,7 +70,7 @@ struct SearchPos
struct SearchResult
{
bool found;
bool found{false};
SearchPos pos;
size_t start;
size_t len;

View File

@ -685,7 +685,11 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time)
}
if (begin != end) {
renderMessages(begin, end);
if (searchResult.found == true && searchResult.pos.logIdx == end) {
renderMessages(begin, end, [this]{enableSearchText();});
} else {
renderMessages(begin, end);
}
} else {
chatWidget->setScroll(true);
}
@ -730,13 +734,23 @@ void GenericChatForm::removeLastsMessages(const int num)
void GenericChatForm::disableSearchText()
{
auto msgIt = messages.find(searchPos.logIdx);
auto msgIt = messages.find(searchResult.pos.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);
@ -933,6 +947,7 @@ void GenericChatForm::onExportChat()
void GenericChatForm::onSearchTriggered()
{
if (searchForm->isHidden()) {
searchResult.found = false;
searchForm->removeSearchPhrase();
}
disableSearchText();
@ -940,39 +955,43 @@ void GenericChatForm::onSearchTriggered()
void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch& parameter)
{
disableSearchText();
if (phrase.isEmpty()) {
disableSearchText();
if (!parameter.time.isNull()) {
LoadHistoryDialog::LoadType type = (parameter.period == PeriodSearch::BeforeDate)
? LoadHistoryDialog::to : LoadHistoryDialog::from;
loadHistory(parameter.time, type);
return;
}
if (chatLog.getNextIdx().get() == messages.rbegin()->first.get() + 1) {
disableSearchText();
} else {
goToCurrentDate();
}
bool bForwardSearch = false;
switch (parameter.period) {
case PeriodSearch::WithTheFirst: {
bForwardSearch = true;
searchPos.logIdx = chatLog.getFirstIdx();
searchPos.numMatches = 0;
searchResult.pos.logIdx = chatLog.getFirstIdx();
searchResult.pos.numMatches = 0;
break;
}
case PeriodSearch::WithTheEnd:
case PeriodSearch::None: {
bForwardSearch = false;
searchPos.logIdx = chatLog.getNextIdx();
searchPos.numMatches = 0;
searchResult.pos.logIdx = chatLog.getNextIdx();
searchResult.pos.numMatches = 0;
break;
}
case PeriodSearch::AfterDate: {
bForwardSearch = true;
searchPos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchPos.numMatches = 0;
searchResult.pos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchResult.pos.numMatches = 0;
break;
}
case PeriodSearch::BeforeDate: {
bForwardSearch = false;
searchPos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchPos.numMatches = 0;
searchResult.pos.logIdx = firstItemAfterDate(parameter.time.date(), chatLog);
searchResult.pos.numMatches = 0;
break;
}
}
@ -986,13 +1005,13 @@ void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch
void GenericChatForm::onSearchUp(const QString& phrase, const ParameterSearch& parameter)
{
auto result = chatLog.searchBackward(searchPos, phrase, parameter);
auto result = chatLog.searchBackward(searchResult.pos, phrase, parameter);
handleSearchResult(result, SearchDirection::Up);
}
void GenericChatForm::onSearchDown(const QString& phrase, const ParameterSearch& parameter)
{
auto result = chatLog.searchForward(searchPos, phrase, parameter);
auto result = chatLog.searchForward(searchResult.pos, phrase, parameter);
handleSearchResult(result, SearchDirection::Down);
}
@ -1005,17 +1024,58 @@ void GenericChatForm::handleSearchResult(SearchResult result, SearchDirection di
disableSearchText();
searchPos = result.pos;
searchResult = result;
auto const firstRenderedIdx = (messages.empty()) ? chatLog.getNextIdx() : messages.begin()->first;
auto searchIdx = result.pos.logIdx;
renderMessages(searchPos.logIdx, firstRenderedIdx, [this, result] {
auto msg = messages.at(searchPos.logIdx);
chatWidget->scrollToLine(msg);
auto firstRenderedIdx = messages.begin()->first;
auto endRenderedIdx = messages.rbegin()->first;
auto text = qobject_cast<Text*>(msg->getContent(1));
text->selectText(result.exp, std::make_pair(result.start, result.len));
});
if (direction == SearchDirection::Up) {
if (searchIdx.get() < firstRenderedIdx.get()) {
if (searchIdx.get() > DEF_NUM_MSG_TO_LOAD / 2) {
firstRenderedIdx = ChatLogIdx(searchIdx.get() - DEF_NUM_MSG_TO_LOAD / 2);
} else {
firstRenderedIdx = ChatLogIdx(0);
}
}
if (endRenderedIdx.get() - firstRenderedIdx.get() > DEF_NUM_MSG_TO_LOAD) {
endRenderedIdx = ChatLogIdx(firstRenderedIdx.get() + DEF_NUM_MSG_TO_LOAD);
}
} else {
if (searchIdx.get() < firstRenderedIdx.get()) {
firstRenderedIdx = searchIdx;
}
if (firstRenderedIdx == searchIdx || searchIdx.get() > endRenderedIdx.get()) {
if (searchIdx.get() + DEF_NUM_MSG_TO_LOAD > chatLog.getNextIdx().get()) {
endRenderedIdx = chatLog.getNextIdx();
} else {
endRenderedIdx = ChatLogIdx(searchIdx.get() + DEF_NUM_MSG_TO_LOAD);
}
}
if (endRenderedIdx.get() - firstRenderedIdx.get() > DEF_NUM_MSG_TO_LOAD) {
if (endRenderedIdx.get() > DEF_NUM_MSG_TO_LOAD) {
firstRenderedIdx = ChatLogIdx(endRenderedIdx.get() - DEF_NUM_MSG_TO_LOAD);
} else {
firstRenderedIdx = ChatLogIdx(0);
}
}
}
if (!messages.empty() && (firstRenderedIdx.get() < messages.begin()->first.get()
|| endRenderedIdx.get() > messages.rbegin()->first.get())) {
chatWidget->clear();
messages.clear();
auto mediator = endRenderedIdx;
endRenderedIdx = firstRenderedIdx;
firstRenderedIdx = mediator;
}
renderMessages(endRenderedIdx, firstRenderedIdx, [this]{enableSearchText();});
}
void GenericChatForm::renderMessage(ChatLogIdx idx)

View File

@ -156,6 +156,7 @@ protected:
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);
@ -198,7 +199,7 @@ protected:
IChatLog& chatLog;
IMessageDispatcher& messageDispatcher;
SearchPos searchPos;
SearchResult searchResult;
std::map<ChatLogIdx, ChatMessage::Ptr> messages;
bool colorizeNames = false;
};