KD Chart 2
[rev.2.5]
|
00001 /**************************************************************************** 00002 ** Copyright (C) 2001-2012 Klaralvdalens Datakonsult AB. All rights reserved. 00003 ** 00004 ** This file is part of the KD Chart library. 00005 ** 00006 ** Licensees holding valid commercial KD Chart licenses may use this file in 00007 ** accordance with the KD Chart Commercial License Agreement provided with 00008 ** the Software. 00009 ** 00010 ** 00011 ** This file may be distributed and/or modified under the terms of the 00012 ** GNU General Public License version 2 and version 3 as published by the 00013 ** Free Software Foundation and appearing in the file LICENSE.GPL.txt included. 00014 ** 00015 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00016 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00017 ** 00018 ** Contact info@kdab.com if any conditions of this licensing are not 00019 ** clear to you. 00020 ** 00021 **********************************************************************/ 00022 00023 #include "KDChartTernaryCoordinatePlane.h" 00024 #include "KDChartTernaryCoordinatePlane_p.h" 00025 00026 #include <QtDebug> 00027 #include <QPainter> 00028 00029 #include "KDChartPaintContext.h" 00030 #include "KDChartPainterSaver_p.h" 00031 #include "KDChartTernaryAxis.h" 00032 #include "KDChartAbstractTernaryDiagram.h" 00033 00034 #include "TernaryConstants.h" 00035 00036 using namespace KDChart; 00037 00038 #define d d_func() 00039 00040 TernaryCoordinatePlane::Private::Private() 00041 : AbstractCoordinatePlane::Private() 00042 { 00043 } 00044 00045 TernaryCoordinatePlane::TernaryCoordinatePlane( Chart* parent ) 00046 : AbstractCoordinatePlane( new Private(), parent ) 00047 { 00048 } 00049 00050 TernaryCoordinatePlane::~TernaryCoordinatePlane() 00051 { 00052 } 00053 00054 void TernaryCoordinatePlane::init() 00055 { 00056 } 00057 00058 void TernaryCoordinatePlane::addDiagram( AbstractDiagram* diagram ) 00059 { 00060 Q_ASSERT_X ( dynamic_cast<AbstractTernaryDiagram*>( diagram ), 00061 "TernaryCoordinatePlane::addDiagram", "Only ternary " 00062 "diagrams can be added to a ternary coordinate plane!" ); 00063 AbstractCoordinatePlane::addDiagram ( diagram ); 00064 } 00065 00066 void TernaryCoordinatePlane::layoutDiagrams() 00067 { // this is our "resize event": 00068 // all diagrams always take the same space, nothing to be done here 00069 // the "inner" margin (adjustments to diagram coordinates) 00070 QRectF diagramNativeRectangle ( QPointF( 0.0, 0.0 ), 00071 QSizeF( TriangleWidth, TriangleHeight ) ); 00072 QPair<QSizeF, QSizeF> margins = grid()->requiredMargins(); 00073 d->diagramRect = areaGeometry(); 00074 diagramNativeRectangle.adjust 00075 (-margins.first.width(), -margins.first.height(), 00076 margins.second.width(), margins.second.height() ); 00077 00078 // the "outer" margin (distance between diagram contents and area, 00079 // determined by axis label overlap 00080 { 00081 QSizeF topleft( 0.0, 0.0 ); 00082 QSizeF bottomRight( 0.0, 0.0 ); 00083 Q_FOREACH( AbstractDiagram* abstractDiagram, diagrams() ) { 00084 AbstractTernaryDiagram* diagram = 00085 qobject_cast<AbstractTernaryDiagram*>( abstractDiagram ); 00086 Q_ASSERT( diagram ); 00087 Q_FOREACH( TernaryAxis* axis, diagram->axes() ) { 00088 QPair<QSizeF, QSizeF> margin = axis->requiredMargins(); 00089 topleft = topleft.expandedTo( margin.first ); 00090 bottomRight = bottomRight.expandedTo( margin.second ); 00091 } 00092 } 00093 d->diagramRectContainer = 00094 d->diagramRect.adjusted( topleft.width(), 00095 topleft.height(), 00096 -bottomRight.width(), 00097 -bottomRight.height() ); 00098 } 00099 00100 // now calculate isometric projection, x and y widget coordinate 00101 // units, and location of (0.0, 0.0) in diagram coordinates 00102 QPointF zeroZeroPoint = d->diagramRectContainer.bottomLeft(); 00103 qreal w = d->diagramRectContainer.width(); 00104 qreal h = d->diagramRectContainer.height(); 00105 qreal usableWidth; 00106 qreal usableHeight; 00107 00108 if ( TriangleHeight * w > h ) { 00109 // shorten width: 00110 usableWidth = h / diagramNativeRectangle.height(); 00111 usableHeight = h; 00112 zeroZeroPoint.setX( zeroZeroPoint.x() + ( w - usableWidth ) / 2 ); 00113 } else { 00114 // reduce height: 00115 usableWidth = w; 00116 usableHeight = diagramNativeRectangle.height() * w; 00117 zeroZeroPoint.setY( zeroZeroPoint.y() - ( h - usableHeight ) / 2 ); 00118 } 00119 // the rectangle has 1 as it's width, and TriangleHeight as it's 00120 // height - so this is how we translate that to widget coordinates: 00121 d->xUnit = usableWidth / diagramNativeRectangle.width(); // only because we normalize the values to [0..1] 00122 d->yUnit = -usableHeight / diagramNativeRectangle.height(); 00123 00124 // now move zeroZeroPoint so that it does not include the tick marks 00125 { 00126 qreal descent = diagramNativeRectangle.height() - TriangleHeight; 00127 qreal rightShift = -diagramNativeRectangle.x(); 00128 zeroZeroPoint += QPointF( rightShift * d->xUnit, descent * d->yUnit ); 00129 } 00130 00131 d->diagramRect.setBottomLeft( zeroZeroPoint ); 00132 d->diagramRect.setTopRight( QPointF( usableWidth, -usableHeight ) + zeroZeroPoint ); 00133 } 00134 00135 const QPointF TernaryCoordinatePlane::translate( const QPointF& point ) const 00136 { 00137 return QPointF( d->diagramRect.bottomLeft().x() + point.x() * d->xUnit, 00138 d->diagramRect.bottomLeft().y() + point.y() * d->yUnit ); 00139 } 00140 00141 QSize TernaryCoordinatePlane::minimumSizeHint() const 00142 { 00143 // FIXME temp 00144 return QSize(); 00145 } 00146 00147 QSizePolicy TernaryCoordinatePlane::sizePolicy() const 00148 { 00149 return QSizePolicy( QSizePolicy::MinimumExpanding, 00150 QSizePolicy::MinimumExpanding ); 00151 } 00152 00153 void TernaryCoordinatePlane::paint( QPainter* painter ) 00154 { 00155 PainterSaver s( painter ); 00156 // FIXME: this is not a good location for that: 00157 painter->setRenderHint(QPainter::Antialiasing, true ); 00158 00159 AbstractDiagramList diags = diagrams(); 00160 if ( !diags.isEmpty() ) 00161 { 00162 PaintContext ctx; 00163 ctx.setPainter ( painter ); 00164 ctx.setCoordinatePlane ( this ); 00165 const QRectF drawArea( areaGeometry() ); 00166 ctx.setRectangle ( drawArea ); 00167 00168 // paint the coordinate system rulers: 00169 Q_ASSERT( d->grid != 0 ); 00170 d->grid->drawGrid( &ctx ); 00171 00172 // paint the diagrams: 00173 for ( int i = 0; i < diags.size(); i++ ) 00174 { 00175 PainterSaver diagramPainterSaver( painter ); 00176 diags[i]->paint ( &ctx ); 00177 } 00178 } 00179 } 00180 00181 DataDimensionsList TernaryCoordinatePlane::getDataDimensionsList() const 00182 { // not needed 00183 return DataDimensionsList(); 00184 } 00185 00186 TernaryGrid* TernaryCoordinatePlane::grid() const 00187 { 00188 TernaryGrid* ternaryGrid = static_cast<TernaryGrid*>( d->grid ); 00189 Q_ASSERT( dynamic_cast<TernaryGrid*>( d->grid ) ); 00190 return ternaryGrid; 00191 } 00192 00193 #undef d