Beispiel #1
0
void TextResultItem::setLatex(Cantor::LatexResult* result)
{
    QTextCursor cursor = textCursor();
    cursor.movePosition(QTextCursor::Start);
    cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
    QString latex = result->toLatex().trimmed();
    if (latex.startsWith(QLatin1String("\\begin{eqnarray*}")) &&
        latex.endsWith(QLatin1String("\\end{eqnarray*}"))) {
        latex = latex.mid(17);
        latex = latex.left(latex.size() - 15);
    }
    if (result->isCodeShown()) {
        if (latex.isEmpty())
            cursor.removeSelectedText();
        else
            cursor.insertText(latex);
    } else {
        QTextImageFormat format;
        format = epsRenderer()->render(cursor.document(),
                                       result->data().toUrl());
        format.setProperty(EpsRenderer::CantorFormula,
                           EpsRenderer::LatexFormula);
        format.setProperty(EpsRenderer::Code, latex);
        format.setProperty(EpsRenderer::Delimiter, QLatin1String("$$"));
        if(format.isValid())
            cursor.insertText(QString(QChar::ObjectReplacementCharacter), format);
        else
            cursor.insertText(i18n("Cannot render Eps file. You may need additional packages"));
    }
}
Beispiel #2
0
/**
 * Inserts an valid image
 * Copies the specific image in the program folder
 */
void MainWindow::on_actionImage_triggered()
{
    QString scriboDir = QDir::home().absolutePath() + QDir::separator() + "scribo";

    if ( !QDir(scriboDir + QDir::separator() + "img").exists() )
        QDir().mkdir(scriboDir + QDir::separator() + "img");

    QString filePath = QFileDialog::getOpenFileName(this, tr("Select an image"),
                                      QDir::home().absolutePath(), "Bitmap Files (*.bmp)\n"
                                        "JPEG (*.jpg *jpeg)\n"
                                        "GIF (*.gif)\n"
                                        "PNG (*.png)");
    QStringList list = filePath.split( "/" );
    QString imageName = list.value(list.length() - 1 );
    QString imagePath = scriboDir + QDir::separator() + "img" + QDir::separator() + imageName;

    QFile::copy(filePath, imagePath);
    QUrl Uri ( QString ( "file://%1" ).arg ( imagePath ) );
    QImage image = QImageReader ( imagePath ).read();

    QTextDocument * textDocument = ui->textEdit_mainWindow_surface->document();
    textDocument->addResource( QTextDocument::ImageResource, Uri, QVariant ( image ) );
    cursor = ui->textEdit_mainWindow_surface->textCursor();
    QTextImageFormat imageFormat;
    imageFormat.setWidth( image.width() );
    imageFormat.setHeight( image.height() );
    imageFormat.setName( Uri.toString() );
    cursor.insertImage(imageFormat);
}
static QImage getImage(QTextDocument *doc, const QTextImageFormat &format)
{
    QImage image;

    QString name = format.name();
    if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
        name.prepend(QLatin1String("qrc"));
    QUrl url = QUrl::fromEncoded(name.toUtf8());
    const QVariant data = doc->resource(QTextDocument::ImageResource, url);
    if (data.type() == QVariant::Image) {
        image = qvariant_cast<QImage>(data);
    } else if (data.type() == QVariant::ByteArray) {
        image.loadFromData(data.toByteArray());
    }

    if (image.isNull()) {
        QString context;
#ifndef QT_NO_TEXTBROWSER
        QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());
        if (browser)
            context = browser->source().toString();
#endif
        if (QTextImageHandler::externalLoader)
            image = QTextImageHandler::externalLoader(name, context);

        if (image.isNull()) { // try direct loading
            name = format.name(); // remove qrc:/ prefix again
            if (name.isEmpty() || !image.load(name))
                return QImage(QLatin1String(":/trolltech/styles/commonstyle/images/file-16.png"));
        }
        doc->addResource(QTextDocument::ImageResource, url, image);
    }

    return image;
}
Beispiel #4
0
void KTextCursor::insertFragment( QList<QPair<QString,QTextCharFormat>>& frags )
{
	for(QList<QPair<QString,QTextCharFormat>>::iterator iter = frags.begin(); iter != frags.end(); iter++)
	{
		QPair<QString,QTextCharFormat>& item = *iter;
		QString text = item.first;
		QTextCharFormat fmt = item.second;
		if(text.at(0) == QChar::ObjectReplacementCharacter)
		{
			//图片
			QTextImageFormat imgFmt = fmt.toImageFormat();
			QString file = imgFmt.name();
			if(!file.isEmpty())
			{
				if(moviePool()->insertMovie(file, file))
				{
					fmt.setProperty(KAnimationImage, true);
					document()->addResource(QTextDocument::ImageResource, QUrl(file), moviePool()->currentImage(file));
				}
				else
				{
					QImage image(file);
					if(!image.isNull())
					{
						document()->addResource(QTextDocument::ImageResource, QUrl(file), image);
					}
				}
			}
		}
		insertText(text, fmt);
	}
}
static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format)
{
    QImage image;

    const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
    const int width = qRound(format.width());
    const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
    const int height = qRound(format.height());

    QSize size(width, height);
    if (!hasWidth || !hasHeight) {
        image = getImage(doc, format);
        if (!hasWidth)
            size.setWidth(image.width());
        if (!hasHeight)
            size.setHeight(image.height());
    }

    qreal scale = 1.0;
    QPaintDevice *pdev = doc->documentLayout()->paintDevice();
    if (pdev) {
        extern int qt_defaultDpi();
        if (image.isNull())
            image = getImage(doc, format);
        if (!image.isNull())
            scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
    }
    size *= scale;

    return size;
}
Beispiel #6
0
void Format::parseBlock( QTextCursor &cursor, const QDomElement &element )
{
//  dbg() << "Format::parseBlock()" << endl;

  QDomNode n;
  for( n = element.firstChild(); !n.isNull(); n = n.nextSibling() ) {
    QDomElement e = n.toElement();
    if ( e.tagName() == "fragment" ) {
      QTextCharFormat format;
      if ( e.hasAttribute( "link" ) ) {
        format.setAnchor( true );
        QString href = e.attribute( "link" );
        format.setAnchorHref( href );
        format.setFontUnderline( true );
        if ( href.startsWith( "todoodle:" ) ) {
          format = TextFormats::topicLinkCharFormat( href );
        } else {
          format = TextFormats::hyperLinkCharFormat( href );
        }
      }
      if ( e.attribute( "bold" ) == "true" ) {
        format.setFontWeight( QFont::Bold );
      }
      if ( e.attribute( "italic" ) == "true" ) {
        format.setFontItalic( true );
      }
      int fontSize = 0;
      if ( e.hasAttribute( "fontsize" ) ) {
        fontSize = e.attribute( "fontsize" ).toInt();
      } else {
        fontSize = 10;
      }
      if ( fontSize > 0 ) format.setFontPointSize( fontSize );
      
      QDomNode n2;
      for( n2 = e.firstChild(); !n2.isNull(); n2 = n2.nextSibling() ) {
//        dbg() << "TICK" << endl;
        QDomText t = n2.toText();
        if ( !t.isNull() ) {
//          dbg() << "TEXT: '" << t.data() << "'" << endl;
          cursor.insertText( t.data(), format );
//          dbg() << "done" << endl;
        } else {
          QDomElement e2 = n2.toElement();
          if ( !e2.isNull() ) {
            if ( e2.tagName() == "todo" ) {
              QTextImageFormat f;
              if ( e2.attribute( "status" ) == "todo" ) {
                f.setName( ":/images/todo.png" );
              } else {
                f.setName( ":/images/tododone.png" );
              }
              cursor.insertImage( f );
            }
          }
        }
      }
    }
  }
}
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit *editor = new QTextEdit;

//! [0]
    QTextDocument *document = new QTextDocument(editor);
    QTextCursor cursor(document);
//! [0]

//! [1]
    QTextImageFormat imageFormat;
    imageFormat.setName(":/images/advert.png");
    cursor.insertImage(imageFormat);
//! [1]

    cursor.insertBlock();
    cursor.insertText("Code less. Create more.");

    editor->setDocument(document);
    editor->setWindowTitle(tr("Text Document Images"));
    editor->resize(320, 480);
    editor->show();
    return app.exec();
}
Beispiel #8
0
void KTextCursor::insertImage( const QString &name, const QTextImageFormat &format )
{
	QTextImageFormat imageForamt = format;

	imageForamt.setName(name);
	if(!imageForamt.hasProperty(KImageKey))
	{
		imageForamt.setProperty(KImageKey, name);
	}

	if(moviePool()->insertMovie(name, name))
	{
		Q_D(KTextCursor);

		imageForamt.setProperty(KAnimationImage,true);
		document()->addResource(QTextDocument::ImageResource, QUrl(name), moviePool()->currentImage(name));
	}
	else
	{
		QImage image(name);
		if(!image.isNull())
		{
			document()->addResource(QTextDocument::ImageResource,QUrl(name), image);
		}
	}

	__super::insertImage(imageForamt);
}
void TextDocument::replaceImageUrl(const QUrl &oldName, const QString &newName) {
	QList <QPair<int, int> > fragments;

	QTextBlock block = begin();

	while(block.isValid()) {
		QTextBlock::iterator iterator;
		for(iterator = block.begin(); !(iterator.atEnd()); ++iterator) {
			QTextFragment fragment = iterator.fragment();
			if(fragment.isValid() && fragment.charFormat().isImageFormat()) {
				QTextImageFormat format = fragment.charFormat().toImageFormat();
				if (QUrl::fromEncoded(format.name().toUtf8()) != oldName) {continue;}
				fragments.append(QPair<int, int>(fragment.position(), fragment.length()));
			}
		}
		block = block.next();
	}


	QTextCursor cursor(this);
	cursor.beginEditBlock();
	QPair<int, int> pair;
	foreach (pair, fragments) {
		cursor.setPosition(pair.first);
		cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, pair.second);
		QTextImageFormat format = cursor.charFormat().toImageFormat();
		format.setName(newName);
		cursor.mergeCharFormat(format);
	}
