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

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Daniel Marth <danielmarth@gmx.at>
0004 //
0005 
0006 #include "OpenCachingItem.h"
0007 #include "GeoPainter.h"
0008 #include "ViewportParams.h"
0009 
0010 #include <QFontMetrics>
0011 
0012 #include "ui_OpenCachingCacheDialog.h"
0013 
0014 namespace Marble
0015 {
0016 
0017 // That's the font we will use to paint.
0018 QFont OpenCachingItem::s_font = QFont( QStringLiteral( "Sans Serif" ), 8 );
0019 QPixmap OpenCachingItem::s_icon = QPixmap( "/usr/share/icons/oxygen/32x32/status/folder-open.png" );
0020 
0021 OpenCachingItem::OpenCachingItem( const OpenCachingCache& cache, QObject *parent )
0022     : AbstractDataPluginItem( parent ), m_cache( cache ), m_infoDialog( 0), m_action( new QAction( this ) ), m_logIndex( 0 )
0023 {
0024     // The size of an item without a text is 0
0025     setSize( QSize( m_cache.difficulty() * 10, m_cache.difficulty() * 10 ) );
0026     s_font.setBold( true );
0027     updateTooltip();
0028     setId(QLatin1String("opencache-") + cache.id());
0029     setCoordinate( GeoDataCoordinates( cache.longitude(), cache.latitude(), 0.0, GeoDataCoordinates::Degree ) );
0030     setTarget( "earth" );
0031     connect( m_action, SIGNAL(triggered()),
0032              this, SLOT(showInfoDialog()) );
0033 }
0034 
0035 OpenCachingItem::~OpenCachingItem()
0036 {
0037     // nothing to do
0038 }
0039 
0040 QString OpenCachingItem::itemType() const
0041 {
0042     // Our itemType:
0043     return "OpenCachingItem";
0044 }
0045 
0046 bool OpenCachingItem::initialized()
0047 {
0048     return m_cache.id() != 0;
0049 }
0050 
0051 void OpenCachingItem::showInfoDialog()
0052 {
0053     if( !m_infoDialog ) {
0054         m_infoDialog = infoDialog();
0055     }
0056     Q_ASSERT( m_infoDialog );
0057     m_infoDialog->show();
0058 }
0059 
0060 QDialog *OpenCachingItem::infoDialog()
0061 {
0062     if ( !m_infoDialog ) {
0063         // Initializing information dialog
0064         m_infoDialog = new QDialog();
0065         m_ui = new Ui::OpenCachingCacheDialog;
0066         m_ui->setupUi( m_infoDialog );
0067         m_ui->m_cacheName->setText( m_cache.cacheName() );
0068         m_ui->m_country->setText( m_cache.country() );
0069         m_ui->m_dateCreated->setText( m_cache.dateCreated().toString( Qt::SystemLocaleShortDate ) );
0070         m_ui->m_dateHidden->setText( m_cache.dateHidden().toString( Qt::SystemLocaleShortDate ) );
0071         m_ui->m_dateLastModified->setText( m_cache.dateCreated().toString( Qt::SystemLocaleShortDate ) );
0072         m_ui->m_difficulty->setText( QString::number( m_cache.difficulty() ) );
0073         m_ui->m_latitude->setText( QString::number( m_cache.latitude() ) );
0074         m_ui->m_longitude->setText( QString::number( m_cache.longitude() ) );
0075         m_ui->m_size->setText( m_cache.sizeString() );
0076         m_ui->m_status->setText( m_cache.status() );
0077         m_ui->m_terrain->setText( QString::number( m_cache.terrain() ) );
0078         m_ui->m_type->setText( m_cache.cacheType() );
0079         m_ui->m_userName->setText( m_cache.userName() );
0080         QHash<QString, OpenCachingCacheDescription> descriptions = m_cache.description();
0081         QStringList languages = descriptions.keys();
0082         qSort( languages );
0083         m_ui->m_languageBox->addItems( languages );
0084         if( descriptions.size() > 0 ) {
0085             updateDescriptionLanguage( languages.first() );
0086             connect( m_ui->m_languageBox, SIGNAL(currentIndexChanged(QString)),
0087                      this, SLOT(updateDescriptionLanguage(QString)) );
0088         }
0089         OpenCachingCacheLog log = m_cache.log();
0090         if( log.size() > 0 ) {
0091             m_ui->m_logText->setHtml( log[m_logIndex].text() );
0092             m_ui->m_logCount->setText(QLatin1String("1 / ") + QString::number(log.size()));
0093             connect( m_ui->m_nextButton, SIGNAL(clicked()),
0094                      this, SLOT(nextLogEntry()) );
0095             connect( m_ui->m_previousButton, SIGNAL(clicked()),
0096                      this, SLOT(previousLogEntry()) );
0097         }
0098         if( log.size() > 1 ) {
0099             m_ui->m_nextButton->setEnabled( true );
0100         }
0101         QPushButton *closeButton = m_ui->m_buttonBox->button( QDialogButtonBox::Close );
0102         connect( closeButton, SIGNAL(clicked()),
0103                  m_infoDialog, SLOT(close()) );
0104         m_infoDialog->setWindowTitle( m_cache.cacheName() );
0105     }
0106     return m_infoDialog;
0107 }
0108 
0109 QAction *OpenCachingItem::action()
0110 {
0111     m_action->setText( m_cache.cacheName() );
0112     return m_action;
0113 }
0114 
0115 bool OpenCachingItem::operator<( const AbstractDataPluginItem *other ) const
0116 {
0117     // FIXME shorter distance to current position?
0118     const OpenCachingItem* item = dynamic_cast<const OpenCachingItem*>( other );
0119     return item ? m_cache.id() < item->m_cache.id() : false;
0120 }
0121 
0122 void OpenCachingItem::paint( GeoPainter *painter, ViewportParams *viewport,
0123                             const QString& renderPos, GeoSceneLayer * layer )
0124 {
0125     Q_UNUSED( viewport )
0126     Q_UNUSED( renderPos )
0127     Q_UNUSED( layer )
0128 
0129     // Save the old painter state.
0130     painter->save();
0131     painter->autoMapQuality();
0132 
0133     qreal width;
0134     qreal height;
0135 
0136     if( !s_icon.isNull() ) {
0137         width = s_icon.width();
0138         height = s_icon.height();
0139         painter->drawPixmap( 0, 0, s_icon );
0140     }
0141     else {
0142         width = m_cache.difficulty() * 10;
0143         height = m_cache.difficulty() * 10;
0144         // Draws the circle with circles' center as rectangle's top-left corner.
0145         QRect arcRect( 0, 0, width, height );
0146         QColor color = Oxygen::brickRed4;
0147         if ( m_cache.difficulty() < 2.0 ) {
0148             color = Oxygen::sunYellow6;
0149         } else if ( m_cache.difficulty() < 4.0 ) {
0150             color = Oxygen::hotOrange4;
0151         }
0152         painter->setPen( QPen( Qt::NoPen ) );
0153         QBrush brush( color );
0154         brush.setColor( color );
0155         painter->setBrush( brush );
0156         painter->drawEllipse( arcRect );
0157     }
0158 
0159     // Draws difficulty of the cache
0160     QFontMetrics metrics( s_font );
0161     QString difficultyText = QString::number( m_cache.difficulty() );
0162     QRect difficultyRect = metrics.boundingRect( difficultyText );
0163     painter->setBrush( QBrush() );
0164     painter->setPen( QPen() );
0165     painter->setFont( s_font );
0166     painter->drawText( QPoint( (width - difficultyRect.width()) / 2, (height - difficultyRect.height()) / 2 + metrics.ascent() ), difficultyText );
0167 
0168     // Restore the old painter state.
0169     painter->restore();
0170 }
0171 
0172 void OpenCachingItem::updateTooltip()
0173 {
0174     QString html = QLatin1String("<table cellpadding=\"2\">");
0175     if ( m_cache.id() != 0 ) {
0176         html += tr("<tr><td align=\"right\">Cache name</td>") +
0177             QLatin1String("<td>") + m_cache.cacheName() + QLatin1String("</td></tr>") +
0178             tr("<tr><td align=\"right\">User name</td><td>") + m_cache.userName() + QLatin1String("</td></tr>");
0179         if ( !m_cache.cacheName().isEmpty() ) {
0180             html += tr("<tr><td align=\"right\">Date hidden</td><td>") + m_cache.dateHidden().toString(Qt::SystemLocaleShortDate) + QLatin1String("</td></tr>");
0181         }
0182         html +=
0183             tr("<tr><td align=\"right\">Difficulty</td><td>") + QString::number(m_cache.difficulty()) + QLatin1String("</td></tr>") +
0184             tr("<tr><td align=\"right\">Size</td><td>") + m_cache.sizeString() + QLatin1String("</td></tr>") +
0185             QLatin1String("</table>");
0186         setToolTip( html );
0187     }
0188 }
0189 
0190 void OpenCachingItem::updateDescriptionLanguage( const QString& language )
0191 {
0192     QHash<QString, OpenCachingCacheDescription> descriptions = m_cache.description();
0193     const QString text = descriptions[language].shortDescription() + QLatin1String("\n\n") +
0194             descriptions[language].description() + QLatin1String("\n\n") +
0195             descriptions[language].hint() + QLatin1String("\n\n");
0196     m_ui->m_descriptionText->setHtml( text );
0197 }
0198 
0199 void OpenCachingItem::nextLogEntry()
0200 {
0201     OpenCachingCacheLog log = m_cache.log();
0202     if( m_logIndex + 1 < log.size() ) {
0203         m_logIndex++;
0204         m_ui->m_logText->setHtml( log[m_logIndex].text() );
0205         m_ui->m_logCount->setText(QString::number(m_logIndex + 1) + QLatin1String(" / ") + QString::number(log.size()));
0206         m_ui->m_previousButton->setEnabled( true );
0207         if( m_logIndex == log.size() - 1 ) {
0208             m_ui->m_nextButton->setEnabled( false );
0209         }
0210     }
0211 }
0212 
0213 void OpenCachingItem::previousLogEntry()
0214 {
0215     OpenCachingCacheLog log = m_cache.log();
0216     if( m_logIndex - 1 >= 0 ) {
0217         m_logIndex--;
0218         m_ui->m_logText->setHtml( log[m_logIndex].text() );
0219         m_ui->m_logCount->setText(QString::number(m_logIndex + 1) + QLatin1String(" / ") + QString::number(log.size()));
0220         m_ui->m_nextButton->setEnabled( true );
0221         if( m_logIndex == 0 ) {
0222             m_ui->m_previousButton->setEnabled( false );
0223         }
0224     }
0225 }
0226 
0227 }
0228 
0229 #include "moc_OpenCachingItem.cpp"