예제 #1
0
/*!
    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
    \snippet doc/src/snippets/code/src_qt3support_other_q3mimefactory.cpp 4
    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
   )
    {
        // handle absolute file names directly
        r = dataInternal(abs_name, d->extensions);
    }
    else { // check list of paths
        for (it = d->path.begin(); !r && it != d->path.end(); ++it) {
            QString filename = *it;
            if (filename[(int)filename.length()-1] != QLatin1Char('/'))
                filename += QLatin1Char('/');
            filename += abs_name;
            r = dataInternal(filename, d->extensions);
        }
    }

    static bool looping = false;
    if (!r && this == defaultFactory()) {
        // we found no mime-source and we are the default factory, so
        // we know all the other installed mime-source factories, so
        // ask them
        if (!looping) {
            // to avoid endless recustions, don't enter the loop below
            // if data() got called from within the loop below
            looping = true;
            for (int i = 0; i < d->factories.size(); ++i) {
                const Q3MimeSourceFactory *f = d->factories.at(i);
                if (f == this)
                    continue;
                r = static_cast<const QMimeSource *>(f->data(abs_name));
                if (r) {
                    looping = false;
                    return r;
                }
            }
            looping = false;
        }
    } else if (!r) {
        // we are not the default mime-source factory, so ask the
        // default one for the mime-source, as this one will loop over
        // all installed mime-source factories and ask these
        r = static_cast<const QMimeSource *>(defaultFactory()->data(abs_name));
    }
    return r;
}
예제 #2
0
/*!
    Returns the property name used to access data for the given \a type of data.
*/
QByteArray QItemEditorFactory::valuePropertyName(QVariant::Type type) const
{
    QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
    if (!creator) {
        const QItemEditorFactory *dfactory = defaultFactory();
        return dfactory == this ? QByteArray() : dfactory->valuePropertyName(type);
    }
    return creator->valuePropertyName();
}
예제 #3
0
/*!
    Creates an editor widget with the given \a parent for the specified \a type of data,
    and returns it as a QWidget.

    \sa registerEditor()
*/
QWidget *QItemEditorFactory::createEditor(QVariant::Type type, QWidget *parent) const
{
    QItemEditorCreatorBase *creator = creatorMap.value(type, 0);
    if (!creator) {
        const QItemEditorFactory *dfactory = defaultFactory();
        return dfactory == this ? 0 : dfactory->createEditor(type, parent);
    }
    return creator->createWidget(parent);
}
예제 #4
0
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();
        if (!QImageReader::imageFormat(abs_name).isEmpty())
            mimetype = "application/x-qt-image";

        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;
}
예제 #5
0
/*!
    Destroys the Q3MimeSourceFactory, deleting all stored content.
*/
Q3MimeSourceFactory::~Q3MimeSourceFactory()
{
    if (defaultFactory() == this)
        defaultfactory = 0;
    delete d;
}