draglabel.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "draglabel.h"
  2. #include <QWidget>
  3. #include <QDropEvent>
  4. #include <QPainter>
  5. DragLabel::DragLabel(const QString &text, QTableWidgetItem *parent)
  6. {
  7. setFlags(Qt::ItemIsEnabled|Qt::ItemIsDragEnabled);
  8. QFontMetrics metric(font());
  9. QSize size = metric.size(Qt::TextSingleLine, text);
  10. //this->setAcceptDrops(true);
  11. QImage image(size.width() + 12, size.height() + 12, QImage::Format_ARGB32_Premultiplied);
  12. image.fill(qRgba(0, 0, 0, 0));
  13. QFont font;
  14. font.setStyleStrategy(QFont::ForceOutline);
  15. QLinearGradient gradient(0, 0, 0, image.height()-1);
  16. gradient.setColorAt(0.0, Qt::white);
  17. gradient.setColorAt(0.2, QColor(200, 200, 255));
  18. gradient.setColorAt(0.8, QColor(200, 200, 255));
  19. gradient.setColorAt(1.0, QColor(127, 127, 200));
  20. QPainter painter;
  21. painter.begin(&image);
  22. painter.setRenderHint(QPainter::Antialiasing);
  23. painter.setBrush(gradient);
  24. // painter.drawRoundedRect(QRectF(0.5, 0.5, image.width()-1, image.height()-1),
  25. //25, 25, Qt::RelativeSize);
  26. painter.setFont(font);
  27. painter.setBrush(Qt::black);
  28. painter.drawText(QRect(QPoint(6, 6), size), Qt::AlignCenter, text);
  29. painter.end();
  30. //setPixmap(QPixmap::fromImage(image));
  31. m_labelText = text;
  32. }
  33. void DragLabel::dragEnterEvent(QDragEnterEvent *event)
  34. {
  35. // event->ignore();
  36. // if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
  37. // if (children().contains(event->source())) {
  38. // event->setDropAction(Qt::MoveAction);
  39. // event->accept();
  40. // } else {
  41. // event->acceptProposedAction();
  42. // }
  43. // } else if (event->mimeData()->hasText()) {
  44. // event->acceptProposedAction();
  45. // } else {
  46. // event->ignore();
  47. // }
  48. }
  49. void DragLabel::dragMoveEvent(QDragMoveEvent *event)
  50. {
  51. // event->ignore();
  52. // if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
  53. // if (children().contains(event->source())) {
  54. // event->setDropAction(Qt::MoveAction);
  55. // event->accept();
  56. // } else {
  57. // event->acceptProposedAction();
  58. // }
  59. // } else if (event->mimeData()->hasText()) {
  60. // event->acceptProposedAction();
  61. // } else {
  62. // event->ignore();
  63. // }
  64. }
  65. void DragLabel::dropEvent(QDropEvent *event)
  66. {
  67. // event->ignore();
  68. // if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
  69. // const QMimeData *mime = event->mimeData();
  70. // QByteArray itemData = mime->data("application/x-fridgemagnet");
  71. // QDataStream dataStream(&itemData, QIODevice::ReadOnly);
  72. // QString text;
  73. // QPoint offset;
  74. // dataStream >> text >> offset;
  75. // DragLabel *newLabel = new DragLabel(text, this);
  76. // newLabel->move(event->pos() - offset);
  77. // newLabel->show();
  78. // //newLabel->setAttribute(Qt::WA_DeleteOnClose);
  79. // if (event->source() == this) {
  80. // event->setDropAction(Qt::MoveAction);
  81. // event->accept();
  82. // } else {
  83. // event->acceptProposedAction();
  84. // }
  85. // } else if (event->mimeData()->hasText()) {
  86. // QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
  87. // QString::SkipEmptyParts);
  88. // QPoint position = event->pos();
  89. // foreach (QString piece, pieces) {
  90. // DragLabel *newLabel = new DragLabel(piece, this);
  91. // newLabel->move(position);
  92. // newLabel->show();
  93. // // newLabel->setAttribute(Qt::WA_DeleteOnClose);
  94. // position += QPoint(newLabel->width(), 0);
  95. // }
  96. // event->acceptProposedAction();
  97. // } else {
  98. // event->ignore();
  99. // }
  100. }
  101. void DragLabel::mousePressEvent(QMouseEvent *event)
  102. {
  103. qDebug("dragLabel::mousePressEvent::DragLabelnochild");
  104. //event->ignore();
  105. // DragLabel *child = static_cast<DragLabel*>(childAt(event->pos()));
  106. //// if (!child)
  107. //// {qDebug()<<"ragLabel::mousePressEvent::DragLabelnochild";return;
  108. ////}else qDebug()<<child->text();
  109. // // qDebug()<<event->pos();
  110. // QPoint hotSpot = event->pos();// - child->pos();
  111. // QByteArray itemData;
  112. // QDataStream dataStream(&itemData, QIODevice::WriteOnly);
  113. // dataStream << this->labelText() << QPoint(hotSpot);
  114. // QMimeData *mimeData = new QMimeData;
  115. // mimeData->setData("application/x-fridgemagnet", itemData);
  116. // //mimeData->setText(child->labelText());
  117. //mimeData->setColorData(QColor(Qt::black));
  118. // QDrag *drag = new QDrag(this);
  119. // drag->setMimeData(mimeData);
  120. // // drag->setPixmap(*child->pixmap());
  121. // drag->setHotSpot(hotSpot);
  122. // // child->hide();
  123. // if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
  124. // {} //child->close();
  125. // else
  126. // {
  127. // }//child->show();
  128. }
  129. QString DragLabel::labelText() const
  130. {
  131. return m_labelText;
  132. }