QCocoaDrag: Fix text, url and image preview pixmap while dragging.

OSX was missing text, url and image preview while dragging causing the
drag operation to show a small dashed box around the mouse cursor.

Task-number: QTBUG-33824
Change-Id: I8d0acd0d6a48b7cb29ad2faba8b9ecb9cdf2a5ab
Reviewed-by: Morten Johan Sørvig <morten.sorvig@theqtcompany.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
This commit is contained in:
Filipe Azevedo 2015-03-16 10:52:05 +01:00 committed by Shawn Rutledge
parent 699e7a0869
commit fcabeb2e47
2 changed files with 52 additions and 6 deletions

View File

@ -67,6 +67,8 @@ private:
NSEvent *m_lastEvent;
NSView *m_lastView;
Qt::DropAction m_executed_drop_action;
QPixmap dragPixmap(QDrag *drag, QPoint &hotSpot) const;
};
class QCocoaDropData : public QInternalMimeData

View File

@ -37,6 +37,8 @@
QT_BEGIN_NAMESPACE
static const int dragImageMaxChars = 26;
QCocoaDrag::QCocoaDrag() :
m_drag(0)
{
@ -116,10 +118,8 @@ Qt::DropAction QCocoaDrag::drag(QDrag *o)
m_drag = o;
m_executed_drop_action = Qt::IgnoreAction;
QPixmap pm = m_drag->pixmap();
if (pm.isNull())
pm = defaultPixmap();
QPoint hotSpot = m_drag->hotSpot();
QPixmap pm = dragPixmap(m_drag, hotSpot);
NSImage *nsimage = qt_mac_create_nsimage(pm);
QMacPasteboard dragBoard((CFStringRef) NSDragPboard, QMacInternalPasteboardMime::MIME_DND);
@ -128,8 +128,8 @@ Qt::DropAction QCocoaDrag::drag(QDrag *o)
NSPoint event_location = [m_lastEvent locationInWindow];
NSPoint local_point = [m_lastView convertPoint:event_location fromView:nil];
local_point.x -= m_drag->hotSpot().x();
CGFloat flippedY = m_drag->pixmap().height() - m_drag->hotSpot().y();
local_point.x -= hotSpot.x();
CGFloat flippedY = pm.height() - hotSpot.y();
local_point.y += flippedY;
NSSize mouseOffset = NSMakeSize(0.0, 0.0);
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
@ -153,6 +153,50 @@ void QCocoaDrag::setAcceptedAction(Qt::DropAction act)
m_executed_drop_action = act;
}
QPixmap QCocoaDrag::dragPixmap(QDrag *drag, QPoint &hotSpot) const
{
const QMimeData* data = drag->mimeData();
QPixmap pm = drag->pixmap();
if (pm.isNull()) {
QFont f(qApp->font());
f.setPointSize(12);
QFontMetrics fm(f);
if (data->hasImage()) {
const QImage img = data->imageData().value<QImage>();
if (!img.isNull()) {
pm = QPixmap::fromImage(img).scaledToWidth(dragImageMaxChars *fm.averageCharWidth());
}
}
if (pm.isNull() && (data->hasText() || data->hasUrls()) ) {
QString s = data->hasText() ? data->text() : data->urls().first().toString();
if (s.length() > dragImageMaxChars)
s = s.left(dragImageMaxChars -3) + QChar(0x2026);
if (!s.isEmpty()) {
const int width = fm.width(s);
const int height = fm.height();
if (width > 0 && height > 0) {
pm = QPixmap(width, height);
QPainter p(&pm);
p.fillRect(0, 0, pm.width(), pm.height(), Qt::color0);
p.setPen(Qt::color1);
p.setFont(f);
p.drawText(0, fm.ascent(), s);
p.end();
hotSpot = QPoint(pm.width() / 2, pm.height() / 2);
}
}
}
}
if (pm.isNull())
pm = defaultPixmap();
return pm;
}
QCocoaDropData::QCocoaDropData(NSPasteboard *pasteboard)
{
dropPasteboard = reinterpret_cast<CFStringRef>(const_cast<const NSString *>([pasteboard name]));