Beispiel #10
0
void KTextCursor::insertImage( const QString &name, const QString &key )
{
	QTextImageFormat imageFormat;
	imageFormat.setName(name);
	imageFormat.setProperty(KImageKey, key);

	insertImage(name, imageFormat);
}
Beispiel #11
0
void KTextCursor::insertImage( const QString & name )
{
	QTextImageFormat format;
	format.setName(name);
	format.setProperty(KImageKey, name);

	insertImage(name, format);
}
Beispiel #12
0
void KTextCursor::insertHtml( const QString &text, const QMap<QString, QString>& imgs )
{
	if(!imgs.isEmpty())
	{
		for(QMap<QString,QString>::const_iterator iter = imgs.begin(); iter != imgs.end(); iter++)
		{
			QString key = iter.key();
			QString file = iter.value();
			if(moviePool()->insertMovie(file, file))
			{
				//修改属性为动画。
				document()->addResource(QTextDocument::ImageResource, QUrl(file), moviePool()->currentImage(file));
			}
			else
			{
				QImage image(file);
				if(!image.isNull())
				{
					document()->addResource(QTextDocument::ImageResource, QUrl(file), image);
				}
			}
		}
	}

	int istart = position();
	insertHtml(text);
	int inow = position();

	if(!imgs.isEmpty() && inow > istart)
	{
		setPosition(istart);
		movePosition(NextCharacter, KeepAnchor, inow-istart);
		QString txt = selectedText();
		int index = txt.indexOf(QChar::ObjectReplacementCharacter, 0);
		while(index >= 0)
		{
			/*修改字体类型。*/
			setPosition(istart+index);
			movePosition(NextCharacter, KeepAnchor, 1);
			QTextCharFormat fmt = charFormat();			
			QTextImageFormat imgFmt = fmt.toImageFormat();
			QString key = imgFmt.name();
			if(imgs.contains(key))
			{
				imgFmt.setProperty(KImageKey, key);
				imgFmt.setName(imgs.value(key));
				imgFmt.setProperty(KAnimationImage, true);
				setCharFormat(imgFmt);
			}
			int idx = index+1;
			index = txt.indexOf(QChar::ObjectReplacementCharacter, idx);
		}
	}

	setPosition(inow);
}
Beispiel #13
0
QString Text::extractImgTooltip(int pos) const
{
    for (QTextBlock::Iterator itr = doc->firstBlock().begin(); itr != doc->firstBlock().end(); ++itr) {
        if (itr.fragment().contains(pos) && itr.fragment().charFormat().isImageFormat()) {
            QTextImageFormat imgFmt = itr.fragment().charFormat().toImageFormat();
            return imgFmt.toolTip();
        }
    }

    return QString();
}
Beispiel #14
0
void InputField::insertEmoji(EmojiPtr emoji, QTextCursor c) {
	QTextImageFormat imageFormat;
	int32 ew = ESize + st::emojiPadding * cIntRetinaFactor() * 2, eh = _st->font->height * cIntRetinaFactor();
	imageFormat.setWidth(ew / cIntRetinaFactor());
	imageFormat.setHeight(eh / cIntRetinaFactor());
	imageFormat.setName(qsl("emoji://e.") + QString::number(emojiKey(emoji), 16));
	imageFormat.setVerticalAlignment(QTextCharFormat::AlignBaseline);

	static QString objectReplacement(QChar::ObjectReplacementCharacter);
	c.insertText(objectReplacement, imageFormat);
}
void TextDocumentRtfOutput::createImage(const QImage &image, const QTextImageFormat &format)
{
#if 0
    QString imageUuid = QString("rtfparser://") + QUuid::createUuid().toString();
    m_document->addResource(QTextDocument::ImageResource, QUrl(imageUuid), QVariant(image));
    format.setName(imageUuid);
    m_cursor->insertImage(format);
#else
    m_cursor->insertImage(image.scaled(format.width(), format.height()));
#endif
}
Beispiel #16
0
void KTextCursor::insertImage( const QString &name, const QImage &image, const QTextImageFormat &format )
{
	QTextImageFormat inserFormat = format;
	inserFormat.setName(name);
	if(!inserFormat.hasProperty(KImageKey))
	{
		inserFormat.setProperty(KImageKey, name);
	}

	document()->addResource(QTextDocument::ImageResource, QUrl(name), image);
	__super::insertImage(inserFormat);
}
Beispiel #17
0
void QTextBrowserEx::insertFromMimeData( const QMimeData *source )
{
	Q_ASSERT(source);

	if(m_pasteFlags == NotSupport)
		return;

	QByteArray bytes = source->data(KTextEditMime);

	KTextCursor tc = textCursor();

	if(!bytes.isEmpty() && m_pasteFlags.testFlag(Custom))
	{
		QDataStream ds(bytes);
		QList<QByteArray> lstFrags;
		ds >> lstFrags;
		for(int i = 0; i < lstFrags.count(); i++)
		{
			const QByteArray frags = lstFrags.at(i);
			QDataStream ds(frags);
			QString text;
			QTextCharFormat format;
			ds >> text >> format;
			bool bHandled = false;
			/*自定义控件,则可由外部处理*/
			emit insertMimiData(tc, text, format, bHandled);
			if(bHandled)
				continue;
			/*尝试主动处理相关信息。*/
			if(text.at(0) == QChar::ObjectReplacementCharacter)
			{
				for(int i = 0; i < text.length(); i++)
				{
					QTextImageFormat fmt = format.toImageFormat();
					if(!fmt.isValid())
					{
						/*自定义控件出现比较多*/
						continue;
					}
					QVariant vimgkey = fmt.property(KImageKey);
					QString imgkey = vimgkey.toString();
					tc.insertImage(imgkey, fmt);
				}
			}
			else
			{
				/*文字处理*/
				tc.insertText(text, format);
			}
		}
		return;
	}
Beispiel #18
0
QTextImageFormat EpsRenderer::render(QTextDocument *document,
                                     const Cantor::LatexRenderer* latex)
{
    QTextImageFormat format = render(document, QUrl::fromLocalFile(latex->imagePath()));

    if (!format.name().isEmpty()) {
        format.setProperty(CantorFormula, latex->method());
        format.setProperty(ImagePath, latex->imagePath());
        format.setProperty(Code, latex->latexCode());
    }

    return format;
}
Beispiel #19
0
QStandardItem *TextDocumentModel::formatItem(const QTextFormat &format)
{
  QStandardItem *item = new QStandardItem;
  if (!format.isValid()) {
    item->setText(tr("no format"));
  } else if (format.isImageFormat()) {
    const QTextImageFormat imgformat = format.toImageFormat();
    item->setText(tr("Image: %1").arg(imgformat.name()));
  } else {
    item->setText(tr("Format type: %1").arg(format.type()));
  }
  return item;
}
void FlatTextarea::insertEmoji(EmojiPtr emoji, QTextCursor c) {
	c.removeSelectedText();

	QPixmap img(App::emojiSingle(emoji, _st.font->height));
	QString url = qsl("emoji://e.") + QString::number(emojiKey(emoji), 16);
	document()->addResource(QTextDocument::ImageResource, QUrl(url), QVariant(img));
	QTextImageFormat imageFormat;
	imageFormat.setWidth(img.width() / cIntRetinaFactor());
	imageFormat.setHeight(img.height() / cIntRetinaFactor());
	imageFormat.setName(url);
	imageFormat.setVerticalAlignment(QTextCharFormat::AlignBaseline);
	c.insertImage(imageFormat);
}
Beispiel #21
0
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QTextEdit *editor = new QTextEdit;

    QTextDocument *document = new QTextDocument(editor);
    QTextCursor cursor(document);

    QTextImageFormat imageFormat;
    imageFormat.setName(":/images/advert.png");
    cursor.insertImage(imageFormat);

    QTextBlock block = cursor.block();
    QTextFragment fragment;
    QTextBlock::iterator it;

    for (it = block.begin(); !(it.atEnd()); ++it) {
        fragment = it.fragment();

        if (fragment.contains(cursor.position()))
            break;
    }

//! [0]
    if (fragment.isValid()) {
        QTextImageFormat newImageFormat = fragment.charFormat().toImageFormat();

        if (newImageFormat.isValid()) {
            newImageFormat.setName(":/images/newimage.png");
            QTextCursor helper = cursor;

            helper.setPosition(fragment.position());
            helper.setPosition(fragment.position() + fragment.length(),
                               QTextCursor::KeepAnchor);
            helper.setCharFormat(newImageFormat);
//! [0] //! [1]
        }
//! [1] //! [2]
    }
//! [2]

    cursor.insertBlock();
    cursor.insertText("Code less. Create more.");

    editor->setDocument(document);
    editor->setWindowTitle(tr("Text Document Image Format"));
    editor->resize(320, 480);
    editor->show();

    return app.exec();
}
Beispiel #22
0
QStandardItem *TextDocumentModel::formatItem(const QTextFormat &format)
{
    auto *item = new QStandardItem;
    if (!format.isValid()) {
        item->setText(tr("no format"));
    } else if (format.isImageFormat()) {
        const QTextImageFormat imgformat = format.toImageFormat();
        item->setText(tr("Image: %1").arg(imgformat.name()));
    } else {
        item->setText(formatTypeToString(format.type()));
    }
    item->setEditable(false);
    return item;
}
Beispiel #23
0
void TextEdit::insertImage( const QString & path ) {
    QPair< QString, QSize > imgKey( this->p_->addImageResource( path ) );

    QTextCursor cursor( this->textCursor() );

    QTextImageFormat format;
    format.setName( imgKey.first );
    format.setToolTip( imgKey.first );
    format.setWidth( imgKey.second.width() );
    format.setHeight( imgKey.second.height() );
    cursor.insertImage( format );

    this->setTextCursor( cursor );
}
Beispiel #24
0
void TextEdit::Private::replaceImage( std::map< QString, QUrl > & mapping, const QFileInfo & oldP, const QFileInfo & newP ) {
    QVariant r( this->host->document()->resource( QTextDocument::ImageResource, QUrl( oldP.fileName() ) ) );
    this->host->document()->addResource( QTextDocument::ImageResource, QUrl( oldP.fileName() ), QVariant() );
    this->host->document()->addResource( QTextDocument::ImageResource, QUrl( newP.fileName() ), r );

    this->currentCursor.deletePreviousChar();
    QTextImageFormat format;
    format.setName( newP.fileName() );
    format.setToolTip( newP.fileName() );
    this->currentCursor.insertImage( format );

    mapping.erase( oldP.fileName() );
    mapping.insert( std::make_pair( newP.fileName(), QUrl::fromLocalFile( newP.absoluteFilePath() ) ) );
    this->testCase->setImageMapping( mapping );
}
Beispiel #25
0
QTextImageFormat EpsRenderer::render(QTextDocument *document, const QUrl &url)
{
    QTextImageFormat epsCharFormat;

    QSizeF s = renderToResource(document, url);

    QUrl internal = url;
    internal.setScheme(QLatin1String("internal"));
    if(s.isValid())
    {
        epsCharFormat.setName(internal.url());
        epsCharFormat.setWidth(s.width());
        epsCharFormat.setHeight(s.height());
    }

    return epsCharFormat;
}
Beispiel #26
0
void ContactListEdit::decodePublicKey(const QTextImageFormat& storage,
  IMailProcessor::TRecipientPublicKey* key) const
  {
  assert(storage.hasProperty(QTextImageFormat::UserProperty));

  QVariant v = storage.property(QTextImageFormat::UserProperty);
  QByteArray pkArray = v.toByteArray();

  fc::ecc::public_key_data s;

  assert(pkArray.size() == s.size());

  memcpy(s.begin(), pkArray.data(), s.size());

  *key = IMailProcessor::TRecipientPublicKey(s);
  assert(key->valid());
  }
