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
69{
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
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
94}
95
97{
98 return RenderPlugin::PanelRenderType;
99}
100
102{
103 return d->s_pen;
104}
105
107{
108 d->s_pen = pen;
109 update();
110}
111
113{
114 return d->s_font;
115}
116
118{
119 d->s_font = font;
120 update();
121}
122
124{
125 return QStringLiteral("ALWAYS");
126}
127
129{
130 return QStringList(QStringLiteral("FLOAT_ITEM"));
131}
132
134{
135 // Reimplemented since AbstractFloatItem does multiple inheritance
136 // and the (set)Visible() methods are available in both base classes!
138}
139
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
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
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
211{
212 setVisible(true);
213}
214
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"
bool visible() const
Check visibility of the float item.
bool positionLocked() const
Check is position locked.
QHash< QString, QVariant > settings() const override
Settings of the plugin.
void setPen(const QPen &pen)
setting current pen for rendering
void setSettings(const QHash< QString, QVariant > &settings) override
Set the settings of the plugin.
QFont font() const
current font for rendering
RenderType renderType() const override
Render type of the plugin.
void setPositionLocked(bool lock)
Set is position locked.
MARBLE_DEPRECATED bool render(GeoPainter *painter, ViewportParams *viewport, const QString &renderPos=QLatin1String("FLOAT_ITEM"), GeoSceneLayer *layer=nullptr) override
Paints the float item on the map.
QString renderPolicy() const override
Return how the plugin settings should be used.
MARBLE_DEPRECATED QStringList renderPosition() const override
Returns the rendering position of this float item.
QPen pen() const
current pen for rendering
void setVisible(bool visible)
Set visibility of the float item.
void setFont(const QFont &font)
setting current font for rendering
A painter that allows to draw geometric primitives on the map.
Definition GeoPainter.h:86
Layer of a GeoScene document.
The data model (not based on QAbstractModel) for a MarbleWidget.
Definition MarbleModel.h:84
The abstract class that creates a renderable item.
void setVisible(bool visible)
setting visible
virtual void setSettings(const QHash< QString, QVariant > &settings)
Set the settings of the plugin.
RenderType
A Type of plugin.
virtual QHash< QString, QVariant > settings() const
Settings of the 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)
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-2025 The KDE developers.
Generated on Fri Jan 31 2025 11:56:26 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.