Link @1234 type text to EIDs in the comments viewer

This commit is contained in:
baldurk
2020-10-23 13:57:27 +01:00
parent 9779c56333
commit 369120cddf
4 changed files with 130 additions and 23 deletions
+104 -23
View File
@@ -29,6 +29,8 @@
#include "scintilla/include/qt/ScintillaEdit.h"
#include "ui_CommentView.h"
static const sptr_t link_style = 100;
CommentView::CommentView(ICaptureContext &ctx, QWidget *parent)
: QFrame(parent), ui(new Ui::CommentView), m_Ctx(ctx)
{
@@ -47,22 +49,67 @@ CommentView::CommentView(ICaptureContext &ctx, QWidget *parent)
ConfigureSyntax(m_commentsEditor, SCLEX_NULL);
QObject::connect(m_commentsEditor, &ScintillaEdit::modified, [this](int type, int, int, int,
const QByteArray &, int, int,
int) {
QObject::connect(
m_commentsEditor, &ScintillaEdit::modified,
[this](int type, int position, int length, int, const QByteArray &, int, int, int) {
if(m_ignoreModifications)
return;
// if there has been a change, restyle the region around the modification. We can't use just
// word boundaries so search back to the last whitespace character - this means we will
// restyle at most a line, likely much less.
if(type & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT))
{
int start = m_commentsEditor->wordStartPosition(position, false);
while(!isspace(m_commentsEditor->charAt(start)) && start > 0)
start--;
int end = m_commentsEditor->wordEndPosition(position + length, false);
while(!isspace(m_commentsEditor->charAt(end)) && end < m_commentsEditor->length())
end++;
if(type & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT | SC_MOD_BEFOREINSERT | SC_MOD_BEFOREDELETE))
{
QString text = QString::fromUtf8(m_commentsEditor->getText(m_commentsEditor->textLength() + 1));
text.remove(QLatin1Char('\r'));
m_ignoreModifications = true;
m_Ctx.SetNotes(lit("comments"), text);
m_ignoreModifications = false;
}
});
if(start < 0)
start = 0;
if(end < 0 || end > m_commentsEditor->length())
end = m_commentsEditor->length();
Restyle(start, end);
}
if(m_ignoreModifications)
return;
if(type & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT | SC_MOD_BEFOREINSERT | SC_MOD_BEFOREDELETE))
{
QString text =
QString::fromUtf8(m_commentsEditor->getText(m_commentsEditor->textLength() + 1));
text.remove(QLatin1Char('\r'));
m_ignoreModifications = true;
m_Ctx.SetNotes(lit("comments"), text);
m_ignoreModifications = false;
}
});
m_commentsEditor->styleSetHotSpot(link_style, true);
m_commentsEditor->styleSetFore(link_style, SCINTILLA_COLOUR(0, 0, 255));
QObject::connect(
m_commentsEditor, &ScintillaEdit::hotSpotClick, [this](int position, int modifiers) {
int start = position;
while(m_commentsEditor->styleAt(start - 1) == link_style)
start--;
int end = position;
while(m_commentsEditor->styleAt(end + 1) == link_style)
end++;
QString eventLink = QString::fromLatin1(m_commentsEditor->get_text_range(start, end + 1));
if(eventLink[0] == QLatin1Char('@'))
{
eventLink.remove(0, 1);
bool ok = false;
uint32_t EID = eventLink.toUInt(&ok);
if(ok)
m_Ctx.SetEventID({}, EID, EID);
}
});
ui->mainLayout->addWidget(m_commentsEditor);
@@ -81,15 +128,13 @@ CommentView::~CommentView()
void CommentView::OnCaptureClosed()
{
SetComments(rdcstr());
m_ignoreModifications = true;
m_commentsEditor->setText("");
m_commentsEditor->emptyUndoBuffer();
}
void CommentView::OnCaptureLoaded()
{
m_commentsEditor->setText(m_Ctx.GetNotes("comments").c_str());
m_commentsEditor->emptyUndoBuffer();
SetComments(m_Ctx.GetNotes("comments"));
m_ignoreModifications = false;
}
@@ -98,14 +143,50 @@ void CommentView::OnEventChanged(uint32_t eventId)
if(m_ignoreModifications)
return;
QString oldText = QString::fromUtf8(m_commentsEditor->getText(m_commentsEditor->textLength() + 1));
QString oldText = GetComments();
QString newText = m_Ctx.GetNotes("comments");
if(oldText != newText)
SetComments(newText);
}
void CommentView::Restyle(int start, int end)
{
m_commentsEditor->startStyling(start, 0);
m_commentsEditor->setStyling(end - start, STYLE_DEFAULT);
static QRegularExpression event_links(lit("(\\b|\\s|^)(@\\d+)\\b"));
QString t(GetComments());
QRegularExpressionMatch match = event_links.match(t, start);
while(match.hasMatch())
{
m_ignoreModifications = true;
m_commentsEditor->setText(newText.toUtf8().data());
m_commentsEditor->emptyUndoBuffer();
m_ignoreModifications = false;
sptr_t offset = match.capturedStart(2);
if(offset > end)
break;
m_commentsEditor->startStyling(offset, 0);
m_commentsEditor->setStyling(match.capturedLength(2), link_style);
match = event_links.match(t, offset + 1);
}
}
void CommentView::SetComments(const rdcstr &text)
{
m_ignoreModifications = true;
m_commentsEditor->setText(text.c_str());
Restyle(0, m_commentsEditor->length());
m_commentsEditor->emptyUndoBuffer();
m_ignoreModifications = false;
}
rdcstr CommentView::GetComments()
{
return QString::fromUtf8(m_commentsEditor->getText(m_commentsEditor->textLength() + 1));
}