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 <QMargins>
15#include <QPainter>
16#include <QPainterPath>
17#include <QPixmapCache>
18#include <QSizeF>
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() = default;
38
39FrameGraphicsItem::FrameType FrameGraphicsItem::frame() const
40{
41 Q_D(const FrameGraphicsItem);
42 return d->m_frame;
43}
44
45void FrameGraphicsItem::setFrame(FrameType type)
46{
47 Q_D(FrameGraphicsItem);
48 d->m_frame = type;
49 setPadding(padding());
50}
51
52qreal FrameGraphicsItem::margin() const
53{
54 Q_D(const FrameGraphicsItem);
55 return d->m_margin;
56}
57
58void FrameGraphicsItem::setMargin(qreal margin)
59{
60 Q_D(FrameGraphicsItem);
61 d->m_margin = margin;
62 d->updateSize();
63 update();
64}
65
66qreal FrameGraphicsItem::marginTop() const
67{
68 Q_D(const FrameGraphicsItem);
69 return d->m_marginTop;
70}
71
72void FrameGraphicsItem::setMarginTop(qreal marginTop)
73{
74 Q_D(FrameGraphicsItem);
75 d->m_marginTop = marginTop;
76 d->updateSize();
77 update();
78}
79
80qreal FrameGraphicsItem::marginBottom() const
81{
82 Q_D(const FrameGraphicsItem);
83 return d->m_marginBottom;
84}
85
86void FrameGraphicsItem::setMarginBottom(qreal marginBottom)
87{
88 Q_D(FrameGraphicsItem);
89 d->m_marginBottom = marginBottom;
90 d->updateSize();
91 update();
92}
93
94qreal FrameGraphicsItem::marginLeft() const
95{
96 Q_D(const FrameGraphicsItem);
97 return d->m_marginLeft;
98}
99
100void FrameGraphicsItem::setMarginLeft(qreal marginLeft)
101{
102 Q_D(FrameGraphicsItem);
103 d->m_marginLeft = marginLeft;
104 d->updateSize();
105 update();
106}
107
108qreal FrameGraphicsItem::marginRight() const
109{
110 Q_D(const FrameGraphicsItem);
111 return d->m_marginRight;
112}
113
114void FrameGraphicsItem::setMarginRight(qreal marginRight)
115{
116 Q_D(FrameGraphicsItem);
117 d->m_marginRight = marginRight;
118 d->updateSize();
119 update();
120}
121
122qreal FrameGraphicsItem::borderWidth() const
123{
124 Q_D(const FrameGraphicsItem);
125 return d->m_borderWidth;
126}
127
128void FrameGraphicsItem::setBorderWidth(qreal width)
129{
130 Q_D(FrameGraphicsItem);
131 d->m_borderWidth = width;
132 d->updateSize();
133 update();
134}
135
136qreal FrameGraphicsItem::padding() const
137{
138 Q_D(const FrameGraphicsItem);
139 return d->m_padding;
140}
141
142void FrameGraphicsItem::setPadding(qreal width)
143{
144 Q_D(FrameGraphicsItem);
145 if (width >= 0) {
146 d->m_padding = width;
147 d->updateSize();
148 }
149}
150
151QBrush FrameGraphicsItem::borderBrush() const
152{
153 Q_D(const FrameGraphicsItem);
154 return d->m_borderBrush;
155}
156
157void FrameGraphicsItem::setBorderBrush(const QBrush &brush)
158{
159 Q_D(FrameGraphicsItem);
160 d->m_borderBrush = brush;
161 update();
162}
163
164Qt::PenStyle FrameGraphicsItem::borderStyle() const
165{
166 Q_D(const FrameGraphicsItem);
167 return d->m_borderStyle;
168}
169
170void FrameGraphicsItem::setBorderStyle(Qt::PenStyle style)
171{
172 Q_D(FrameGraphicsItem);
173 d->m_borderStyle = style;
174 update();
175}
176
177QBrush FrameGraphicsItem::background() const
178{
179 Q_D(const FrameGraphicsItem);
180 return d->m_backgroundBrush;
181}
182
183void FrameGraphicsItem::setBackground(const QBrush &background)
184{
185 Q_D(FrameGraphicsItem);
186 d->m_backgroundBrush = background;
187 update();
188}
189
190QRectF FrameGraphicsItem::contentRect() const
191{
192 Q_D(const FrameGraphicsItem);
193 qreal marginTop = (d->m_marginTop == 0.0) ? d->m_margin : d->m_marginTop;
194 qreal marginLeft = (d->m_marginLeft == 0.0) ? d->m_margin : d->m_marginLeft;
195
196 QRectF contentRect = QRectF(marginLeft + d->m_padding, marginTop + d->m_padding, d->m_contentSize.width(), d->m_contentSize.height());
197
198 return contentRect;
199}
200
201QSizeF FrameGraphicsItem::contentSize() const
202{
203 Q_D(const FrameGraphicsItem);
204 return d->m_contentSize;
205}
206
207QRectF FrameGraphicsItem::paintedRect() const
208{
209 Q_D(const FrameGraphicsItem);
210 qreal marginTop = (d->m_marginTop == 0.0) ? d->m_margin : d->m_marginTop;
211 qreal marginBottom = (d->m_marginBottom == 0.0) ? d->m_margin : d->m_marginBottom;
212 qreal marginLeft = (d->m_marginLeft == 0.0) ? d->m_margin : d->m_marginLeft;
213 qreal marginRight = (d->m_marginRight == 0.0) ? d->m_margin : d->m_marginRight;
214
215 QSizeF size = this->size();
216
217 QRectF paintedRect = QRectF(marginLeft, marginTop, size.width() - (marginLeft + marginRight), size.height() - (marginTop + marginBottom));
218 return paintedRect;
219}
220
221void FrameGraphicsItem::setContentSize(const QSizeF &size)
222{
223 Q_D(FrameGraphicsItem);
224 d->m_contentSize = size;
225 d->updateSize();
226}
227
228QPainterPath FrameGraphicsItem::backgroundShape() const
229{
230 Q_D(const FrameGraphicsItem);
232 if (d->m_frame == RectFrame || d->m_frame == ShadowFrame) {
233 QRectF renderedRect = paintedRect();
234 path.addRect(QRectF(0.0, 0.0, renderedRect.size().width(), renderedRect.size().height()));
235 } else if (d->m_frame == RoundedRectFrame) {
236 QSizeF paintedSize = paintedRect().size();
237 path.addRoundedRect(QRectF(0.0, 0.0, paintedSize.width() - 1, paintedSize.height() - 1), 6, 6);
238 }
239 return path;
240}
241
242void FrameGraphicsItem::paintBackground(QPainter *painter)
243{
244 Q_D(FrameGraphicsItem);
245 painter->save();
246 painter->setPen(QPen(d->m_borderBrush, d->m_borderWidth, d->m_borderStyle));
247 painter->setBrush(d->m_backgroundBrush);
248 painter->drawPath(backgroundShape());
249
250 painter->restore();
251}
252
253void FrameGraphicsItem::paint(QPainter *painter)
254{
255 Q_D(FrameGraphicsItem);
256 painter->save();
257
258 // Needs to be done here cause we don't want the margin translation
259 if (frame() == ShadowFrame) {
260 QPixmap shadow;
261 if (!QPixmapCache::find(QStringLiteral("marble/frames/shadowframe.png"), &shadow)) {
262 shadow = QPixmap(QStringLiteral(":/marble/frames/shadowframe.png"));
263 QPixmapCache::insert(QStringLiteral("marble/frames/shadowframe.png"), shadow);
264 }
265 qDrawBorderPixmap(painter, QRect(QPoint(0, 0), size().toSize()), QMargins(10, 10, 10, 10), shadow);
266 }
267
268 painter->translate(paintedRect().topLeft());
269 paintBackground(painter);
270 painter->translate(d->m_padding, d->m_padding);
271 paintContent(painter);
272 painter->restore();
273}
274
275void FrameGraphicsItem::paintContent(QPainter *painter)
276{
277 Q_UNUSED(painter)
278}
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 Mon Nov 4 2024 16:37:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.