-
Notifications
You must be signed in to change notification settings - Fork 4
/
dragwidget.cpp
221 lines (197 loc) · 6.27 KB
/
dragwidget.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "dragwidget.h"
#include "domelement.h"
#include "htmlheading.h"
#include "htmlparagraph.h"
#include "htmlunderline.h"
#include "htmlstrikethrough.h"
#include "htmlitalic.h"
#include "htmlbold.h"
#include "htmlimage.h"
#include "htmlanchor.h"
#include "htmlsubscript.h"
#include "htmlsuperscript.h"
#include <QtWidgets>
/**
* @brief Constructs a DragWidget object.
* @param parent the parent widget of this DragWidget.
*/
DragWidget::DragWidget(QWidget *parent)
: QFrame(parent)
{
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
dialogService = new DialogService();
}
/**
* @brief This function overrides {#link QWidget#paintEvent}.
* This is done to ensure that custom stylesheets can be set for this element.
* If this function is not overridden, stylesheets would not work!
*/
void DragWidget::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
/**
* @brief Handler for the QDragEnterEvent.
* @param event the QDragEnterEvent to handle.
*/
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
/**
* @brief Handler for the QDragMoveEvent.
* @param event the QDragMoveEvent to handle.
*/
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
/**
* @brief Handler for the QDropEvent.
* @param event the QDropEvent to handle.
*/
void DragWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
QPixmap pixmap;
QPoint offset;
int type;
dataStream >> pixmap >> offset >> type;
// Create a new DomElement at the spot,
// where the drop happened.
DomElement *newElement = createDomElement(type);
if (!tmp.isEmpty()) {
newElement->setAttributes(tmp);
tmp.clear();
}
newElement->setDoubleClickEnabled(true);
newElement->setPixmap(pixmap);
newElement->move(event->pos() - offset);
newElement->show();
newElement->setAttribute(Qt::WA_DeleteOnClose);
if (event->source() == this) {
event->setDropAction(Qt::MoveAction);
event->accept();
} else {
event->acceptProposedAction();
}
} else {
event->ignore();
}
}
/**
* @brief Creates a new {@link DomElement} object based on the given type.
* @param type
* @return A new {@link DomElement} object of the correct type.
*/
DomElement *DragWidget::createDomElement(int type)
{
DomElement *newElement;
switch(type)
{
case DomElement::HEADING_TYPE: newElement = new HtmlHeading(this); break;
case DomElement::PARAGRAPH_TYPE: newElement = new HtmlParagraph(this); break;
case DomElement::UNDERLINE_TYPE: newElement = new HtmlUnderline(this); break;
case DomElement::STRIKETHROUGH_TYPE: newElement = new HtmlStrikethrough(this); break;
case DomElement::ITALIC_TYPE: newElement = new HtmlItalic(this); break;
case DomElement::BOLD_TYPE: newElement = new HtmlBold(this); break;
case DomElement::IMAGE_TYPE: newElement = new HtmlImage(this); break;
case DomElement::ANCHOR_TYPE:newElement = new HtmlAnchor(this); break;
case DomElement::SUBSCRIPT_TYPE:newElement = new HtmlSubscript(this); break;
case DomElement::SUPERSCRIPT_TYPE:newElement = new HtmlSuperscript(this); break;
}
return newElement;
}
/**
* @brief Handler for the mouse press event.
* @param event the QMouseEvent to handle.
*/
void DragWidget::mousePressEvent(QMouseEvent *event)
{
DomElement *child = static_cast<DomElement*>(childAt(event->pos()));
if (!child)
return;
if(event->button() == Qt::RightButton && child->getDoubleClickEnabled())
{
deleteElement(child);
return;
}
tmp = child->getAttributes();
QPixmap pixmap = *child->pixmap();
QByteArray itemData;
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
dataStream << pixmap << QPoint(event->pos() - child->pos()) << child->getType();
QMimeData *mimeData = new QMimeData;
// Set a custom mime type, so that we can differentiate
// drag & drop events from others.
mimeData->setData("application/x-dnditemdata", itemData);
// Start the actual drag.
QDrag *drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(event->pos() - child->pos());
QPixmap tempPixmap = pixmap;
QPainter painter;
painter.begin(&tempPixmap);
painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
painter.end();
child->setPixmap(tempPixmap);
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
child->close();
} else {
child->show();
child->setPixmap(pixmap);
}
}
/**
* @brief Handler for the mouse double click event.
*
* NOTE: Keep in mind that the {@link mousePressEvent} handler
* is always executed first on double click.
*
* @param event the QMouseEvent to handle.
*/
void DragWidget::mouseDoubleClickEvent(QMouseEvent * event)
{
DomElement *child = static_cast<DomElement*>(childAt(event->pos()));
if (!child)
return;
if (child->getDoubleClickEnabled())
{
QMap<QString, QString> result = dialogService->showDialog(child->getAttributes());
if (!result.isEmpty()) {
child->setAttributes(result);
}
}
}
/**
* @brief Deletes the given {@link DomElement}
* @param element The element to delete
*/
void DragWidget::deleteElement(DomElement *element)
{
delete element;
}