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

📄 q3mimefactory.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3mimefactory.h"#ifndef QT_NO_MIMEFACTORY#include "qmap.h"#include "qmime.h"#include "qstringlist.h"#include "qfileinfo.h"#include "qdir.h"#include "q3dragobject.h"#include "qpixmap.h"#include "qimagereader.h"#include "q3cleanuphandler.h"#include "private/qtextimagehandler_p.h"static Q3MimeSourceFactory* defaultfactory = 0;static Q3SingleCleanupHandler<Q3MimeSourceFactory> qmime_cleanup_factory;class Q3MimeSourceFactoryData {public:    Q3MimeSourceFactoryData() :        last(0)    {    }    ~Q3MimeSourceFactoryData()    {        QMap<QString, QMimeSource*>::Iterator it = stored.begin();        while (it != stored.end()) {            delete *it;            ++it;        }        delete last;    }    QMap<QString, QMimeSource*> stored;    QMap<QString, QString> extensions;    QStringList path;    QMimeSource* last;    QList<Q3MimeSourceFactory*> factories;};static QImage richTextImageLoader(const QString &name, const QString &context){    QImage img;    const QMimeSource *src = Q3MimeSourceFactory::defaultFactory()->data(name, context);    if (src && Q3ImageDrag::decode(src, img))        return img;    return QImage();}/*!    \class Q3MimeSourceFactory    \brief The Q3MimeSourceFactory class is an extensible provider of mime-typed data.    \compat    A Q3MimeSourceFactory provides an abstract interface to a    collection of information. Each piece of information is    represented by a QMimeSource object which can be examined and    converted to concrete data types by functions such as    Q3ImageDrag::canDecode() and Q3ImageDrag::decode().    The base Q3MimeSourceFactory can be used in two ways: as an    abstraction of a collection of files or as specifically stored    data. For it to access files, call setFilePath() before accessing    data. For stored data, call setData() for each item (there are    also convenience functions, e.g. setText(), setImage() and    setPixmap(), that simply call setData() with appropriate    parameters).    The rich text widgets, QTextEdit and QTextBrowser, use    Q3MimeSourceFactory to resolve references such as images or links    within rich text documents. They either access the default factory    (see \l{defaultFactory()}) or their own. Other classes that are    capable of displaying rich text (such as QLabel, QWhatsThis or    QMessageBox) always use the default factory.    A factory can also be used as a container to store data associated    with a name. This technique is useful whenever rich text contains    images that are stored in the program itself, not loaded from the    hard disk. Your program may, for example, define some image data    as:    \code    static const char* myimage_data[]={    "...",    ...    "..."};    \endcode    To be able to use this image within some rich text, for example    inside a QLabel, you must create a QImage from the raw data and    insert it into the factory with a unique name:    \code    Q3MimeSourceFactory::defaultFactory()->setImage("myimage", QImage(myimage_data));    \endcode    Now you can create a rich text QLabel with    \code    QLabel* label = new QLabel(        "Rich text with embedded image:<img source=\"myimage\">"        "Isn't that <em>cute</em>?");    \endcode    When no longer needed, you can clear the data from the factory:    \code    delete label;    Q3MimeSourceFactory::defaultFactory()->setData("myimage", 0);    \endcode*//*!    Constructs a Q3MimeSourceFactory that has no file path and no    stored content.*/Q3MimeSourceFactory::Q3MimeSourceFactory() :    d(new Q3MimeSourceFactoryData){    addFilePath(QLatin1String(":/qt/q3mimesourcefactory/")); //to get from the resources    // add some reasonable defaults    setExtensionType(QLatin1String("htm"), "text/html;charset=iso8859-1");    setExtensionType(QLatin1String("html"), "text/html;charset=iso8859-1");    setExtensionType(QLatin1String("txt"), "text/plain");    setExtensionType(QLatin1String("xml"), "text/xml;charset=UTF-8");    setExtensionType(QLatin1String("jpg"), "image/jpeg"); // support misspelled jpeg files}/*!    Destroys the Q3MimeSourceFactory, deleting all stored content.*/Q3MimeSourceFactory::~Q3MimeSourceFactory(){    if (defaultFactory() == this)        defaultfactory = 0;    delete d;}QMimeSource* Q3MimeSourceFactory::dataInternal(const QString& abs_name, const QMap<QString, QString> &extensions) const{    QMimeSource* r = 0;    QStringList attempted_names(abs_name);    QFileInfo fi(abs_name);    if (fi.isReadable()) {        // get the right mimetype        QString e = fi.extension(false);        QByteArray mimetype("application/octet-stream");        if (extensions.contains(e))            mimetype = extensions[e].latin1();        QByteArray imgfmt = QImageReader::imageFormat(abs_name);        if (!imgfmt.isEmpty())            mimetype = "image/" + imgfmt.toLower();        QFile f(abs_name);        if (f.open(QIODevice::ReadOnly) && f.size()) {            QByteArray ba;            ba.resize(f.size());            f.readBlock(ba.data(), ba.size());            Q3StoredDrag* sr = new Q3StoredDrag(mimetype);            sr->setEncodedData(ba);            delete d->last;            d->last = r = sr;        }    }    // we didn't find the mime-source, so ask the default factory for    // the mime-source (this one will iterate over all installed ones)    //    // this looks dangerous, as this dataInternal() function will be    // called again when the default factory loops over all installed    // factories (including this), but the static bool looping in    // data() avoids endless recursions    if (!r && this != defaultFactory())        r = (QMimeSource*)defaultFactory()->data(abs_name);    return r;}/*!    Returns a reference to the data associated with \a abs_name. The    return value remains valid only until the next data() or setData()    call, so you should immediately decode the result.    If there is no data associated with \a abs_name in the factory's    store, the factory tries to access the local filesystem. If \a    abs_name isn't an absolute file name, the factory will search for    it in all defined paths (see \l{setFilePath()}).    The factory understands all the image formats supported by    QImageReader. Any other mime types are determined by the file name    extension. The default settings are    \code    setExtensionType("html", "text/html;charset=iso8859-1");    setExtensionType("htm", "text/html;charset=iso8859-1");    setExtensionType("txt", "text/plain");    setExtensionType("xml", "text/xml;charset=UTF-8");    \endcode    The effect of these is that file names ending in "txt" will be    treated as text encoded in the local encoding; those ending in    "xml" will be treated as text encoded in Unicode UTF-8 encoding.    The text/html type is treated specially, since the encoding can be    specified in the html file itself. "html" or "htm" will be treated    as text encoded in the encoding specified by the html meta tag, if    none could be found, the charset of the mime type will be used.    The text subtype ("html", "plain", or "xml") does not affect the    factory, but users of the factory may behave differently. We    recommend creating "xml" files where practical. These files can be    viewed regardless of the runtime encoding and can encode any    Unicode characters without resorting to encoding definitions    inside the file.    Any file data that is not recognized will be retrieved as a    QMimeSource providing the "application/octet-stream" mime type,    meaning uninterpreted binary data.    You can add further extensions or change existing ones with    subsequent calls to setExtensionType(). If the extension mechanism    is not sufficient for your problem domain, you can inherit    Q3MimeSourceFactory and reimplement this function to perform some    more specialized mime-type detection. The same applies if you want    to use the mime source factory to access URL referenced data over    a network.*/const QMimeSource *Q3MimeSourceFactory::data(const QString& abs_name) const{    if (d->stored.contains(abs_name))        return d->stored[abs_name];    const QMimeSource *r = 0;    if (abs_name.isEmpty())        return r;    QStringList::Iterator it;    if (abs_name[0] == QLatin1Char('/')#ifdef Q_WS_WIN            || (abs_name[0].isLetter() && abs_name[1] == QLatin1Char(':')) || abs_name.startsWith(QLatin1String("\\\\"))#endif   )    {

⌨️ 快捷键说明

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