-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqwt_painter.cpp
More file actions
160 lines (134 loc) · 4.5 KB
/
Copy pathqwt_painter.cpp
File metadata and controls
160 lines (134 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include "qwt_painter.h"
#include "qwt_color_map.h"
#include "qwt_scale_map.h"
#include <qwidget.h>
#include <qrect.h>
#include <qpainter.h>
#include <qpaintdevice.h>
#include <qpixmap.h>
#include <qstyle.h>
#include <qstyleoption.h>
#include <qapplication.h>
#include <qdesktopwidget.h>
void QwtPainter::unscaleFont( QPainter *painter )
{
if ( painter->font().pixelSize() >= 0 )
return;
static QSize screenResolution;
if ( !screenResolution.isValid() )
{
QDesktopWidget *desktop = QApplication::desktop();
if ( desktop )
{
screenResolution.setWidth( desktop->logicalDpiX() );
screenResolution.setHeight( desktop->logicalDpiY() );
}
}
const QPaintDevice *pd = painter->device();
if ( pd->logicalDpiX() != screenResolution.width() ||
pd->logicalDpiY() != screenResolution.height() )
{
QFont pixelFont( painter->font(), QApplication::desktop() );
pixelFont.setPixelSize( QFontInfo( pixelFont ).pixelSize() );
painter->setFont( pixelFont );
}
}
//! Wrapper for QPainter::drawImage()
void QwtPainter::drawImage( QPainter *painter,
const QRectF &rect, const QImage &image )
{
const QRect alignedRect = rect.toAlignedRect();
if ( alignedRect != rect )
{
const QRectF clipRect = rect.adjusted( 0.0, 0.0, -1.0, -1.0 );
painter->save();
painter->setClipRect( clipRect, Qt::IntersectClip );
painter->drawImage( alignedRect, image );
painter->restore();
}
else
{
painter->drawImage( alignedRect, image );
}
}
//! Draw a focus rectangle on a widget using its style.
void QwtPainter::drawFocusRect( QPainter *painter, QWidget *widget,
const QRect &rect )
{
QStyleOptionFocusRect opt;
opt.init( widget );
opt.rect = rect;
opt.state |= QStyle::State_HasFocus;
widget->style()->drawPrimitive( QStyle::PE_FrameFocusRect,
&opt, painter, widget );
}
/*!
Draw a color bar into a rectangle
\param painter Painter
\param colorMap Color map
\param interval Value range
\param scaleMap Scale map
\param orientation Orientation
\param rect Traget rectangle
*/
void QwtPainter::drawColorBar( QPainter *painter,
const QwtColorMap &colorMap, const QwtInterval &interval,
const QwtScaleMap &scaleMap, Qt::Orientation orientation,
const QRectF &rect )
{
QColor c;
const QRect devRect = rect.toAlignedRect();
/*
We paint to a pixmap first to have something scalable for printing
( f.e. in a Pdf document )
*/
QPixmap pixmap( devRect.size() );
QPainter pmPainter( &pixmap );
pmPainter.translate( -devRect.x(), -devRect.y() );
if ( orientation == Qt::Horizontal )
{
QwtScaleMap sMap = scaleMap;
sMap.setPaintInterval( rect.left(), rect.right() );
for ( int x = devRect.left(); x <= devRect.right(); x++ )
{
const double value = sMap.invTransform( x );
c.setRgb( colorMap.rgb( interval, value ) );
pmPainter.setPen( c );
pmPainter.drawLine( x, devRect.top(), x, devRect.bottom() );
}
}
else // Vertical
{
QwtScaleMap sMap = scaleMap;
sMap.setPaintInterval( rect.bottom(), rect.top() );
for ( int y = devRect.top(); y <= devRect.bottom(); y++ )
{
const double value = sMap.invTransform( y );
c.setRgb( colorMap.rgb( interval, value ) );
pmPainter.setPen( c );
pmPainter.drawLine( devRect.left(), y, devRect.right(), y );
}
}
pmPainter.end();
const QRect alignedRect = rect.toAlignedRect();
if ( alignedRect != rect )
{
const QRectF clipRect = rect.adjusted( 0.0, 0.0, -1.0, -1.0 );
painter->save();
painter->setClipRect( clipRect, Qt::IntersectClip );
painter->drawPixmap( alignedRect, pixmap );
painter->restore();
}
else
{
painter->drawPixmap( alignedRect, pixmap );
}
}