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 "KChartAbstractAreaWidget.h"
21 #include "KChartAbstractAreaWidget_p.h"
22 
23 #include "KChartMath_p.h"
24 
25 
26 using namespace KChart;
27 
28 
Private()29 AbstractAreaWidget::Private::Private()
30 {
31     // this block left empty intentionally
32 }
33 
~Private()34 AbstractAreaWidget::Private::~Private()
35 {
36     // this block left empty intentionally
37 }
38 
39 
resizeLayout(AbstractAreaWidget * widget,const QSize & size)40 void AbstractAreaWidget::Private::resizeLayout(
41     AbstractAreaWidget* widget, const QSize& size )
42 {
43     if ( size == currentLayoutSize ) return;
44 
45     currentLayoutSize = size;
46 
47     // Now we call adjust the size, for the inner parts of the widget.
48     int left;
49     int top;
50     int right;
51     int bottom;
52     widget->getFrameLeadings( left, top, right, bottom );
53     const QSize innerSize( size.width() - left - right,
54                            size.height() - top - bottom );
55     // With this adjusted size we call the real resizeLayout method,
56     // which normally will call resizeLayout( size ) in the derived class
57     // - which in turn is the place to resize the layout of that class.
58     widget->resizeLayout( innerSize );
59 }
60 
61 
AbstractAreaWidget(QWidget * parent)62 AbstractAreaWidget::AbstractAreaWidget( QWidget* parent )
63     : QWidget( parent )
64     , AbstractAreaBase( new Private() )
65 {
66     init();
67 }
68 
~AbstractAreaWidget()69 AbstractAreaWidget::~AbstractAreaWidget()
70 {
71     // this block left empty intentionally
72 }
73 
init()74 void AbstractAreaWidget::init()
75 {
76     // this block left empty intentionally
77 }
78 
needSizeHint()79 void AbstractAreaWidget::needSizeHint()
80 {
81     // this block left empty intentionally
82 }
83 
84 #define d d_func()
85 
resizeLayout(const QSize & size)86 void AbstractAreaWidget::resizeLayout( const QSize& size )
87 {
88     Q_UNUSED( size );
89     // this block left empty intentionally
90 }
91 
paintEvent(QPaintEvent * event)92 void AbstractAreaWidget::paintEvent( QPaintEvent* event )
93 {
94     Q_UNUSED( event );
95     QPainter painter( this );
96     if ( size() != d->currentLayoutSize ) {
97         d->resizeLayout( this, size() );
98     }
99     paintAll( painter );
100 }
101 
paintIntoRect(QPainter & painter,const QRect & rect)102 void AbstractAreaWidget::paintIntoRect( QPainter& painter, const QRect& rect )
103 {
104     if ( rect.isEmpty() ) return;
105 
106     d->resizeLayout( this, rect.size() );
107 
108     const QPoint translation( rect.topLeft() );
109     painter.translate( translation );
110     paintAll( painter );
111     painter.translate( -translation.x(), -translation.y() );
112 
113 /*
114     // guide for subclassing
115 
116     // set up the contents of the widget so we get a useful geometry
117     needSizeHint();
118 
119     const QRect oldGeometry( layout()->geometry() );
120     const QRect newGeo( QPoint(0,0), rect.size() );
121     const bool mustChangeGeo = layout() && oldGeometry != newGeo;
122     if ( mustChangeGeo )
123         layout()->setGeometry( newGeo );
124     painter.translate( rect.left(), rect.top() );
125     paintAll( painter );
126     painter.translate( -rect.left(), -rect.top() );
127     if ( mustChangeGeo )
128         layout()->setGeometry( oldGeometry );
129 */
130 }
131 
forceRebuild()132 void AbstractAreaWidget::forceRebuild()
133 {
134     //bloc left empty intentionally
135 }
136 
paintAll(QPainter & painter)137 void AbstractAreaWidget::paintAll( QPainter& painter )
138 {
139     paintBackground( painter, QRect(QPoint(0, 0), size() ) );
140     paintFrame( painter, QRect(QPoint(0, 0), size() ) );
141 
142 /*
143     // guide for subclassing
144 
145     // we do not call setContentsMargins() now,
146     // but we call resizeLayout() whenever the size or the frame has changed
147 
148     // adjust the widget's content margins,
149     // to be sure all content gets calculated
150     // to fit into the inner rectangle
151     const QRect oldGeometry( areaGeometry() );
152     const QRect inner( innerRect() );
153     //qDebug() << "areaGeometry():" << oldGeometry
154     //         << "  contentsRect():" << contentsRect() << "  inner:" << inner;
155     if ( contentsRect() != inner ) {
156         //qDebug() << "old contentsRect():" << contentsRect() << "  new innerRect:" << inner;
157         setContentsMargins(
158             inner.left(),
159             inner.top(),
160             oldGeometry.width() - inner.width() - 1,
161             oldGeometry.height() - inner.height() - 1 );
162         //forceRebuild();
163     }
164 */
165     int left;
166     int top;
167     int right;
168     int bottom;
169     getFrameLeadings( left, top, right, bottom );
170     const QPoint translation( left, top );
171     painter.translate( translation );
172     paint( &painter );
173     painter.translate( -translation.x(), -translation.y() );
174 }
175 
areaGeometry() const176 QRect AbstractAreaWidget::areaGeometry() const
177 {
178     return geometry();
179 }
180 
positionHasChanged()181 void AbstractAreaWidget::positionHasChanged()
182 {
183     Q_EMIT positionChanged( this );
184 }
185