Marble

FrameGraphicsItem.cpp
1// SPDX-License-Identifier: LGPL-2.1-or-later
2//
3// SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
4//
5
6// Self
7#include "FrameGraphicsItem.h"
8#include "FrameGraphicsItem_p.h"
9
10// Marble
11#include "MarbleDebug.h"
12
13// Qt
14#include <QSizeF>
15#include <QPainter>
16#include <QPainterPath>
17#include <QPixmapCache>
18#include <QMargins>
19#include <qdrawutil.h>
20
21using namespace Marble;
22
23FrameGraphicsItem::FrameGraphicsItem( MarbleGraphicsItem *parent )
24 : ScreenGraphicsItem(new FrameGraphicsItemPrivate(this, parent))
25{
26 Q_D(FrameGraphicsItem);
27 d->updateSize();
28}
29
30FrameGraphicsItem::FrameGraphicsItem(FrameGraphicsItemPrivate *dd)
31 : ScreenGraphicsItem(dd)
32{
33 Q_D(FrameGraphicsItem);
34 d->updateSize();
35}
36
37FrameGraphicsItem::~FrameGraphicsItem()
38{
39}
40
41FrameGraphicsItem::FrameType FrameGraphicsItem::frame() const
42{
43 Q_D(const FrameGraphicsItem);
44 return d->m_frame;
45}
46
47void FrameGraphicsItem::setFrame( FrameType type )
48{
49 Q_D(FrameGraphicsItem);
50 d->m_frame = type;
51 setPadding( padding() );
52}
53
54qreal FrameGraphicsItem::margin() const
55{
56 Q_D(const FrameGraphicsItem);
57 return d->m_margin;
58}
59
60void FrameGraphicsItem::setMargin( qreal margin )
61{
62 Q_D(FrameGraphicsItem);
63 d->m_margin = margin;
64 d->updateSize();
65 update();
66}
67
68qreal FrameGraphicsItem::marginTop() const
69{
70 Q_D(const FrameGraphicsItem);
71 return d->m_marginTop;
72}
73
74void FrameGraphicsItem::setMarginTop( qreal marginTop )
75{
76 Q_D(FrameGraphicsItem);
77 d->m_marginTop = marginTop;
78 d->updateSize();
79 update();
80}
81
82qreal FrameGraphicsItem::marginBottom() const
83{
84 Q_D(const FrameGraphicsItem);
85 return d->m_marginBottom;
86}
87
88void FrameGraphicsItem::setMarginBottom( qreal marginBottom )
89{
90 Q_D(FrameGraphicsItem);
91 d->m_marginBottom = marginBottom;
92 d->updateSize();
93 update();
94}
95
96qreal FrameGraphicsItem::marginLeft() const
97{
98 Q_D(const FrameGraphicsItem);
99 return d->m_marginLeft;
100}
101
102void FrameGraphicsItem::setMarginLeft( qreal marginLeft )
103{
104 Q_D(FrameGraphicsItem);
105 d->m_marginLeft = marginLeft;
106 d->updateSize();
107 update();
108}
109
110qreal FrameGraphicsItem::marginRight() const
111{
112 Q_D(const FrameGraphicsItem);
113 return d->m_marginRight;
114}
115
116void FrameGraphicsItem::setMarginRight( qreal marginRight )
117{
118 Q_D(FrameGraphicsItem);
119 d->m_marginRight = marginRight;
120 d->updateSize();
121 update();
122}
123
124qreal FrameGraphicsItem::borderWidth() const
125{
126 Q_D(const FrameGraphicsItem);
127 return d->m_borderWidth;
128}
129
130void FrameGraphicsItem::setBorderWidth( qreal width )
131{
132 Q_D(FrameGraphicsItem);
133 d->m_borderWidth = width;
134 d->updateSize();
135 update();
136}
137
138qreal FrameGraphicsItem::padding() const
139{
140 Q_D(const FrameGraphicsItem);
141 return d->m_padding;
142}
143
144void FrameGraphicsItem::setPadding( qreal width )
145{
146 Q_D(FrameGraphicsItem);
147 if ( width >= 0 ) {
148 d->m_padding = width;
149 d->updateSize();
150 }
151}
152
153QBrush FrameGraphicsItem::borderBrush() const
154{
155 Q_D(const FrameGraphicsItem);
156 return d->m_borderBrush;
157}
158
159void FrameGraphicsItem::setBorderBrush( const QBrush &brush )
160{
161 Q_D(FrameGraphicsItem);
162 d->m_borderBrush = brush;
163 update();
164}
165
166Qt::PenStyle FrameGraphicsItem::borderStyle () const
167{
168 Q_D(const FrameGraphicsItem);
169 return d->m_borderStyle;
170}
171
172void FrameGraphicsItem::setBorderStyle( Qt::PenStyle style )
173{
174 Q_D(FrameGraphicsItem);
175 d->m_borderStyle = style;
176 update();
177}
178
179QBrush FrameGraphicsItem::background() const
180{
181 Q_D(const FrameGraphicsItem);
182 return d->m_backgroundBrush;
183}
184
185void FrameGraphicsItem::setBackground( const QBrush &background )
186{
187 Q_D(FrameGraphicsItem);
188 d->m_backgroundBrush = background;
189 update();
190}
191
192QRectF FrameGraphicsItem::contentRect() const
193{
194 Q_D(const FrameGraphicsItem);
195 qreal marginTop = ( d->m_marginTop == 0.0 ) ? d->m_margin : d->m_marginTop;
196 qreal marginLeft = ( d->m_marginLeft == 0.0 ) ? d->m_margin : d->m_marginLeft;
197
198 QRectF contentRect = QRectF( marginLeft + d->m_padding,
199 marginTop + d->m_padding,
200 d->m_contentSize.width(),
201 d->m_contentSize.height() );
202
203 return contentRect;
204}
205
206QSizeF FrameGraphicsItem::contentSize() const
207{
208 Q_D(const FrameGraphicsItem);
209 return d->m_contentSize;
210}
211
212QRectF FrameGraphicsItem::paintedRect() const
213{
214 Q_D(const FrameGraphicsItem);
215 qreal marginTop = ( d->m_marginTop == 0.0 ) ? d->m_margin : d->m_marginTop;
216 qreal marginBottom = ( d->m_marginBottom == 0.0 ) ? d->m_margin : d->m_marginBottom;
217 qreal marginLeft = ( d->m_marginLeft == 0.0 ) ? d->m_margin : d->m_marginLeft;
218 qreal marginRight = ( d->m_marginRight == 0.0 ) ? d->m_margin : d->m_marginRight;
219
220 QSizeF size = this->size();
221
222 QRectF paintedRect = QRectF( marginLeft, marginTop,
223 size.width() - ( marginLeft + marginRight ),
224 size.height() - ( marginTop + marginBottom ) );
225 return paintedRect;
226}
227
228void FrameGraphicsItem::setContentSize( const QSizeF& size )
229{
230 Q_D(FrameGraphicsItem);
231 d->m_contentSize = size;
232 d->updateSize();
233}
234
235QPainterPath FrameGraphicsItem::backgroundShape() const
236{
237 Q_D(const FrameGraphicsItem);
239 if ( d->m_frame == RectFrame || d->m_frame == ShadowFrame ) {
240 QRectF renderedRect = paintedRect();
241 path.addRect( QRectF( 0.0, 0.0, renderedRect.size().width(), renderedRect.size().height() ) );
242 }
243 else if ( d->m_frame == RoundedRectFrame ) {
244 QSizeF paintedSize = paintedRect().size();
245 path.addRoundedRect( QRectF( 0.0, 0.0, paintedSize.width() - 1, paintedSize.height() - 1 ),
246 6, 6 );
247 }
248 return path;
249}
250
251void FrameGraphicsItem::paintBackground( QPainter *painter )
252{
253 Q_D(FrameGraphicsItem);
254 painter->save();
255 painter->setPen( QPen( d->m_borderBrush, d->m_borderWidth, d->m_borderStyle ) );
256 painter->setBrush( d->m_backgroundBrush );
257 painter->drawPath( backgroundShape() );
258
259 painter->restore();
260}
261
262void FrameGraphicsItem::paint( QPainter *painter )
263{
264 Q_D(FrameGraphicsItem);
265 painter->save();
266
267 // Needs to be done here cause we don't want the margin translation
268 if ( frame() == ShadowFrame )
269 {
270 QPixmap shadow;
271 if ( !QPixmapCache::find( "marble/frames/shadowframe.png", &shadow ) ) {
272 shadow = QPixmap(QStringLiteral(":/marble/frames/shadowframe.png"));
273 QPixmapCache::insert( "marble/frames/shadowframe.png", shadow );
274 }
275 qDrawBorderPixmap( painter, QRect( QPoint( 0, 0 ), size().toSize() ),
276 QMargins( 10, 10, 10, 10 ), shadow );
277 }
278
279 painter->translate( paintedRect().topLeft() );
280 paintBackground( painter );
281 painter->translate( d->m_padding, d->m_padding );
282 paintContent( painter );
283 painter->restore();
284}
285
286void FrameGraphicsItem::paintContent( QPainter *painter )
287{
288 Q_UNUSED( painter )
289}
QString path(const QString &relativePath)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
Binds a QML item to a specific geodetic location in screen coordinates.
void drawPath(const QPainterPath &path)
void restore()
void save()
void setBrush(Qt::BrushStyle style)
void setPen(Qt::PenStyle style)
void translate(const QPoint &offset)
bool find(const Key &key, QPixmap *pixmap)
Key insert(const QPixmap &pixmap)
QSizeF size() const const
qreal height() const const
qreal width() const const
PenStyle
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jun 7 2024 12:03:13 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.