DnDPixmap
::DnDPixmap(const QPixmap &pixmap, uitzicht
* a
){
// Make sure we allow drops.
setAcceptDrops(true);
connect(this, SIGNAL(moveMade( int, int, int, int)), a, SLOT(myslot( int, int, int, int)));
}
{
// We need to overload this method to make sure that
// nothing happens on a mouse press (e.g. select, move).
//
// This causes e.g. setFlag(QGraphicsItem::ItemIsMovable)
// to stop working, since flags modifies the mousePressEvent
// method of the base class, which is not called anymore now.
if (event->button() != Qt::LeftButton) {
event->ignore();
return;
}
}
{
// Check if move distance is above standard treshold.
if (QLineF(event
->screenPos
(), event
->buttonDownScreenPos
(Qt
::LeftButton)) {
return;
}
this->oudeX = event->lastScenePos().rx()/50;
this->oudeY = event->lastScenePos().ry()/50;
// Create a drag object with mimetype data.
drag->setMimeData(mime);
// Add text to the QMimeData object.
// QVariant is like a union, it can
// contain different kinds of types.
// Show a scaled icon of the current image next to
// the cursor when dragging. Scale it to be 30px wide.
drag->setPixmap(pixmap().scaledToWidth(30, Qt::FastTransformation));
// Set the cursor's tip (or hot spot) to point to
// the center of the pixmap's bottom edge.
drag
->setHotSpot
(QPoint(drag
->pixmap
().
width()/2, drag
->pixmap
().
height()));
label->setText("pointerINMOUSEMOVEEVENT="+p);
label->show();
// Start the drag operation.
drag->exec();
}
void DnDPixmap::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
// Is the data correct (an image that can be loaded)?
bool imageLoaded = false;
// Are there URLs present?
if (mime->hasUrls())
{
// Fetch the list of URLs dragged over.
const QList<QUrl> &urls = mime->urls();
if (!urls.empty())
{
// Try to load the first URL as an image.
// Modify imageLoaded according to
// the success of this operation.
imageLoaded = pix.load(urls[0].toLocalFile());
}
}
// Is there an image present?
else if (mime->hasImage())
{
// Convert the data to an QImage.
// The construct qvariant_cast is used to convert
// a stored value to another type.
QImage img
= qvariant_cast<QImage>
(mime
->imageData
());
// Load the QImage in a QPixmap.
// Check if the pixmap is not blank.
imageLoaded = !pix.isNull();
}
label->setText("pointerINDRAGENTEREVENT="+p);
label->show();
// Only allow drops if the image loaded succesfully.
event->setAccepted(imageLoaded);
}
void DnDPixmap::dropEvent(QGraphicsSceneDragDropEvent *event)
{
label->setText("pointerINDROPEVENT="+p);
label->show();
emit moveMade(/*x location where i drop my pixmap, havent found a simple way to get this*/,/*y location, description to the left, oudeX, oudeY);
// Set the currently displayed pixmap to the dropped pixmap.
//setPixmap(pix);
}
DnDPixmap::DnDPixmap(const QPixmap &pixmap, uitzicht* a)
: QGraphicsPixmapItem(pixmap)
{
// Make sure we allow drops.
setAcceptDrops(true);
connect(this, SIGNAL(moveMade( int, int, int, int)), a, SLOT(myslot( int, int, int, int)));
}
void DnDPixmap::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
// We need to overload this method to make sure that
// nothing happens on a mouse press (e.g. select, move).
//
// This causes e.g. setFlag(QGraphicsItem::ItemIsMovable)
// to stop working, since flags modifies the mousePressEvent
// method of the base class, which is not called anymore now.
if (event->button() != Qt::LeftButton) {
event->ignore();
return;
}
}
void DnDPixmap::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
// Check if move distance is above standard treshold.
if (QLineF(event->screenPos(), event->buttonDownScreenPos(Qt::LeftButton))
.length() < QApplication::startDragDistance())
{
return;
}
this->oudeX = event->lastScenePos().rx()/50;
this->oudeY = event->lastScenePos().ry()/50;
// Create a drag object with mimetype data.
QDrag *drag = new QDrag(event->widget());
QMimeData *mime = new QMimeData;
drag->setMimeData(mime);
// Add text to the QMimeData object.
// QVariant is like a union, it can
// contain different kinds of types.
mime->setImageData(QVariant(pixmap()));
// Show a scaled icon of the current image next to
// the cursor when dragging. Scale it to be 30px wide.
drag->setPixmap(pixmap().scaledToWidth(30, Qt::FastTransformation));
// Set the cursor's tip (or hot spot) to point to
// the center of the pixmap's bottom edge.
drag->setHotSpot(QPoint(drag->pixmap().width()/2, drag->pixmap().height()));
QLabel* label = new QLabel;
QString p=QString::number((int)this,16);
label->setText("pointerINMOUSEMOVEEVENT="+p);
label->show();
// Start the drag operation.
drag->exec();
}
void DnDPixmap::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
// Is the data correct (an image that can be loaded)?
bool imageLoaded = false;
const QMimeData *mime = event->mimeData();
// Are there URLs present?
if (mime->hasUrls())
{
// Fetch the list of URLs dragged over.
const QList<QUrl> &urls = mime->urls();
if (!urls.empty())
{
// Try to load the first URL as an image.
// Modify imageLoaded according to
// the success of this operation.
imageLoaded = pix.load(urls[0].toLocalFile());
}
}
// Is there an image present?
else if (mime->hasImage())
{
// Convert the data to an QImage.
// The construct qvariant_cast is used to convert
// a stored value to another type.
QImage img = qvariant_cast<QImage>(mime->imageData());
// Load the QImage in a QPixmap.
pix = QPixmap::fromImage(img);
// Check if the pixmap is not blank.
imageLoaded = !pix.isNull();
}
QLabel* label = new QLabel;
QString p=QString::number((int)this,16);
label->setText("pointerINDRAGENTEREVENT="+p);
label->show();
// Only allow drops if the image loaded succesfully.
event->setAccepted(imageLoaded);
}
void DnDPixmap::dropEvent(QGraphicsSceneDragDropEvent *event)
{
QLabel* label = new QLabel;
QString p=QString::number((int)this,16);
label->setText("pointerINDROPEVENT="+p);
label->show();
emit moveMade(/*x location where i drop my pixmap, havent found a simple way to get this*/,/*y location, description to the left, oudeX, oudeY);
// Set the currently displayed pixmap to the dropped pixmap.
//setPixmap(pix);
}
To copy to clipboard, switch view to plain text mode
Bookmarks