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 "FrameGraphicsItem.h"
0008 #include "FrameGraphicsItem_p.h"
0009 
0010 // Marble
0011 #include "MarbleDebug.h"
0012 
0013 // Qt
0014 #include <QSizeF>
0015 #include <QPainter>
0016 #include <QPainterPath>
0017 #include <QPixmapCache>
0018 #include <QMargins>
0019 #include <qdrawutil.h>
0020 
0021 using namespace Marble;
0022 
0023 FrameGraphicsItem::FrameGraphicsItem( MarbleGraphicsItem *parent )
0024     : ScreenGraphicsItem(new FrameGraphicsItemPrivate(this, parent))
0025 {
0026     Q_D(FrameGraphicsItem);
0027     d->updateSize();
0028 }
0029 
0030 FrameGraphicsItem::FrameGraphicsItem(FrameGraphicsItemPrivate *dd)
0031     : ScreenGraphicsItem(dd)
0032 {
0033     Q_D(FrameGraphicsItem);
0034     d->updateSize();
0035 }
0036 
0037 FrameGraphicsItem::~FrameGraphicsItem()
0038 {
0039 }
0040 
0041 FrameGraphicsItem::FrameType FrameGraphicsItem::frame() const
0042 {
0043     Q_D(const FrameGraphicsItem);
0044     return d->m_frame;
0045 }
0046 
0047 void FrameGraphicsItem::setFrame( FrameType type )
0048 {
0049     Q_D(FrameGraphicsItem);
0050     d->m_frame = type;
0051     setPadding( padding() );
0052 }
0053 
0054 qreal FrameGraphicsItem::margin() const
0055 {
0056     Q_D(const FrameGraphicsItem);
0057     return d->m_margin;
0058 }
0059 
0060 void FrameGraphicsItem::setMargin( qreal margin )
0061 {
0062     Q_D(FrameGraphicsItem);
0063     d->m_margin = margin;
0064     d->updateSize();
0065     update();
0066 }
0067 
0068 qreal FrameGraphicsItem::marginTop() const
0069 {
0070     Q_D(const FrameGraphicsItem);
0071     return d->m_marginTop;
0072 }
0073 
0074 void FrameGraphicsItem::setMarginTop( qreal marginTop )
0075 {
0076     Q_D(FrameGraphicsItem);
0077     d->m_marginTop = marginTop;
0078     d->updateSize();
0079     update();
0080 }
0081 
0082 qreal FrameGraphicsItem::marginBottom() const
0083 {
0084     Q_D(const FrameGraphicsItem);
0085     return d->m_marginBottom;
0086 }
0087 
0088 void FrameGraphicsItem::setMarginBottom( qreal marginBottom )
0089 {
0090     Q_D(FrameGraphicsItem);
0091     d->m_marginBottom = marginBottom;
0092     d->updateSize();
0093     update();
0094 }
0095 
0096 qreal FrameGraphicsItem::marginLeft() const
0097 {
0098     Q_D(const FrameGraphicsItem);
0099     return d->m_marginLeft;
0100 }
0101 
0102 void FrameGraphicsItem::setMarginLeft( qreal marginLeft )
0103 {
0104     Q_D(FrameGraphicsItem);
0105     d->m_marginLeft = marginLeft;
0106     d->updateSize();
0107     update();
0108 }
0109 
0110 qreal FrameGraphicsItem::marginRight() const
0111 {
0112     Q_D(const FrameGraphicsItem);
0113     return d->m_marginRight;
0114 }
0115 
0116 void FrameGraphicsItem::setMarginRight( qreal marginRight )
0117 {
0118     Q_D(FrameGraphicsItem);
0119     d->m_marginRight = marginRight;
0120     d->updateSize();
0121     update();
0122 }
0123 
0124 qreal FrameGraphicsItem::borderWidth() const
0125 {
0126     Q_D(const FrameGraphicsItem);
0127     return d->m_borderWidth;
0128 }
0129 
0130 void FrameGraphicsItem::setBorderWidth( qreal width )
0131 {
0132     Q_D(FrameGraphicsItem);
0133     d->m_borderWidth = width;
0134     d->updateSize();
0135     update();
0136 }
0137 
0138 qreal FrameGraphicsItem::padding() const
0139 {
0140     Q_D(const FrameGraphicsItem);
0141     return d->m_padding;
0142 }
0143 
0144 void FrameGraphicsItem::setPadding( qreal width )
0145 {
0146     Q_D(FrameGraphicsItem);
0147     if ( width >= 0 ) {
0148         d->m_padding = width;
0149         d->updateSize();
0150     }
0151 }
0152 
0153 QBrush FrameGraphicsItem::borderBrush() const
0154 {
0155     Q_D(const FrameGraphicsItem);
0156     return d->m_borderBrush;
0157 }
0158 
0159 void FrameGraphicsItem::setBorderBrush( const QBrush &brush )
0160 {
0161     Q_D(FrameGraphicsItem);
0162     d->m_borderBrush = brush;
0163     update();
0164 }
0165 
0166 Qt::PenStyle FrameGraphicsItem::borderStyle () const
0167 {
0168     Q_D(const FrameGraphicsItem);
0169     return d->m_borderStyle;
0170 }
0171 
0172 void FrameGraphicsItem::setBorderStyle( Qt::PenStyle style )
0173 {
0174     Q_D(FrameGraphicsItem);
0175     d->m_borderStyle = style;
0176     update();
0177 }
0178 
0179 QBrush FrameGraphicsItem::background() const
0180 {
0181     Q_D(const FrameGraphicsItem);
0182     return d->m_backgroundBrush;
0183 }
0184 
0185 void FrameGraphicsItem::setBackground( const QBrush &background )
0186 {
0187     Q_D(FrameGraphicsItem);
0188     d->m_backgroundBrush = background;
0189     update();
0190 }
0191 
0192 QRectF FrameGraphicsItem::contentRect() const
0193 {
0194     Q_D(const FrameGraphicsItem);
0195     qreal marginTop = ( d->m_marginTop == 0.0 ) ? d->m_margin : d->m_marginTop;
0196     qreal marginLeft = ( d->m_marginLeft == 0.0 ) ? d->m_margin : d->m_marginLeft;
0197 
0198     QRectF contentRect = QRectF( marginLeft + d->m_padding,
0199                                  marginTop + d->m_padding,
0200                                  d->m_contentSize.width(),
0201                                  d->m_contentSize.height() );
0202 
0203     return contentRect;
0204 }
0205 
0206 QSizeF FrameGraphicsItem::contentSize() const
0207 {
0208     Q_D(const FrameGraphicsItem);
0209     return d->m_contentSize;
0210 }
0211 
0212 QRectF FrameGraphicsItem::paintedRect() const
0213 {
0214     Q_D(const FrameGraphicsItem);
0215     qreal marginTop = ( d->m_marginTop == 0.0 ) ? d->m_margin : d->m_marginTop;
0216     qreal marginBottom = ( d->m_marginBottom == 0.0 ) ? d->m_margin : d->m_marginBottom;
0217     qreal marginLeft = ( d->m_marginLeft == 0.0 ) ? d->m_margin : d->m_marginLeft;
0218     qreal marginRight = ( d->m_marginRight == 0.0 ) ? d->m_margin : d->m_marginRight;
0219 
0220     QSizeF size = this->size();
0221 
0222     QRectF paintedRect = QRectF( marginLeft, marginTop,
0223                                  size.width() - ( marginLeft + marginRight ),
0224                                  size.height() - ( marginTop + marginBottom ) );
0225     return paintedRect;
0226 }
0227 
0228 void FrameGraphicsItem::setContentSize( const QSizeF& size )
0229 {
0230     Q_D(FrameGraphicsItem);
0231     d->m_contentSize = size;
0232     d->updateSize();
0233 }
0234 
0235 QPainterPath FrameGraphicsItem::backgroundShape() const
0236 {
0237     Q_D(const FrameGraphicsItem);
0238     QPainterPath path;
0239     if ( d->m_frame == RectFrame || d->m_frame == ShadowFrame ) {
0240         QRectF renderedRect = paintedRect();
0241         path.addRect( QRectF( 0.0, 0.0, renderedRect.size().width(), renderedRect.size().height() ) );
0242     }
0243     else if ( d->m_frame == RoundedRectFrame ) {
0244         QSizeF paintedSize = paintedRect().size();
0245         path.addRoundedRect( QRectF( 0.0, 0.0, paintedSize.width() - 1, paintedSize.height() - 1 ),
0246                              6, 6 );
0247     }
0248     return path;
0249 }
0250 
0251 void FrameGraphicsItem::paintBackground( QPainter *painter )
0252 {
0253     Q_D(FrameGraphicsItem);
0254     painter->save();
0255     painter->setPen( QPen( d->m_borderBrush, d->m_borderWidth, d->m_borderStyle ) );
0256     painter->setBrush( d->m_backgroundBrush );
0257     painter->drawPath( backgroundShape() );
0258 
0259     painter->restore();
0260 }
0261 
0262 void FrameGraphicsItem::paint( QPainter *painter )
0263 {
0264     Q_D(FrameGraphicsItem);
0265     painter->save();
0266 
0267     // Needs to be done here cause we don't want the margin translation
0268     if ( frame() == ShadowFrame )
0269     {
0270         QPixmap shadow;
0271         if ( !QPixmapCache::find( "marble/frames/shadowframe.png", &shadow ) ) {
0272             shadow = QPixmap(QStringLiteral(":/marble/frames/shadowframe.png"));
0273             QPixmapCache::insert( "marble/frames/shadowframe.png", shadow );
0274         }
0275         qDrawBorderPixmap( painter, QRect( QPoint( 0, 0 ), size().toSize() ),
0276                            QMargins( 10, 10, 10, 10 ), shadow );
0277     }
0278 
0279     painter->translate( paintedRect().topLeft() );
0280     paintBackground( painter );
0281     painter->translate( d->m_padding, d->m_padding );
0282     paintContent( painter );
0283     painter->restore();
0284 }
0285 
0286 void FrameGraphicsItem::paintContent( QPainter *painter )
0287 {
0288     Q_UNUSED( painter )
0289 }