File indexing completed on 2024-05-12 04:20:28

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "KChartAbstractAreaWidget.h"
0010 #include "KChartAbstractAreaWidget_p.h"
0011 
0012 #include "KChartMath_p.h"
0013 
0014 
0015 using namespace KChart;
0016 
0017 
0018 AbstractAreaWidget::Private::Private()
0019 {
0020     // this block left empty intentionally
0021 }
0022 
0023 AbstractAreaWidget::Private::~Private()
0024 {
0025     // this block left empty intentionally
0026 }
0027 
0028 
0029 void AbstractAreaWidget::Private::resizeLayout(
0030     AbstractAreaWidget* widget, const QSize& size )
0031 {
0032     if ( size == currentLayoutSize ) return;
0033 
0034     currentLayoutSize = size;
0035 
0036     // Now we call adjust the size, for the inner parts of the widget.
0037     int left;
0038     int top;
0039     int right;
0040     int bottom;
0041     widget->getFrameLeadings( left, top, right, bottom );
0042     const QSize innerSize( size.width() - left - right,
0043                            size.height() - top - bottom );
0044     // With this adjusted size we call the real resizeLayout method,
0045     // which normally will call resizeLayout( size ) in the derived class
0046     // - which in turn is the place to resize the layout of that class.
0047     widget->resizeLayout( innerSize );
0048 }
0049 
0050 
0051 AbstractAreaWidget::AbstractAreaWidget( QWidget* parent )
0052     : QWidget( parent )
0053     , AbstractAreaBase( new Private() )
0054 {
0055     init();
0056 }
0057 
0058 AbstractAreaWidget::~AbstractAreaWidget()
0059 {
0060     // this block left empty intentionally
0061 }
0062 
0063 void AbstractAreaWidget::init()
0064 {
0065     // this block left empty intentionally
0066 }
0067 
0068 void AbstractAreaWidget::needSizeHint()
0069 {
0070     // this block left empty intentionally
0071 }
0072 
0073 #define d d_func()
0074 
0075 void AbstractAreaWidget::resizeLayout( const QSize& size )
0076 {
0077     Q_UNUSED( size );
0078     // this block left empty intentionally
0079 }
0080 
0081 void AbstractAreaWidget::paintEvent( QPaintEvent* event )
0082 {
0083     Q_UNUSED( event );
0084     QPainter painter( this );
0085     if ( size() != d->currentLayoutSize ) {
0086         d->resizeLayout( this, size() );
0087     }
0088     paintAll( painter );
0089 }
0090 
0091 void AbstractAreaWidget::paintIntoRect( QPainter& painter, const QRect& rect )
0092 {
0093     if ( rect.isEmpty() ) return;
0094 
0095     d->resizeLayout( this, rect.size() );
0096 
0097     const QPoint translation( rect.topLeft() );
0098     painter.translate( translation );
0099     paintAll( painter );
0100     painter.translate( -translation.x(), -translation.y() );
0101 
0102 /*
0103     // guide for subclassing
0104 
0105     // set up the contents of the widget so we get a useful geometry
0106     needSizeHint();
0107 
0108     const QRect oldGeometry( layout()->geometry() );
0109     const QRect newGeo( QPoint(0,0), rect.size() );
0110     const bool mustChangeGeo = layout() && oldGeometry != newGeo;
0111     if ( mustChangeGeo )
0112         layout()->setGeometry( newGeo );
0113     painter.translate( rect.left(), rect.top() );
0114     paintAll( painter );
0115     painter.translate( -rect.left(), -rect.top() );
0116     if ( mustChangeGeo )
0117         layout()->setGeometry( oldGeometry );
0118 */
0119 }
0120 
0121 void AbstractAreaWidget::forceRebuild()
0122 {
0123     // this block left empty intentionally
0124 }
0125 
0126 void AbstractAreaWidget::paintAll( QPainter& painter )
0127 {
0128     paintBackground( painter, QRect(QPoint(0, 0), size() ) );
0129     paintFrame( painter, QRect(QPoint(0, 0), size() ) );
0130 
0131 /*
0132     // guide for subclassing
0133 
0134     // we do not call setContentsMargins() now,
0135     // but we call resizeLayout() whenever the size or the frame has changed
0136 
0137     // adjust the widget's content margins,
0138     // to be sure all content gets calculated
0139     // to fit into the inner rectangle
0140     const QRect oldGeometry( areaGeometry() );
0141     const QRect inner( innerRect() );
0142     //qDebug() << "areaGeometry():" << oldGeometry
0143     //         << "  contentsRect():" << contentsRect() << "  inner:" << inner;
0144     if ( contentsRect() != inner ) {
0145         //qDebug() << "old contentsRect():" << contentsRect() << "  new innerRect:" << inner;
0146         setContentsMargins(
0147             inner.left(),
0148             inner.top(),
0149             oldGeometry.width() - inner.width() - 1,
0150             oldGeometry.height() - inner.height() - 1 );
0151         //forceRebuild();
0152     }
0153 */
0154     int left;
0155     int top;
0156     int right;
0157     int bottom;
0158     getFrameLeadings( left, top, right, bottom );
0159     const QPoint translation( left, top );
0160     painter.translate( translation );
0161     paint( &painter );
0162     painter.translate( -translation.x(), -translation.y() );
0163 }
0164 
0165 QRect AbstractAreaWidget::areaGeometry() const
0166 {
0167     return geometry();
0168 }
0169 
0170 void AbstractAreaWidget::positionHasChanged()
0171 {
0172     Q_EMIT positionChanged( this );
0173 }