Changeset 323 for trunk/src


Ignore:
Timestamp:
Nov 16, 2009, 1:06:34 AM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

gui/kernel: More QPMMime work.

Location:
trunk/src/gui/kernel
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gui/kernel/kernel.pri

    r95 r323  
    9898                kernel/qdesktopwidget_pm.cpp \
    9999                kernel/qdnd_pm.cpp \
     100                kernel/qmime_pm.cpp \
    100101                kernel/qkeymapper_pm.cpp \
    101102                kernel/qsound_pm.cpp \
  • trunk/src/gui/kernel/qclipboard_pm.cpp

    r322 r323  
    5555#include "qdnd_p.h"
    5656
     57#define QCLIPBOARD_DEBUG
     58
    5759QT_BEGIN_NAMESPACE
    5860
     
    7577        ULONG cf = 0;
    7678        while ((cf = WinEnumClipbrdFmts(NULLHANDLE, cf))) {
    77 //          if (QPMMime::converterToMime(mime, cf)) {
    78 //              ok = true;
    79 //              break;
    80 //          }
     79            if (QPMMime::converterToMime(mime, cf)) {
     80                ok = true;
     81                break;
     82            }
    8183        }
    8284        WinCloseClipbrd(NULLHANDLE);
     
    9496    QStringList fmts;
    9597    if (WinOpenClipbrd(NULLHANDLE)) {
    96 //      fmts = QPMMime::allMimesForFormats();
     98        QVector<ULONG> cfs;
     99        ULONG cf = 0;
     100        while ((cf = WinEnumClipbrdFmts(NULLHANDLE, cf)))
     101            cfs << cf;
     102        fmts = QPMMime::allMimesForFormats(cfs);
    97103        WinCloseClipbrd(NULLHANDLE);
    98104    }
     
    108114                                             QVariant::Type type) const
    109115{
    110     QStringList fmts;
     116    QVariant result;
     117
    111118    if (WinOpenClipbrd(NULLHANDLE)) {
    112         // @todo enumerate all formats and select the best converter
    113 //      QPMMime *converter = QPMMime::converterToMime(mime, cf);
    114 //      if (converter)
    115 //          result = converter->convertToMime(mime, cf, type);
     119        ULONG cf = 0;
     120        while ((cf = WinEnumClipbrdFmts(NULLHANDLE, cf))) {
     121            QPMMime *converter = QPMMime::converterToMime(mime, cf);
     122            if (converter) {
     123                ULONG flags;
     124                if (WinQueryClipbrdFmtInfo(NULLHANDLE, cf, &flags)) {
     125                    ULONG data = WinQueryClipbrdData(NULLHANDLE, cf);
     126                    if (data) {
     127                        result = converter->convertToMime(mime, type, cf, flags, data);
     128                    }
     129                }
     130                break;
     131            }
     132        }
    116133        WinCloseClipbrd(NULLHANDLE);
    117134    }
     
    121138                 WinGetLastError(NULLHANDLE));
    122139#endif
    123     return fmts;
     140    return result;
    124141}
    125142
     
    171188void QClipboard::setMimeData(QMimeData *src, Mode mode)
    172189{
    173     // @todo implement
     190    if (mode != Clipboard)
     191        return;
     192
     193    if (!WinOpenClipbrd(NULLHANDLE)) {
     194#ifndef QT_NO_DEBUG
     195        qWarning("QClipboard: WinOpenClipbrd failed with %lX",
     196                 WinGetLastError(NULLHANDLE));
     197#endif
     198        return;
     199    }
     200
     201    // @todo
     202//  QClipboardData *d = clipboardData();
     203//  d->setSource(src);
     204
     205    WinCloseClipbrd(NULLHANDLE);
    174206}
    175207
  • trunk/src/gui/kernel/qmime.h

    r321 r323  
    114114*/
    115115
     116typedef unsigned long ULONG;
     117
    116118class Q_GUI_EXPORT QPMMime
    117119{
     
    119121    QPMMime();
    120122    virtual ~QPMMime();
     123
     124    // for converting from Qt
     125    virtual bool canConvertFromMime(ULONG format, const QMimeData *mimeData) const = 0;
     126    virtual bool convertFromMime(ULONG format, const QMimeData *mimeData,
     127                                 ULONG &flags, ULONG &data) const = 0;
     128    virtual QVector<ULONG> formatsForMime(const QString &mimeType, const QMimeData *mimeData) const = 0;
     129
     130    // for converting to Qt
     131    virtual bool canConvertToMime(const QString &mimeType, ULONG format) const = 0;
     132    virtual QVariant convertToMime(const QString &mimeType, QVariant::Type preferredType,
     133                                   ULONG format, ULONG flags, ULONG data) const = 0;
     134    virtual QString mimeForFormat(ULONG format) const = 0;
     135
     136    static ULONG registerMimeType(const QString &mime);
     137
     138private:
     139    friend class QClipboardWatcher;
     140
     141    static QPMMime *converterToMime(const QString &mimeType, ULONG format);
     142    static QStringList allMimesForFormats(const QVector<ULONG> &formats);
     143    static QPMMime *converterFromMime(ULONG format, const QMimeData *mimeData);
     144    static QVector<ULONG> allFormatsForMime(const QMimeData *mimeData);
    121145};
    122146
  • trunk/src/gui/kernel/qmime_pm.cpp

    r321 r323  
    6060#include "qdir.h"
    6161
     62#define QMIME_DEBUG
     63
    6264QT_BEGIN_NAMESPACE
     65
     66class QPMMimeList
     67{
     68public:
     69    QPMMimeList();
     70    ~QPMMimeList();
     71    void addMime(QPMMime *mime);
     72    void removeMime(QPMMime *mime);
     73    QList<QPMMime*> mimes();
     74
     75private:
     76    void init();
     77    bool initialized;
     78    QList<QPMMime*> list;
     79};
     80
     81Q_GLOBAL_STATIC(QPMMimeList, theMimeList);
     82
    6383
    6484/*!
     
    99119*/
    100120
    101 // @todo lots of stuff...
    102 
     121/*!
     122Constructs a new conversion object, adding it to the globally accessed
     123list of available converters.
     124*/
    103125QPMMime::QPMMime()
    104126{
    105 }
    106 
     127    theMimeList()->addMime(this);
     128}
     129
     130/*!
     131Destroys a conversion object, removing it from the global
     132list of available converters.
     133*/
    107134QPMMime::~QPMMime()
    108135{
     136    theMimeList()->removeMime(this);
     137}
     138
     139/*!
     140    Registers the MIME type \a mime, and returns an ID number
     141    identifying the format on OS/2.
     142*/
     143ULONG QPMMime::registerMimeType(const QString &mime)
     144{
     145    QString atom = QLatin1String("mime:") + mime;
     146
     147    ULONG cf = WinAddAtom(WinQuerySystemAtomTable(), atom.toLocal8Bit());
     148    if (!cf) {
     149#ifndef QT_NO_DEBUG
     150        qWarning("QPMMime: WinAddAtom failed with %lX",
     151                 WinGetLastError(NULLHANDLE));
     152#endif
     153        return 0;
     154    }
     155
     156    return cf;
     157}
     158
     159/*!
     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.
     164
     165    All subclasses must reimplement this pure virtual function.
     166*/
     167
     168/*!
     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.
     175
     176    Return true if the conversion was successful.
     177
     178    All subclasses must reimplement this pure virtual function.
     179*/
     180
     181/*!
     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.
     186
     187    All subclasses must reimplement this pure virtual function.
     188*/
     189
     190/*!
     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.
     195
     196    All subclasses must reimplement this pure virtual function.
     197*/
     198
     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
     219// static
     220QPMMime *QPMMime::converterToMime(const QString &mimeType, ULONG format)
     221{
     222    QList<QPMMime*> mimes = theMimeList()->mimes();
     223    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
     231QStringList 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;
     248            }
     249        }
     250    }
     251
     252    return fmts;
     253}
     254
     255// static
     256QPMMime *QPMMime::converterFromMime(ULONG format, const QMimeData *mimeData)
     257{
     258    QList<QPMMime*> mimes = theMimeList()->mimes();
     259    for (int i = mimes.size()-1; i >= 0; --i) {
     260        if (mimes.at(i)->canConvertFromMime(format, mimeData))
     261            return mimes.at(i);
     262    }
     263    return 0;
     264}
     265
     266// static
     267QVector<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;
     277}
     278
     279////////////////////////////////////////////////////////////////////////////////
     280
     281////////////////////////////////////////////////////////////////////////////////
     282
     283QPMMimeList::QPMMimeList()
     284    : initialized(false)
     285{
     286}
     287
     288QPMMimeList::~QPMMimeList()
     289{
     290    while (list.size())
     291        delete list.first();
     292}
     293
     294
     295void QPMMimeList::init()
     296{
     297    if (!initialized) {
     298        initialized = true;
     299        // @todo new QPMMimeXXX;
     300    }
     301}
     302
     303void QPMMimeList::addMime(QPMMime *mime)
     304{
     305    init();
     306    list.append(mime);
     307}
     308
     309void QPMMimeList::removeMime(QPMMime *mime)
     310{
     311    init();
     312    list.removeAll(mime);
     313}
     314
     315QList<QPMMime*> QPMMimeList::mimes()
     316{
     317    init();
     318    return list;
    109319}
    110320
Note: See TracChangeset for help on using the changeset viewer.