BeaconWidgetDrag
::BeaconWidgetDrag(QWidget *parent
){
BeaconWidget * test;
int x = 5;
int y = 5;
test = new BeaconWidget("x01", BeaconStateDown, this);
test->move(x, y);
test->show();
test->setAttribute(Qt::WA_DeleteOnClose);
//setMinimumSize(400, qMax(200, y));
setAcceptDrops(true);
}
BeaconWidgetDrag::~BeaconWidgetDrag()
{
}
{
if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
if (children().contains(event->source())) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else if (event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
{
if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
if (children().contains(event->source())) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else if (event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
void BeaconWidgetDrag
::dropEvent(QDropEvent *event
) {
if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
QByteArray itemData
= mime
->data
("application/x-beaconwidget");
dataStream >> text >> offset;
BeaconWidget * newBeacon = new BeaconWidget(text, BeaconStateDown, this);
newBeacon->move(event->pos() - offset);
newBeacon->show();
newBeacon->setAttribute(Qt::WA_DeleteOnClose);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else if (event->mimeData()->hasText()) {
QPoint position
= event
->pos
();
BeaconWidget * newBeacon = new BeaconWidget(piece, BeaconStateDown, this);
newBeacon->move(position);
newBeacon->show();
newBeacon->setAttribute(Qt::WA_DeleteOnClose);
position
+= QPoint(newBeacon
->width
(),
0);
}
event->acceptProposedAction();
} else {
event->ignore();
}
}
void BeaconWidgetDrag
::mousePressEvent(QMouseEvent * event
) {
BeaconWidget *child = static_cast<BeaconWidget *>(childAt(event->pos()));
if (!child)
return;
QPoint hotSpot
= event
->pos
() - child
->pos
();
dataStream << child
->labelText
() <<
QPoint(hotSpot
);
mimeData->setData("application/x-beaconwidget", itemData);
mimeData->setText(child->labelText());
drag->setMimeData(mimeData);
drag->setPixmap(*child->pixmap());
drag->setHotSpot(hotSpot);
child->hide();
if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
child->close();
else
child->show();
}
void BeaconWidgetDrag
::Log(QString s
) {
emit LogChanged(s);
}
BeaconWidgetDrag::BeaconWidgetDrag(QWidget *parent)
: QWidget(parent)
{
BeaconWidget * test;
int x = 5;
int y = 5;
test = new BeaconWidget("x01", BeaconStateDown, this);
test->move(x, y);
test->show();
test->setAttribute(Qt::WA_DeleteOnClose);
//setMinimumSize(400, qMax(200, y));
setAcceptDrops(true);
}
BeaconWidgetDrag::~BeaconWidgetDrag()
{
}
void BeaconWidgetDrag::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
if (children().contains(event->source())) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else if (event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
void BeaconWidgetDrag::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
if (children().contains(event->source())) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else if (event->mimeData()->hasText()) {
event->acceptProposedAction();
} else {
event->ignore();
}
}
void BeaconWidgetDrag::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("application/x-beaconwidget")) {
const QMimeData *mime = event->mimeData();
QByteArray itemData = mime->data("application/x-beaconwidget");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
QString text;
QPoint offset;
dataStream >> text >> offset;
BeaconWidget * newBeacon = new BeaconWidget(text, BeaconStateDown, this);
newBeacon->move(event->pos() - offset);
newBeacon->show();
newBeacon->setAttribute(Qt::WA_DeleteOnClose);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else if (event->mimeData()->hasText()) {
QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
QString::SkipEmptyParts);
QPoint position = event->pos();
foreach (QString piece, pieces) {
BeaconWidget * newBeacon = new BeaconWidget(piece, BeaconStateDown, this);
newBeacon->move(position);
newBeacon->show();
newBeacon->setAttribute(Qt::WA_DeleteOnClose);
position += QPoint(newBeacon->width(), 0);
}
event->acceptProposedAction();
} else {
event->ignore();
}
}
void BeaconWidgetDrag::mousePressEvent(QMouseEvent * event)
{
BeaconWidget *child = static_cast<BeaconWidget *>(childAt(event->pos()));
if (!child)
return;
QPoint hotSpot = event->pos() - child->pos();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << child->labelText() << QPoint(hotSpot);
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/x-beaconwidget", itemData);
mimeData->setText(child->labelText());
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(*child->pixmap());
drag->setHotSpot(hotSpot);
child->hide();
if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
child->close();
else
child->show();
}
void BeaconWidgetDrag::Log(QString s)
{
emit LogChanged(s);
}
To copy to clipboard, switch view to plain text mode
void BeaconMonitor::on_tabWidget_tabBarDoubleClicked(int index)
{
BeaconWidgetDrag * beaconWidgetDrag = new BeaconWidgetDrag(ui->frame);
beaconWidgetDrag->show();
}
void BeaconMonitor::on_tabWidget_tabBarDoubleClicked(int index)
{
BeaconWidgetDrag * beaconWidgetDrag = new BeaconWidgetDrag(ui->frame);
beaconWidgetDrag->show();
}
To copy to clipboard, switch view to plain text mode
Bookmarks