File indexing completed on 2024-05-05 03:49:47

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0004 //
0005 
0006 // Self
0007 #include "LabelGraphicsItem.h"
0008 #include "LabelGraphicsItem_p.h"
0009 
0010 // Qt
0011 #include <QString>
0012 #include <QApplication>
0013 #include <QFont>
0014 #include <QPainter>
0015 
0016 using namespace Marble;
0017 
0018 LabelGraphicsItemPrivate::LabelGraphicsItemPrivate(LabelGraphicsItem *labelGraphicsItem,
0019                                                    MarbleGraphicsItem *parent)
0020     : FrameGraphicsItemPrivate(labelGraphicsItem, parent)
0021 {
0022 }
0023 
0024 QFont LabelGraphicsItemPrivate::font()
0025 {
0026     return QApplication::font();
0027 }
0028 
0029 void LabelGraphicsItem::setContentSize( const QSizeF &contentSize )
0030 {
0031     Q_D(LabelGraphicsItem);
0032     QSizeF updatedSize = contentSize;
0033     if ( updatedSize.isEmpty() ) {
0034         updatedSize.setHeight( 0 );
0035         updatedSize.setWidth( 0 );
0036     }
0037     else {
0038         if ( d->m_minimumSize.width() > updatedSize.width() ) {
0039             updatedSize.setWidth( d->m_minimumSize.width() );
0040         }
0041         if ( d->m_minimumSize.height() > updatedSize.height() ) {
0042             updatedSize.setHeight( d->m_minimumSize.height() );
0043         }
0044     }
0045 
0046     FrameGraphicsItem::setContentSize( updatedSize );
0047 }
0048 
0049 // ----------------------------------------------------------------
0050 
0051 LabelGraphicsItem::LabelGraphicsItem( MarbleGraphicsItem *parent )
0052     : FrameGraphicsItem(new LabelGraphicsItemPrivate(this, parent))
0053 {
0054 }
0055 
0056 LabelGraphicsItem::~LabelGraphicsItem()
0057 {
0058 }
0059 
0060 QString LabelGraphicsItem::text() const
0061 {
0062     Q_D(const LabelGraphicsItem);
0063     return d->m_text;
0064 }
0065 
0066 void LabelGraphicsItem::setText( const QString& text )
0067 {
0068     Q_D(LabelGraphicsItem);
0069     clear();
0070     d->m_text = text;
0071     QFontMetrics metrics( d->font() );
0072     QSizeF size = metrics.boundingRect( text ).size() + QSizeF( 14, 2 );
0073     setContentSize( size );
0074 }
0075 
0076 QImage LabelGraphicsItem::image() const
0077 {
0078     Q_D(const LabelGraphicsItem);
0079     return d->m_image;
0080 }
0081 
0082 void LabelGraphicsItem::setImage( const QImage& image, const QSize& size )
0083 {
0084     Q_D(LabelGraphicsItem);
0085     clear();
0086     d->m_image = image;
0087     if ( size.isEmpty() ) {
0088         setContentSize( image.size() );
0089     }
0090     else {
0091         setContentSize( size );
0092     }
0093 }
0094 
0095 QIcon LabelGraphicsItem::icon() const
0096 {
0097     Q_D(const LabelGraphicsItem);
0098     return d->m_icon;
0099 }
0100 
0101 void LabelGraphicsItem::setIcon( const QIcon& icon, const QSize& size )
0102 {
0103     Q_D(LabelGraphicsItem);
0104     clear();
0105     d->m_icon = icon;
0106     setContentSize( size );
0107 }
0108 
0109 QSizeF LabelGraphicsItem::minimumSize() const
0110 {
0111     Q_D(const LabelGraphicsItem);
0112     return d->m_minimumSize;
0113 }
0114 
0115 void LabelGraphicsItem::setMinimumSize( const QSizeF& size )
0116 {
0117     Q_D(LabelGraphicsItem);
0118     const QSizeF oldContentSize = contentSize();
0119     d->m_minimumSize = size;
0120     setContentSize( oldContentSize );
0121 }
0122 
0123 void LabelGraphicsItem::clear()
0124 {
0125     Q_D(LabelGraphicsItem);
0126     d->m_text.clear();
0127     d->m_image = QImage();
0128     d->m_icon = QIcon();
0129     setContentSize( QSizeF( 0.0, 0.0 ) );
0130 }
0131 
0132 void LabelGraphicsItem::paintContent( QPainter *painter )
0133 {
0134     Q_D(LabelGraphicsItem);
0135     painter->save();
0136 
0137     if ( !d->m_text.isNull() ) {
0138         painter->setFont( d->font() );
0139         painter->setPen( QColor( Qt::black ) );
0140         painter->drawText( QRect( QPoint( 0, 0 ), contentSize().toSize() ),
0141                            Qt::AlignVCenter | Qt::AlignLeft,
0142                            d->m_text );
0143     }
0144     else if ( !d->m_image.isNull() ) {
0145         painter->drawImage( QRectF( QPointF( 0, 0 ), contentSize() ),
0146                             d->m_image );
0147     }
0148     else if ( !d->m_icon.isNull() ) {
0149         d->m_icon.paint( painter,
0150                          QRect( QPoint( 0, 0 ), contentSize().toSize() ),
0151                          Qt::AlignCenter );
0152     }
0153 
0154     painter->restore();
0155 }