21 #include "../documentcache.h"
23 #include <QAbstractTextDocumentLayout>
24 #include <QApplication>
26 #include <QDesktopServices>
27 #include <QFontMetrics>
28 #include <QGraphicsSceneMouseEvent>
32 #include <QTextFragment>
34 Text::Text(
const QString& txt,
const QFont& font,
bool enableElide,
const QString& rwText,
35 const TextType& type,
const QColor& custom)
39 , defStyleSheet(
Style::getStylesheet(QStringLiteral(
"chatArea/innerStyle.css"), font))
45 setAcceptedMouseButtons(Qt::LeftButton);
46 setAcceptHoverEvents(
true);
69 auto cursor =
doc->find(txt, point.first);
82 auto cursor =
doc->find(exp, point.first);
144 QTextCursor cursor(
doc);
145 cursor.setPosition(cur);
146 cursor.select(QTextCursor::WordUnderCursor);
165 QTextCursor cursor(
doc);
166 cursor.setPosition(cur);
167 cursor.select(QTextCursor::BlockUnderCursor);
172 if (cursor.block().isValid() && cursor.block().blockNumber() != 0)
208 return QRectF(QPointF(0, 0),
size);
211 void Text::paint(QPainter* painter,
const QStyleOptionGraphicsItem* option, QWidget* widget)
222 QAbstractTextDocumentLayout::PaintContext ctx;
223 QAbstractTextDocumentLayout::Selection sel;
226 sel.cursor = QTextCursor(
doc);
235 ctx.selections.append(sel);
236 ctx.palette.setColor(QPalette::Text,
color);
239 doc->documentLayout()->draw(painter, ctx);
266 if (event->button() == Qt::LeftButton)
275 QString anchor =
doc->documentLayout()->anchorAt(event->pos());
278 if (!anchor.isEmpty())
279 QDesktopServices::openUrl(anchor);
287 QString anchor =
doc->documentLayout()->anchorAt(event->pos());
289 if (anchor.isEmpty())
290 setCursor(Qt::IBeamCursor);
292 setCursor(Qt::PointingHandCursor);
310 QTextCursor cursor(
doc);
312 return cursor.charFormat().anchorHref();
326 QFontMetrics metrics = QFontMetrics(
defFont);
327 QString elidedText = metrics.elidedText(
text, Qt::ElideRight, qRound(
width));
329 doc->setPlainText(elidedText);
337 opt.setWrapMode(
elide ? QTextOption::NoWrap : QTextOption::WrapAtWordBoundaryOrAnywhere);
338 doc->setDefaultTextOption(opt);
342 doc->documentLayout()->update();
345 if (
doc->firstBlock().layout()->lineCount() > 0)
346 ascent =
doc->firstBlock().layout()->lineAt(0).ascent();
350 prepareGeometryChange();
380 return doc->documentLayout()->hitTest(mapFromScene(scenePos),
381 fuzzy ? Qt::FuzzyHit : Qt::ExactHit);
408 QTextBlock begin =
doc->findBlock(from);
409 QTextBlock end =
doc->findBlock(to);
410 for (QTextBlock block = begin; block != end.next() && block.isValid(); block = block.next()) {
411 for (QTextBlock::Iterator itr = block.begin(); itr != block.end(); ++itr) {
412 int pos = itr.fragment().position();
415 if (itr.fragment().charFormat().isImageFormat()) {
416 QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat();
417 QString key = imgFmt.name();
418 QString rune = key.mid(4);
420 if (pos >= from && pos < to) {
425 for (QChar c : itr.fragment().text()) {
426 if (pos >= from && pos < to)
444 for (QTextBlock::Iterator itr =
doc->firstBlock().begin(); itr !=
doc->firstBlock().end(); ++itr) {
445 if (itr.fragment().contains(pos) && itr.fragment().charFormat().isImageFormat()) {
446 QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat();
447 return imgFmt.toolTip();
456 if (!cursor.isNull()) {
457 cursor.beginEditBlock();
458 cursor.setPosition(point.first);
459 cursor.setPosition(point.first + point.second, QTextCursor::KeepAnchor);
460 cursor.endEditBlock();
462 QTextCharFormat format;
464 cursor.mergeCharFormat(format);