2014-11-16 19:58:43 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
2014-11-12 21:11:25 +08:00
|
|
|
#include "text.h"
|
|
|
|
|
2014-11-16 19:40:44 +08:00
|
|
|
#include "../customtextdocument.h"
|
|
|
|
|
2014-11-12 21:11:25 +08:00
|
|
|
#include <QFontMetrics>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QTextBlock>
|
|
|
|
#include <QAbstractTextDocumentLayout>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QFontMetrics>
|
2014-12-11 02:56:08 +08:00
|
|
|
#include <QRegExp>
|
2014-11-12 21:11:25 +08:00
|
|
|
#include <QDesktopServices>
|
|
|
|
|
2014-11-16 19:40:44 +08:00
|
|
|
Text::Text(const QString& txt, QFont font, bool enableElide)
|
2014-11-12 21:11:25 +08:00
|
|
|
: ChatLineContent()
|
|
|
|
, elide(enableElide)
|
2014-11-16 19:40:44 +08:00
|
|
|
, defFont(font)
|
2014-11-12 21:11:25 +08:00
|
|
|
{
|
|
|
|
setText(txt);
|
|
|
|
setAcceptedMouseButtons(Qt::LeftButton | Qt::RightButton);
|
2014-11-14 01:27:32 +08:00
|
|
|
|
|
|
|
ensureIntegrity();
|
|
|
|
freeResources();
|
2015-01-03 03:37:51 +08:00
|
|
|
//setCacheMode(QGraphicsItem::DeviceCoordinateCache);
|
2014-11-12 21:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Text::~Text()
|
|
|
|
{
|
|
|
|
delete doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::setText(const QString& txt)
|
|
|
|
{
|
|
|
|
text = txt;
|
|
|
|
text.replace("\n", "<br/>");
|
|
|
|
|
2014-12-11 02:56:08 +08:00
|
|
|
detectAnchors();
|
2014-11-12 21:11:25 +08:00
|
|
|
ensureIntegrity();
|
|
|
|
freeResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::setWidth(qreal w)
|
|
|
|
{
|
|
|
|
if(w == width)
|
|
|
|
return;
|
|
|
|
|
|
|
|
width = w;
|
|
|
|
|
|
|
|
if(elide)
|
|
|
|
{
|
|
|
|
QFontMetrics metrics = QFontMetrics(QFont());
|
|
|
|
elidedText = metrics.elidedText(text, Qt::ElideRight, width);
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureIntegrity();
|
|
|
|
freeResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::selectionMouseMove(QPointF scenePos)
|
|
|
|
{
|
|
|
|
ensureIntegrity();
|
|
|
|
int cur = cursorFromPos(scenePos);
|
|
|
|
if(cur >= 0)
|
2014-12-09 20:11:42 +08:00
|
|
|
{
|
2014-11-12 21:11:25 +08:00
|
|
|
cursor.setPosition(cur, QTextCursor::KeepAnchor);
|
2014-12-09 20:11:42 +08:00
|
|
|
selectedText = cursor.selectedText();
|
|
|
|
}
|
2014-11-12 21:11:25 +08:00
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::selectionStarted(QPointF scenePos)
|
|
|
|
{
|
|
|
|
ensureIntegrity();
|
|
|
|
int cur = cursorFromPos(scenePos);
|
|
|
|
if(cur >= 0)
|
|
|
|
cursor.setPosition(cur);
|
2014-12-09 20:11:42 +08:00
|
|
|
|
|
|
|
selectedText.clear();
|
|
|
|
selectedText.squeeze();
|
2014-11-12 21:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Text::selectionCleared()
|
|
|
|
{
|
|
|
|
ensureIntegrity();
|
|
|
|
cursor.setPosition(0);
|
2014-12-09 20:11:42 +08:00
|
|
|
selectedText.clear();
|
|
|
|
selectedText.squeeze();
|
2014-11-12 21:11:25 +08:00
|
|
|
freeResources();
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::selectAll()
|
|
|
|
{
|
|
|
|
ensureIntegrity();
|
|
|
|
cursor.select(QTextCursor::Document);
|
2014-12-09 20:11:42 +08:00
|
|
|
selectedText = text;
|
2014-11-12 21:11:25 +08:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Text::isOverSelection(QPointF scenePos) const
|
|
|
|
{
|
|
|
|
int cur = cursorFromPos(scenePos);
|
|
|
|
if(cur >= 0 && cursor.selectionStart() < cur && cursor.selectionEnd() >= cur)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Text::getSelectedText() const
|
|
|
|
{
|
2014-12-09 20:11:42 +08:00
|
|
|
return selectedText;
|
2014-11-12 21:11:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Text::boundingSceneRect() const
|
|
|
|
{
|
|
|
|
return QRectF(scenePos(), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRectF Text::boundingRect() const
|
|
|
|
{
|
|
|
|
return QRectF(QPointF(0, 0), size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
|
|
|
|
{
|
|
|
|
if(doc)
|
|
|
|
{
|
|
|
|
// draw selection
|
|
|
|
QAbstractTextDocumentLayout::PaintContext ctx;
|
|
|
|
QAbstractTextDocumentLayout::Selection sel;
|
|
|
|
sel.cursor = cursor;
|
|
|
|
sel.format.setBackground(QApplication::palette().color(QPalette::Highlight));
|
|
|
|
sel.format.setForeground(QApplication::palette().color(QPalette::HighlightedText));
|
|
|
|
ctx.selections.append(sel);
|
|
|
|
|
|
|
|
// draw text
|
|
|
|
doc->documentLayout()->draw(painter, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_UNUSED(option)
|
|
|
|
Q_UNUSED(widget)
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::visibilityChanged(bool visible)
|
|
|
|
{
|
|
|
|
isVisible = visible;
|
|
|
|
|
|
|
|
if(visible)
|
|
|
|
ensureIntegrity();
|
|
|
|
else
|
|
|
|
freeResources();
|
|
|
|
}
|
|
|
|
|
2014-12-10 23:45:12 +08:00
|
|
|
qreal Text::getAscent() const
|
2014-11-12 21:11:25 +08:00
|
|
|
{
|
|
|
|
return vOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if(event->button() == Qt::LeftButton)
|
|
|
|
event->accept(); // grabber
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
QString anchor = doc->documentLayout()->anchorAt(event->pos());
|
|
|
|
|
|
|
|
// open anchors in browser
|
|
|
|
if(!anchor.isEmpty())
|
|
|
|
QDesktopServices::openUrl(anchor);
|
|
|
|
}
|
|
|
|
|
2014-12-09 20:11:42 +08:00
|
|
|
QString Text::getText() const
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2014-11-12 21:11:25 +08:00
|
|
|
void Text::ensureIntegrity()
|
|
|
|
{
|
|
|
|
if(!doc)
|
|
|
|
{
|
2014-11-16 19:40:44 +08:00
|
|
|
doc = new CustomTextDocument();
|
|
|
|
doc->setDefaultFont(defFont);
|
2014-11-12 21:11:25 +08:00
|
|
|
|
|
|
|
if(!elide)
|
|
|
|
{
|
|
|
|
doc->setHtml(text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QTextOption opt;
|
|
|
|
opt.setWrapMode(QTextOption::NoWrap);
|
|
|
|
doc->setDefaultTextOption(opt);
|
|
|
|
doc->setPlainText(elidedText);
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = QTextCursor(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
doc->setTextWidth(width);
|
|
|
|
doc->documentLayout()->update();
|
|
|
|
|
|
|
|
if(doc->firstBlock().layout()->lineCount() > 0)
|
2014-12-10 23:35:07 +08:00
|
|
|
vOffset = doc->firstBlock().layout()->lineAt(0).ascent();
|
2014-11-12 21:11:25 +08:00
|
|
|
|
|
|
|
if(size != idealSize())
|
|
|
|
{
|
|
|
|
prepareGeometryChange();
|
|
|
|
size = idealSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Text::freeResources()
|
|
|
|
{
|
|
|
|
if(doc && !isVisible && !cursor.hasSelection())
|
|
|
|
{
|
|
|
|
delete doc;
|
|
|
|
doc = nullptr;
|
|
|
|
cursor = QTextCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QSizeF Text::idealSize()
|
|
|
|
{
|
|
|
|
if(doc)
|
|
|
|
return QSizeF(doc->idealWidth(), doc->size().height());
|
|
|
|
|
|
|
|
return QSizeF();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Text::cursorFromPos(QPointF scenePos) const
|
|
|
|
{
|
|
|
|
if(doc)
|
2014-12-11 02:31:27 +08:00
|
|
|
return doc->documentLayout()->hitTest(mapFromScene(scenePos), Qt::FuzzyHit);
|
2014-11-12 21:11:25 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-11 02:56:08 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 16:59:35 +08:00
|
|
|
|
|
|
|
QString Text::toHtmlChars(const QString &str)
|
|
|
|
{
|
|
|
|
static QList<QPair<QString, QString>> replaceList = {{"&","&"}, {">",">"}, {"<","<"}};
|
|
|
|
QString res = str;
|
|
|
|
|
|
|
|
for (auto &it : replaceList)
|
|
|
|
res = res.replace(it.first,it.second);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|