Ignore:
Timestamp:
Nov 20, 2009, 7:39:03 PM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

gui/kernel: mime: Implemented mime<-clipboard interface for "text/plain" (getting from the system clipboard).

File:
1 edited

Legend:

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

    r331 r334  
    7474{
    7575public:
    76     QClipboardWatcher() : isDirty(true) {}
     76    QClipboardWatcher() {}
    7777
    7878    bool hasFormat_sys(const QString &mimetype) const;
     
    8282private:
    8383
    84     void peekData() const;
     84    bool peekData(bool leaveOpen = false) const;
    8585
    8686    mutable QList<ULONG> formats;
    8787    mutable QList<QPMMime::Match> matches;
    88     mutable bool isDirty;
    8988};
    9089
    91 void QClipboardWatcher::peekData() const
    92 {
    93     if (!isDirty)
    94         return;
    95 
     90bool QClipboardWatcher::peekData(bool leaveOpen) const
     91{
    9692    if (!WinOpenClipbrd(NULLHANDLE)) {
    9793#ifndef QT_NO_DEBUG
     
    9995                 "failed with 0x%lX", WinGetLastError(NULLHANDLE));
    10096#endif
    101         return;
    102     }
    103 
    104     formats.clear();
     97        return false;
     98    }
     99
     100    QList<ULONG> newFormats;
    105101    ULONG cf = 0;
    106102    while ((cf = WinEnumClipbrdFmts(NULLHANDLE, cf)))
    107         formats << cf;
    108 
    109     WinCloseClipbrd(NULLHANDLE);
    110 
     103        newFormats << cf;
     104
     105    if (!leaveOpen)
     106        WinCloseClipbrd(NULLHANDLE);
     107
     108    // optimization: we don't want to call the potentially expensive
     109    // allConvertersFromFormats() unlesss we really got a different set
     110    if (newFormats == formats)
     111        return true;
     112
     113    formats = newFormats;
    111114    matches = QPMMime::allConvertersFromFormats(formats);
    112     isDirty = false;
     115
     116#ifdef QCLIPBOARD_DEBUG
     117    foreach(QPMMime::Match match, matches)
     118        DEBUG(("QClipboardWatcher::peekData: converter %p mime \"%ls\" "
     119               "format 0x%lX priority %d", match.converter, match.mime.utf16(),
     120               match.format, match.priority));
     121#endif
     122
     123    return true;
    113124}
    114125
    115126bool QClipboardWatcher::hasFormat_sys(const QString &mime) const
    116127{
    117     peekData();
    118     if (isDirty)
    119         return false; // peekData() failed
     128    if (!peekData())
     129        return false;
    120130
    121131    foreach (QPMMime::Match match, matches)
     
    130140    QStringList fmts;
    131141
    132     peekData();
    133     if (isDirty)
    134         return fmts; // peekData() failed
     142    if (!peekData())
     143        return fmts;
    135144
    136145    foreach (QPMMime::Match match, matches)
     
    145154    QVariant result;
    146155
    147     peekData();
    148     if (isDirty)
    149         return result; // peekData() failed
     156    if (!peekData(true /*leaveOpen*/))
     157        return result;
    150158
    151159    foreach (QPMMime::Match match, matches) {
     
    157165                                                            data, match.mime, type);
    158166            }
    159             return result;
    160         }
    161     }
     167            break;
     168        }
     169    }
     170
     171    WinCloseClipbrd(NULLHANDLE);
    162172
    163173    return result;
     
    175185
    176186    void setAsClipboardViewer();
    177     bool ownsClipboard();
     187    bool ownsClipboard() const;
    178188    void putAllMimeToClipboard(bool isDelayed);
    179189    void flushClipboard();
     190
     191    const QMimeData *mimeData() const;
    180192
    181193    static QClipboardData *instance()
     
    202214    QList<QPMMime::Match> matches;
    203215    HWND prevClipboardViewer;
     216
     217    QClipboardWatcher watcher;
    204218
    205219    bool ignore_WM_DESTROYCLIPBOARD;
     
    265279}
    266280
    267 bool QClipboardData::ownsClipboard()
     281bool QClipboardData::ownsClipboard() const
    268282{
    269283    return src && hwnd() == WinQueryClipbrdOwner(NULLHANDLE);
     
    346360        setSource(0);
    347361    }
     362}
     363
     364const QMimeData *QClipboardData::mimeData() const
     365{
     366    // short cut for local copy / paste
     367    if (ownsClipboard() && src)
     368        return src;
     369    return &watcher;
    348370}
    349371
     
    481503const QMimeData *QClipboard::mimeData(Mode mode) const
    482504{
    483     // @todo implement
    484     return 0;
     505    if (mode != Clipboard)
     506        return 0;
     507
     508    return QClipboardData::instance()->mimeData();
    485509}
    486510
Note: See TracChangeset for help on using the changeset viewer.