Beispiel #27
0
void QTextOdfWriter::writeInlineCharacter(QXmlStreamWriter &writer, const QTextFragment &fragment) const
{
    writer.writeStartElement(drawNS, QString::fromLatin1("frame"));
    if (m_strategy == 0) {
        // don't do anything.
    }
    else if (fragment.charFormat().isImageFormat()) {
        QTextImageFormat imageFormat = fragment.charFormat().toImageFormat();
        writer.writeAttribute(drawNS, QString::fromLatin1("name"), imageFormat.name());

        // vvv  Copy pasted mostly from Qt =================
        QImage image;
        QString name = imageFormat.name();
        if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
            name.prepend(QLatin1String("qrc"));
        QUrl url = QUrl::fromEncoded(name.toUtf8());
        const QVariant data = m_document->resource(QTextDocument::ImageResource, url);
        if (data.type() == QVariant::Image) {
            image = qvariant_cast<QImage>(data);
        } else if (data.type() == QVariant::ByteArray) {
            image.loadFromData(data.toByteArray());
        }

        if (image.isNull()) {
            QString context;
            if (QTextImageHandler::externalLoader)
                image = QTextImageHandler::externalLoader(name, context);

            if (image.isNull()) { // try direct loading
                name = imageFormat.name(); // remove qrc:/ prefix again
                image.load(name);
            }
        }

        // ^^^ Copy pasted mostly from Qt =================
        if (! image.isNull()) {
            QBuffer imageBytes;
            QImageWriter imageWriter(&imageBytes, "png");
            imageWriter.write(image);
            QString filename = m_strategy->createUniqueImageName();
            m_strategy->addFile(filename, QString::fromLatin1("image/png"), imageBytes.data());

            // get the width/height from the format.
            qreal width = (imageFormat.hasProperty(QTextFormat::ImageWidth)) ? imageFormat.width() : image.width();
            writer.writeAttribute(svgNS, QString::fromLatin1("width"), pixelToPoint(width));
            qreal height = (imageFormat.hasProperty(QTextFormat::ImageHeight)) ? imageFormat.height() : image.height();
            writer.writeAttribute(svgNS, QString::fromLatin1("height"), pixelToPoint(height));

            writer.writeStartElement(drawNS, QString::fromLatin1("image"));
            writer.writeAttribute(xlinkNS, QString::fromLatin1("href"), filename);
            writer.writeEndElement(); // image
        }
    }

    writer.writeEndElement(); // frame
}
Beispiel #28
0
void TextEmojiWrapper::refresh()
{
    if(!p->document)
        return;
    if(!p->emojis)
        return;

    QTextDocument *document = p->document->textDocument();
    document->clear();

    QTextCursor cursor(document);
    cursor.setPosition(0);

    QString &text = p->text;
    const QHash<QString,QString> &emojis = p->emojis->emojis();
    for( int i=0; i<text.size(); i++ )
    {
        QString image;
        for( int j=1; j<5; j++ )
        {
            QString emoji = text.mid(i,j);
            if( !emojis.contains(emoji) )
                continue;

            image = emojis.value(emoji);
            i += emoji.size()-1;
            break;
        }

        if(image.isEmpty())
        {
            cursor.insertText(QString(text[i]));
        }
        else
        {
            QTextImageFormat format;
            format.setName(AsemanDevices::localFilesPrePath()+image);
            format.setHeight(18);
            format.setWidth(18);

            cursor.insertImage(format);
        }
    }
}
Beispiel #29
0
bool Converter::convertFrame( const QDomElement &element )
{
  QDomElement child = element.firstChildElement();
  while ( !child.isNull() ) {
    if ( child.tagName() == QLatin1String( "image" ) ) {
      const QString href = child.attribute( QStringLiteral("href") );
      QTextImageFormat format;
      format.setWidth( StyleParser::convertUnit( element.attribute( QStringLiteral("width") ) ) );
      format.setHeight( StyleParser::convertUnit( element.attribute( QStringLiteral("height") ) ) );
      format.setName( href );

      mCursor->insertImage( format );
    }

    child = child.nextSiblingElement();
  }

  return true;
}
Beispiel #30
0
void QTextPanel::stressTestPaint()
{
    /////////pageClear();  /* remove all if exist text */
    
    
    QTextDocument *playdoc = new QTextDocument();
    QTextCursor c(playdoc);
    c.setPosition(0,QTextCursor::MoveAnchor);
    
    int loop = -1;
    c.beginEditBlock();
    
    QStringList colorNames = QColor::colorNames();
    foreach (QString name, colorNames) {
        loop++;
        
        if (loop != 0) { 
        c.insertBlock();
        }
        
        ////////qDebug() << "### name ->" << name;
        
        QPixmap e = createColorMaps( name );
        playdoc->addResource(QTextDocument::ImageResource,QUrl(name),e);
        QTextImageFormat format;
        format.setName( name );
        format.setHeight ( e.height() );
        format.setWidth ( e.width() );
        format.setToolTip(name);
        c.insertImage( format );
        c.insertText(QString(QChar::LineSeparator));   /* br */
        ////////////QTextBlockFormat bbformat = c.blockFormat();
        /////////bbformat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);
        ////////c.setBlockFormat(bbformat);
        
        for (int i = 0; i < 15; ++i)  {
        c.insertText(name+ "   .");   
        }
        c.endEditBlock();
        c.atBlockEnd();
        
    }