Exemplo n.º 1
0
bool Converter::convertFrame(const QDomElement &element)
{
    QDomElement child = element.firstChildElement();
    QDomElement suchild = element.lastChildElement("title");
    /// svg:title alt && title 
    while (!child.isNull())
    {
        if (child.tagName() == QLatin1String("image"))
        {
            const QString href = child.attribute("href");
            const QString xname = element.attribute("name");
            QString alttitle = QString("Name:%1 - Ref:%2").arg(xname).arg(href);
            if (suchild.tagName() == QLatin1String("title"))
            {
                alttitle.append(QString(" - Title:%1").arg(suchild.text()));
            }
            QTextImageFormat format;
            format.setToolTip(alttitle);
            format.setWidth(4.0/3.0*StyleParser::convertUnit(element.attribute("width")));
            format.setHeight(4.0/3.0*StyleParser::convertUnit(element.attribute("height")));
            format.setName(href);
            format.setBackground(Qt::white);
            m_Cursor->insertImage(format);
        }

        child = child.nextSiblingElement();
    }

    return true;
}
Exemplo n.º 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);
}
Exemplo n.º 3
0
void KDReports::TextDocumentData::updatePercentSize( QTextImageFormat& imageFormat, const QSizeF& size )
{
    // "W50" means W=50%.  "H60" means H=60%.
    QString prop = imageFormat.property( ResizableImageProperty ).toString();
    const qreal imageRatio = imageFormat.height() / imageFormat.width();
    const qreal pageWidth = size.width();
    const qreal pageHeight = size.height();
    const qreal pageRatio = pageWidth ? pageHeight / pageWidth : 0;
    if ( prop[0] == QLatin1Char( 'T' ) ) {
        //qDebug() << "updatePercentSize fitToPage" << imageRatio << pageRatio;
        if ( imageRatio < pageRatio ) {
            prop = QString::fromLatin1( "W100" );
        } else {
            prop = QString::fromLatin1( "H100" );
        }
    }
    const qreal percent = prop.mid( 1 ).toDouble();
    switch ( prop[0].toLatin1() ) {
    case 'W':
        {
        const qreal newWidth = pageWidth * percent / 100.0;
        imageFormat.setWidth( newWidth );
        imageFormat.setHeight( newWidth * imageRatio );
        // ### I needed to add this -2 here for 100%-width images to fit in
        if ( percent == 100.0 )
            imageFormat.setWidth( imageFormat.width() - 2 );
        }
        break;
    case 'H':
        imageFormat.setHeight( pageHeight * percent / 100.0 );
        // ### I needed to add -6 here for 100%-height images to fit in (with Qt-4.4)
        // and it became -9 with Qt-4.5, and even QtSw doesn't know why.
        // Task number 241890
        if ( percent == 100.0 )
            imageFormat.setHeight( imageFormat.height() - 10 );
        imageFormat.setWidth( imageRatio ? imageFormat.height() / imageRatio : 0 );
        //qDebug() << "updatePercentSize" << size << "->" << imageFormat.width() << "x" << imageFormat.height();
        break;
    default:
        qWarning( "Unhandled image format property type - internal error" );
    }
}
Exemplo n.º 4
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);
}
Exemplo n.º 5
0
void Utils::addImageToTextEdit(const QString& fileName, QTextEdit* textEdit)
{
	if (!fileName.isNull()) {
		QTextCursor c(textEdit->document());
		c.movePosition(QTextCursor::End);

		QImage image;
		image.load(fileName);
		QTextImageFormat imageFormat;
		float maxImageWidth = textEdit->width() / 2.f;
		if (image.width() < maxImageWidth) {
			imageFormat.setWidth(image.width());
			imageFormat.setHeight(image.height());
		} else {
			imageFormat.setWidth(maxImageWidth);
			imageFormat.setHeight(maxImageWidth / image.width() * image.height());
		}
		imageFormat.setName(fileName);
		c.insertImage(imageFormat);
	}
}
Exemplo n.º 6
0
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);
}
Exemplo n.º 7
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 );
}
Exemplo n.º 8
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;
}
Exemplo n.º 9
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);
        }
    }
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
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();
        
    }
