File indexing completed on 2024-05-05 03:50:45

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2012 Dennis Nienhüser <nienhueser@kde.org>
0004 // SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
0005 //
0006 
0007 #include "License.h"
0008 #include "MarbleWidget.h"
0009 #include "MarbleModel.h"
0010 #include "MarbleAboutDialog.h"
0011 #include "WidgetGraphicsItem.h"
0012 #include "MarbleGraphicsGridLayout.h"
0013 #include "ViewportParams.h"
0014 #include "GeoSceneDocument.h"
0015 #include "GeoSceneHead.h"
0016 #include "GeoSceneLicense.h"
0017 
0018 #include <QCommonStyle>
0019 #include <QContextMenuEvent>
0020 #include <QPainter>
0021 #include <QPainterPath>
0022 #include <QLabel>
0023 #include <QMenu>
0024 #include <QMouseEvent>
0025 
0026 namespace Marble
0027 {
0028 
0029 class OutlinedStyle : public QCommonStyle {
0030 public:
0031     void drawItemText( QPainter *painter, const QRect &rect, int alignment, const QPalette &palette,
0032                        bool enabled, const QString& text, QPalette::ColorRole textRole ) const override {
0033         Q_UNUSED( alignment );
0034         Q_UNUSED( enabled );
0035 
0036         if ( text.isEmpty() ) {
0037             return;
0038         }
0039 
0040         QPen savedPen;
0041         if ( textRole != QPalette::NoRole ) {
0042             savedPen = painter->pen();
0043             painter->setPen( QPen( palette.brush( textRole ), savedPen.widthF() ) );
0044         }
0045 
0046         QPainterPath path;
0047         QFontMetricsF metrics( painter->font() );
0048         QPointF point( rect.x() + 7.0, rect.y() + metrics.ascent() );
0049         path.addText( point, painter->font(), text );
0050         QPen pen( Qt::white );
0051         pen.setWidth( 3 );
0052         painter->setPen( pen );
0053         painter->setBrush( QBrush( Qt::black ) );
0054         painter->setRenderHint( QPainter::Antialiasing, true );
0055         painter->drawPath( path );
0056 
0057         painter->setPen( Qt::NoPen );
0058         painter->drawPath( path );
0059 
0060         if ( textRole != QPalette::NoRole ) {
0061             painter->setPen( savedPen );
0062         }
0063     }
0064 };
0065 
0066 License::License( const MarbleModel *marbleModel )
0067     : AbstractFloatItem( marbleModel, QPointF( -10.0, -5.0 ), QSizeF( 150.0, 20.0 ) ),
0068       m_widgetItem( nullptr ),
0069       m_label( nullptr ),
0070       m_showFullLicense( false ),
0071       m_contextMenu( nullptr )
0072 {
0073     setEnabled( true );
0074     setVisible( true );
0075     setBackground( QBrush( QColor( Qt::transparent ) ) );
0076     setFrame( NoFrame );
0077 }
0078 
0079 License::~License()
0080 {
0081 }
0082 
0083 QStringList License::backendTypes() const
0084 {
0085     return QStringList(QStringLiteral("License"));
0086 }
0087 
0088 QString License::name() const
0089 {
0090     return tr( "License" );
0091 }
0092 
0093 QString License::guiString() const
0094 {
0095     return tr( "&License" );
0096 }
0097 
0098 QString License::nameId() const
0099 {
0100     return QStringLiteral("license");
0101 }
0102 
0103 QString License::version() const
0104 {
0105     return QStringLiteral("1.0");
0106 }
0107 
0108 QString License::description() const
0109 {
0110     return tr( "This is a float item that provides copyright information." );
0111 }
0112 
0113 QString License::copyrightYears() const
0114 {
0115     return QStringLiteral("2012");
0116 }
0117 
0118 QVector<PluginAuthor> License::pluginAuthors() const
0119 {
0120     return QVector<PluginAuthor>()
0121            << PluginAuthor(QStringLiteral("Dennis Nienhüser"), QStringLiteral("nienhueser@kde.org"))
0122            << PluginAuthor(QStringLiteral("Illya Kovalevskyy"), QStringLiteral("illya.kovalevskyy@gmail.com"));
0123 }
0124 
0125 QIcon License::icon () const
0126 {
0127     return QIcon(QStringLiteral(":/icons/license.png"));
0128 }
0129 
0130 void License::initialize ()
0131 {
0132     delete m_widgetItem;
0133     m_widgetItem = new WidgetGraphicsItem( this );
0134     m_label = new QLabel;
0135     auto style = new OutlinedStyle;
0136     style->setParent(this);
0137     m_label->setStyle( style );
0138     m_label->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Fixed );
0139     m_widgetItem->setWidget( m_label );
0140 
0141     MarbleGraphicsGridLayout *layout = new MarbleGraphicsGridLayout( 1, 1 );
0142     layout->addItem( m_widgetItem, 0, 0 );
0143     setLayout( layout );
0144     setPadding( 0 );
0145 
0146     updateLicenseText();
0147     connect( marbleModel(), SIGNAL(themeChanged(QString)), this, SLOT(updateLicenseText()) );
0148 }
0149 
0150 void License::updateLicenseText()
0151 {
0152     const GeoSceneDocument *const mapTheme = marbleModel()->mapTheme();
0153     if ( !mapTheme )
0154         return;
0155 
0156     const GeoSceneHead *const head = mapTheme->head();
0157     if ( !head )
0158         return;
0159 
0160     const GeoSceneLicense *license = marbleModel()->mapTheme()->head()->license();
0161     m_label->setText( m_showFullLicense ? license->license() : license->shortLicense() );
0162     m_label->setToolTip( license->license() );
0163     if( license->attribution() == GeoSceneLicense::Always ) {
0164         setUserCheckable( false );
0165     } else if( license->attribution() == GeoSceneLicense::Never ) {
0166         setVisible( false );
0167         setUserCheckable( false );
0168     } else if( license->attribution() == GeoSceneLicense::OptIn ) {
0169         setUserCheckable( true );
0170         setVisible( false );
0171     } else {
0172         setUserCheckable( true );
0173         setVisible( true );
0174     }
0175     QSizeF const magic( 6,0 );
0176     m_widgetItem->setSize( m_label->sizeHint()+magic );
0177     setSize( m_label->sizeHint()+magic );
0178     update();
0179     emit repaintNeeded();
0180 }
0181 
0182 void License::toggleLicenseSize()
0183 {
0184     m_showFullLicense = !m_showFullLicense;
0185     updateLicenseText();
0186 }
0187 
0188 void License::showAboutDialog()
0189 {
0190     QPointer<MarbleAboutDialog> aboutDialog = new MarbleAboutDialog;
0191     aboutDialog->setInitialTab( MarbleAboutDialog::Data );
0192     aboutDialog->exec();
0193     delete aboutDialog;
0194 }
0195 
0196 bool License::isInitialized () const
0197 {
0198     return m_widgetItem;
0199 }
0200 
0201 bool License::eventFilter( QObject *object, QEvent *event )
0202 {
0203     if ( !enabled() || !visible() )
0204         return false;
0205 
0206     MarbleWidget *widget = dynamic_cast<MarbleWidget*>( object );
0207     if ( !widget ) {
0208         return AbstractFloatItem::eventFilter( object,event );
0209     }
0210 
0211     if( event->type() == QEvent::MouseMove ) {
0212         QMouseEvent *mouseEvent = static_cast<QMouseEvent*>( event );
0213         QRectF floatItemRect = QRectF( positivePosition(), size() );
0214         if ( floatItemRect.contains( mouseEvent->pos() ) ) {
0215             widget->setCursor( QCursor( Qt::ArrowCursor ) );
0216             return true;
0217         }
0218     }
0219 
0220     return AbstractFloatItem::eventFilter( object, event );
0221 }
0222 
0223 void License::contextMenuEvent( QWidget *w, QContextMenuEvent *e )
0224 {
0225     if ( !m_contextMenu ) {
0226         m_contextMenu = contextMenu();
0227 
0228         QAction *toggleAction = m_contextMenu->addAction( tr("&Full License"), this,
0229                                                 SLOT(toggleLicenseSize()) );
0230         toggleAction->setCheckable( true );
0231         toggleAction->setChecked( m_showFullLicense );
0232 
0233         m_contextMenu->addAction( tr("&Show Details"), this, SLOT(showAboutDialog()) );
0234     }
0235 
0236     Q_ASSERT( m_contextMenu );
0237     m_contextMenu->exec( w->mapToGlobal( e->pos() ) );
0238 }
0239 
0240 }
0241 
0242 #include "moc_License.cpp"