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
222
223
224
225
226
227
228
229 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2023-05-15
* Description : geolocation engine based on Marble.
* (c) 2007-2022 Marble Team
* https://invent.kde.org/education/marble/-/raw/master/data/credits_authors.html
*
* SPDX-FileCopyrightText: 2023-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* ============================================================ */
#include "ScreenGraphicsItem.h"
#include "ScreenGraphicsItem_p.h"
// Qt includes
#include <QMouseEvent>
// Local includes
#include "MarbleWidget.h"
#include "digikam_debug.h"
namespace Marble
{
ScreenGraphicsItem::ScreenGraphicsItem(MarbleGraphicsItem* parent)
: MarbleGraphicsItem(new ScreenGraphicsItemPrivate(this, parent))
{
}
ScreenGraphicsItem::ScreenGraphicsItem(ScreenGraphicsItemPrivate* dd)
: MarbleGraphicsItem(dd)
{
}
ScreenGraphicsItem::~ScreenGraphicsItem()
{
}
QPointF ScreenGraphicsItem::position() const
{
Q_D(const ScreenGraphicsItem);
return d->m_position;
}
void ScreenGraphicsItem::setPosition(const QPointF& position)<--- Shadow argument
{
Q_D(ScreenGraphicsItem);
d->m_position = position;
}
QPointF ScreenGraphicsItem::positivePosition() const
{
Q_D(const ScreenGraphicsItem);
return d->positivePosition();
}
QVector<QPointF> ScreenGraphicsItem::absolutePositions() const
{
Q_D(const ScreenGraphicsItem);
return d->absolutePositions();
}
ScreenGraphicsItem::GraphicsItemFlags ScreenGraphicsItem::flags() const
{
Q_D(const ScreenGraphicsItem);
return d->m_flags;
}
void ScreenGraphicsItem::setFlags(ScreenGraphicsItem::GraphicsItemFlags flags)<--- Shadow argument
{
Q_D(ScreenGraphicsItem);
d->m_flags = flags;
}
bool ScreenGraphicsItem::eventFilter(QObject* object, QEvent* e)
{
MarbleWidget* widget = dynamic_cast<MarbleWidget*>(object);
if (!widget)
{
return MarbleGraphicsItem::eventFilter(object, e);
}
Q_D(ScreenGraphicsItem);
if (!d->m_floatItemMoving)
{
if (MarbleGraphicsItem::eventFilter(object, e))
{
return true;
}
if (!visible() || !d->isMovable())
{
return false;
}
if (e->type() == QEvent::MouseMove)
{
return false;
}
// Move ScreenGraphicsItem
if (e->type() == QEvent::MouseButtonPress)
{
QMouseEvent* event = static_cast<QMouseEvent*>(e);
// Click and move above a float item triggers moving the float item
if (contains(event->pos()))
{
if (event->button() == Qt::LeftButton)
{
d->m_floatItemMoveStartPos = event->pos();
d->m_floatItemMoving = true;
return true;
}
}
}
return false;
}
else
{
// Move ScreenGraphicsItem
if (
(e->type() == QEvent::MouseMove)
|| (e->type() == QEvent::MouseButtonPress)
|| (e->type() == QEvent::MouseButtonRelease)
)
{
QMouseEvent* event = static_cast<QMouseEvent*>(e);
// The rect the item was painted on before. We add one pixel as antialiasing could
// result into painting on these pixels to.
QRectF floatItemRect = QRectF(positivePosition() - QPoint(1, 1),
size() + QSize(2, 2));
if ((e->type() == QEvent::MouseMove) && (event->buttons() & Qt::LeftButton))
{
const QPoint& point = event->pos();
QPointF position = positivePosition();<--- Shadow local variable
qreal newX = qMax<qreal>(0, position.x() + point.x() - d->m_floatItemMoveStartPos.x());
qreal newY = qMax<qreal>(0, position.y() + point.y() - d->m_floatItemMoveStartPos.y());
// docking behavior
const qreal dockArea = 60.0; // Alignment area width/height
const qreal dockJump = 30.0; // Alignment indicator jump size
if ((widget->width() - size().width() - newX) < dockArea)
{
newX = qMin(qreal(-1.0), size().width() + newX - widget->width());
if (d->m_floatItemMoveStartPos.x() < event->pos().x())
{
// Indicate change to right alignment with a short jump
newX = qMax(newX, -(dockArea - dockJump));
}
}
if ((widget->height() - size().height() - newY) < dockArea)
{
newY = qMin(qreal(-1.0), size().height() + newY - widget->height());
if (d->m_floatItemMoveStartPos.y() < event->pos().y())
{
// Indicate change to bottom alignment with a short jump
newY = qMax(newY, -(dockArea - dockJump));
}
}
setPosition(QPointF(newX, newY));
// The rect the item will be painted on now. We add one pixel as
// antialiasing could result into painting on these pixels to.
QRect newFloatItemRect = QRectF(positivePosition() - QPoint(1, 1),
size() + QSize(2, 2)).toRect();
d->m_floatItemMoveStartPos = event->pos();
QRegion dirtyRegion(floatItemRect.toRect());
dirtyRegion = dirtyRegion.united(newFloatItemRect);
widget->setAttribute(Qt::WA_NoSystemBackground, false);
widget->update(dirtyRegion);
widget->setAttribute(Qt::WA_NoSystemBackground, widget->viewport()->mapCoversViewport());
return true;
}
if (e->type() == QEvent::MouseButtonRelease)
{
d->m_floatItemMoving = false;
}
// Use a special cursor as long as the item is moved
if (d->m_floatItemMoving)
{
widget->setCursor(QCursor(Qt::SizeAllCursor));
return true;
}
}
return MarbleGraphicsItem::eventFilter(object, e);
}
}
} // namespace Marble
|