mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
Text: detect anchors
This commit is contained in:
parent
170e2e6e29
commit
380d7b9a07
|
@ -27,6 +27,7 @@
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QGraphicsSceneMouseEvent>
|
#include <QGraphicsSceneMouseEvent>
|
||||||
#include <QFontMetrics>
|
#include <QFontMetrics>
|
||||||
|
#include <QRegExp>
|
||||||
#include <QDesktopServices>
|
#include <QDesktopServices>
|
||||||
|
|
||||||
Text::Text(const QString& txt, QFont font, bool enableElide)
|
Text::Text(const QString& txt, QFont font, bool enableElide)
|
||||||
|
@ -52,6 +53,7 @@ void Text::setText(const QString& txt)
|
||||||
text = txt;
|
text = txt;
|
||||||
text.replace("\n", "<br/>");
|
text.replace("\n", "<br/>");
|
||||||
|
|
||||||
|
detectAnchors();
|
||||||
ensureIntegrity();
|
ensureIntegrity();
|
||||||
freeResources();
|
freeResources();
|
||||||
}
|
}
|
||||||
|
@ -257,3 +259,30 @@ int Text::cursorFromPos(QPointF scenePos) const
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Text::detectAnchors()
|
||||||
|
{
|
||||||
|
// detect urls
|
||||||
|
QRegExp exp("(?:\\b)(www\\.|http[s]?:\\/\\/|ftp:\\/\\/|tox:\\/\\/|tox:)\\S+");
|
||||||
|
int offset = 0;
|
||||||
|
while ((offset = exp.indexIn(text, offset)) != -1)
|
||||||
|
{
|
||||||
|
QString url = exp.cap();
|
||||||
|
|
||||||
|
// If there's a trailing " it's a HTML attribute, e.g. a smiley img's title=":tox:"
|
||||||
|
if (url == "tox:\"")
|
||||||
|
{
|
||||||
|
offset += url.length();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add scheme if not specified
|
||||||
|
if (exp.cap(1) == "www.")
|
||||||
|
url.prepend("http://");
|
||||||
|
|
||||||
|
QString htmledUrl = QString("<a href=\"%1\">%1</a>").arg(url);
|
||||||
|
text.replace(offset, exp.cap().length(), htmledUrl);
|
||||||
|
|
||||||
|
offset += htmledUrl.length();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,3 @@
|
||||||
#ifndef TEXT_H
|
|
||||||
#define TEXT_H
|
|
||||||
|
|
||||||
#include "../chatlinecontent.h"
|
|
||||||
|
|
||||||
#include <QTextDocument>
|
|
||||||
#include <QTextCursor>
|
|
||||||
|
|
||||||
class CustomTextDocument;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Copyright (C) 2014 by Project Tox <https://tox.im>
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
||||||
|
|
||||||
|
@ -24,6 +14,16 @@ class CustomTextDocument;
|
||||||
See the COPYING file for more details.
|
See the COPYING file for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef TEXT_H
|
||||||
|
#define TEXT_H
|
||||||
|
|
||||||
|
#include "../chatlinecontent.h"
|
||||||
|
|
||||||
|
#include <QTextDocument>
|
||||||
|
#include <QTextCursor>
|
||||||
|
|
||||||
|
class CustomTextDocument;
|
||||||
|
|
||||||
class Text : public ChatLineContent
|
class Text : public ChatLineContent
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -61,6 +61,8 @@ protected:
|
||||||
|
|
||||||
int cursorFromPos(QPointF scenePos) const;
|
int cursorFromPos(QPointF scenePos) const;
|
||||||
|
|
||||||
|
void detectAnchors();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CustomTextDocument* doc = nullptr;
|
CustomTextDocument* doc = nullptr;
|
||||||
QString text;
|
QString text;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user