📄 q3dragobject.cpp
字号:
const char * Q3TextDrag::format(int i) const{ if (i > 0) return 0; return d_func()->fmt.constData();}QTextCodec* qt_findcharset(const QByteArray& mimetype){ int i=mimetype.indexOf("charset="); if (i >= 0) { QByteArray cs = mimetype.mid(i+8); stripws(cs); i = cs.indexOf(';'); if (i >= 0) cs = cs.left(i); // win98 often has charset=utf16, and we need to get the correct codec for // it to be able to get Unicode text drops. if (cs == "utf16") cs = "ISO-10646-UCS-2"; // May return 0 if unknown charset return QTextCodec::codecForName(cs); } // no charset=, use locale return QTextCodec::codecForName("utf-8");}static QTextCodec *codecForHTML(const QByteArray &ba){ // determine charset int mib = 0; int pos; QTextCodec *c = 0; if (ba.size() > 1 && (((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff) || ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe))) { mib = 1015; // utf16 } else if (ba.size() > 2 && (uchar)ba[0] == 0xef && (uchar)ba[1] == 0xbb && (uchar)ba[2] == 0xbf) { mib = 106; // utf-8 } else { pos = 0; while ((pos = ba.indexOf('<', pos)) != -1) { int end = ba.indexOf('>', pos+1); if (end == -1) break; const QString str(QString::fromLatin1(ba.mid(pos, end-pos))); if (str.contains(QLatin1String("meta http-equiv="), Qt::CaseInsensitive)) { pos = str.indexOf(QLatin1String("charset="), 0, Qt::CaseInsensitive) + int(strlen("charset=")); if (pos != -1) { int pos2 = ba.indexOf('\"', pos+1); QByteArray cs = ba.mid(pos, pos2-pos); c = QTextCodec::codecForName(cs); if (c) return c; } } pos = end; } } if (mib) c = QTextCodec::codecForMib(mib); return c;}staticQTextCodec* findcodec(const QMimeSource* e){ QTextCodec* r = 0; const char* f; int i; for (i=0; (f=e->format(i)); i++) { bool html = !qstrnicmp(f, "text/html", 9); if (html) r = codecForHTML(e->encodedData(f)); if (!r) r = qt_findcharset(QByteArray(f).toLower()); if (r) return r; } return 0;}/*! \reimp*/QByteArray Q3TextDrag::encodedData(const char* mime) const{ Q_D(const Q3TextDrag); if (mime != d->fmt) return QByteArray(); return d->txt.toUtf8();}/*! \fn bool Q3TextDrag::canDecode(const QMimeSource *source) Returns true if the information in the MIME \a source can be decoded into a QString; otherwise returns false. \sa decode()*/bool Q3TextDrag::canDecode(const QMimeSource* e){ const char* f; for (int i=0; (f=e->format(i)); i++) { if (0==qstrnicmp(f,"text/",5)) { return findcodec(e) != 0; } } return false;}/*! \fn bool Q3TextDrag::decode(const QMimeSource *source, QString &string, QString &subtype) \overload Attempts to decode the dropped information in the MIME \a source into the \a string given. Returns true if successful; otherwise returns false. If \a subtype is null, any text subtype is accepted; otherwise only the specified \a subtype is accepted. \sa canDecode()*/bool Q3TextDrag::decode(const QMimeSource* e, QString& str, QString& subtype){ if(!e) return false; const char* mime; for (int i=0; (mime = e->format(i)); i++) { if (0==qstrnicmp(mime,"text/",5)) { QByteArray m(mime); m = m.toLower(); int semi = m.indexOf(';'); if (semi < 0) semi = m.length(); QString foundst(QString::fromLatin1(m.mid(5,semi-5))); if (subtype.isNull() || foundst == subtype) { bool html = !qstrnicmp(mime, "text/html", 9); QTextCodec* codec = 0; if (html) // search for the charset tag in the HTML codec = codecForHTML(e->encodedData(mime)); if (!codec) codec = qt_findcharset(m); if (codec) { QByteArray payload; payload = e->encodedData(mime); if (payload.size()) { int l; if (codec->mibEnum() != 1015) { // length is at NUL or payload.size() l = 0; while (l < (int)payload.size() && payload[l]) l++; } else { l = payload.size(); } str = codec->toUnicode(payload,l); if (subtype.isNull()) subtype = foundst; return true; } } } } } return false;}/*! \fn bool Q3TextDrag::decode(const QMimeSource *source, QString &string) Attempts to decode the dropped information in the MIME \a source into the \a string given. Returns true if successful; otherwise returns false. \sa canDecode()*/bool Q3TextDrag::decode(const QMimeSource* e, QString& str){ QString st; return decode(e, str, st);}/* Q3ImageDrag could use an internal MIME type for communicating QPixmaps and QImages rather than always converting to raw data. This is available for that purpose and others. It is not currently used.*//*! \class Q3ImageDrag qdragobject.h \brief The Q3ImageDrag class provides a drag and drop object for transferring images. \compat Images are offered to the receiving application in multiple formats, determined by Qt's output formats.*//*! Constructs an image drag object with the given \a name, and sets its data to \a image. The \a dragSource is the widget that the drag operation started from.*/Q3ImageDrag::Q3ImageDrag(QImage image, QWidget * dragSource, const char * name) : Q3DragObject(*(new Q3ImageDragPrivate), dragSource){ setObjectName(QLatin1String(name)); setImage(image);}/*! Constructs a default image drag object with the given \a name. The \a dragSource is the widget that the drag operation started from.*/Q3ImageDrag::Q3ImageDrag(QWidget * dragSource, const char * name) : Q3DragObject(*(new Q3ImageDragPrivate), dragSource){ setObjectName(QLatin1String(name));}/*! \internal */Q3ImageDrag::Q3ImageDrag(Q3ImageDragPrivate &dd, QWidget *dragSource) : Q3DragObject(dd, dragSource){}/*! Destroys the image drag object.*/Q3ImageDrag::~Q3ImageDrag(){ // nothing}/*! Sets the \a image to be dragged. You will need to call this if you did not pass the image during construction.*/void Q3ImageDrag::setImage(QImage image){ Q_D(Q3ImageDrag); d->img = image; QList<QByteArray> formats = QImageWriter::supportedImageFormats(); formats.removeAll("PBM"); // remove non-raw PPM if (image.depth()!=32) { // BMP better than PPM for paletted images if (formats.removeAll("BMP")) // move to front formats.insert(0,"BMP"); } // PNG is best of all if (formats.removeAll("PNG")) // move to front formats.insert(0,"PNG"); for(int i = 0; i < formats.count(); i++) { QByteArray format("image/"); format += formats.at(i); format = format.toLower(); if (format == "image/pbmraw") format = "image/ppm"; d->ofmts.append(format); }}/*! \reimp*/const char * Q3ImageDrag::format(int i) const{ Q_D(const Q3ImageDrag); return i < d->ofmts.count() ? d->ofmts.at(i).data() : 0;}/*! \reimp*/QByteArray Q3ImageDrag::encodedData(const char* fmt) const{ Q_D(const Q3ImageDrag); if (qstrnicmp(fmt, "image/", 6)==0) { QByteArray f(fmt+6); QByteArray dat; QBuffer w(&dat); w.open(QIODevice::WriteOnly); QImageWriter writer(&w, f.toUpper()); if (!writer.write(d->img)) return QByteArray(); w.close(); return dat; } else { return QByteArray(); }}/*! \fn bool Q3ImageDrag::canDecode(const QMimeSource *source) Returns true if the information in the MIME \a source can be decoded into an image; otherwise returns false. \sa decode()*/bool Q3ImageDrag::canDecode(const QMimeSource* e){ const QList<QByteArray> fileFormats = QImageReader::supportedImageFormats(); for (int i = 0; i < fileFormats.count(); ++i) { if (e->provides("image" + fileFormats.at(i).toLower())) return true; } return false;}/*! \fn bool Q3ImageDrag::decode(const QMimeSource *source, QImage &image) Decode the dropped information in the MIME \a source into the \a image. Returns true if successful; otherwise returns false. \sa canDecode()*/bool Q3ImageDrag::decode(const QMimeSource* e, QImage& img){ if (!e) return false; QByteArray payload; QList<QByteArray> fileFormats = QImageReader::supportedImageFormats(); // PNG is best of all // (this is a rather strange hack, but it works now) for (int i=0; i < fileFormats.count(); i++) { if (fileFormats.at(i).toLower() == "png") { fileFormats.prepend("png"); break ; } } // move to front for (int i = 0; i < fileFormats.count(); ++i) { QByteArray type = "image/" + fileFormats.at(i).toLower(); if (! e->provides(type)) continue; payload = e->encodedData(type); if (!payload.isEmpty()) break; } if (payload.isEmpty()) return false; img.loadFromData(payload); if (img.isNull()) return false; return true;}/*! \fn bool Q3ImageDrag::decode(const QMimeSource *source, QPixmap &pixmap) \overload Decodes the dropped information in the MIME \a source into the \a pixmap. Returns true if successful; otherwise returns false. This is a convenience function that converts the information to a QPixmap via a QImage. \sa canDecode()*/bool Q3ImageDrag::decode(const QMimeSource* e, QPixmap& pm){ if (!e) return false; QImage img; // We avoid dither, since the image probably came from this display if (decode(e, img)) { pm = QPixmap::fromImage(img, Qt::AvoidDither); if (pm.isNull()) return false; return true; } return false;}/*! \class Q3StoredDrag qdragobject.h \brief The Q3StoredDrag class provides a simple stored-value drag object for arbitrary MIME data. \compat When a block of data has only one representation, you can use a Q3StoredDrag to hold it. For more information about drag and drop, see the Q3DragObject class and the \link dnd.html drag and drop documentation\endlink.*//*! Constructs a Q3StoredDrag. The \a dragSource and \a name are passed to the Q3DragObject constructor, and the format is set to \a mimeType. The data will be unset. Use setEncodedData() to set it.*/Q3StoredDrag::Q3StoredDrag(const char* mimeType, QWidget * dragSource, const char * name) : Q3DragObject(*new Q3StoredDragPrivate, dragSource){ Q_D(Q3StoredDrag); setObjectName(QLatin1String(name)); d->fmt = qstrdup(mimeType);}/*! \internal */Q3StoredDrag::Q3StoredDrag(Q3StoredDragPrivate &dd, const char* mimeType, QWidget * dragSource) : Q3DragObject(dd, dragSource){ d_func()->fmt = qstrdup(mimeType);}/*! Destroys the drag object.*/Q3StoredDrag::~Q3StoredDrag(){ delete [] (char*)d_func()->fmt;}/*! \reimp*/const char * Q3StoredDrag::format(int i) const{ if (i==0) return d_func()->fmt; else return 0;}/*! \fn void Q3StoredDrag::setEncodedData(const QByteArray &data) Sets the encoded \a data of this drag object. The encoded data is delivered to drop sites; it must be in a strictly defined and portable format. The drag object can't be dropped (by the user) until this function has been called.*/void Q3StoredDrag::setEncodedData(const QByteArray & encodedData){ d_func()->enc = encodedData;}/*! \fn QByteArray Q3StoredDrag::encodedData(const char *format) const Returns the stored data in the \a format given. \sa setEncodedData()*/QByteArray Q3StoredDrag::encodedData(const char* m) const{ if (!qstricmp(m, d_func()->fmt)) return d_func()->enc; else return QByteArray();}/*! \class Q3UriDrag qdragobject.h \brief The Q3UriDrag class provides a drag object for a list of URI references. \compat URIs are a useful way to refer to files that may be distributed across multiple machines. A URI will often refer to a file on a machine local to both the drag source and the drop target, so the URI can be equivalent to passing a file name but is more extensible. Use URIs in Unicode form so that the user can comfortably edit and view them. For use in HTTP or other protocols, use the correctly escaped ASCII form. You can convert a list of file names to file URIs using setFileNames(), or into human-readable form with setUnicodeUris(). Static functions are provided to convert between filenames and URIs; e.g. uriToLocalFile() and localFileToUri(). Static functions are also provided to convert URIs to and from human-readable form; e.g. uriToUnicodeUri() and unicodeUriToUri(). You can also decode URIs from a MIME source into a list with decodeLocalFiles() and decodeToUnicodeUris().*//*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -