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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* ============================================================
 *
 * 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-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * ============================================================ */

#include "AbstractFloatItem.h"

// Qt includes

#include <QMenu>
#include <QAction>
#include <QContextMenuEvent>
#include <QDialog>
#include <QHelpEvent>
#include <QPen>

// KDE includes

#include <klocalizedstring.h>

// Local includes

#include "DialogConfigurationInterface.h"
#include "GeoPainter.h"
#include "digikam_debug.h"

namespace Marble
{

class Q_DECL_HIDDEN AbstractFloatItemPrivate
{
public:
    AbstractFloatItemPrivate()
        : m_contextMenu(nullptr)
    {
    }

    ~AbstractFloatItemPrivate()
    {
        delete m_contextMenu;
    }


    static QPen  s_pen;
    static QFont s_font;

    QMenu*       m_contextMenu;
};

QPen  AbstractFloatItemPrivate::s_pen  = QPen(Qt::black);

#ifdef Q_OS_MACX

QFont AbstractFloatItemPrivate::s_font = QFont(QStringLiteral("Sans Serif"), 10);

#else

QFont AbstractFloatItemPrivate::s_font = QFont(QStringLiteral("Sans Serif"), 8);

#endif

AbstractFloatItem::AbstractFloatItem(const MarbleModel* marbleModel, const QPointF& point, const QSizeF& size)
    : RenderPlugin(marbleModel),
      FrameGraphicsItem(),
      d(new AbstractFloatItemPrivate())
{
    setCacheMode(ItemCoordinateCache);
    setFrame(RectFrame);
    setPadding(4.0);
    setContentSize(size);
    setPosition(point);
}

AbstractFloatItem::~AbstractFloatItem()
{
    delete d;
}

QHash<QString, QVariant> AbstractFloatItem::settings() const
{
    QHash<QString, QVariant> updated = RenderPlugin::settings();

#ifdef Q_OS_OSX

    updated.insert(QStringLiteral("position"), position().toPoint());

#else

    updated.insert(QStringLiteral("position"), position());

#endif

    return updated;
}

void AbstractFloatItem::setSettings(const QHash<QString, QVariant>& settings)
{

#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))

    if (settings.value(QStringLiteral("position")).metaType().id() == QMetaType::QString)

#else

    if (settings.value(QStringLiteral("position")).type() == QVariant::String)

#endif

    {

#ifdef Q_OS_OSX

        setPosition(settings.value(QStringLiteral("position"), position()).toPointF());

#else

        // work around KConfig turning QPointFs into QStrings

        const QStringList coordinates = settings.value(QStringLiteral("position")).toString().split(QLatin1Char(','));
        setPosition(QPointF(coordinates.at(0).toFloat(), coordinates.at(1).toFloat()));

#endif

    }

    else
    {
        setPosition(settings.value(QStringLiteral("position"), position()).toPointF());
    }

    RenderPlugin::setSettings(settings);
}

RenderPlugin::RenderType AbstractFloatItem::renderType() const
{
    return RenderPlugin::PanelRenderType;
}

QPen AbstractFloatItem::pen() const
{
    return d->s_pen;
}

void AbstractFloatItem::setPen(const QPen& pen)
{
    d->s_pen = pen;
    update();
}

QFont AbstractFloatItem::font() const
{
    return d->s_font;
}

void AbstractFloatItem::setFont(const QFont& font)
{
    d->s_font = font;
    update();
}

QString AbstractFloatItem::renderPolicy() const
{
    return QStringLiteral("ALWAYS");
}

QStringList AbstractFloatItem::renderPosition() const
{
    return QStringList(QStringLiteral("FLOAT_ITEM"));
}

void AbstractFloatItem::setVisible(bool visible)<--- Derived function 'AbstractFloatItem::setVisible'<--- Derived function 'AbstractFloatItem::setVisible'
{
    // Reimplemented since AbstractFloatItem does multiple inheritance
    // and the (set)Visible() methods are available in both base classes!
    RenderPlugin::setVisible(visible);
}

bool AbstractFloatItem::visible() const<--- Derived function 'AbstractFloatItem::visible'<--- Derived function 'AbstractFloatItem::visible'
{
    // Reimplemented since AbstractFloatItem does multiple inheritance
    // and the (set)Visible() methods are available in both base classes!
    return RenderPlugin::visible();
}

void AbstractFloatItem::setPositionLocked(bool lock)
{
    ScreenGraphicsItem::GraphicsItemFlags flags = this->flags();

    if (lock)
    {
        flags &= ~ScreenGraphicsItem::ItemIsMovable;
    }

    else
    {
        flags |= ScreenGraphicsItem::ItemIsMovable;
    }

    setFlags(flags);
}

bool AbstractFloatItem::positionLocked() const
{
    return (flags() & ScreenGraphicsItem::ItemIsMovable) == 0;
}

bool AbstractFloatItem::eventFilter(QObject* object, QEvent* e)
{
    if (!enabled() || !visible())
    {
        return false;
    }

    if (e->type() == QEvent::ContextMenu)
    {
        QWidget* widget = qobject_cast<QWidget*>(object);
        QContextMenuEvent* menuEvent = dynamic_cast<QContextMenuEvent*>(e);

        if (widget != nullptr && menuEvent != nullptr && contains(menuEvent->pos()))
        {
            contextMenuEvent(widget, menuEvent);
            return true;
        }

        return false;
    }

    else if (e->type() == QEvent::ToolTip)
    {
        QHelpEvent* helpEvent = dynamic_cast<QHelpEvent*>(e);

        if (helpEvent != nullptr && contains(helpEvent->pos()))
        {
            toolTipEvent(helpEvent);
            return true;
        }

        return false;
    }

    else
    {
        return ScreenGraphicsItem::eventFilter(object, e);
    }
}

void AbstractFloatItem::contextMenuEvent(QWidget* w, QContextMenuEvent* e)
{
    contextMenu()->exec(w->mapToGlobal(e->pos()));
}

void AbstractFloatItem::toolTipEvent(QHelpEvent* e)
{
    Q_UNUSED(e);
}

bool AbstractFloatItem::render(GeoPainter* painter, ViewportParams* viewport,
                               const QString& renderPos, GeoSceneLayer* layer)
{
    Q_UNUSED(painter)
    Q_UNUSED(viewport)
    Q_UNUSED(renderPos)
    Q_UNUSED(layer)

    return true;
}

void AbstractFloatItem::show()<--- Derived function 'AbstractFloatItem::show'
{
    setVisible(true);
}

void AbstractFloatItem::hide()<--- Derived function 'AbstractFloatItem::hide'
{
    setVisible(false);
}

QMenu* AbstractFloatItem::contextMenu()
{
    if (!d->m_contextMenu)
    {
        d->m_contextMenu = new QMenu;

        QAction* lockAction = d->m_contextMenu->addAction(QIcon::fromTheme(QStringLiteral("unlock")), i18n("&Lock"));
        lockAction->setCheckable(true);
        lockAction->setChecked(positionLocked());
        connect(lockAction, SIGNAL(triggered(bool)), this, SLOT(setPositionLocked(bool)));

        if (!(flags() & ItemIsHideable))
        {
            QAction* hideAction = d->m_contextMenu->addAction(i18n("&Hide"));
            connect(hideAction, SIGNAL(triggered()), this, SLOT(hide()));
        }

        DialogConfigurationInterface* configInterface = qobject_cast<DialogConfigurationInterface*>(this);
        QDialog* dialog = configInterface ? configInterface->configDialog() : nullptr;

        if (dialog)
        {
            d->m_contextMenu->addSeparator();
            QAction* configAction = d->m_contextMenu->addAction(QIcon::fromTheme(QLatin1String("configure")), i18n("&Configure..."));
            connect(configAction, SIGNAL(triggered()), dialog, SLOT(exec()));
        }
    }

    Q_ASSERT(d->m_contextMenu);
    return d->m_contextMenu;
}

} // namespace Marble

#include "moc_AbstractFloatItem.cpp"