gui: anchor not only first word of nickname, if any

This commit is contained in:
chelovechishko 2018-07-02 21:22:22 +09:00
parent 4eb060e154
commit 5925aa06fe

View File

@ -251,22 +251,53 @@ bool RSTextBrowser::checkImage(QPoint pos, QString &imageStr)
*/ */
QString RSTextBrowser::anchorForPosition(const QPoint &pos) const QString RSTextBrowser::anchorForPosition(const QPoint &pos) const
{ {
// Many calls when time label shows up
QTextCursor cursor = cursorForPosition(pos); QTextCursor cursor = cursorForPosition(pos);
cursor.select(QTextCursor::WordUnderCursor); cursor.select(QTextCursor::WordUnderCursor);
QString word = cursor.selectedText();
QString anchor = ""; QString anchor = "";
if (!cursor.selectedText().isEmpty()){ if (word.isEmpty())
return anchor;
// For finding positions
QTextCursor cursor_line = cursorForPosition(pos);
cursor_line.select(QTextCursor::LineUnderCursor);
QString line = cursor_line.selectedText();
// End of nickname (more or less, of course, because some can has colon in
// name)
int end_of_name = line.indexOf(": ") + 1;
// Start of nickname (after time)
int space_index = line.indexOf(' ') + 1;
int word_index = line.indexOf(word) + 1;
int once = 1;
cursor_line.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor, once);
bool after_name = cursor_line.position() + end_of_name < cursor.position();
bool cursor_after_time = space_index < word_index;
bool cursor_within_name = word_index <= end_of_name;
if (!after_name && cursor_after_time && cursor_within_name) {
cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor, once);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor,
space_index);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor,
end_of_name - space_index);
if (cursor.selectedText().isEmpty()) {
return anchor;
}
} else {
return anchor;
}
QRegExp rx("<a name=\"(.*)\"",Qt::CaseSensitive, QRegExp::RegExp2); QRegExp rx("<a name=\"(.*)\"",Qt::CaseSensitive, QRegExp::RegExp2);
rx.setMinimal(true); rx.setMinimal(true);
QString sel = cursor.selection().toHtml(); QString sel = cursor.selection().toHtml();
QStringList anchors; QStringList anchors;
int pos=0; int position = 0;
while ((pos = rx.indexIn(sel,pos)) != -1) { while ((position = rx.indexIn(sel, position)) != -1) {
anchors << rx.cap(1); anchors << rx.cap(1);
pos += rx.matchedLength(); position += rx.matchedLength();
} }
if (!anchors.isEmpty()){ if (!anchors.isEmpty()) {
anchor = anchors.at(0); anchor = anchors.at(0);
} }
}
return anchor; return anchor;
} }