File indexing completed on 2024-05-12 04:20:36

0001 /*
0002  * SPDX-FileCopyrightText: 2001-2015 Klaralvdalens Datakonsult AB. All rights reserved.
0003  *
0004  * This file is part of the KD Chart library.
0005  *
0006  * SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #ifndef KCHARTPAINTERSAVER_P_H
0010 #define KCHARTPAINTERSAVER_P_H
0011 
0012 //
0013 //  W A R N I N G
0014 //  -------------
0015 //
0016 // This file is not part of the KD Chart API.  It exists purely as an
0017 // implementation detail.  This header file may change from version to
0018 // version without notice, or even be removed.
0019 //
0020 // We mean it.
0021 //
0022 
0023 #include "KChartMath_p.h"
0024 
0025 #include <QPainter>
0026 
0027 
0028 namespace KChart {
0029 
0030 /**
0031    \internal
0032 
0033    @short Resource Allocation Is Initialization for QPainter::save and
0034    restore
0035 
0036    Usage:
0037 
0038    Instead of
0039    \code
0040    painter.save();
0041    // ...
0042    painter.restore();
0043    \endcode
0044 
0045    Use this:
0046 
0047    \code
0048    const KChart::PainterSaver saver( &painter );
0049    // ...
0050    \endcode
0051 
0052    which makes sure that restore() is called when exiting from guard
0053    clauses, or when exceptions are thrown.
0054 */
0055 class PainterSaver {
0056     Q_DISABLE_COPY( PainterSaver )
0057 public:
0058     explicit PainterSaver( QPainter* p )
0059         : painter( p ) 
0060     {
0061 #if defined Q_OS_WIN
0062         static bool initialized = false;
0063         static bool isQt4_3_0;
0064         if ( !initialized )
0065         {
0066             isQt4_3_0 = ( QString::fromLatin1( qVersion() ) == QString::fromLatin1( "4.3.0" ) );
0067         }
0068         initialized = true;
0069         m_isQt4_3_0 = isQt4_3_0;
0070 #endif
0071         p->save();
0072     }
0073 
0074     ~PainterSaver()
0075     {
0076 #if defined Q_OS_WIN
0077         // the use of setClipRect is a workaround for a bug in Qt 4.3.0 which could
0078         // lead to an assert on Windows
0079         if ( m_isQt4_3_0 )
0080         {
0081             painter->setClipRect( 0, 0, 2, 2 );
0082         }
0083 #endif
0084         painter->restore(); 
0085     }
0086 
0087 private:
0088 #if defined Q_OS_WIN
0089     bool m_isQt4_3_0;
0090 #endif
0091     QPainter* const painter;
0092 };
0093 
0094 }
0095 
0096 #endif /* KCHARTPAINTERSAVER_P_H */
0097