Exemplo n.º 12
0
void MailViewer::displayAttachments(const MessageHeader& msg)
{   
#if 0 //code doesn't yet work
   QTextDocument* textDocument = ui->message_content->document();
   QImage image;
   int i = 1;
   foreach (const bts::bitchat::attachment& attachment, msg.attachments)
      {
      image.fromData(attachment.body.data());
      QUrl url(QString("attachment_image_%1").arg(i++));
      textDocument->addResource( QTextDocument::ImageResource, url, QVariant ( image ) );
      QTextCursor cursor = ui->message_content->textCursor();
      QTextImageFormat imageFormat;
      imageFormat.setWidth( image.width() );
      imageFormat.setHeight( image.height() );
      imageFormat.setName( url.toString() );
      cursor.insertImage(imageFormat);
      }
#endif
}
Exemplo n.º 13
0
  /*!
     * \class TextCursorInsertImage
   * \author Anders Fernström
   * \date 2005-11-18
     *
     * \brief Command for inserting an image
     */
  void TextCursorInsertImage::execute()
  {
    QTextCursor cursor( document()->getCursor()->currentCell()->textCursor() );
    if( !cursor.isNull() )
    {
      QImage* image = new QImage( filepath_ );
      if( !image->isNull() )
      {
        QString imagename = document()->addImage( image );

        QTextCursor cursor( document()->getCursor()->currentCell()->textCursor() );
        if( !cursor.isNull() )
        {
          QTextEdit *editor = document()->getCursor()->currentCell()->textEdit();
          if( editor )
          {
            // save text settings and set them after image have been inserted
            QTextCharFormat format = cursor.charFormat();
            if( editor->toPlainText().isEmpty() )
              format = *document()->getCursor()->currentCell()->style()->textCharFormat();

            QTextImageFormat imageformat;
            imageformat.merge( format );
            imageformat.setHeight( height_ );
            imageformat.setWidth( width_ );
            imageformat.setName( imagename );

            cursor.insertImage( imageformat );
          }
        }
      }
      else
      {
        string str = string("Could not open image: ") + filepath_.toStdString().c_str();
        throw runtime_error( str.c_str() );
      }
    }
  }
Exemplo n.º 14
0
QTextHtmlImporter::ProcessNodeResult QTextHtmlImporter::processSpecialNodes()
{
    switch (currentNode->id) {
        case Html_body:
            if (currentNode->charFormat.background().style() != Qt::NoBrush) {
                QTextFrameFormat fmt = doc->rootFrame()->frameFormat();
                fmt.setBackground(currentNode->charFormat.background());
                doc->rootFrame()->setFrameFormat(fmt);
                const_cast<QTextHtmlParserNode *>(currentNode)->charFormat.clearProperty(QTextFormat::BackgroundBrush);
            }
            break;

        case Html_ol:
        case Html_ul: {
            QTextListFormat::Style style = currentNode->listStyle;

            if (currentNode->id == Html_ul && !currentNode->hasOwnListStyle && currentNode->parent) {
                const QTextHtmlParserNode *n = &at(currentNode->parent);
                while (n) {
                    if (n->id == Html_ul) {
                        style = nextListStyle(currentNode->listStyle);
                    }
                    if (n->parent)
                        n = &at(n->parent);
                    else
                        n = 0;
                }
            }

            QTextListFormat listFmt;
            listFmt.setStyle(style);

            ++indent;
            if (currentNode->hasCssListIndent)
                listFmt.setIndent(currentNode->cssListIndent);
            else
                listFmt.setIndent(indent);

            List l;
            l.format = listFmt;
            l.listNode = currentNodeIdx;
            lists.append(l);
            compressNextWhitespace = true;

            // broken html: <ul>Text here<li>Foo
            const QString simpl = currentNode->text.simplified();
            if (simpl.isEmpty() || simpl.at(0).isSpace())
                return ContinueWithNextNode;
            break;
        }

        case Html_table: {
            Table t = scanTable(currentNodeIdx);
            tables.append(t);
            hasBlock = false;
            return ContinueWithNextNode;
        }

        case Html_tr:
            return ContinueWithNextNode;

        case Html_img: {
            QTextImageFormat fmt;
            fmt.setName(currentNode->imageName);

            fmt.merge(currentNode->charFormat);

            if (currentNode->imageWidth >= 0)
                fmt.setWidth(currentNode->imageWidth);
            if (currentNode->imageHeight >= 0)
                fmt.setHeight(currentNode->imageHeight);

            cursor.insertImage(fmt, QTextFrameFormat::Position(currentNode->cssFloat));

            cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
            cursor.mergeCharFormat(currentNode->charFormat);
            cursor.movePosition(QTextCursor::Right);

            hasBlock = false;
            return ContinueWithNextNode;
        }

        case Html_hr: {
            QTextBlockFormat blockFormat = currentNode->blockFormat;
            blockFormat.setTopMargin(topMargin(currentNodeIdx));
            blockFormat.setBottomMargin(bottomMargin(currentNodeIdx));
            blockFormat.setProperty(QTextFormat::BlockTrailingHorizontalRulerWidth, currentNode->width);
            if (hasBlock && importMode == ImportToDocument)
                cursor.mergeBlockFormat(blockFormat);
            else
                appendBlock(blockFormat);
            hasBlock = false;
            return ContinueWithNextNode;
        }

        default: break;
    }
    return ContinueWithCurrentNode;
}