KDChartLayoutItems.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (C) 2001-2010 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 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 #ifndef KDCHARTLAYOUTITEMS_H
00024 #define KDCHARTLAYOUTITEMS_H
00025 
00026 #include <QBrush>
00027 #include <QFont>
00028 #include <QFontMetricsF>
00029 #include <QLayout>
00030 #include <QLayoutItem>
00031 #include <QPen>
00032 
00033 #include "KDChartTextAttributes.h"
00034 #include "KDChartMarkerAttributes.h"
00035 
00036 class QPainter;
00037 class KDTextDocument;
00038 
00039 QPointF rotatedPoint( const QPointF& pt, qreal rotation, const QPointF& center = QPointF() );
00040 QRectF rotatedRect( const QRectF& pt, qreal rotation, const QPointF& center = QPointF() );
00041 
00042 namespace KDChart {
00043     class AbstractDiagram;
00044     class PaintContext;
00045 
00050     class KDCHART_EXPORT AbstractLayoutItem : public QLayoutItem
00051     {
00052     public:
00053         AbstractLayoutItem( Qt::Alignment itemAlignment = 0 ) :
00054             QLayoutItem( itemAlignment ),
00055             mParent( 0 ),
00056             mParentLayout( 0 ) {}
00057 
00064         virtual void paintAll( QPainter& painter );
00065 
00066         virtual void paint( QPainter* ) = 0;
00067 
00068         virtual void paintCtx( PaintContext* context );
00069         virtual void setParentWidget( QWidget* widget );
00070         virtual void sizeHintChanged()const;
00071 
00072         void setParentLayout( QLayout* lay )
00073         {
00074             mParentLayout = lay;
00075         }
00076         QLayout* parentLayout()
00077         {
00078             return mParentLayout;
00079         }
00080         void removeFromParentLayout()
00081         {
00082             if( mParentLayout ){
00083                 if( widget() )
00084                     mParentLayout->removeWidget( widget() );
00085                 else
00086                     mParentLayout->removeItem( this );
00087             }
00088         }
00089     protected:
00090         QWidget* mParent;
00091         QLayout* mParentLayout;
00092     };
00093 
00098     class KDCHART_EXPORT TextLayoutItem : public AbstractLayoutItem
00099     {
00100     public:
00101         TextLayoutItem();
00102         TextLayoutItem( const QString& text,
00103                         const TextAttributes& attributes,
00104                         const QObject* autoReferenceArea,
00105                         KDChartEnums::MeasureOrientation autoReferenceOrientation,
00106                         Qt::Alignment alignment = 0 );
00107 
00108         void setAutoReferenceArea( const QObject* area );
00109         const QObject* autoReferenceArea() const;
00110 
00111         void setText(const QString & text);
00112         QString text() const;
00113 
00114         void setTextAlignment( Qt::Alignment );
00115         Qt::Alignment textAlignment() const;
00116 
00117         void setTextAttributes( const TextAttributes& a );
00118         TextAttributes textAttributes() const;
00119 
00121         virtual bool isEmpty() const;
00123         virtual Qt::Orientations expandingDirections() const;
00125         virtual QSize maximumSize() const;
00127         virtual QSize minimumSize() const;
00129         virtual QSize sizeHint() const;
00131         virtual void setGeometry( const QRect& r );
00133         virtual QRect geometry() const;
00134 
00135         virtual QSize sizeHintAndRotatedCorners(
00136                     QPoint& topLeftPt, QPoint& topRightPt, QPoint& bottomRightPt, QPoint& bottomLeftPt) const;
00137         virtual QSize sizeHintUnrotated() const;
00138 
00139         virtual bool intersects( const TextLayoutItem& other, const QPointF& myPos, const QPointF& otherPos ) const;
00140         virtual bool intersects( const TextLayoutItem& other, const QPoint& myPos, const QPoint& otherPos ) const;
00141 
00142         virtual qreal realFontSize() const;
00143         virtual QFont realFont() const;
00144 
00145         virtual void paint( QPainter* );
00146 
00147     private:
00148         QPolygon rotatedCorners() const;
00149         bool realFontWasRecalculated() const;
00150         QSize unrotatedSizeHint( QFont fnt = QFont() ) const;
00151         QSize calcSizeHint( QFont fnt,
00152                             QPoint& topLeftPt, QPoint& topRightPt, QPoint& bottomRightPt, QPoint& bottomLeftPt ) const;
00153 
00154         qreal fitFontSizeToGeometry() const;
00155 
00156         QRect mRect;
00157         QString mText;
00158         Qt::Alignment mTextAlignment;
00159         TextAttributes mAttributes;
00160         const QObject* mAutoReferenceArea;
00161         KDChartEnums::MeasureOrientation mAutoReferenceOrientation;
00162         mutable QSize cachedSizeHint;
00163         mutable QPoint cachedTopLeft;
00164         mutable QPoint cachedTopRight;
00165         mutable QPoint cachedBottomRight;
00166         mutable QPoint cachedBottomLeft;
00167         mutable qreal cachedFontSize;
00168         mutable QFont cachedFont;
00169     };
00170 
00171     class KDCHART_EXPORT TextBubbleLayoutItem : public AbstractLayoutItem
00172     {
00173     public:
00174         TextBubbleLayoutItem();
00175         TextBubbleLayoutItem( const QString& text,
00176                               const TextAttributes& attributes,
00177                               const QObject* autoReferenceArea,
00178                               KDChartEnums::MeasureOrientation autoReferenceOrientation,
00179                               Qt::Alignment alignment = 0 );
00180 
00181         ~TextBubbleLayoutItem();
00182 
00183         void setAutoReferenceArea( const QObject* area );
00184         const QObject* autoReferenceArea() const;
00185 
00186         void setText(const QString & text);
00187         QString text() const;
00188 
00189         void setTextAttributes( const TextAttributes& a );
00190         TextAttributes textAttributes() const;
00191 
00193         virtual bool isEmpty() const;
00195         virtual Qt::Orientations expandingDirections() const;
00197         virtual QSize maximumSize() const;
00199         virtual QSize minimumSize() const;
00201         virtual QSize sizeHint() const;
00203         virtual void setGeometry( const QRect& r );
00205         virtual QRect geometry() const;
00206 
00207         virtual void paint( QPainter* painter );
00208 
00209     protected:
00210         int borderWidth() const;
00211 
00212     private:
00213         TextLayoutItem* const m_text;
00214     };
00215 
00220     class KDCHART_EXPORT MarkerLayoutItem : public AbstractLayoutItem
00221     {
00222         public:
00223             MarkerLayoutItem( AbstractDiagram* diagram,
00224                               const MarkerAttributes& marker,
00225                               const QBrush& brush,
00226                               const QPen& pen,
00227                               Qt::Alignment alignment = 0 );
00228 
00229             virtual Qt::Orientations expandingDirections() const;
00230             virtual QRect geometry() const;
00231             virtual bool isEmpty() const;
00232             virtual QSize maximumSize() const;
00233             virtual QSize minimumSize() const;
00234             virtual void setGeometry( const QRect& r );
00235             virtual QSize sizeHint() const;
00236 
00237             virtual void paint( QPainter* );
00238 
00239             static void paintIntoRect(
00240                     QPainter* painter,
00241                     const QRect& rect,
00242                     AbstractDiagram* diagram,
00243                     const MarkerAttributes& marker,
00244                     const QBrush& brush,
00245                     const QPen& pen );
00246 
00247         private:
00248             AbstractDiagram* mDiagram;
00249             QRect mRect;
00250             MarkerAttributes mMarker;
00251             QBrush mBrush;
00252             QPen mPen;
00253     };
00254 
00259     class KDCHART_EXPORT LineLayoutItem : public AbstractLayoutItem
00260     {
00261         public:
00262             LineLayoutItem( AbstractDiagram* diagram,
00263                             int length,
00264                             const QPen& pen,
00265                             Qt::Alignment alignment = 0 );
00266 
00267             virtual Qt::Orientations expandingDirections() const;
00268             virtual QRect geometry() const;
00269             virtual bool isEmpty() const;
00270             virtual QSize maximumSize() const;
00271             virtual QSize minimumSize() const;
00272             virtual void setGeometry( const QRect& r );
00273             virtual QSize sizeHint() const;
00274 
00275             virtual void paint( QPainter* );
00276 
00277             static void paintIntoRect(
00278                     QPainter* painter,
00279                     const QRect& rect,
00280                     const QPen& pen );
00281 
00282         private:
00283             AbstractDiagram* mDiagram;
00284             int mLength;
00285             QPen mPen;
00286             QRect mRect;
00287     };
00288 
00293     class KDCHART_EXPORT LineWithMarkerLayoutItem : public AbstractLayoutItem
00294     {
00295         public:
00296             LineWithMarkerLayoutItem( AbstractDiagram* diagram,
00297                                       int lineLength,
00298                                       const QPen& linePen,
00299                                       int markerOffs,
00300                                       const MarkerAttributes& marker,
00301                                       const QBrush& markerBrush,
00302                                       const QPen& markerPen,
00303                                       Qt::Alignment alignment = 0 );
00304 
00305             virtual Qt::Orientations expandingDirections() const;
00306             virtual QRect geometry() const;
00307             virtual bool isEmpty() const;
00308             virtual QSize maximumSize() const;
00309             virtual QSize minimumSize() const;
00310             virtual void setGeometry( const QRect& r );
00311             virtual QSize sizeHint() const;
00312 
00313             virtual void paint( QPainter* );
00314 
00315         private:
00316             AbstractDiagram* mDiagram;
00317             QRect mRect;
00318             int mLineLength;
00319             QPen mLinePen;
00320             int mMarkerOffs;
00321             MarkerAttributes mMarker;
00322             QBrush mMarkerBrush;
00323             QPen mMarkerPen;
00324     };
00325 
00326 
00331     class KDCHART_EXPORT HorizontalLineLayoutItem : public AbstractLayoutItem
00332     {
00333     public:
00334         HorizontalLineLayoutItem();
00335 
00336         virtual Qt::Orientations expandingDirections() const;
00337         virtual QRect geometry() const;
00338         virtual bool isEmpty() const;
00339         virtual QSize maximumSize() const;
00340         virtual QSize minimumSize() const;
00341         virtual void setGeometry( const QRect& r );
00342         virtual QSize sizeHint() const;
00343 
00344         virtual void paint( QPainter* );
00345 
00346     private:
00347         QRect mRect;
00348     };
00349 
00354     class KDCHART_EXPORT VerticalLineLayoutItem : public AbstractLayoutItem
00355     {
00356         public:
00357             VerticalLineLayoutItem();
00358 
00359             virtual Qt::Orientations expandingDirections() const;
00360             virtual QRect geometry() const;
00361             virtual bool isEmpty() const;
00362             virtual QSize maximumSize() const;
00363             virtual QSize minimumSize() const;
00364             virtual void setGeometry( const QRect& r );
00365             virtual QSize sizeHint() const;
00366 
00367             virtual void paint( QPainter* );
00368 
00369         private:
00370             QRect mRect;
00371     };
00372 
00443     class KDCHART_EXPORT AutoSpacerLayoutItem : public AbstractLayoutItem
00444     {
00445         public:
00446             AutoSpacerLayoutItem(
00447                     bool layoutIsAtTopPosition, QHBoxLayout *rightLeftLayout,
00448                     bool layoutIsAtLeftPosition,  QVBoxLayout *topBottomLayout );
00449 
00450             virtual Qt::Orientations expandingDirections() const;
00451             virtual QRect geometry() const;
00452             virtual bool isEmpty() const;
00453             virtual QSize maximumSize() const;
00454             virtual QSize minimumSize() const;
00455             virtual void setGeometry( const QRect& r );
00456             virtual QSize sizeHint() const;
00457 
00458             virtual void paint( QPainter* );
00459 
00460         private:
00461             QRect mRect;
00462             bool mLayoutIsAtTopPosition;
00463             QHBoxLayout *mRightLeftLayout;
00464             bool mLayoutIsAtLeftPosition;
00465             QVBoxLayout *mTopBottomLayout;
00466 
00467             mutable QBrush mCommonBrush;
00468             mutable QSize mCachedSize;
00469     };
00470 
00471 }
00472 
00473 #endif /* KDCHARTLAYOUTITEMS_H */