1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/chatlog/content/text.cpp

371 lines
8.2 KiB
C++
Raw Normal View History

2014-11-16 19:58:43 +08:00
/*
Copyright © 2014-2015 by The qTox Project
2014-11-16 19:58:43 +08:00
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
2014-11-16 19:58:43 +08:00
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.
qTox is distributed in the hope that it will be useful,
2014-11-16 19:58:43 +08:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2014-11-16 19:58:43 +08:00
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
2014-11-16 19:58:43 +08:00
*/
2014-11-12 21:11:25 +08:00
#include "text.h"
2015-02-02 18:01:01 +08:00
#include "../documentcache.h"
2014-11-16 19:40:44 +08:00
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 <QDesktopServices>
2015-01-09 22:50:13 +08:00
#include <QTextFragment>
2014-11-12 21:11:25 +08:00
2015-02-15 17:45:07 +08:00
Text::Text(const QString& txt, QFont font, bool enableElide, const QString &rwText, const QColor c)
: rawText(rwText)
2014-11-12 21:11:25 +08:00
, elide(enableElide)
2014-11-16 19:40:44 +08:00
, defFont(font)
2015-02-15 17:45:07 +08:00
, color(c)
2014-11-12 21:11:25 +08:00
{
setText(txt);
2015-02-01 01:37:20 +08:00
setAcceptedMouseButtons(Qt::LeftButton);
2015-02-12 00:03:38 +08:00
setAcceptHoverEvents(true);
2014-11-12 21:11:25 +08:00
}
Text::~Text()
{
if (doc)
2015-02-02 18:01:01 +08:00
DocumentCache::getInstance().push(doc);
2014-11-12 21:11:25 +08:00
}
void Text::setText(const QString& txt)
{
2015-01-03 22:03:33 +08:00
text = txt;
dirty = true;
2014-11-12 21:11:25 +08:00
}
void Text::setWidth(qreal w)
{
width = w;
2015-01-19 18:14:53 +08:00
dirty = true;
2014-11-12 21:11:25 +08:00
if (elide)
2014-11-12 21:11:25 +08:00
{
QFontMetrics metrics = QFontMetrics(defFont);
2014-11-12 21:11:25 +08:00
elidedText = metrics.elidedText(text, Qt::ElideRight, width);
}
2015-01-19 18:14:53 +08:00
regenerate();
2014-11-12 21:11:25 +08:00
}
void Text::selectionMouseMove(QPointF scenePos)
{
if (!doc)
return;
2014-11-12 21:11:25 +08:00
int cur = cursorFromPos(scenePos);
if (cur >= 0)
2014-12-09 20:11:42 +08:00
{
selectionEnd = cur;
selectedText = extractSanitizedText(getSelectionStart(), getSelectionEnd());
2014-12-09 20:11:42 +08:00
}
2014-11-12 21:11:25 +08:00
update();
}
void Text::selectionStarted(QPointF scenePos)
{
int cur = cursorFromPos(scenePos);
if (cur >= 0)
{
selectionEnd = cur;
selectionAnchor = cur;
}
2014-11-12 21:11:25 +08:00
}
void Text::selectionCleared()
{
2014-12-09 20:11:42 +08:00
selectedText.clear();
selectedText.squeeze();
2014-11-12 21:11:25 +08:00
// Do not reset selectionAnchor!
selectionEnd = -1;
2014-11-12 21:11:25 +08:00
update();
}
2015-01-19 22:19:54 +08:00
void Text::selectionDoubleClick(QPointF scenePos)
{
if (!doc)
2015-01-19 22:19:54 +08:00
return;
int cur = cursorFromPos(scenePos);
if (cur >= 0)
2015-01-19 22:19:54 +08:00
{
QTextCursor cursor(doc);
cursor.setPosition(cur);
cursor.select(QTextCursor::WordUnderCursor);
selectionAnchor = cursor.selectionStart();
selectionEnd = cursor.selectionEnd();
selectedText = extractSanitizedText(getSelectionStart(), getSelectionEnd());
2015-01-19 22:19:54 +08:00
}
update();
}
void Text::selectionFocusChanged(bool focusIn)
{
selectionHasFocus = focusIn;
update();
}
2014-11-12 21:11:25 +08:00
bool Text::isOverSelection(QPointF scenePos) const
{
int cur = cursorFromPos(scenePos);
if (getSelectionStart() < cur && getSelectionEnd() >= cur)
2014-11-12 21:11:25 +08:00
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::boundingRect() const
{
return QRectF(QPointF(0, 0), size);
}
void Text::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
if (doc)
2014-11-12 21:11:25 +08:00
{
2015-02-03 23:17:37 +08:00
painter->setClipRect(boundingRect());
2014-11-12 21:11:25 +08:00
// draw selection
QAbstractTextDocumentLayout::PaintContext ctx;
QAbstractTextDocumentLayout::Selection sel;
if (hasSelection())
{
sel.cursor = QTextCursor(doc);
sel.cursor.setPosition(getSelectionStart());
sel.cursor.setPosition(getSelectionEnd(), QTextCursor::KeepAnchor);
}
const QColor selectionColor = QColor::fromRgbF(0.23, 0.68, 0.91);
sel.format.setBackground(selectionColor.lighter(selectionHasFocus ? 100 : 160));
sel.format.setForeground(selectionHasFocus ? Qt::white : Qt::black);
2014-11-12 21:11:25 +08:00
ctx.selections.append(sel);
2015-02-15 17:45:07 +08:00
ctx.palette.setColor(QPalette::Text, color);
2014-11-12 21:11:25 +08:00
// draw text
doc->documentLayout()->draw(painter, ctx);
}
Q_UNUSED(option)
Q_UNUSED(widget)
}
void Text::visibilityChanged(bool visible)
{
keepInMemory = visible;
2014-11-12 21:11:25 +08:00
2015-01-19 18:14:53 +08:00
regenerate();
update();
2014-11-12 21:11:25 +08:00
}
2014-12-10 23:45:12 +08:00
qreal Text::getAscent() const
2014-11-12 21:11:25 +08:00
{
2015-01-19 18:14:53 +08:00
return ascent;
2014-11-12 21:11:25 +08:00
}
void Text::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
2014-11-12 21:11:25 +08:00
event->accept(); // grabber
}
void Text::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if (!doc)
return;
2014-11-12 21:11:25 +08:00
QString anchor = doc->documentLayout()->anchorAt(event->pos());
2015-01-19 18:14:53 +08:00
// open anchor in browser
if (!anchor.isEmpty())
2014-11-12 21:11:25 +08:00
QDesktopServices::openUrl(anchor);
}
2015-02-12 00:03:38 +08:00
void Text::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
if (!doc)
2015-02-12 16:02:49 +08:00
return;
2015-02-12 00:03:38 +08:00
QString anchor = doc->documentLayout()->anchorAt(event->pos());
if (anchor.isEmpty())
setCursor(Qt::IBeamCursor);
2015-02-12 00:03:38 +08:00
else
setCursor(Qt::PointingHandCursor);
2015-02-25 19:02:52 +08:00
// tooltip
setToolTip(extractImgTooltip(cursorFromPos(event->scenePos(), false)));
2015-02-12 00:03:38 +08:00
}
2014-12-09 20:11:42 +08:00
QString Text::getText() const
{
2015-01-05 01:21:35 +08:00
return rawText;
2014-12-09 20:11:42 +08:00
}
2015-01-19 18:14:53 +08:00
void Text::regenerate()
2014-11-12 21:11:25 +08:00
{
if (!doc)
2014-11-12 21:11:25 +08:00
{
2015-02-02 18:01:01 +08:00
doc = DocumentCache::getInstance().pop();
dirty = true;
}
2014-11-12 21:11:25 +08:00
if (dirty)
{
2015-02-06 23:12:24 +08:00
doc->setDefaultFont(defFont);
if (!elide)
2014-11-12 21:11:25 +08:00
doc->setHtml(text);
else
doc->setPlainText(elidedText);
2015-02-04 05:06:31 +08:00
// wrap mode
QTextOption opt;
opt.setWrapMode(elide ? QTextOption::NoWrap : QTextOption::WrapAtWordBoundaryOrAnywhere);
doc->setDefaultTextOption(opt);
// width
doc->setTextWidth(width);
2015-02-08 01:52:00 +08:00
doc->documentLayout()->update();
2014-11-12 21:11:25 +08:00
2015-02-02 18:01:01 +08:00
// update ascent
if (doc->firstBlock().layout()->lineCount() > 0)
2015-02-02 18:01:01 +08:00
ascent = doc->firstBlock().layout()->lineAt(0).ascent();
2014-11-12 21:11:25 +08:00
2015-02-02 18:01:01 +08:00
// let the scene know about our change in size
if (size != idealSize())
2015-02-02 18:01:01 +08:00
prepareGeometryChange();
2014-11-12 21:11:25 +08:00
2015-02-02 18:01:01 +08:00
// get the new width and height
size = idealSize();
2015-02-02 18:01:01 +08:00
dirty = false;
}
2015-01-19 18:14:53 +08:00
// if we are not visible -> free mem
if (!keepInMemory)
2015-01-19 18:14:53 +08:00
freeResources();
2014-11-12 21:11:25 +08:00
}
void Text::freeResources()
{
2015-02-02 18:01:01 +08:00
DocumentCache::getInstance().push(doc);
2015-01-19 18:14:53 +08:00
doc = nullptr;
2014-11-12 21:11:25 +08:00
}
QSizeF Text::idealSize()
{
if (doc)
return QSizeF(qMin(doc->idealWidth(), width), doc->size().height());
2014-11-12 21:11:25 +08:00
return size;
2014-11-12 21:11:25 +08:00
}
2015-02-25 19:02:52 +08:00
int Text::cursorFromPos(QPointF scenePos, bool fuzzy) const
2014-11-12 21:11:25 +08:00
{
if (doc)
2015-02-25 19:02:52 +08:00
return doc->documentLayout()->hitTest(mapFromScene(scenePos), fuzzy ? Qt::FuzzyHit : Qt::ExactHit);
2014-11-12 21:11:25 +08:00
return -1;
}
int Text::getSelectionEnd() const
{
return qMax(selectionAnchor, selectionEnd);
}
int Text::getSelectionStart() const
{
return qMin(selectionAnchor, selectionEnd);
}
bool Text::hasSelection() const
{
return selectionEnd >= 0;
}
QString Text::extractSanitizedText(int from, int to) const
{
if (!doc)
return "";
QString txt;
QTextBlock block = doc->firstBlock();
for (QTextBlock::Iterator itr = block.begin(); itr!=block.end(); ++itr)
{
int pos = itr.fragment().position(); //fragment position -> position of the first character in the fragment
if (itr.fragment().charFormat().isImageFormat())
{
QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat();
QString key = imgFmt.name(); //img key (eg. key::D for :D)
QString rune = key.mid(4);
if (pos >= from && pos < to)
{
txt += rune;
pos++;
}
}
else
{
for (QChar c : itr.fragment().text())
{
if (pos >= from && pos < to)
txt += c;
pos++;
}
}
}
return txt;
}
2015-02-25 19:02:52 +08:00
QString Text::extractImgTooltip(int pos) const
{
for (QTextBlock::Iterator itr = doc->firstBlock().begin(); itr!=doc->firstBlock().end(); ++itr)
2015-02-25 19:02:52 +08:00
{
if (itr.fragment().contains(pos) && itr.fragment().charFormat().isImageFormat())
2015-02-25 19:02:52 +08:00
{
QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat();
return imgFmt.toolTip();
}
}
return QString();
}