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

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2010 Utku Aydin <utkuaydin34@gmail.com>
0004 //
0005 
0006 #include "EarthquakeItem.h"
0007 
0008 #include "MarbleColors.h"
0009 #include "ViewportParams.h"
0010 
0011 #include <QFontMetrics>
0012 #include <QPainter>
0013 #include <QSvgRenderer>
0014 #include <QLocale>
0015 
0016 namespace Marble
0017 {
0018 
0019 // That's the font we will use to paint.
0020 const QFont EarthquakeItem::s_font = QFont( QStringLiteral( "Sans Serif" ), 8, QFont::Bold );
0021 
0022 EarthquakeItem::EarthquakeItem( QObject *parent )
0023     : AbstractDataPluginItem( parent ), m_magnitude( 0.0 ), m_depth( 0.0 )
0024 {
0025     // The size of an item without a text is 0
0026     setSize( QSize( 0, 0 ) );
0027     setCacheMode( ItemCoordinateCache );
0028 }
0029 
0030 EarthquakeItem::~EarthquakeItem()
0031 {
0032     // nothing to do
0033 }
0034 
0035 bool EarthquakeItem::initialized() const
0036 {
0037     return m_magnitude > 0.0;
0038 }
0039 
0040 bool EarthquakeItem::operator<( const AbstractDataPluginItem *other ) const
0041 {
0042     // Larger magnitude first
0043     const EarthquakeItem* item = dynamic_cast<const EarthquakeItem*>( other );
0044     return item ? magnitude() > item->magnitude() : false;
0045 }
0046 
0047 double EarthquakeItem::magnitude() const
0048 {
0049     return m_magnitude;
0050 }
0051 
0052 void EarthquakeItem::setMagnitude( double magnitude )
0053 {
0054     m_magnitude = magnitude;
0055     setSize( QSize( m_magnitude * 10, m_magnitude * 10 ) );
0056     updateTooltip();
0057 }
0058 
0059 void EarthquakeItem::paint( QPainter *painter )
0060 {
0061     // Save the old painter state.
0062     painter->save();
0063 
0064     // Draw the arch into the given rect.
0065     qreal width = magnitude() * 10;
0066     qreal height = magnitude() * 10;
0067 
0068     // Draws the circle with circles' center as rectangle's top-left corner.
0069     QRect arcRect( 0, 0, width, height );
0070     QColor color = Oxygen::brickRed4;
0071     if ( magnitude() < 5.0 ) {
0072         color = Oxygen::sunYellow6;
0073     } else if ( magnitude() < 6.0 ) {
0074         color = Oxygen::hotOrange4;
0075     }
0076     painter->setPen( QPen( Qt::NoPen ) );
0077     QBrush brush( color );
0078     brush.setColor( color );
0079     painter->setBrush( brush );
0080     painter->drawEllipse( arcRect );
0081 
0082     // Draws the seismograph
0083     QSvgRenderer renderer(QStringLiteral(":/seismograph.svg"));
0084     renderer.render( painter, QRectF( 0.0, 0.0, width, height ) );
0085 
0086     // Draws magnitude of the earthquake
0087     QFontMetrics metrics( s_font );
0088     const QString magnitudeText = QLocale::system().toString(m_magnitude);
0089     QRect magnitudeRect = metrics.boundingRect( magnitudeText );
0090     painter->setBrush( QBrush() );
0091     painter->setPen( QPen() );
0092     painter->setFont( s_font );
0093     painter->drawText( QPoint( (arcRect.width() - magnitudeRect.width()) / 2, (arcRect.height() - magnitudeRect.height()) / 2 + metrics.ascent() ), magnitudeText );
0094 
0095     // Restore the old painter state.
0096     painter->restore();
0097 }
0098 
0099 void EarthquakeItem::setDateTime( const QDateTime &dateTime )
0100 {
0101     m_dateTime = dateTime;
0102     updateTooltip();
0103 }
0104 
0105 QDateTime EarthquakeItem::dateTime() const
0106 {
0107     return m_dateTime;
0108 }
0109 
0110 double EarthquakeItem::depth() const
0111 {
0112     return m_depth;
0113 }
0114 
0115 void EarthquakeItem::setDepth( double depth )
0116 {
0117     m_depth = depth;
0118     updateTooltip();
0119 }
0120 
0121 void EarthquakeItem::updateTooltip()
0122 {
0123     QLocale locale = QLocale::system();
0124     QString html = QLatin1String("<table cellpadding=\"2\">");
0125     if ( m_dateTime.isValid() ) {
0126         html += QLatin1String("<tr><td align=\"right\">") + tr("Date:") + QLatin1String("</td><td>") + locale.toString(m_dateTime, QLocale::ShortFormat) + QLatin1String("</td></tr>");
0127     }
0128     html +=
0129         QLatin1String("<tr><td align=\"right\">") + tr("Magnitude:") + QLatin1String("</td><td>") + locale.toString(m_magnitude) + QLatin1String("</td></tr><tr><td align=\"right\">") + tr("Depth:") + QLatin1String("</td><td>") + locale.toString(m_depth) + QLatin1String(" km</td></tr></table>");
0130     setToolTip( html );
0131 }
0132 
0133 }
0134 
0135 #include "moc_EarthquakeItem.cpp"