File indexing completed on 2024-05-12 15:54:10

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