📄 q3dragobject.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qplatformdefs.h"#ifndef QT_NO_MIME#include "q3dragobject.h"#include "qpixmap.h"#include "qevent.h"#include "qfile.h"#include "qtextcodec.h"#include "qapplication.h"#include "qpoint.h"#include "qwidget.h"#include "qbuffer.h"#include "qimagereader.h"#include "qimagewriter.h"#include "qimage.h"#include "qregexp.h"#include "qdir.h"#include "qdrag.h"#include "q3strlist.h"#include "q3cstring.h"#include <private/qobject_p.h>#include <ctype.h>static QWidget *last_target = 0;class QDragMime;class Q3DragObjectPrivate : public QObjectPrivate{ Q_DECLARE_PUBLIC(Q3DragObject)public: Q3DragObjectPrivate(): hot(0,0),pm_cursor(0) {} QPixmap pixmap; QPoint hot; // store default cursors QPixmap *pm_cursor;};class Q3TextDragPrivate : public Q3DragObjectPrivate{ Q_DECLARE_PUBLIC(Q3TextDrag)public: Q3TextDragPrivate() { setSubType(QLatin1String("plain")); } void setSubType(const QString & st) { subtype = st; fmt = QString(QLatin1String("text/")).toLatin1() + subtype.toLatin1(); } QString txt; QString subtype; QByteArray fmt;};class Q3StoredDragPrivate : public Q3DragObjectPrivate{ Q_DECLARE_PUBLIC(Q3StoredDrag)public: Q3StoredDragPrivate() {} const char* fmt; QByteArray enc;};class Q3ImageDragPrivate : public Q3DragObjectPrivate{ Q_DECLARE_PUBLIC(Q3ImageDrag)public: QImage img; QList<QByteArray> ofmts;};class QDragMime : public QMimeData{public: QDragMime(Q3DragObject *parent) : QMimeData(), dragObject(parent) { } ~QDragMime(); QByteArray data(const QString &mimetype) const; bool hasFormat(const QString &mimetype) const; QStringList formats() const; QPointer<Q3DragObject> dragObject;};QDragMime::~QDragMime(){ delete dragObject;}QByteArray QDragMime::data(const QString &mimetype) const{ return dragObject->encodedData(mimetype.latin1());}bool QDragMime::hasFormat(const QString &mimetype) const{ return dragObject->provides(mimetype.latin1());}QStringList QDragMime::formats() const{ int i = 0; const char *format; QStringList f; while ((format = dragObject->format(i))) { f.append(QLatin1String(format)); ++i; } return f;}/*! Constructs a drag object called \a name with a parent \a dragSource. Note that the drag object will be deleted when the \a dragSource is deleted.*/Q3DragObject::Q3DragObject(QWidget * dragSource, const char * name) : QObject(*(new Q3DragObjectPrivate), dragSource){ setObjectName(QLatin1String(name));}/*! \internal */Q3DragObject::Q3DragObject(Q3DragObjectPrivate &dd, QWidget *dragSource) : QObject(dd, dragSource){}/*! Destroys the drag object, canceling any drag and drop operation in which it is involved.*/Q3DragObject::~Q3DragObject(){}#ifndef QT_NO_DRAGANDDROP/*! Set the pixmap, \a pm, to display while dragging the object. The platform-specific implementation will use this where it can - so provide a small masked pixmap, and do not assume that the user will actually see it. For example, cursors on Windows 95 are of limited size. The \a hotspot is the point on (or off) the pixmap that should be under the cursor as it is dragged. It is relative to the top-left pixel of the pixmap. \warning We have seen problems with drag cursors on different graphics hardware and driver software on Windows. Setting the graphics acceleration in the display settings down one tick solved the problems in all cases.*/void Q3DragObject::setPixmap(QPixmap pm, const QPoint& hotspot){ Q_D(Q3DragObject); d->pixmap = pm; d->hot = hotspot;#if 0 QDragManager *manager = QDragManager::self(); if (manager && manager->object == d->data) manager->updatePixmap();#endif}/*! \overload Uses a hotspot that positions the pixmap below and to the right of the mouse pointer. This allows the user to clearly see the point on the window where they are dragging the data.*/void Q3DragObject::setPixmap(QPixmap pm){ setPixmap(pm,QPoint(-10, -10));}/*! Returns the currently set pixmap, or a null pixmap if none is set. \sa QPixmap::isNull()*/QPixmap Q3DragObject::pixmap() const{ return d_func()->pixmap;}/*! Returns the currently set pixmap hotspot. \sa setPixmap()*/QPoint Q3DragObject::pixmapHotSpot() const{ return d_func()->hot;}/*! Starts a drag operation using the contents of this object, using DragDefault mode. The function returns true if the caller should delete the original copy of the dragged data (but see target()); otherwise returns false. If the drag contains \e references to information (e.g. file names in a Q3UriDrag are references) then the return value should always be ignored, as the target is expected to directly manipulate the content referred to by the drag object. On X11 the return value should always be correct anyway, but on Windows this is not necessarily the case; e.g. the file manager starts a background process to move files, so the source \e{must not} delete the files! Note that on Windows the drag operation will start a blocking modal event loop that will not dispatch any QTimers.*/bool Q3DragObject::drag(){ return drag(DragDefault);}/*! After the drag completes, this function will return the QWidget which received the drop, or 0 if the data was dropped on another application. This can be useful for detecting the case where drag and drop is to and from the same widget.*/QWidget *Q3DragObject::target(){ return last_target;}/*! Starts a drag operation using the contents of this object, using \c DragMove mode. Be sure to read the constraints described in drag(). Returns true if the data was dragged as a \e move, indicating that the caller should remove the original source of the data (the drag object must continue to have a copy); otherwise returns false. \sa drag() dragCopy() dragLink()*/bool Q3DragObject::dragMove(){ return drag(DragMove);}/*! Starts a drag operation using the contents of this object, using \c DragCopy mode. Be sure to read the constraints described in drag(). \sa drag() dragMove() dragLink()*/void Q3DragObject::dragCopy(){ (void)drag(DragCopy);}/*! Starts a drag operation using the contents of this object, using \c DragLink mode. Be sure to read the constraints described in drag(). \sa drag() dragCopy() dragMove()*/void Q3DragObject::dragLink(){ (void)drag(DragLink);}/*! \enum Q3DragObject::DragMode This enum describes the possible drag modes. \value DragDefault The mode is determined heuristically. \value DragCopy The data is copied. \value DragMove The data is moved. \value DragLink The data is linked. \value DragCopyOrMove The user chooses the mode by using the \key{Shift} key to switch from the default copy mode to move mode.*//*! \overload Starts a drag operation using the contents of this object. At this point, the object becomes owned by Qt, not the application. You should not delete the drag object or anything it references. The actual transfer of data to the target application will be done during future event processing - after that time the drag object will be deleted. Returns true if the dragged data was dragged as a \e move, indicating that the caller should remove the original source of the data (the drag object must continue to have a copy); otherwise returns false. The \a mode specifies the drag mode (see \l{Q3DragObject::DragMode}.) Normally one of the simpler drag(), dragMove(), or dragCopy() functions would be used instead.*/bool Q3DragObject::drag(DragMode mode){ Q_D(Q3DragObject); QDragMime *data = new QDragMime(this); int i = 0; const char *fmt; while ((fmt = format(i))) { data->setData(QLatin1String(fmt), encodedData(fmt)); ++i; } QDrag *drag = new QDrag(qobject_cast<QWidget *>(parent())); drag->setMimeData(data); drag->setPixmap(d->pixmap); drag->setHotSpot(d->hot); Qt::DropActions op; switch(mode) { case DragDefault: case DragCopyOrMove: op = Qt::CopyAction|Qt::MoveAction; break; case DragCopy: op = Qt::CopyAction; break; case DragMove: op = Qt::MoveAction; break; case DragLink: op = Qt::LinkAction; break; } bool retval = (drag->start(op) == Qt::MoveAction); last_target = drag->target(); return retval;}#endif/*! Returns a pointer to the widget where this object originated (the drag source).*/QWidget * Q3DragObject::source(){ if (parent() && parent()->isWidgetType()) return (QWidget *)parent(); else return 0;}/*! \class Q3DragObject qdragobject.h \brief The Q3DragObject class encapsulates MIME-based data transfer. \compat Q3DragObject is the base class for all data that needs to be transferred between and within applications, both for drag and drop and for the clipboard. See the \link dnd.html Drag and drop documentation\endlink for an overview of how to provide drag and drop in your application. See the QClipboard documentation for an overview of how to provide cut and paste in your application. The drag() function is used to start a drag operation. You can specify the \l DragMode in the call or use one of the convenience functions dragCopy(), dragMove(), or dragLink(). The drag source where the data originated is retrieved with source(). If the data was dropped on a widget within the application, target() will return a pointer to that widget. Specify the pixmap to display during the drag with setPixmap().*/staticvoid stripws(QByteArray& s){ int f; while ((f = s.indexOf(' ')) >= 0) s.remove(f,1);}/*! \class Q3TextDrag qdragobject.h \brief The Q3TextDrag class is a drag and drop object for transferring plain and Unicode text. \compat Plain text is passed in a QString which may contain multiple lines (i.e. may contain newline characters). The drag target will receive the newlines according to the runtime environment, e.g. LF on Unix, and CRLF on Windows. Qt provides no built-in mechanism for delivering only a single-line. For more information about drag and drop, see the Q3DragObject class and the \link dnd.html drag and drop documentation\endlink.*//*! Constructs a text drag object with the given \a name, and sets its data to \a text. The \a dragSource is the widget that the drag operation started from.*/Q3TextDrag::Q3TextDrag(const QString &text, QWidget * dragSource, const char * name) : Q3DragObject(*new Q3TextDragPrivate, dragSource){ setObjectName(QLatin1String(name)); setText(text);}/*! Constructs a default text drag object with the given \a name. The \a dragSource is the widget that the drag operation started from.*/Q3TextDrag::Q3TextDrag(QWidget * dragSource, const char * name) : Q3DragObject(*(new Q3TextDragPrivate), dragSource){ setObjectName(QLatin1String(name));}/*! \internal */Q3TextDrag::Q3TextDrag(Q3TextDragPrivate &dd, QWidget *dragSource) : Q3DragObject(dd, dragSource){}/*! Destroys the text drag object.*/Q3TextDrag::~Q3TextDrag(){}/*! \fn void Q3TextDrag::setSubtype(const QString &subtype) Sets the MIME \a subtype of the text being dragged. The default subtype is "plain", so the default MIME type of the text is "text/plain". You might use this to declare that the text is "text/html" by calling setSubtype("html").*/void Q3TextDrag::setSubtype(const QString & st){ d_func()->setSubType(st);}/*! Sets the \a text to be dragged. You will need to call this if you did not pass the text during construction.*/void Q3TextDrag::setText(const QString &text){ d_func()->txt = text;}/*! \reimp*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -