KChart

KChartAbstractAreaBase.cpp
1/*
2 * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
3 *
4 * This file is part of the KD Chart library.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "KChartAbstractAreaBase_p.h"
10
11#include <KChartBackgroundAttributes.h>
12#include <KChartFrameAttributes.h>
13#include <KChartTextAttributes.h>
14#include "KChartPainterSaver_p.h"
15#include "KChartPrintingParameters.h"
16#include "KChartMath_p.h"
17
18#include <QPainter>
19#include <QPainterPath>
20
21
22using namespace KChart;
23
24AbstractAreaBase::Private::Private() :
25 visible( true )
26{
27 init();
28}
29
30
31AbstractAreaBase::Private::~Private() {}
32
33
34void AbstractAreaBase::Private::init()
35{
36}
37
38
39AbstractAreaBase::AbstractAreaBase() :
40 _d( new Private() )
41{
42}
43
44AbstractAreaBase::~AbstractAreaBase()
45{
46 delete _d; _d = nullptr;
47}
48
49
50void AbstractAreaBase::init()
51{
52}
53
54
55#define d d_func()
56
58{
59 if ( other == this ) return true;
60 if ( !other ) {
61 return false;
62 }
63 return (frameAttributes() == other->frameAttributes()) &&
64 (backgroundAttributes() == other->backgroundAttributes());
65}
66
67void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )
68{
69 Q_UNUSED( position );
70 // PENDING(kalle) FIXME
71 qWarning( "Sorry, not implemented: void AbstractAreaBase::alignToReferencePoint( const RelativePosition& position )" );
72}
73
74void AbstractAreaBase::setFrameAttributes( const FrameAttributes &a )
75{
76 if ( d->frameAttributes == a )
77 return;
78
79 d->frameAttributes = a;
81}
82
83FrameAttributes AbstractAreaBase::frameAttributes() const
84{
85 return d->frameAttributes;
86}
87
88void AbstractAreaBase::setBackgroundAttributes( const BackgroundAttributes &a )
89{
90 if ( d->backgroundAttributes == a )
91 return;
92
93 d->backgroundAttributes = a;
95}
96
97BackgroundAttributes AbstractAreaBase::backgroundAttributes() const
98{
99 return d->backgroundAttributes;
100}
101
102
103/* static */
104void AbstractAreaBase::paintBackgroundAttributes( QPainter& painter, const QRect& rect,
105 const KChart::BackgroundAttributes& attributes )
106{
107 if ( !attributes.isVisible() ) return;
108
109 /* first draw the brush (may contain a pixmap)*/
110 if ( Qt::NoBrush != attributes.brush().style() ) {
111 KChart::PainterSaver painterSaver( &painter );
112 painter.setPen( Qt::NoPen );
113 const QPointF newTopLeft( painter.deviceTransform().map( rect.topLeft() ) );
114 painter.setBrushOrigin( newTopLeft );
115 painter.setBrush( attributes.brush() );
116 painter.drawRect( rect.adjusted( 0, 0, -1, -1 ) );
117 }
118 /* next draw the backPixmap over the brush */
119 if ( !attributes.pixmap().isNull() &&
120 attributes.pixmapMode() != BackgroundAttributes::BackgroundPixmapModeNone ) {
121 QPointF ol = rect.topLeft();
122 if ( BackgroundAttributes::BackgroundPixmapModeCentered == attributes.pixmapMode() )
123 {
124 ol.setX( rect.center().x() - attributes.pixmap().width() / 2 );
125 ol.setY( rect.center().y() - attributes.pixmap().height()/ 2 );
126 painter.drawPixmap( ol, attributes.pixmap() );
127 } else {
128 QTransform m;
129 qreal zW = (qreal)rect.width() / (qreal)attributes.pixmap().width();
130 qreal zH = (qreal)rect.height() / (qreal)attributes.pixmap().height();
131 switch ( attributes.pixmapMode() ) {
132 case BackgroundAttributes::BackgroundPixmapModeScaled:
133 {
134 qreal z;
135 z = qMin( zW, zH );
136 m.scale( z, z );
137 }
138 break;
139 case BackgroundAttributes::BackgroundPixmapModeStretched:
140 m.scale( zW, zH );
141 break;
142 default:
143 ; // Cannot happen, previously checked
144 }
145 QPixmap pm = attributes.pixmap().transformed( m );
146 ol.setX( rect.center().x() - pm.width() / 2 );
147 ol.setY( rect.center().y() - pm.height()/ 2 );
148 painter.drawPixmap( ol, pm );
149 }
150 }
151}
152
153/* static */
154void AbstractAreaBase::paintFrameAttributes( QPainter& painter, const QRect& rect,
155 const KChart::FrameAttributes& attributes )
156{
157
158 if ( !attributes.isVisible() ) return;
159
160 // Note: We set the brush to NoBrush explicitly here.
161 // Otherwise we might get a filled rectangle, so any
162 // previously drawn background would be overwritten by that area.
163
164 const QPen oldPen( painter.pen() );
165 const QBrush oldBrush( painter.brush() );
166
167 painter.setPen( PrintingParameters::scalePen( attributes.pen() ) );
168 painter.setBrush( Qt::NoBrush );
169 painter.drawRoundedRect( rect.adjusted( 0, 0, -1, -1 ), attributes.cornerRadius(), attributes.cornerRadius() );
170
171 painter.setBrush( oldBrush );
172 painter.setPen( oldPen );
173}
174
175void AbstractAreaBase::paintBackground( QPainter& painter, const QRect& rect )
176{
177 Q_ASSERT_X ( d != nullptr, "AbstractAreaBase::paintBackground()",
178 "Private class was not initialized!" );
179
180 PainterSaver painterSaver( &painter );
181
182 const qreal radius = d->frameAttributes.cornerRadius();
184 path.addRoundedRect( rect.adjusted( 0, 0, -1, -1 ), radius, radius );
185 painter.setClipPath(path);
186
187 paintBackgroundAttributes( painter, rect, d->backgroundAttributes );
188}
189
190
191void AbstractAreaBase::paintFrame( QPainter& painter, const QRect& rect )
192{
193 Q_ASSERT_X ( d != nullptr, "AbstractAreaBase::paintFrame()",
194 "Private class was not initialized!" );
195 paintFrameAttributes( painter, rect, d->frameAttributes );
196}
197
198
199void AbstractAreaBase::getFrameLeadings(int& left, int& top, int& right, int& bottom ) const
200{
201 int padding = 0;
202 if ( d && d->frameAttributes.isVisible() ) {
203 padding = qMax( d->frameAttributes.padding(), 0 );
204 }
205 left = padding;
206 top = padding;
207 right = padding;
208 bottom = padding;
209}
210
212{
213 int left;
214 int top;
215 int right;
216 int bottom;
217 getFrameLeadings( left, top, right, bottom );
218 return QRect ( QPoint( 0, 0 ), areaGeometry().size() ).adjusted( left, top, -right, -bottom );
219}
220
222{
223 // this block left empty intentionally
224}
Base class for AbstractArea and AbstractAreaWidget: An area in the chart with a background,...
void getFrameLeadings(int &left, int &top, int &right, int &bottom) const
virtual QRect areaGeometry() const =0
bool compare(const AbstractAreaBase *other) const
Returns true if both areas have the same settings.
Set of attributes usable for background pixmaps.
A set of attributes for frames around items.
Defines relative position information: reference area, position in this area (reference position),...
QString path(const QString &relativePath)
QCA_EXPORT void init()
Qt::BrushStyle style() const const
const QBrush & brush() const const
const QTransform & deviceTransform() const const
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawRect(const QRect &rectangle)
void drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
const QPen & pen() const const
void setBrush(Qt::BrushStyle style)
void setBrushOrigin(const QPoint &position)
void setClipPath(const QPainterPath &path, Qt::ClipOperation operation)
void setPen(Qt::PenStyle style)
int height() const const
bool isNull() const const
QPixmap transformed(const QTransform &transform, Qt::TransformationMode mode) const const
int width() const const
int x() const const
int y() const const
void setX(qreal x)
void setY(qreal y)
QRect adjusted(int dx1, int dy1, int dx2, int dy2) const const
QPoint center() const const
int height() const const
QPoint topLeft() const const
int width() const const
QLine map(const QLine &l) const const
QTransform & scale(qreal sx, qreal sy)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.