⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qsvggenerator.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    QString fileName;};/*!    \class QSvgGenerator    \ingroup multimedia    \since 4.3    \brief The QSvgGenerator class provides a paint device that is used to create SVG drawings.    \sa QSvgRenderer, QSvgWidget*//*!    Constructs a new generator.*/QSvgGenerator::QSvgGenerator()    : d_ptr(new QSvgGeneratorPrivate){    Q_D(QSvgGenerator);    d->engine = new QSvgPaintEngine;    d->owns_iodevice = false;}/*!    Destroys the generator.*/QSvgGenerator::~QSvgGenerator(){    Q_D(QSvgGenerator);    if (d->owns_iodevice)        delete d->engine->outputDevice();    delete d->engine;}/*!  Returns the size of the generated SVG.*/QSize QSvgGenerator::size() const{    return d_func()->engine->size();}/*!  Sets the size of the generated SVG to \a size.  It is not possible to set the size while the SVG is being generated.*/void QSvgGenerator::setSize(const QSize &size){    Q_D(QSvgGenerator);    if (d->engine->isActive()) {        qWarning("QSvgGenerator::setSize(), cannot set size while svg is being generated");        return;    }    d->engine->setSize(size);}/*!  Sets the target filename for generated SVGs to \a fileName.  \sa setOutputDevice()*/void QSvgGenerator::setFileName(const QString &fileName){    Q_D(QSvgGenerator);    if (d->engine->isActive()) {        qWarning("QSvgGenerator::setFileName(), cannot set fileName svg is being generated");        return;    }    if (d->owns_iodevice)        delete d->engine->outputDevice();    d->owns_iodevice = true;    d->fileName = fileName;    QFile *file = new QFile(fileName);    d->engine->setOutputDevice(file);}/*!  Returns the target output device for generated SVGs.*/QIODevice *QSvgGenerator::outputDevice() const{    Q_D(const QSvgGenerator);    return d->engine->outputDevice();}/*!  Sets the output device for generated SVGs to \a outputDevice.  If both output device and file name are specified, the output device  will have precedence.*/void QSvgGenerator::setOutputDevice(QIODevice *outputDevice){    Q_D(QSvgGenerator);    if (d->engine->isActive()) {        qWarning("QSvgGenerator::setOutputDevice(), cannot set output device svg is being generated");        return;    }    d->owns_iodevice = false;    d->engine->setOutputDevice(outputDevice);    d->fileName = QString();}/*!    Returns the paint engine used to render graphics to be converted to SVG    format information.*/QPaintEngine *QSvgGenerator::paintEngine() const{    Q_D(const QSvgGenerator);    return d->engine;}/*!    \reimp*/int QSvgGenerator::metric(QPaintDevice::PaintDeviceMetric metric) const{    Q_D(const QSvgGenerator);    switch (metric) {    case QPaintDevice::PdmDepth:        return 32;    case QPaintDevice::PdmWidth:        return d->engine->size().width();    case QPaintDevice::PdmHeight:        return d->engine->size().height();    case QPaintDevice::PdmDpiX:        return d->engine->resolution();    case QPaintDevice::PdmDpiY:        return d->engine->resolution();    case QPaintDevice::PdmHeightMM:        return qRound(d->engine->size().height() * 25.4 / d->engine->resolution());    case QPaintDevice::PdmWidthMM:        return qRound(d->engine->size().width() * 25.4 / d->engine->resolution());    case QPaintDevice::PdmNumColors:        return 0xffffffff;    case QPaintDevice::PdmPhysicalDpiX:        return d->engine->resolution();    case QPaintDevice::PdmPhysicalDpiY:        return d->engine->resolution();    default:        qWarning("QSvgGenerator::metric(), unhandled metric %d\n", metric);        break;    }    return 0;}/*!    \fn QString QSvgGenerator::fileName() const    Returns the name of the file to be created by the generator.*/QString QSvgGenerator::fileName() const{    Q_D(const QSvgGenerator);    return d->fileName;}/*!    \fn void QSvgGenerator::setResolution(int resolution)    Sets the resolution of the generated output to \a resolution.    The argument is specified in dots per inch.    The resolution is used to calculate the physical size of    an SVG drawing.*/void QSvgGenerator::setResolution(int dpi){    Q_D(QSvgGenerator);    d->engine->setResolution(dpi);}/*!    Returns the resolution of the generated output in dots per inch.*/int QSvgGenerator::resolution() const{    Q_D(const QSvgGenerator);    return d->engine->resolution();}/***************************************************************************** * class QSvgPaintEngine */bool QSvgPaintEngine::begin(QPaintDevice *){    Q_D(QSvgPaintEngine);    if (!d->outputDevice) {        qWarning("QSvgPaintEngine::begin(), no output device");        return false;    }    if (!d->outputDevice->isOpen()) {        if (!d->outputDevice->open(QIODevice::WriteOnly | QIODevice::Text)) {            qWarning("QSvgPaintEngine::begin(), could not open output device: '%s'",                     qPrintable(d->outputDevice->errorString()));            return false;        }    } else if (!d->outputDevice->isWritable()) {        qWarning("QSvgPaintEngine::begin(), could not write to read-only output device: '%s'",                 qPrintable(d->outputDevice->errorString()));        return false;    }    d->stream = new QTextStream(&d->header);    int w = d->size.width();    int h = d->size.height();    qreal wmm = w * 25.4 / d->resolution;    qreal hmm = h * 25.4 / d->resolution;    // stream out the header...    *d->stream << "<?xml version=\"1.0\" standalone=\"no\"?>" << endl;    *d->stream << "<svg width=\"" << wmm << "mm\" height=\"" << hmm << "mm\"" << endl;    *d->stream << " viewBox=\"0 0 " << w << " " << h << "\"" << endl;    *d->stream << " xmlns=\"http://www.w3.org/2000/svg\""               << " xmlns:xlink=\"http://www.w3.org/1999/xlink\" "               << " version=\"1.2\" baseProfile=\"tiny\">" << endl;    if (!d->attributes.document_title.isEmpty()) {        *d->stream << "<title>" << d->attributes.document_title << "</title>" << endl;    }    if (!d->attributes.document_description.isEmpty()) {        *d->stream << "<desc>" << d->attributes.document_description << "</desc>" << endl;    }    d->stream->setString(&d->defs);    *d->stream << "<defs>\n";    d->stream->setString(&d->body);    // Start the initial graphics state...    *d->stream << "<g ";    generateQtDefaults();    *d->stream << endl;    return true;}bool QSvgPaintEngine::end(){    Q_D(QSvgPaintEngine);    d->stream->setString(&d->defs);    *d->stream << "</defs>\n";    d->stream->setDevice(d->outputDevice);    *d->stream << d->header;    *d->stream << d->defs;    *d->stream << d->body;    if (d->afterFirstUpdate)        *d->stream << "</g>" << endl; // close the updateState    *d->stream << "</g>" << endl // close the Qt defaults               << "</svg>" << endl;    delete d->stream;    return true;}void QSvgPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm,                                 const QRectF &sr){    drawImage(r, pm.toImage(), sr);}void QSvgPaintEngine::drawImage(const QRectF &r, const QImage &image,                                const QRectF &sr,                                Qt::ImageConversionFlag flags){    //Q_D(QSvgPaintEngine);    Q_UNUSED(sr);    Q_UNUSED(flags);    stream() << "<image ";    stream() << "x=\""<<r.x()<<"\" ";    stream() << "y=\""<<r.y()<<"\" ";    stream() << "width=\""<<r.width()<<"\" ";    stream() << "height=\""<<r.height()<<"\" ";    QByteArray data;    QBuffer buffer(&data);    buffer.open(QBuffer::ReadWrite);    image.save(&buffer, "PNG");    buffer.close();    stream() << "xlink:href=\"data:image/png;base64,"             << data.toBase64()             <<"\" ";    stream() << "/>\n";}void QSvgPaintEngine::updateState(const QPaintEngineState &state){    Q_D(QSvgPaintEngine);    QPaintEngine::DirtyFlags flags = state.state();    // always stream full gstate, which is not required, but...    flags |= QPaintEngine::AllDirty;    // close old state and start a new one...    if (d->afterFirstUpdate)        *d->stream << "</g>\n\n";    *d->stream << "<g ";    if (flags & QPaintEngine::DirtyBrush) {        qbrushToSvg(state.brush());    }    if (flags & QPaintEngine::DirtyPen) {        qpenToSvg(state.pen());    }    if (flags & QPaintEngine::DirtyTransform) {        d->matrix = state.matrix();        *d->stream << "transform=\"matrix(" << d->matrix.m11() << ","                   << d->matrix.m12() << ","                   << d->matrix.m21() << "," << d->matrix.m22() << ","                   << d->matrix.dx() << "," << d->matrix.dy()                   << ")\""                   << endl;    }    if (flags & QPaintEngine::DirtyFont) {        qfontToSvg(state.font());    }    if (flags & QPaintEngine::DirtyOpacity) {        if (!qFuzzyCompare(state.opacity(), 1))            stream() << "opacity=\""<<state.opacity()<<"\" ";    }    *d->stream << ">" << endl;    d->afterFirstUpdate = true;}void QSvgPaintEngine::drawPath(const QPainterPath &p){    Q_D(QSvgPaintEngine);    *d->stream << "<path ";    *d->stream << "fill-rule=";    if (p.fillRule() == Qt::OddEvenFill)        *d->stream << "\"evenodd\" ";    else        *d->stream << "\"nonzero\" ";    *d->stream << "d=\"";    for (int i=0; i<p.elementCount(); ++i) {        const QPainterPath::Element &e = p.elementAt(i);        switch (e.type) {        case QPainterPath::MoveToElement:            *d->stream << "M" << e.x << "," << e.y;            break;        case QPainterPath::LineToElement:            *d->stream << "L" << e.x << "," << e.y;            break;        case QPainterPath::CurveToElement:            *d->stream << "C" << e.x << "," << e.y;            ++i;            while (i < p.elementCount()) {                const QPainterPath::Element &e = p.elementAt(i);                if (e.type != QPainterPath::CurveToDataElement) {                    --i;                    break;                } else                    *d->stream << " ";                *d->stream << e.x << "," << e.y;                ++i;            }            break;        default:            break;        }        if (i != p.elementCount() - 1) {            *d->stream << " ";        }    }    *d->stream << "\"/>" << endl;}void QSvgPaintEngine::drawPolygon(const QPointF *points, int pointCount,                                  PolygonDrawMode mode){    Q_ASSERT(pointCount >= 2);    //Q_D(QSvgPaintEngine);    QPainterPath path(points[0]);    for (int i=1; i<pointCount; ++i)        path.lineTo(points[i]);    if (mode == PolylineMode) {        stream() << "<polyline fill=\"none\" points=\"";        for (int i = 0; i < pointCount; ++i) {            const QPointF &pt = points[i];            stream() << pt.x() << "," << pt.y() << " ";        }        stream() << "\" />" <<endl;    } else {        path.closeSubpath();        drawPath(path);    }}void QSvgPaintEngine::drawTextItem(const QPointF &pt, const QTextItem &textItem){    Q_D(QSvgPaintEngine);    if (d->pen.style() == Qt::NoPen)        return;    const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);    QString s = QString::fromRawData(ti.chars, ti.num_chars);    *d->stream << "<text "               << "fill=\"" << d->attributes.stroke << "\" "               << "fill-opacity=\"" << d->attributes.strokeOpacity << "\" "               << "stroke=\"none\" "               << "x=\"" << pt.x() << "\" y=\"" << pt.y() << "\" ";    qfontToSvg(textItem.font());    *d->stream << " >"               << Qt::escape(s)               << "</text>"               << endl;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -