Marble

AbstractFloatItem.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2008 Torsten Rahn <rahn@kde.org>
4//
5
6// Self
7#include "AbstractFloatItem.h"
8
9// Qt
10#include <QAction>
11#include <QContextMenuEvent>
12#include <QDialog>
13#include <QHelpEvent>
14#include <QMenu>
15#include <QPen>
16
17// Marble
18#include "DialogConfigurationInterface.h"
19#include "GeoPainter.h"
20#include "MarbleDebug.h"
21
22namespace Marble
23{
24
25class AbstractFloatItemPrivate
26{
27public:
28 AbstractFloatItemPrivate()
29 : m_contextMenu(nullptr)
30 {
31 }
32
33 ~AbstractFloatItemPrivate()
34 {
35 delete m_contextMenu;
36 }
37
38 static QPen s_pen;
39 static QFont s_font;
40
41 QMenu *m_contextMenu;
42};
43
44QPen AbstractFloatItemPrivate::s_pen = QPen(Qt::black);
45#ifdef Q_OS_MACX
46QFont AbstractFloatItemPrivate::s_font = QFont(QStringLiteral("Sans Serif"), 10);
47#else
48QFont AbstractFloatItemPrivate::s_font = QFont(QStringLiteral("Sans Serif"), 8);
49#endif
50
51AbstractFloatItem::AbstractFloatItem(const MarbleModel *marbleModel, const QPointF &point, const QSizeF &size)
52 : RenderPlugin(marbleModel)
53 , FrameGraphicsItem()
54 , d(new AbstractFloatItemPrivate())
55{
56 setCacheMode(ItemCoordinateCache);
57 setFrame(RectFrame);
58 setPadding(4.0);
59 setContentSize(size);
60 setPosition(point);
61}
62
63AbstractFloatItem::~AbstractFloatItem()
64{
65 delete d;
66}
67
68QHash<QString, QVariant> AbstractFloatItem::settings() const
69{
70 QHash<QString, QVariant> updated = RenderPlugin::settings();
71#ifdef Q_OS_OSX
72 updated.insert(QStringLiteral("position"), position().toPoint());
73#else
74 updated.insert(QStringLiteral("position"), position());
75#endif
76 return updated;
77}
78
79void AbstractFloatItem::setSettings(const QHash<QString, QVariant> &settings)
80{
81 if (settings.value(QStringLiteral("position")).type() == QVariant::String) {
82#ifdef Q_OS_OSX
83 setPosition(settings.value(QStringLiteral("position"), position()).toPointF());
84#else
85 // work around KConfig turning QPointFs into QStrings
86 const QStringList coordinates = settings.value(QStringLiteral("position")).toString().split(QLatin1Char(','));
87 setPosition(QPointF(coordinates.at(0).toFloat(), coordinates.at(1).toFloat()));
88#endif
89 } else {
90 setPosition(settings.value(QStringLiteral("position"), position()).toPointF());
91 }
92
93 RenderPlugin::setSettings(settings);
94}
95
96RenderPlugin::RenderType AbstractFloatItem::renderType() const
97{
98 return RenderPlugin::PanelRenderType;
99}
100
101QPen AbstractFloatItem::pen() const
102{
103 return d->s_pen;
104}
105
106void AbstractFloatItem::setPen(const QPen &pen)
107{
108 d->s_pen = pen;
109 update();
110}
111
112QFont AbstractFloatItem::font() const
113{
114 return d->s_font;
115}
116
117void AbstractFloatItem::setFont(const QFont &font)
118{
119 d->s_font = font;
120 update();
121}
122
123QString AbstractFloatItem::renderPolicy() const
124{
125 return QStringLiteral("ALWAYS");
126}
127
128QStringList AbstractFloatItem::renderPosition() const
129{
130 return QStringList(QStringLiteral("FLOAT_ITEM"));
131}
132
133void AbstractFloatItem::setVisible(bool visible)
134{
135 // Reimplemented since AbstractFloatItem does multiple inheritance
136 // and the (set)Visible() methods are available in both base classes!
137 RenderPlugin::setVisible(visible);
138}
139
140bool AbstractFloatItem::visible() const
141{
142 // Reimplemented since AbstractFloatItem does multiple inheritance
143 // and the (set)Visible() methods are available in both base classes!
144 return RenderPlugin::visible();
145}
146
147void AbstractFloatItem::setPositionLocked(bool lock)
148{
149 ScreenGraphicsItem::GraphicsItemFlags flags = this->flags();
150
151 if (lock) {
152 flags &= ~ScreenGraphicsItem::ItemIsMovable;
153 } else {
154 flags |= ScreenGraphicsItem::ItemIsMovable;
155 }
156
157 setFlags(flags);
158}
159
160bool AbstractFloatItem::positionLocked() const
161{
162 return (flags() & ScreenGraphicsItem::ItemIsMovable) == 0;
163}
164
165bool AbstractFloatItem::eventFilter(QObject *object, QEvent *e)
166{
167 if (!enabled() || !visible()) {
168 return false;
169 }
170
171 if (e->type() == QEvent::ContextMenu) {
172 QWidget *widget = qobject_cast<QWidget *>(object);
173 auto menuEvent = dynamic_cast<QContextMenuEvent *>(e);
174 if (widget != nullptr && menuEvent != nullptr && contains(menuEvent->pos())) {
175 contextMenuEvent(widget, menuEvent);
176 return true;
177 }
178 return false;
179 } else if (e->type() == QEvent::ToolTip) {
180 auto helpEvent = dynamic_cast<QHelpEvent *>(e);
181 if (helpEvent != nullptr && contains(helpEvent->pos())) {
182 toolTipEvent(helpEvent);
183 return true;
184 }
185 return false;
186 } else
187 return ScreenGraphicsItem::eventFilter(object, e);
188}
189
190void AbstractFloatItem::contextMenuEvent(QWidget *w, QContextMenuEvent *e)
191{
192 contextMenu()->exec(w->mapToGlobal(e->pos()));
193}
194
195void AbstractFloatItem::toolTipEvent(QHelpEvent *e)
196{
197 Q_UNUSED(e);
198}
199
200bool AbstractFloatItem::render(GeoPainter *painter, ViewportParams *viewport, const QString &renderPos, GeoSceneLayer *layer)
201{
202 Q_UNUSED(painter)
203 Q_UNUSED(viewport)
204 Q_UNUSED(renderPos)
205 Q_UNUSED(layer)
206
207 return true;
208}
209
210void AbstractFloatItem::show()
211{
212 setVisible(true);
213}
214
215void AbstractFloatItem::hide()
216{
217 setVisible(false);
218}
219
220QMenu *AbstractFloatItem::contextMenu()
221{
222 if (!d->m_contextMenu) {
223 d->m_contextMenu = new QMenu;
224
225 QAction *lockAction = d->m_contextMenu->addAction(QIcon(QStringLiteral(":/icons/unlock.png")), tr("&Lock"));
226 lockAction->setCheckable(true);
227 lockAction->setChecked(positionLocked());
228 connect(lockAction, &QAction::triggered, this, &AbstractFloatItem::setPositionLocked);
229
230 if (!(flags() & ItemIsHideable)) {
231 QAction *hideAction = d->m_contextMenu->addAction(tr("&Hide"));
232 connect(hideAction, &QAction::triggered, this, &AbstractFloatItem::hide);
233 }
234
235 DialogConfigurationInterface *configInterface = qobject_cast<DialogConfigurationInterface *>(this);
236 QDialog *dialog = configInterface ? configInterface->configDialog() : nullptr;
237 if (dialog) {
238 d->m_contextMenu->addSeparator();
239 QAction *configAction = d->m_contextMenu->addAction(QIcon(QStringLiteral(":/icons/settings-configure.png")), tr("&Configure..."));
240 connect(configAction, &QAction::triggered, dialog, &QDialog::exec);
241 }
242 }
243
244 Q_ASSERT(d->m_contextMenu);
245 return d->m_contextMenu;
246}
247
248}
249
250#include "moc_AbstractFloatItem.cpp"
A painter that allows to draw geometric primitives on the map.
Definition GeoPainter.h:86
Layer of a GeoScene document.
RenderType
A Type of plugin.
A public class that controls what is visible in the viewport of a Marble map.
Binds a QML item to a specific geodetic location in screen coordinates.
void setCheckable(bool)
void setChecked(bool)
void triggered(bool checked)
const QPoint & pos() const const
virtual int exec()
Type type() const const
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
const_reference at(qsizetype i) const const
QPoint mapToGlobal(const QPoint &pos) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 8 2024 12:02:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.