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 #ifndef KCHARTPAINTERSAVER_P_H
21 #define KCHARTPAINTERSAVER_P_H
22 
23 //
24 //  W A R N I N G
25 //  -------------
26 //
27 // This file is not part of the KD Chart API.  It exists purely as an
28 // implementation detail.  This header file may change from version to
29 // version without notice, or even be removed.
30 //
31 // We mean it.
32 //
33 
34 #include "KChartMath_p.h"
35 
36 #include <qpainter.h>
37 
38 
39 namespace KChart {
40 
41 /**
42    \internal
43 
44    @short Resource Allocation Is Initialization for QPainter::save and
45    restore
46 
47    Usage:
48 
49    Instead of
50    \code
51    painter.save();
52    // ...
53    painter.restore();
54    \endcode
55 
56    Use this:
57 
58    \code
59    const KChart::PainterSaver saver( &painter );
60    // ...
61    \endcode
62 
63    which makes sure that restore() is called when exiting from guard
64    clauses, or when exceptions are thrown.
65 */
66 class PainterSaver {
Q_DISABLE_COPY(PainterSaver)67     Q_DISABLE_COPY( PainterSaver )
68 public:
69     explicit PainterSaver( QPainter* p )
70         : painter( p )
71     {
72 #if defined Q_OS_WIN
73         static bool initialized = false;
74         static bool isQt4_3_0;
75         if ( !initialized )
76         {
77             isQt4_3_0 = ( QString::fromLatin1( qVersion() ) == QString::fromLatin1( "4.3.0" ) );
78         }
79         initialized = true;
80         m_isQt4_3_0 = isQt4_3_0;
81 #endif
82         p->save();
83     }
84 
~PainterSaver()85     ~PainterSaver()
86     {
87 #if defined Q_OS_WIN
88         // the use of setClipRect is a workaround for a bug in Qt 4.3.0 which could
89         // lead to an assert on Windows
90         if ( m_isQt4_3_0 )
91         {
92             painter->setClipRect( 0, 0, 2, 2 );
93         }
94 #endif
95         painter->restore();
96     }
97 
98 private:
99 #if defined Q_OS_WIN
100     bool m_isQt4_3_0;
101 #endif
102     QPainter* const painter;
103 };
104 
105 }
106 
107 #endif /* KCHARTPAINTERSAVER_P_H */
108 
109