/
draglabel.cpp
107 lines (91 loc) · 3.49 KB
/
draglabel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "draglabel.h"
#include <QtGui>
void DragLabel::setTamanho(int t){
this->tamanho = t;
}
//DragLabel::DragLabel(const QString &text, QWidget *parent,int r, int g, int b, int alpha)
//DragLabel::DragLabel(Task trab, const QString &text, QWidget *parent, QColor color, int tamanho)
DragLabel::DragLabel(const QString &text,QWidget *parent,QString tooltip, QColor color, float tamanho, bool tempo_setup)
: QLabel(parent){
this->coordenada = QPoint(0,0);
this->tempo_setup = tempo_setup;
this->tamanho = tamanho;
this->tooltip = tooltip;
//this->trabalho = trab;
this->cor = color;
QFontMetrics metric(font());
QSize size = metric.size(Qt::TextSingleLine, text);
//QImage image(size.width() + 12, size.height() + 12,
// QImage::Format_ARGB32_Premultiplied);
int height;
if(!tempo_setup){
height = size.height()+12; //se trabalho for tempo_setup da maquina, desenhar label menor
}else{
height = 6;
}
//QImage image(tamanho, size.height()+12, QImage::Format_ARGB32_Premultiplied);
QImage image(tamanho, height, QImage::Format_ARGB32_Premultiplied);
image.fill(qRgba(0,0,0,0));
QFont font;
font.setStyleStrategy(QFont::ForceOutline);
QPainter painter;
painter.begin(&image);
painter.setRenderHint(QPainter::Antialiasing);
//Cor do background do label
painter.setBrush(cor);
//Especificação do desenho do label
painter.drawRect(0,0,image.width(),image.height());
painter.setFont(font);
//Cor do texto
painter.setBrush(Qt::green);
//Define texto do label
painter.drawText(QRect(QPoint(6,6),size), Qt::AlignCenter,text);
painter.end();
setPixmap(QPixmap::fromImage(image));
labelText = text;
}
void DragLabel::mousePressEvent(QMouseEvent *event)
{
// if(this->tempo_setup){ //desabilita ação de drag para labels que representarem tempo_setup de maquina
// //return;
// }
// qDebug() << "mousePressEvent called";
dragstart = event->pos();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
//passar variavel tamanho como float zoa o drop
dataStream << labelText << tooltip << QPoint(event->pos() - rect().topLeft()) << (int)tamanho << cor.red() << cor.green() << cor.blue() << cor.alpha() << tempo_setup << coordenada;
QMimeData *mimeData = new QMimeData;
//pra que serve mimeData?
mimeData->setData("application/x-fridgemagnet", itemData);
mimeData->setText(labelText);
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setHotSpot(event->pos() - rect().topLeft());
drag->setPixmap(*pixmap());
hide();
//como funciona drag->exec() ?
if(drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
close();
else
show();
}
void DragLabel::mouseMoveEvent(QMouseEvent *event){
// qsrand(QTime::currentTime().msec());
// int rand = qrand()%40;
qDebug() << "mouseMoveEvent ";// << rand;
//setGeometry(QRect(QPoint(event->pos().x(),dragstart.y()),rect().size()));
}
void DragLabel::mouseDoubleClickEvent(QMouseEvent *event){
//qDebug() << "double click";
}
void DragLabel::mouseReleaseEvent(QMouseEvent *event){
//qDebug() << "mouse release";
}
QString DragLabel::generateString(int num){
QString str;
for(int x=0; x<num; x++){
str.append(" ");
}
return str;
}