1 /*
2  * Copyright (C) 2001-2015 Klaralvdalens Datakonsult AB.  All rights reserved.
3  *
4  * This file is part of the KD Chart library.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #include "KChartHeaderFooter.h"
21 #include "KChartHeaderFooter_p.h"
22 
23 #include "KChartChart.h"
24 #include <KChartTextAttributes.h>
25 #include "KTextDocument.h"
26 #include "KChartMath_p.h"
27 
28 #include <QFont>
29 #include <QPainter>
30 #include <QAbstractTextDocumentLayout>
31 #include <QTextDocumentFragment>
32 #include <QTextBlock>
33 #include <QtDebug>
34 #include <QLabel>
35 
36 using namespace KChart;
37 
Private()38 HeaderFooter::Private::Private() :
39     type( Header ),
40     position( Position::North )
41 {
42 }
43 
~Private()44 HeaderFooter::Private::~Private()
45 {
46 }
47 
48 #define d d_func()
49 
HeaderFooter(Chart * parent)50 HeaderFooter::HeaderFooter( Chart* parent ) :
51     TextArea( new Private() )
52 {
53     setParent( parent );
54     init();
55 }
56 
~HeaderFooter()57 HeaderFooter::~HeaderFooter()
58 {
59     Q_EMIT destroyedHeaderFooter( this );
60 }
61 
setParent(QObject * parent)62 void HeaderFooter::setParent( QObject* parent )
63 {
64     QObject::setParent( parent );
65     setParentWidget( qobject_cast<QWidget*>( parent ) );
66     if ( parent && ! autoReferenceArea() )
67         setAutoReferenceArea( parent );
68 }
69 
init()70 void HeaderFooter::init()
71 {
72     TextAttributes ta;
73     ta.setPen( QPen(Qt::black) );
74     ta.setFont( QFont( QLatin1String( "helvetica" ), 10, QFont::Bold, false ) );
75 
76     Measure m( 35.0 );
77     m.setRelativeMode( autoReferenceArea(), KChartEnums::MeasureOrientationMinimum );
78     ta.setFontSize( m );
79 
80     m.setValue( 8.0 );
81     m.setCalculationMode( KChartEnums::MeasureCalculationModeAbsolute );
82     ta.setMinimalFontSize( m );
83 
84     setTextAttributes( ta );
85 }
86 
clone() const87 HeaderFooter * HeaderFooter::clone() const
88 {
89     HeaderFooter* headerFooter = new HeaderFooter( new Private( *d ), nullptr );
90     headerFooter->setType( type() );
91     headerFooter->setPosition( position() );
92     headerFooter->setText( text() );
93     headerFooter->setTextAttributes( textAttributes() );
94     return headerFooter;
95 }
96 
compare(const HeaderFooter & other) const97 bool HeaderFooter::compare( const HeaderFooter& other ) const
98 {
99     return  (type()           == other.type()) &&
100             (position()       == other.position()) &&
101             // also compare members inherited from the base class:
102             (autoReferenceArea() == other.autoReferenceArea()) &&
103             (text()              == other.text()) &&
104             (textAttributes()    == other.textAttributes());
105 }
106 
setType(HeaderFooterType type)107 void HeaderFooter::setType( HeaderFooterType type )
108 {
109     if ( d->type != type ) {
110         d->type = type;
111         Q_EMIT positionChanged( this );
112     }
113 }
114 
type() const115 HeaderFooter::HeaderFooterType HeaderFooter::type() const
116 {
117     return d->type;
118 }
119 
setPosition(Position position)120 void HeaderFooter::setPosition( Position position )
121 {
122     if ( d->position != position ) {
123         d->position = position;
124         Q_EMIT positionChanged( this );
125     }
126 }
127 
position() const128 Position HeaderFooter::position() const
129 {
130     return d->position;
131 }
132