mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
feat: save selected search text after scrolling up
This commit is contained in:
parent
76679de2e0
commit
a6a0481ddc
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ struct SearchPos
|
|||
|
||||
struct SearchResult
|
||||
{
|
||||
bool found;
|
||||
bool found{false};
|
||||
SearchPos pos;
|
||||
size_t start;
|
||||
size_t len;
|
||||
|
|
|
@ -685,7 +685,11 @@ void GenericChatForm::loadHistoryTo(const QDateTime &time)
|
|||
}
|
||||
|
||||
if (begin != end) {
|
||||
renderMessages(begin, end);
|
||||
if (searchResult.found == true) {
|
||||
renderMessages(begin, searchResult.pos.logIdx, [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();
|
||||
|
@ -962,27 +977,27 @@ void GenericChatForm::searchInBegin(const QString& phrase, const ParameterSearch
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
@ -996,13 +1011,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);
|
||||
|
||||
if (result.found && result.pos.logIdx.get() > messages.end()->first.get()) {
|
||||
const auto dt = chatLog.at(result.pos.logIdx).getTimestamp();
|
||||
|
@ -1021,18 +1036,11 @@ void GenericChatForm::handleSearchResult(SearchResult result, SearchDirection di
|
|||
|
||||
disableSearchText();
|
||||
|
||||
searchPos = result.pos;
|
||||
searchResult = result;
|
||||
|
||||
auto const firstRenderedIdx = (messages.empty()) ? chatLog.getNextIdx() : messages.begin()->first;
|
||||
|
||||
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));
|
||||
});
|
||||
renderMessages(searchResult.pos.logIdx, firstRenderedIdx, [this]{enableSearchText();});
|
||||
}
|
||||
|
||||
void GenericChatForm::renderMessage(ChatLogIdx idx)
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user