📄 q3dragobject.cpp
字号:
Constructs an object to drag the list of \a uris. The \a dragSource and \a name are passed to the Q3StoredDrag constructor. Note that URIs are always in escaped UTF8 encoding.*/Q3UriDrag::Q3UriDrag(const Q3StrList &uris, QWidget * dragSource, const char * name) : Q3StoredDrag("text/uri-list", dragSource){ setObjectName(QLatin1String(name)); setUris(uris);}/*! Constructs an object to drag with the given \a name. You must call setUris() before you start the drag(). Both the \a dragSource and the \a name are passed to the Q3StoredDrag constructor.*/Q3UriDrag::Q3UriDrag(QWidget * dragSource, const char * name) : Q3StoredDrag("text/uri-list", dragSource){ setObjectName(QLatin1String(name));}#endif/*! Destroys the URI drag object.*/Q3UriDrag::~Q3UriDrag(){}/*! \fn void Q3UriDrag::setUris(const QList<QByteArray> &list) Changes the \a list of URIs to be dragged. Note that URIs are always in escaped UTF8 encoding.*/void Q3UriDrag::setUris(const QList<QByteArray> &uris){ QByteArray a; int c = 0; int i; int count = uris.count(); for (i = 0; i < count; ++i) c += uris.at(i).size() + 2; //length + \r\n a.reserve(c+1); for (i = 0; i < count; ++i) { a.append(uris.at(i)); a.append("\r\n"); } a[c] = 0; setEncodedData(a);}/*! \fn bool Q3UriDrag::canDecode(const QMimeSource *source) Returns true if decode() can decode the MIME \a source; otherwise returns false.*/bool Q3UriDrag::canDecode(const QMimeSource* e){ return e->provides("text/uri-list");}/*! \fn bool Q3UriDrag::decode(const QMimeSource* source, Q3StrList& list) Decodes URIs from the MIME \a source, placing the result in the \a list. The list is cleared before any items are inserted. Returns true if the MIME \a source contained a valid list of URIs; otherwise returns false.*/bool Q3UriDrag::decode(const QMimeSource* e, Q3StrList& l){ QByteArray payload = e->encodedData("text/uri-list"); if (payload.size()) { l.clear(); l.setAutoDelete(true); uint c=0; const char* data = payload.data(); while ((int)c < payload.size() && data[c]) { uint f = c; // Find line end while ((int)c < payload.size() && data[c] && data[c]!='\r' && data[c] != '\n') c++; Q3CString s(data+f,c-f+1); if (s[0] != '#') // non-comment? l.append(s); // Skip junk while ((int)c < payload.size() && data[c] && (data[c]=='\n' || data[c]=='\r')) c++; } return true; } return false;}static uint htod(int h){ if (isdigit(h)) return h - '0'; return tolower(h) - 'a' + 10;}/*! \fn Q3UriDrag::setFilenames(const QStringList &list) \obsolete Sets the filename's in the drag object to those in the given \a list. Use setFileNames() instead.*//*! \fn void Q3UriDrag::setFileNames(const QStringList &filenames) Sets the URIs to be local file URIs equivalent to the \a filenames. \sa localFileToUri(), setUris()*/void Q3UriDrag::setFileNames(const QStringList & fnames){ QList<QByteArray> uris; for (QStringList::ConstIterator i = fnames.begin(); i != fnames.end(); ++i) { QByteArray fileUri = localFileToUri(*i); if (!fileUri.isEmpty()) uris.append(fileUri); } setUris(uris);}/*! \fn void Q3UriDrag::setFileNames(const QString &name) \fn void Q3UriDrag::setFilenames(const QString &name) Same as setFileNames(QStringList(\a name)).*//*! \fn void Q3UriDrag::setUnicodeUris(const QStringList &list) Sets the URIs in the \a list to be Unicode URIs (only useful for displaying to humans). \sa localFileToUri(), setUris()*/void Q3UriDrag::setUnicodeUris(const QStringList & uuris){ QList<QByteArray> uris; for (int i = 0; i < uuris.count(); ++i) uris.append(unicodeUriToUri(uuris.at(i))); setUris(uris);}/*! \fn QByteArray Q3UriDrag::unicodeUriToUri(const QString &string) Returns the URI equivalent of the Unicode URI given in the \a string (only useful for displaying to humans). \sa uriToLocalFile()*/QByteArray Q3UriDrag::unicodeUriToUri(const QString& uuri){ QByteArray utf8 = uuri.toUtf8(); QByteArray escutf8; int n = utf8.length(); bool isFile = uuri.startsWith(QLatin1String("file://")); for (int i=0; i<n; i++) { if (utf8[i] >= 'a' && utf8[i] <= 'z' || utf8[i] == '/' || utf8[i] >= '0' && utf8[i] <= '9' || utf8[i] >= 'A' && utf8[i] <= 'Z' || utf8[i] == '-' || utf8[i] == '_' || utf8[i] == '.' || utf8[i] == '!' || utf8[i] == '~' || utf8[i] == '*' || utf8[i] == '(' || utf8[i] == ')' || utf8[i] == '\'' // Allow this through, so that all URI-references work. || (!isFile && utf8[i] == '#') || utf8[i] == ';' || utf8[i] == '?' || utf8[i] == ':' || utf8[i] == '@' || utf8[i] == '&' || utf8[i] == '=' || utf8[i] == '+' || utf8[i] == '$' || utf8[i] == ',') { escutf8 += utf8[i]; } else { // Everything else is escaped as %HH QString s; s.sprintf("%%%02x",(uchar)utf8[i]); escutf8 += s.latin1(); } } return escutf8;}/*! Returns the URI equivalent to the absolute local \a filename. \sa uriToLocalFile()*/QByteArray Q3UriDrag::localFileToUri(const QString& filename){ QString r = filename; //check that it is an absolute file if (QDir::isRelativePath(r)) return QByteArray();#ifdef Q_WS_WIN bool hasHost = false; // convert form network path if (r.left(2) == QLatin1String("\\\\") || r.left(2) == QLatin1String("//")) { r.remove(0, 2); hasHost = true; } // Slosh -> Slash int slosh; while ((slosh=r.indexOf(QLatin1Char('\\'))) >= 0) { r[slosh] = QLatin1Char('/'); } // Drive if (r[0] != QLatin1Char('/') && !hasHost) r.insert(0,QLatin1Char('/'));#endif#if defined (Q_WS_X11) && 0 // URL without the hostname is considered to be errorneous by XDnD. // See: http://www.newplanetsoftware.com/xdnd/dragging_files.html // This feature is not active because this would break dnd between old and new qt apps. char hostname[257]; if (gethostname(hostname, 255) == 0) { hostname[256] = '\0'; r.prepend(QString::fromLatin1(hostname)); }#endif return unicodeUriToUri(QString(QLatin1String("file://") + r));}/*! \fn QString Q3UriDrag::uriToUnicodeUri(const char *string) Returns the Unicode URI (only useful for displaying to humans) equivalent of the URI given in the \a string. Note that URIs are always in escaped UTF8 encoding. \sa localFileToUri()*/QString Q3UriDrag::uriToUnicodeUri(const char* uri){ QByteArray utf8; while (*uri) { switch (*uri) { case '%': { uint ch = (uchar) uri[1]; if (ch && uri[2]) { ch = htod(ch) * 16 + htod((uchar) uri[2]); utf8 += (char) ch; uri += 2; } } break; default: utf8 += *uri; } ++uri; } return QString::fromUtf8(utf8);}/*! \fn QString Q3UriDrag::uriToLocalFile(const char *string) Returns the name of a local file equivalent to the URI given in the \a string, or an empty string if it does not refer to a local file. Note that URIs are always in escaped UTF8 encoding. \sa localFileToUri()*/QString Q3UriDrag::uriToLocalFile(const char* uri){ QString file; if (!uri) return file; if (0==qstrnicmp(uri,"file:/",6)) // It is a local file uri uri += 6; else if (QString(QLatin1String(uri)).indexOf(QLatin1String(":/")) != -1) // It is a different scheme uri return file; bool local = uri[0] != '/' || (uri[0] != '\0' && uri[1] == '/');#ifdef Q_WS_X11 // do we have a hostname? if (!local && uri[0] == '/' && uri[2] != '/') { // then move the pointer to after the 'hostname/' part of the uri const char* hostname_end = strchr(uri+1, '/'); if (hostname_end != NULL) { char hostname[257]; if (gethostname(hostname, 255) == 0) { hostname[256] = '\0'; if (qstrncmp(uri+1, hostname, hostname_end - (uri+1)) == 0) { uri = hostname_end + 1; // point after the slash local = true; } } } }#endif if (local) { file = uriToUnicodeUri(uri); if (uri[1] == '/') { file.remove((uint)0,1); } else { file.insert(0, QLatin1Char('/')); }#ifdef Q_WS_WIN if (file.length() > 2 && file[0] == QLatin1Char('/') && file[2] == QLatin1Char('|')) { file[2] = QLatin1Char(':'); file.remove(0,1); } else if (file.length() > 2 && file[0] == QLatin1Char('/') && file[1].isLetter() && file[2] == QLatin1Char(':')) { file.remove(0, 1); } // Leave slash as slashes.#endif }#ifdef Q_WS_WIN else { file = uriToUnicodeUri(uri); // convert to network path file.insert(1, QLatin1Char('/')); // leave as forward slashes }#endif return file;}/*! \fn bool Q3UriDrag::decodeLocalFiles(const QMimeSource *source, QStringList &list) Decodes URIs from the MIME \a source, converting them to local filenames where possible, and places them in the \a list (which is first cleared). Returns true if the MIME \a source contained a valid list of URIs; otherwise returns false. The list will be empty if no URIs referred to local files.*/bool Q3UriDrag::decodeLocalFiles(const QMimeSource* e, QStringList& l){ Q3StrList u; if (!decode(e, u)) return false; l.clear(); for (uint i = 0; i < u.count(); ++i) { QString lf = uriToLocalFile(u.at(i)); if (!lf.isEmpty()) l.append(lf); } return true;}/*! \fn bool Q3UriDrag::decodeToUnicodeUris(const QMimeSource *source, QStringList &list) Decodes URIs from the MIME \a source, converting them to Unicode URIs (only useful for displaying to humans), and places them in the \a list (which is first cleared). Returns true if the MIME \a source contained a valid list of URIs; otherwise returns false.*/bool Q3UriDrag::decodeToUnicodeUris(const QMimeSource* e, QStringList& l){ Q3StrList u; if (!decode(e, u)) return false; l.clear(); for (uint i = 0; i < u.count(); ++i) l.append(uriToUnicodeUri(u.at(i))); return true;}/*! \class Q3ColorDrag \brief The Q3ColorDrag class provides a drag and drop object for transferring colors between widgets. \compat This class provides a drag object which can be used to transfer data about colors for drag and drop and in the clipboard. For example, it is used in QColorDialog. The color is set in the constructor but can be changed with setColor(). For more information about drag and drop, see the Q3DragObject class and the \link dnd.html drag and drop documentation\endlink.*//*! Constructs a color drag object with the given \a col. Passes \a dragsource and \a name to the Q3StoredDrag constructor.*/Q3ColorDrag::Q3ColorDrag(const QColor &col, QWidget *dragsource, const char *name) : Q3StoredDrag("application/x-color", dragsource){ setObjectName(QLatin1String(name)); setColor(col);}/*! Constructs a color drag object with a white color. Passes \a dragsource and \a name to the Q3StoredDrag constructor.*/Q3ColorDrag::Q3ColorDrag(QWidget *dragsource, const char *name) : Q3StoredDrag("application/x-color", dragsource){ setObjectName(QLatin1String(name)); setColor(Qt::white);}/*! \fn void Q3ColorDrag::setColor(const QColor &color) Sets the \a color of the color drag.*/void Q3ColorDrag::setColor(const QColor &col){ short r = (col.red() << 8) | col.red(); short g = (col.green() << 8) | col.green(); short b = (col.blue() << 8) | col.blue(); // make sure we transmit data in network order r = htons(r); g = htons(g); b = htons(b); ushort rgba[4] = { r, g, b, 0xffff // Alpha not supported yet. }; QByteArray data; data.resize(sizeof(rgba)); memcpy(data.data(), rgba, sizeof(rgba)); setEncodedData(data);}/*! \fn bool Q3ColorDrag::canDecode(QMimeSource *source) Returns true if the color drag object can decode the MIME \a source; otherwise returns false.*/bool Q3ColorDrag::canDecode(QMimeSource *e){ return e->provides("application/x-color");}/*! \fn bool Q3ColorDrag::decode(QMimeSource *source, QColor &color) Decodes the MIME \a source, and sets the decoded values to the given \a color. Returns true if the decoding is successful. Returns false if the size of the encoded data is not the expected size.*/bool Q3ColorDrag::decode(QMimeSource *e, QColor &col){ QByteArray data = e->encodedData("application/x-color"); ushort rgba[4]; if (data.size() != sizeof(rgba)) return false; memcpy(rgba, data.constData(), sizeof(rgba)); short r = rgba[0]; short g = rgba[1]; short b = rgba[2]; short a = rgba[3]; // data is in network order r = ntohs(r); g = ntohs(g); b = ntohs(b); a = ntohs(a); r = (r >> 8) & 0xff; g = (g >> 8) & 0xff; b = (b >> 8) & 0xff; a = (a >> 8) & 0xff; col.setRgb(r, g, b, a); return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -