-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQPushButtonLoop.cpp
147 lines (125 loc) · 4.21 KB
/
QPushButtonLoop.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "QPushButtonLoop.h"
#include <QDebug>
#include <QMimeData>
#include <QStringList>
#include <QFileInfo>
#include <QDragEnterEvent>
#include <QtWidgets>
#include <QDrag>
QPushButtonLoop::QPushButtonLoop(QWidget *parent) : QPushButton(parent)
{
setAcceptDrops(true);
this->setContextMenuPolicy(Qt::CustomContextMenu);
this->connect(this,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(ShowContextMenu(const QPoint &)));
}
void QPushButtonLoop::ShowContextMenu(const QPoint &pos)
{
QMenu contextMenu(tr("Context menu"), this);
QAction action1(tr("Ajouter Boucle"), this);
contextMenu.addAction(&action1);
connect(&action1, SIGNAL(triggered()), this, SLOT(addShowContextMenu()));
QAction action2(tr("Insérer Boucle"), this);
connect(&action2, SIGNAL(triggered()), this, SLOT(insertShowContextMenu()));
contextMenu.addAction(&action2);
QAction action3(tr("Supprimer Boucle"), this);
connect(&action3, SIGNAL(triggered()), this, SLOT(deleteShowContextMenu()));
contextMenu.addAction(&action3);
contextMenu.exec(mapToGlobal(pos));
}
void QPushButtonLoop::addShowContextMenu()
{
emit contextMenuAction( loopContextMenu::ajouter,this->property("myId").toInt() );
}
void QPushButtonLoop::insertShowContextMenu()
{
emit contextMenuAction( loopContextMenu::inserer,this->property("myId").toInt() );
}
void QPushButtonLoop::deleteShowContextMenu()
{
emit contextMenuAction( loopContextMenu::supprimer,this->property("myId").toInt() );
}
void QPushButtonLoop::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::RightButton ){
ShowContextMenu(event->pos());
return;
}
if (event->button() == Qt::LeftButton )
{
QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;
mimeData->setData("application/QPushbutton","");
drag->setMimeData(mimeData);
QPixmap image = QPixmap(":/icones/Loop-icon");
drag->setPixmap(image.scaled(this->width(),this->height(),Qt::KeepAspectRatio));
drag->exec();
}
this->setChecked(true);
emit clicked();
}
void QPushButtonLoop::dragEnterEvent(QDragEnterEvent *event){
if (event->mimeData()->hasFormat("application/QPushbutton")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void QPushButtonLoop::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat("application/QPushbutton")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
void QPushButtonLoop::dropEvent(QDropEvent *event){
QVariant origMyId = event->source()->property("myId");
QVariant destMyId = this->property("myId");
if (event->mimeData()->hasFormat("application/QPushbutton")) {
if (origMyId.isValid()) {
if( origMyId.toInt() != destMyId.toInt() && destMyId.toInt() != 0){
emit changePosButtonLoop(origMyId.toInt(), destMyId.toInt() ) ;
}
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
}
else {
event->acceptProposedAction();
}
}
else {
event->ignore();
}
}
}
void QPushButtonLoop::mouseDoubleClickEvent(QMouseEvent* event){
if (event->button() == Qt::LeftButton ){
qle = new QLineEdit(this->text(), this);
qle->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
qle->setAlignment(Qt::AlignHCenter);
hbl = new QVBoxLayout();
hbl->setContentsMargins(8,4,8,4);
this->setLayout(hbl);
hbl->addWidget(qle);
qle->setFocus();
qle->connect(qle,SIGNAL(editingFinished()),this, SLOT(editedNameLoop()));
}
}
void QPushButtonLoop::editedNameLoop( )
{
this->setText(qle->text());
emit editedNameLoop(this->property(("myId")).toInt(),this->text());
qle->deleteLater();
hbl->deleteLater();
}