AbstractFloatItem.cpp 6.64 KB
Newer Older
1
//
2
// This file is part of the Marble Virtual Globe.
3 4 5 6 7 8 9 10
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2008      Torsten Rahn   <rahn@kde.org>
//

11
// Self
12
#include "AbstractFloatItem.h"
13

14
// Qt
15 16
#include <QMenu>
#include <QAction>
17
#include <QContextMenuEvent>
18
#include <QDialog>
19
#include <QHelpEvent>
20
#include <QPen>
21

22
// Marble
23
#include "DialogConfigurationInterface.h"
24
#include "GeoPainter.h"
25
#include "MarbleDebug.h"
26

27 28 29
namespace Marble
{

30
class AbstractFloatItemPrivate
31 32
{
  public:
33
    AbstractFloatItemPrivate() : m_contextMenu( 0 )
34 35 36
    {
    }

37
    ~AbstractFloatItemPrivate()
38
    {
39
        delete m_contextMenu;
40 41 42 43
    }


    static QPen         s_pen;
44
    static QFont        s_font;
45 46

    QMenu* m_contextMenu;
47 48
};

49
QPen         AbstractFloatItemPrivate::s_pen = QPen( Qt::black );
50
#ifdef Q_OS_MACX
Dāvis Mosāns's avatar
Dāvis Mosāns committed
51
    QFont AbstractFloatItemPrivate::s_font = QFont( QStringLiteral("Sans Serif"), 10 );
52
#else
Dāvis Mosāns's avatar
Dāvis Mosāns committed
53
    QFont AbstractFloatItemPrivate::s_font = QFont( QStringLiteral("Sans Serif"), 8 );
54
#endif
55

56 57
AbstractFloatItem::AbstractFloatItem( const MarbleModel *marbleModel, const QPointF &point, const QSizeF &size )
    : RenderPlugin( marbleModel ),
Bastian Holst's avatar
Bastian Holst committed
58
      FrameGraphicsItem(),
59
      d( new AbstractFloatItemPrivate() )
60
{
61
    setCacheMode( ItemCoordinateCache );
62
    setFrame( RectFrame );
63
    setPadding( 4.0 );
Bastian Holst's avatar
Bastian Holst committed
64 65
    setContentSize( size );
    setPosition( point );
66 67
}

68
AbstractFloatItem::~AbstractFloatItem()
69 70 71 72
{
    delete d;
}

73 74 75
QHash<QString,QVariant> AbstractFloatItem::settings() const
{
    QHash<QString,QVariant> updated = RenderPlugin::settings();
76 77 78
#ifdef Q_OS_OSX
    updated.insert(QStringLiteral("position"), position().toPoint());
#else
79
    updated.insert(QStringLiteral("position"), position());
80
#endif
81 82 83 84 85
    return updated;
}

void AbstractFloatItem::setSettings(const QHash<QString, QVariant> &settings)
{
86
    if (settings.value(QStringLiteral("position")).type() == QVariant::String) {
87 88 89
#ifdef Q_OS_OSX
        setPosition(settings.value(QStringLiteral("position"), position()).toPointF());
#else
90
        // work around KConfig turning QPointFs into QStrings
91
        const QStringList coordinates = settings.value(QStringLiteral("position")).toString().split(QLatin1Char(','));
92
        setPosition( QPointF( coordinates.at( 0 ).toFloat(), coordinates.at( 1 ).toFloat() ) );
93
#endif
94 95
    }
    else {
96
        setPosition(settings.value(QStringLiteral("position"), position()).toPointF());
97 98
    }

99 100 101
    RenderPlugin::setSettings(settings);
}

102 103 104 105 106
RenderPlugin::RenderType AbstractFloatItem::renderType() const
{
    return RenderPlugin::PanelRenderType;
}

107
QPen AbstractFloatItem::pen() const
108 109 110 111
{
    return d->s_pen;
}

112
void AbstractFloatItem::setPen( const QPen &pen )
113 114
{
    d->s_pen = pen;
115
    update();
116 117
}

118
QFont AbstractFloatItem::font() const
119 120 121 122
{
    return d->s_font;
}

123
void AbstractFloatItem::setFont( const QFont &font )
124 125
{
    d->s_font = font;
126
    update();
127 128
}

129
QString AbstractFloatItem::renderPolicy() const
130
{
131
    return QStringLiteral("ALWAYS");
132 133
}

134
QStringList AbstractFloatItem::renderPosition() const
135
{
136
    return QStringList(QStringLiteral("FLOAT_ITEM"));
137 138
}

139 140
void AbstractFloatItem::setVisible( bool visible )
{
141 142
    // Reimplemented since AbstractFloatItem does multiple inheritance 
    // and the (set)Visible() methods are available in both base classes!
143
    RenderPlugin::setVisible( visible );
144 145
}

146 147
bool AbstractFloatItem::visible() const
{
148 149
    // Reimplemented since AbstractFloatItem does multiple inheritance 
    // and the (set)Visible() methods are available in both base classes!
150
    return RenderPlugin::visible();
151 152
}

153 154
void AbstractFloatItem::setPositionLocked( bool lock )
{
155
    ScreenGraphicsItem::GraphicsItemFlags flags = this->flags();
Inge Wallin's avatar
Inge Wallin committed
156

157 158 159 160 161 162
    if ( lock ) {
        flags &= ~ScreenGraphicsItem::ItemIsMovable;
    }
    else {
        flags |= ScreenGraphicsItem::ItemIsMovable;
    }
163

164
    setFlags( flags );
165 166
}

167
bool AbstractFloatItem::positionLocked() const
168
{
169
    return ( flags() & ScreenGraphicsItem::ItemIsMovable ) == 0;
170 171
}

172 173
bool AbstractFloatItem::eventFilter( QObject *object, QEvent *e )
{
174 175 176
    if ( !enabled() || !visible() ) {
        return false;
    }
177

Torsten Rahn's avatar
Torsten Rahn committed
178 179
    if( e->type() == QEvent::ContextMenu )
    {
Dennis Nienhüser's avatar
Dennis Nienhüser committed
180
        QWidget *widget = qobject_cast<QWidget *>( object );
Torsten Rahn's avatar
Torsten Rahn committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        QContextMenuEvent *menuEvent = dynamic_cast<QContextMenuEvent *> ( e );
        if( widget != NULL && menuEvent != NULL && contains( menuEvent->pos() ) )
        {
            contextMenuEvent( widget, menuEvent );
            return true;
        }
        return false;
    }
    else if( e->type() == QEvent::ToolTip )
    {
        QHelpEvent *helpEvent = dynamic_cast<QHelpEvent *>( e );
        if( helpEvent != NULL && contains( helpEvent->pos() ) )
        {
            toolTipEvent( helpEvent );
            return true;
        }
        return false;
    }
    else
        return ScreenGraphicsItem::eventFilter( object, e );
}

void AbstractFloatItem::contextMenuEvent ( QWidget *w, QContextMenuEvent *e )
{
205
    contextMenu()->exec( w->mapToGlobal( e->pos() ) );
Torsten Rahn's avatar
Torsten Rahn committed
206 207 208 209 210
}

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

213
bool AbstractFloatItem::render( GeoPainter *painter, ViewportParams *viewport,
214
             const QString& renderPos, GeoSceneLayer * layer )
215
{
216 217 218 219
    Q_UNUSED(painter)
    Q_UNUSED(viewport)
    Q_UNUSED(renderPos)
    Q_UNUSED(layer)
220

221
    return true;
222
}
223

224 225 226 227 228 229 230 231 232 233
void AbstractFloatItem::show()
{
    setVisible( true );
}

void AbstractFloatItem::hide()
{
    setVisible( false );
}

234 235 236 237 238 239
QMenu* AbstractFloatItem::contextMenu()
{
    if ( !d->m_contextMenu )
    {
        d->m_contextMenu = new QMenu;

240
        QAction *lockAction = d->m_contextMenu->addAction(QIcon(QStringLiteral(":/icons/unlock.png")), tr("&Lock"));
241 242
        lockAction->setCheckable( true );
        lockAction->setChecked( positionLocked() );
243
        connect( lockAction, SIGNAL(triggered(bool)), this, SLOT(setPositionLocked(bool)) );
244 245 246

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

250 251
        DialogConfigurationInterface *configInterface = qobject_cast<DialogConfigurationInterface *>( this );
        QDialog *dialog = configInterface ? configInterface->configDialog() : 0;
252 253 254
        if( dialog )
        {
            d->m_contextMenu->addSeparator();
255
            QAction *configAction = d->m_contextMenu->addAction(QIcon(QStringLiteral(":/icons/settings-configure.png")), tr("&Configure..."));
256
            connect( configAction, SIGNAL(triggered()), dialog, SLOT(exec()) );
257
        }
258 259 260 261 262 263
    }

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

264
}
265

266
#include "moc_AbstractFloatItem.cpp"