File indexing completed on 2024-04-28 03:50:13

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2008 Torsten Rahn <tackat@kde.org>
0004 // SPDX-FileCopyrightText: 2010 Cezar Mocan <mocancezar@gmail.com>
0005 //
0006 
0007 #include "CrosshairsPlugin.h"
0008 #include "ui_CrosshairsConfigWidget.h"
0009 
0010 #include "GeoPainter.h"
0011 #include "MarbleDebug.h"
0012 #include "MarbleDirs.h"
0013 #include "ViewportParams.h"
0014 
0015 #include <QRect>
0016 #include <QColor>
0017 #include <QPushButton>
0018 #include <QSvgRenderer>
0019 #include <QImageReader>
0020 
0021 
0022 namespace Marble
0023 {
0024 
0025 CrosshairsPlugin::CrosshairsPlugin()
0026     : RenderPlugin( nullptr ),
0027       m_svgobj( nullptr ),
0028       m_themeIndex( 0 ),
0029       m_configDialog( nullptr ),
0030       m_uiConfigWidget( nullptr )
0031 {
0032 }
0033 
0034 CrosshairsPlugin::CrosshairsPlugin( const MarbleModel *marbleModel )
0035     : RenderPlugin( marbleModel ),
0036       m_isInitialized( false ),
0037       m_svgobj( nullptr ),
0038       m_themeIndex( 0 ),
0039       m_configDialog( nullptr ),
0040       m_uiConfigWidget( nullptr )
0041 {
0042 }
0043 
0044 CrosshairsPlugin::~CrosshairsPlugin ()
0045 {
0046     delete m_svgobj;
0047 }
0048 
0049 
0050 QStringList CrosshairsPlugin::backendTypes() const
0051 {
0052     return QStringList(QStringLiteral("crosshairs"));
0053 }
0054 
0055 QString CrosshairsPlugin::renderPolicy() const
0056 {
0057     return QStringLiteral("ALWAYS");
0058 }
0059 
0060 QStringList CrosshairsPlugin::renderPosition() const
0061 {
0062     return QStringList(QStringLiteral("FLOAT_ITEM")); // although this is not a float item we choose the position of one
0063 }
0064 
0065 RenderPlugin::RenderType CrosshairsPlugin::renderType() const
0066 {
0067     return RenderPlugin::TopLevelRenderType;
0068 }
0069 
0070 QString CrosshairsPlugin::name() const
0071 {
0072     return tr( "Crosshairs" );
0073 }
0074 
0075 QString CrosshairsPlugin::guiString() const
0076 {
0077     return tr( "Cross&hairs" );
0078 }
0079 
0080 QString CrosshairsPlugin::nameId() const
0081 {
0082     return QStringLiteral("crosshairs");
0083 }
0084 
0085 QString CrosshairsPlugin::version() const
0086 {
0087     return QStringLiteral("1.0");
0088 }
0089 
0090 QString CrosshairsPlugin::description() const
0091 {
0092     return tr( "A plugin that shows crosshairs." );
0093 }
0094 
0095 QString CrosshairsPlugin::copyrightYears() const
0096 {
0097     return QStringLiteral("2009, 2010");
0098 }
0099 
0100 QVector<PluginAuthor> CrosshairsPlugin::pluginAuthors() const
0101 {
0102     return QVector<PluginAuthor>()
0103             << PluginAuthor(QStringLiteral("Cezar Mocan"), QStringLiteral("cezarmocan@gmail.com"))
0104             << PluginAuthor(QStringLiteral("Torsten Rahn"), QStringLiteral("tackat@kde.org"));
0105 }
0106 
0107 QIcon CrosshairsPlugin::icon () const
0108 {
0109     return QIcon(QStringLiteral(":/icons/crosshairs.png"));
0110 }
0111 
0112 void CrosshairsPlugin::initialize ()
0113 {
0114     readSettings();
0115     m_isInitialized = true;
0116 }
0117 
0118 bool CrosshairsPlugin::isInitialized () const
0119 {
0120     return m_isInitialized;
0121 }
0122 
0123 QDialog *CrosshairsPlugin::configDialog()
0124 {
0125     if ( !m_configDialog ) {
0126         m_configDialog = new QDialog();
0127         m_uiConfigWidget = new Ui::CrosshairsConfigWidget;
0128         m_uiConfigWidget->setupUi( m_configDialog );
0129         readSettings();
0130         connect( m_uiConfigWidget->m_buttonBox, SIGNAL(accepted()),
0131                 SLOT(writeSettings()) );
0132         connect( m_uiConfigWidget->m_buttonBox, SIGNAL(rejected()),
0133                 SLOT(readSettings()) );
0134         QPushButton *applyButton = m_uiConfigWidget->m_buttonBox->button( QDialogButtonBox::Apply );
0135         connect( applyButton, SIGNAL(clicked()),
0136                  this,        SLOT(writeSettings()) );
0137     }
0138 
0139     return m_configDialog;
0140 }
0141 
0142 QHash<QString,QVariant> CrosshairsPlugin::settings() const
0143 {
0144     QHash<QString, QVariant> result = RenderPlugin::settings();
0145 
0146     result.insert(QStringLiteral("theme"), m_themeIndex);
0147 
0148     return result;
0149 }
0150 
0151 void CrosshairsPlugin::setSettings( const QHash<QString,QVariant> &settings )
0152 {
0153     RenderPlugin::setSettings( settings );
0154 
0155     m_themeIndex = settings.value(QStringLiteral("theme"), 0).toInt();
0156 
0157     readSettings();
0158 }
0159 
0160 
0161 void CrosshairsPlugin::readSettings()
0162 {
0163     if ( m_uiConfigWidget && m_themeIndex >= 0 && m_themeIndex < m_uiConfigWidget->m_themeList->count() ) {
0164         m_uiConfigWidget->m_themeList->setCurrentRow( m_themeIndex );
0165     }
0166 
0167     m_theme = QStringLiteral(":/crosshairs-darkened.png");
0168     switch( m_themeIndex ) {
0169     case 1:
0170         m_theme = QStringLiteral(":/crosshairs-gun1.svg");
0171         break;
0172     case 2:
0173         m_theme = QStringLiteral(":/crosshairs-gun2.svg");
0174         break;
0175     case 3:
0176         m_theme = QStringLiteral(":/crosshairs-circled.svg");
0177         break;
0178     case 4:
0179         m_theme = QStringLiteral(":/crosshairs-german.svg");
0180         break;
0181     }
0182 
0183     if (QImageReader::imageFormat(m_theme) == QLatin1String("svg")) {
0184         delete m_svgobj;
0185         m_svgobj = new QSvgRenderer( m_theme, this );
0186     }
0187     m_crosshairs = QPixmap();
0188 }
0189 
0190 void CrosshairsPlugin::writeSettings()
0191 {
0192     if ( m_uiConfigWidget ) {
0193         m_themeIndex = m_uiConfigWidget->m_themeList->currentRow();
0194     }
0195     readSettings();
0196     emit settingsChanged( nameId() );
0197 }
0198 
0199 bool CrosshairsPlugin::render( GeoPainter *painter, ViewportParams *viewport,
0200                                const QString& renderPos,
0201                                GeoSceneLayer * layer )
0202 {
0203     Q_UNUSED( renderPos )
0204     Q_UNUSED( layer )
0205 
0206     if ( m_crosshairs.isNull() ) {
0207         if (QImageReader::imageFormat(m_theme) == QLatin1String("svg")) {
0208             painter->setRenderHint( QPainter::Antialiasing, true );
0209             m_crosshairs = QPixmap( QSize( 21, 21 ) );
0210             m_crosshairs.fill( Qt::transparent );
0211 
0212             QPainter mapPainter( &m_crosshairs );
0213             m_svgobj->render( &mapPainter );
0214         }
0215         else {
0216             m_crosshairs.load( m_theme );
0217         }
0218     }
0219 
0220     const int width = m_crosshairs.width();
0221     const int height = m_crosshairs.height();
0222 
0223     int posX;
0224     int posY;
0225 
0226     GeoDataCoordinates const focusPoint = viewport->focusPoint();
0227     GeoDataCoordinates const centerPoint = GeoDataCoordinates( viewport->centerLongitude(), viewport->centerLatitude() );
0228     if ( focusPoint == centerPoint ) {
0229         // Focus point is in the middle of the screen. Special casing this avoids jittering.
0230         const QSize viewPortSize = viewport->size();
0231         posX = (viewPortSize.width() - width) / 2;
0232         posY = (viewPortSize.height() - height) / 2;
0233     } else {
0234         qreal centerX = 0.0;
0235         qreal centerY = 0.0;
0236         viewport->screenCoordinates( focusPoint, centerX, centerY );
0237         posX = qRound(centerX - width / 2.0);
0238         posY = qRound(centerY - height / 2.0);
0239     }
0240     painter->drawPixmap(posX, posY, m_crosshairs );
0241 
0242     return true;
0243 }
0244 
0245 }
0246 
0247 #include "moc_CrosshairsPlugin.cpp"