File indexing completed on 2024-04-14 03:48:02

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2008 Torsten Rahn <rahn@kde.org>
0004 // SPDX-FileCopyrightText: 2008 Inge Wallin <inge@lysator.liu.se>
0005 // SPDX-FileCopyrightText: 2011, 2012 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0006 // SPDX-FileCopyrightText: 2012 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
0007 //
0008 
0009 // Self
0010 #include "RenderPlugin.h"
0011 
0012 // Marble
0013 #include "DialogConfigurationInterface.h"
0014 #include "MarbleModel.h"
0015 #include "MarbleDebug.h"
0016 #include "RenderPluginModel.h"
0017 #include "RenderState.h"
0018 
0019 // Qt
0020 #include <QAction>
0021 #include <QStandardItem>
0022 
0023 
0024 namespace Marble
0025 {
0026 
0027 class Q_DECL_HIDDEN RenderPlugin::Private
0028 {
0029   public:
0030     Private( const MarbleModel *marbleModel )
0031         : m_marbleModel( marbleModel ),
0032           m_action(nullptr),
0033           m_item(),
0034           m_enabled(true),
0035           m_visible(true),
0036           m_userCheckable(true)
0037     {
0038     }
0039 
0040     ~Private()
0041     {
0042     }
0043 
0044     // const: RenderPlugins should only read the model, not modify it
0045     const MarbleModel  *const m_marbleModel;
0046 
0047     QAction            m_action;
0048     QStandardItem      m_item;
0049 
0050     bool                m_enabled;
0051     bool                m_visible;
0052     bool                m_userCheckable;
0053 };
0054 
0055 
0056 RenderPlugin::RenderPlugin( const MarbleModel *marbleModel )
0057     : d( new Private( marbleModel ) )
0058 {
0059     connect( &d->m_action, SIGNAL(toggled(bool)),
0060              this,         SLOT(setVisible(bool)) );
0061     connect( this,         SIGNAL(visibilityChanged(bool,QString)),
0062              &d->m_action, SLOT(setChecked(bool)) );
0063     connect( this,         SIGNAL(enabledChanged(bool)),
0064              &d->m_action, SLOT(setVisible(bool)) );
0065     connect( this,         SIGNAL(enabledChanged(bool)),
0066                            SIGNAL(actionGroupsChanged()) );
0067 
0068     connect( this, SIGNAL(visibilityChanged(bool,QString)),
0069              this, SIGNAL(repaintNeeded()) );
0070     connect( this, SIGNAL(settingsChanged(QString)),
0071              this, SIGNAL(repaintNeeded()) );
0072 }
0073 
0074 RenderPlugin::~RenderPlugin()
0075 {
0076     delete d;
0077 }
0078 
0079 const MarbleModel* RenderPlugin::marbleModel() const
0080 {
0081     return d->m_marbleModel;
0082 }
0083 
0084 QAction* RenderPlugin::action() const
0085 {
0086     d->m_action.setCheckable( true );
0087     d->m_action.setChecked( visible() );
0088     d->m_action.setIcon( icon() );
0089     d->m_action.setText( guiString() );
0090     d->m_action.setToolTip( description() );
0091     return &d->m_action;
0092 }
0093 
0094 const QList<QActionGroup*>* RenderPlugin::actionGroups() const
0095 {
0096     return nullptr;
0097 }
0098 
0099 const QList<QActionGroup*>* RenderPlugin::toolbarActionGroups() const
0100 {
0101     return nullptr;
0102 }
0103 
0104 QStandardItem* RenderPlugin::item()
0105 {
0106     d->m_item.setIcon( icon() );
0107     d->m_item.setText( name() );
0108     d->m_item.setEditable( false );
0109     d->m_item.setCheckable( true );
0110     d->m_item.setCheckState( enabled() ?  Qt::Checked : Qt::Unchecked  );
0111     d->m_item.setToolTip( description() );
0112     d->m_item.setFlags( d->m_item.flags() & ~Qt::ItemIsSelectable );
0113 
0114     // Custom data
0115     d->m_item.setData( nameId(), RenderPluginModel::NameId );
0116     d->m_item.setData( (bool) qobject_cast<DialogConfigurationInterface *>( this ), RenderPluginModel::ConfigurationDialogAvailable );
0117     d->m_item.setData( backendTypes(), RenderPluginModel::BackendTypes );
0118     d->m_item.setData( version(), RenderPluginModel::Version );
0119     d->m_item.setData( aboutDataText(), RenderPluginModel::AboutDataText );
0120     d->m_item.setData( copyrightYears(), RenderPluginModel::CopyrightYears );
0121 
0122     return &d->m_item;
0123 }
0124 
0125 void RenderPlugin::applyItemState()
0126 {
0127     setEnabled( d->m_item.checkState() == Qt::Checked );
0128 }
0129 
0130 void RenderPlugin::retrieveItemState()
0131 {
0132     d->m_item.setCheckState( enabled() ? Qt::Checked : Qt::Unchecked  );
0133 }
0134 
0135 void RenderPlugin::setEnabled( bool enabled )
0136 {
0137     if ( enabled == d->m_enabled )
0138         return;
0139 
0140     d->m_enabled = enabled;
0141 
0142     d->m_item.setCheckState( enabled ? Qt::Checked : Qt::Unchecked  );
0143 
0144     emit enabledChanged( enabled );
0145 }
0146 
0147 void RenderPlugin::setVisible( bool visible )
0148 {
0149     if ( visible == d->m_visible )
0150         return;
0151 
0152     d->m_visible = visible;
0153 
0154     emit visibilityChanged( visible, nameId() );
0155 }
0156 
0157 void RenderPlugin::setUserCheckable( bool checkable )
0158 {
0159     if ( checkable != d->m_userCheckable ) {
0160         d->m_action.setEnabled( checkable );
0161         d->m_userCheckable = checkable;
0162         emit userCheckableChanged( checkable );
0163     }
0164 }
0165 
0166 bool RenderPlugin::enabled() const
0167 {
0168     return d->m_enabled;
0169 }
0170 
0171 bool RenderPlugin::visible() const
0172 {
0173     return d->m_visible;
0174 }
0175 
0176 bool RenderPlugin::isUserCheckable() const
0177 {
0178     return d->m_userCheckable;
0179 }
0180 
0181 QHash<QString,QVariant> RenderPlugin::settings() const
0182 {
0183     QHash<QString,QVariant> result;
0184 
0185     result.insert(QStringLiteral("enabled"), enabled());
0186     result.insert(QStringLiteral("visible"), visible());
0187 
0188     return result;
0189 }
0190 
0191 void RenderPlugin::setSettings( const QHash<QString,QVariant> &settings )
0192 {
0193     setEnabled(settings.value(QStringLiteral("enabled"), enabled()).toBool());
0194     setVisible(settings.value(QStringLiteral("visible"), visible()).toBool());
0195 }
0196 
0197 RenderPlugin::RenderType RenderPlugin::renderType() const
0198 {
0199     return UnknownRenderType;
0200 }
0201 
0202 RenderState RenderPlugin::renderState() const
0203 {
0204     return RenderState( name() );
0205 }
0206 
0207 QString RenderPlugin::runtimeTrace() const
0208 {
0209     return name();
0210 }
0211 
0212 bool RenderPlugin::eventFilter( QObject *, QEvent * )
0213 {
0214     return false;
0215 }
0216 
0217 void RenderPlugin::restoreDefaultSettings()
0218 {
0219     setSettings( QHash<QString,QVariant>() );
0220 }
0221 
0222 QStringList RenderPlugin::settingKeys() const
0223 {
0224     return settings().keys();
0225 }
0226 
0227 bool RenderPlugin::setSetting( const QString & key, const QVariant & value )
0228 {
0229     QHash< QString, QVariant> settings = this->settings();
0230     if( settings.contains( key ) )
0231     {
0232         settings [ key ] = value;
0233         setSettings( settings );
0234         return true;
0235     } else {
0236         return false;
0237     }
0238 }
0239 
0240 QVariant RenderPlugin::setting( const QString & name ) const
0241 {
0242     return settings().value( name, QVariant() );
0243 }
0244 
0245 } // namespace Marble
0246 
0247 #include "moc_RenderPlugin.cpp"