Ignore:
Timestamp:
Nov 18, 2009, 1:49:31 AM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

gui/kernel: Improved mime <-> PM clipboard format conversion interfaces.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gui/kernel/qmime_pm.cpp

    r323 r324  
    114114    header, or even just passing on the data. See \l{Drag and Drop} for more
    115115    information on choosing and definition MIME types.
    116 
    117     You can check if a MIME type is convertible using canConvertFromMime() and
    118     can perform conversions with convertToMime() and convertFromMime().
    119116*/
    120117
     
    158155
    159156/*!
    160     \fn bool QPMMime::canConvertFromMime(ULONG format, const QMimeData *mimeData) const
    161 
    162     Returns true if the converter can convert from the \a mimeData to
    163     the specified \a format.
     157    \fn QList<ULONG> QPMMime::formatsForMimeData(const QMimeData *mimeData) const
     158
     159    Returns a list of ULONG values representing the different OS/2 PM
     160    clipboard formats that can be provided for the \a mimeData, in order of
     161    precedence (the most suitable format goes first), or an empty list if
     162    neither of the mime types provided by \a mimeData is supported by this
     163    converter.
    164164
    165165    All subclasses must reimplement this pure virtual function.
     
    167167
    168168/*!
    169     \fn bool QPMMime::convertFromMime(ULONG format, const QMimeData *mimeData,
    170                                       ULONG &flags, ULONG &data) const
    171 
    172     Convert the \a mimeData to the specified \a format.
    173     The converted data should then be placed in the \a data variable with the
    174     necessary flags returned in the \a flags variable.
     169    \fn bool QPMMime::convertFromMimeData(const QMimeData *mimeData, ULONG format,
     170                                          ULONG &flags, ULONG *data) const
     171
     172    Converts the \a mimeData to the specified \a format.
     173
     174    If \a data is not NULL, a handle to the converted data should then be placed
     175    in a variable pointed to by \a data and with the necessary flags describing
     176    the handle returned in the \a flags variable.
     177
     178    The following flags describing the data type are recognized:
     179
     180    \table
     181    \row \o \c CFI_POINTER        \o \a data is a pointer to a block of memory
     182                                      allocated with QPMMime::allocMem()
     183    \row \o \c CFI_HANDLE         \o \a data is a handle to the appropriate
     184                                      PM resource
     185    \endtable
     186
     187    If \a data is NULL then a delayed conversion is requested by the caller.
     188    The implementation should return the appropriate flags in the \a flags
     189    variable and may perform the real data conversion later when this method is
     190    called again with \a data being non-NULL.
    175191
    176192    Return true if the conversion was successful.
     
    180196
    181197/*!
    182     \fn QVector<ULONG> QPMMime::formatsForMime(const QString &mimeType, const QMimeData *mimeData) const
    183 
    184     Returns a QVector of ULONG values representing the different OS/2 PM clipboard
    185     formats that can be provided for the \a mimeType from the \a mimeData.
     198    \fn QStringList QPMMime::mimesForFormats(const QList<ULONG> &formats) const
     199
     200    Returns a list of mime types that will be created form the specified \a list
     201    of \a formats, in order of precedence (the most suitable mime type comes
     202    first), or an empty list if neither of the \a formats is supported by this
     203    converter.
    186204
    187205    All subclasses must reimplement this pure virtual function.
     
    189207
    190208/*!
    191     \fn bool QPMMime::canConvertToMime(const QString &mimeType, ULONG format) const
    192 
    193     Returns true if the converter can convert to the \a mimeType from
    194     the specified \a format.
     209    \fn QVariant QPMMime::convertFromFormat(ULONG format, ULONG flags, ULONG data,
     210                                            const QString &mimeType,
     211                                            QVariant::Type preferredType) const
     212
     213    Returns a QVariant containing the converted from the \a data in the
     214    specified \a format with the given \a flags to the requested \a mimeType. If
     215    possible the QVariant should be of the \a preferredType to avoid needless
     216    conversions.
    195217
    196218    All subclasses must reimplement this pure virtual function.
    197219*/
    198220
    199 /*!
    200     \fn QVariant QPMMime::convertToMime(const QString &mimeType, QVariant::Type preferredType,
    201                                         ULONG format, ULONG flags, ULONG data) const
    202 
    203     Returns a QVariant containing the converted data for \a mimeType from the
    204     \a data in the specified \a format and \a flags. If possible the QVariant
    205     should be of the \a preferredType to avoid needless conversions.
    206 
    207     All subclasses must reimplement this pure virtual function.
    208 */
    209 
    210 /*!
    211     \fn QString QPMMime::mimeForFormat(ULONG format) const
    212 
    213     Returns the mime type that will be created form the specified \a format, or an
    214     empty string if this converter does not support \a format.
    215 
    216     All subclasses must reimplement this pure virtual function.
    217 */
    218 
    219221// static
    220 QPMMime *QPMMime::converterToMime(const QString &mimeType, ULONG format)
    221 {
     222QList<QPMMime::Match> QPMMime::allConvertersFromFormats(const QList<ULONG> &formats)
     223{
     224    QList<Match> matches;
     225
    222226    QList<QPMMime*> mimes = theMimeList()->mimes();
    223227    for (int i = mimes.size()-1; i >= 0; --i) {
    224         if (mimes.at(i)->canConvertToMime(mimeType, format))
    225             return mimes.at(i);
    226     }
    227     return 0;
    228 }
    229 
    230 // static
    231 QStringList QPMMime::allMimesForFormats(const QVector<ULONG> &formats)
    232 {
    233     QList<QPMMime*> mimes = theMimeList()->mimes();
    234     QStringList fmts;
    235 
    236     foreach(ULONG cf, formats) {
    237 #ifdef QMIME_DEBUG
    238         HATOMTBL tbl = WinQuerySystemAtomTable();
    239         ULONG len = WinQueryAtomLength(tbl, cf);
    240         QByteArray atom(len, 0);
    241         WinQueryAtomName(tbl, cf, atom.data(), atom.size() + 1 /* '\0' */);
    242         qDebug("QPMMime::allMimesForFormats() CF %lu (%s)", cf, atom.constData());
    243 #endif
    244         for (int i = mimes.size()-1; i >= 0; --i) {
    245             QString format = mimes.at(i)->mimeForFormat(cf);
    246             if (!format.isEmpty() && !fmts.contains(format)) {
    247                 fmts += format;
     228        QStringList fmts = mimes[i]->mimesForFormats(formats);
     229        int priority = 0;
     230        foreach (QString fmt, fmts) {
     231            ++priority;
     232            QList<Match>::iterator it = matches.begin();
     233            for (; it != matches.end(); ++it) {
     234                Match &match = *it;
     235                if (match.mime == fmt) {
     236                    // replace if priority is higher, ignore otherwise
     237                    if (priority < match.priority) {
     238                        match.converter = mimes[i];
     239                        match.priority = priority;
     240                    }
     241                    break;
     242                }
     243            }
     244            if (it == matches.end()) {
     245                matches += Match(mimes[i], fmt, priority);
    248246            }
    249247        }
    250248    }
    251249
    252     return fmts;
     250    return matches;
    253251}
    254252
    255253// static
    256 QPMMime *QPMMime::converterFromMime(ULONG format, const QMimeData *mimeData)
    257 {
     254QList<QPMMime::Match> QPMMime::allConvertersFromMimeData(const QMimeData *mimeData)
     255{
     256    QList<Match> matches;
     257
    258258    QList<QPMMime*> mimes = theMimeList()->mimes();
    259259    for (int i = mimes.size()-1; i >= 0; --i) {
    260         if (mimes.at(i)->canConvertFromMime(format, mimeData))
    261             return mimes.at(i);
     260        QList<ULONG> cfs = mimes[i]->formatsForMimeData(mimeData);
     261        int priority = 0;
     262        foreach (ULONG cf, cfs) {
     263            ++priority;
     264            QList<Match>::iterator it = matches.begin();
     265            for (; it != matches.end(); ++it) {
     266                Match &match = *it;
     267                if (match.format == cf) {
     268                    // replace if priority is higher, ignore otherwise
     269                    if (priority < match.priority) {
     270                        match.converter = mimes[i];
     271                        match.priority = priority;
     272                    }
     273                    break;
     274                }
     275            }
     276            if (it == matches.end()) {
     277                matches += Match(mimes[i], cf, priority);
     278            }
     279        }
    262280    }
    263     return 0;
    264 }
    265 
    266 // static
    267 QVector<ULONG> QPMMime::allFormatsForMime(const QMimeData *mimeData)
    268 {
    269     QList<QPMMime*> mimes = theMimeList()->mimes();
    270     QVector<ULONG> cfs(4);
    271     QStringList formats = QInternalMimeData::formatsHelper(mimeData);
    272     for (int f = 0; f < formats.size(); ++f) {
    273         for (int i = mimes.size()-1; i >= 0; --i)
    274             cfs += mimes.at(i)->formatsForMime(formats.at(f), mimeData);
    275     }
    276     return cfs;
     281
     282    return matches;
    277283}
    278284
Note: See TracChangeset for help on using the changeset viewer.