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

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 "KChartHeaderFooter.h"
0021 #include "KChartHeaderFooter_p.h"
0022 
0023 #include "KChartChart.h"
0024 #include <KChartTextAttributes.h>
0025 #include "KTextDocument.h"
0026 #include "KChartMath_p.h"
0027 
0028 #include <QFont>
0029 #include <QPainter>
0030 #include <QAbstractTextDocumentLayout>
0031 #include <QTextDocumentFragment>
0032 #include <QTextBlock>
0033 #include <QtDebug>
0034 #include <QLabel>
0035 
0036 using namespace KChart;
0037 
0038 HeaderFooter::Private::Private() :
0039     type( Header ),
0040     position( Position::North )
0041 {
0042 }
0043 
0044 HeaderFooter::Private::~Private()
0045 {
0046 }
0047 
0048 #define d d_func()
0049 
0050 HeaderFooter::HeaderFooter( Chart* parent ) :
0051     TextArea( new Private() )
0052 {
0053     setParent( parent );
0054     init();
0055 }
0056 
0057 HeaderFooter::~HeaderFooter()
0058 {
0059     Q_EMIT destroyedHeaderFooter( this );
0060 }
0061 
0062 void HeaderFooter::setParent( QObject* parent )
0063 {
0064     QObject::setParent( parent );
0065     setParentWidget( qobject_cast<QWidget*>( parent ) );
0066     if ( parent && ! autoReferenceArea() )
0067         setAutoReferenceArea( parent );
0068 }
0069 
0070 void HeaderFooter::init()
0071 {
0072     TextAttributes ta;
0073     ta.setPen( QPen(Qt::black) );
0074     ta.setFont( QFont( QLatin1String( "helvetica" ), 10, QFont::Bold, false ) );
0075 
0076     Measure m( 35.0 );
0077     m.setRelativeMode( autoReferenceArea(), KChartEnums::MeasureOrientationMinimum );
0078     ta.setFontSize( m );
0079 
0080     m.setValue( 8.0 );
0081     m.setCalculationMode( KChartEnums::MeasureCalculationModeAbsolute );
0082     ta.setMinimalFontSize( m );
0083 
0084     setTextAttributes( ta );
0085 }
0086 
0087 HeaderFooter * HeaderFooter::clone() const
0088 {
0089     HeaderFooter* headerFooter = new HeaderFooter( new Private( *d ), nullptr );
0090     headerFooter->setType( type() );
0091     headerFooter->setPosition( position() );
0092     headerFooter->setText( text() );
0093     headerFooter->setTextAttributes( textAttributes() );
0094     return headerFooter;
0095 }
0096 
0097 bool HeaderFooter::compare( const HeaderFooter& other ) const
0098 {
0099     return  (type()           == other.type()) &&
0100             (position()       == other.position()) &&
0101             // also compare members inherited from the base class:
0102             (autoReferenceArea() == other.autoReferenceArea()) &&
0103             (text()              == other.text()) &&
0104             (textAttributes()    == other.textAttributes());
0105 }
0106 
0107 void HeaderFooter::setType( HeaderFooterType type )
0108 {
0109     if ( d->type != type ) {
0110         d->type = type;
0111         Q_EMIT positionChanged( this );
0112     }
0113 }
0114 
0115 HeaderFooter::HeaderFooterType HeaderFooter::type() const
0116 {
0117     return d->type;
0118 }
0119 
0120 void HeaderFooter::setPosition( Position position )
0121 {
0122     if ( d->position != position ) {
0123         d->position = position;
0124         Q_EMIT positionChanged( this );
0125     }
0126 }
0127 
0128 Position HeaderFooter::position() const
0129 {
0130     return d->position;
0131 }